2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2013-01-04 09:16:14 +04:00
|
|
|
#if !defined(mozilla_dom_HTMLCanvasElement_h)
|
|
|
|
#define mozilla_dom_HTMLCanvasElement_h
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-05-18 08:04:22 +04:00
|
|
|
#include "nsIDOMHTMLCanvasElement.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsSize.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2013-05-29 17:32:30 +04:00
|
|
|
#include "mozilla/gfx/Rect.h"
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2012-06-06 11:42:47 +04:00
|
|
|
class nsICanvasRenderingContextInternal;
|
2012-08-31 21:45:45 +04:00
|
|
|
class nsITimerCallback;
|
2010-09-18 05:14:59 +04:00
|
|
|
|
2012-07-31 04:42:26 +04:00
|
|
|
namespace mozilla {
|
2012-11-04 12:05:25 +04:00
|
|
|
|
2012-07-31 04:42:26 +04:00
|
|
|
namespace layers {
|
|
|
|
class CanvasLayer;
|
|
|
|
class LayerManager;
|
|
|
|
}
|
2014-04-17 09:29:52 +04:00
|
|
|
namespace gfx {
|
|
|
|
class SourceSurface;
|
|
|
|
}
|
2012-11-04 12:05:25 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
class File;
|
2013-09-12 02:35:51 +04:00
|
|
|
class FileCallback;
|
2013-01-04 09:16:14 +04:00
|
|
|
class HTMLCanvasPrintState;
|
2013-09-12 02:35:51 +04:00
|
|
|
class PrintCallback;
|
2012-07-31 04:42:26 +04:00
|
|
|
|
2015-01-26 01:22:07 +03:00
|
|
|
enum class CanvasContextType : uint8_t {
|
2014-11-11 02:16:50 +03:00
|
|
|
Canvas2D,
|
|
|
|
WebGL1,
|
|
|
|
WebGL2
|
2015-01-26 01:22:07 +03:00
|
|
|
};
|
2014-11-11 02:16:50 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class HTMLCanvasElement final : public nsGenericHTMLElement,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsIDOMHTMLCanvasElement
|
2010-05-18 08:04:22 +04:00
|
|
|
{
|
2013-04-13 11:05:40 +04:00
|
|
|
enum {
|
|
|
|
DEFAULT_CANVAS_WIDTH = 300,
|
|
|
|
DEFAULT_CANVAS_HEIGHT = 150
|
|
|
|
};
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
typedef layers::CanvasLayer CanvasLayer;
|
|
|
|
typedef layers::LayerManager LayerManager;
|
2010-05-18 08:04:22 +04:00
|
|
|
|
|
|
|
public:
|
2014-08-05 17:19:51 +04:00
|
|
|
explicit HTMLCanvasElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
NS_IMPL_FROMCONTENT_HTML_WITH_TAG(HTMLCanvasElement, canvas)
|
2012-09-26 18:17:51 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
// nsIDOMHTMLCanvasElement
|
|
|
|
NS_DECL_NSIDOMHTMLCANVASELEMENT
|
|
|
|
|
2010-06-16 01:38:05 +04:00
|
|
|
// CC
|
2013-01-04 09:16:14 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLCanvasElement,
|
2011-06-22 01:12:50 +04:00
|
|
|
nsGenericHTMLElement)
|
2010-06-16 01:38:05 +04:00
|
|
|
|
2013-04-13 11:05:40 +04:00
|
|
|
// WebIDL
|
|
|
|
uint32_t Height()
|
|
|
|
{
|
|
|
|
return GetUnsignedIntAttr(nsGkAtoms::height, DEFAULT_CANVAS_HEIGHT);
|
|
|
|
}
|
|
|
|
void SetHeight(uint32_t aHeight, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
SetUnsignedIntAttr(nsGkAtoms::height, aHeight, aRv);
|
|
|
|
}
|
|
|
|
uint32_t Width()
|
|
|
|
{
|
|
|
|
return GetUnsignedIntAttr(nsGkAtoms::width, DEFAULT_CANVAS_WIDTH);
|
|
|
|
}
|
|
|
|
void SetWidth(uint32_t aWidth, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
SetUnsignedIntAttr(nsGkAtoms::width, aWidth, aRv);
|
|
|
|
}
|
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
GetContext(JSContext* aCx, const nsAString& aContextId,
|
2013-06-07 22:42:12 +04:00
|
|
|
JS::Handle<JS::Value> aContextOptions,
|
|
|
|
ErrorResult& aRv);
|
2013-04-13 11:05:51 +04:00
|
|
|
void ToDataURL(JSContext* aCx, const nsAString& aType,
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Handle<JS::Value> aParams,
|
2013-05-16 20:36:54 +04:00
|
|
|
nsAString& aDataURL, ErrorResult& aRv)
|
2013-04-13 11:05:40 +04:00
|
|
|
{
|
2014-02-19 19:13:38 +04:00
|
|
|
aRv = ToDataURL(aType, aParams, aCx, aDataURL);
|
2013-04-13 11:05:40 +04:00
|
|
|
}
|
2013-07-24 15:17:00 +04:00
|
|
|
void ToBlob(JSContext* aCx,
|
2013-09-12 02:35:51 +04:00
|
|
|
FileCallback& aCallback,
|
2013-07-24 15:17:00 +04:00
|
|
|
const nsAString& aType,
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Handle<JS::Value> aParams,
|
2013-09-12 02:35:51 +04:00
|
|
|
ErrorResult& aRv);
|
2013-04-13 11:05:40 +04:00
|
|
|
|
|
|
|
bool MozOpaque() const
|
|
|
|
{
|
|
|
|
return GetBoolAttr(nsGkAtoms::moz_opaque);
|
|
|
|
}
|
|
|
|
void SetMozOpaque(bool aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
SetHTMLBoolAttr(nsGkAtoms::moz_opaque, aValue, aRv);
|
|
|
|
}
|
2014-10-08 20:15:23 +04:00
|
|
|
already_AddRefed<File> MozGetAsFile(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
ErrorResult& aRv);
|
2013-04-13 11:05:40 +04:00
|
|
|
already_AddRefed<nsISupports> MozGetIPCContext(const nsAString& aContextId,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> context;
|
|
|
|
aRv = MozGetIPCContext(aContextId, getter_AddRefs(context));
|
|
|
|
return context.forget();
|
|
|
|
}
|
|
|
|
void MozFetchAsStream(nsIInputStreamCallback* aCallback,
|
|
|
|
const nsAString& aType, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv = MozFetchAsStream(aCallback, aType);
|
|
|
|
}
|
2013-09-12 02:35:51 +04:00
|
|
|
PrintCallback* GetMozPrintCallback() const;
|
|
|
|
void SetMozPrintCallback(PrintCallback* aCallback);
|
2013-04-13 11:05:40 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
/**
|
|
|
|
* Get the size in pixels of this canvas element
|
|
|
|
*/
|
|
|
|
nsIntSize GetSize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether the canvas is write-only.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsWriteOnly();
|
2010-05-18 08:04:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Force the canvas to be write-only.
|
|
|
|
*/
|
|
|
|
void SetWriteOnly();
|
|
|
|
|
2011-03-28 03:59:47 +04:00
|
|
|
/**
|
|
|
|
* Notify that some canvas content has changed and the window may
|
|
|
|
* need to be updated. aDamageRect is in canvas coordinates.
|
|
|
|
*/
|
2012-11-04 12:05:25 +04:00
|
|
|
void InvalidateCanvasContent(const mozilla::gfx::Rect* aDamageRect);
|
2010-05-18 08:04:22 +04:00
|
|
|
/*
|
2011-03-28 03:59:47 +04:00
|
|
|
* Notify that we need to repaint the entire canvas, including updating of
|
|
|
|
* the layer tree.
|
2010-05-18 08:04:22 +04:00
|
|
|
*/
|
2011-03-28 03:59:47 +04:00
|
|
|
void InvalidateCanvas();
|
2010-05-18 08:04:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the number of contexts in this canvas, and request a context at
|
|
|
|
* an index.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t CountContexts ();
|
|
|
|
nsICanvasRenderingContextInternal *GetContextAtIndex (int32_t index);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the canvas context content is guaranteed to be opaque
|
|
|
|
* across its entire area.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetIsOpaque();
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2014-04-17 09:29:52 +04:00
|
|
|
virtual TemporaryRef<gfx::SourceSurface> GetSurfaceSnapshot(bool* aPremultAlpha = nullptr);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual bool ParseAttribute(int32_t aNamespaceID,
|
2010-05-18 08:04:22 +04:00
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsAttrValue& aResult) override;
|
|
|
|
nsChangeHint GetAttributeChangeHint(const nsIAtom* aAttribute, int32_t aModType) const override;
|
2010-05-18 08:04:22 +04:00
|
|
|
|
|
|
|
// SetAttr override. C++ is stupid, so have to override both
|
|
|
|
// overloaded methods.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2011-09-29 10:19:26 +04:00
|
|
|
const nsAString& aValue, bool aNotify)
|
2010-05-18 08:04:22 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2010-05-18 08:04:22 +04:00
|
|
|
nsIAtom* aPrefix, const nsAString& aValue,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aNotify) override;
|
2013-10-01 01:02:40 +04:00
|
|
|
|
|
|
|
virtual nsresult UnsetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aNotify) override;
|
2013-10-01 01:02:40 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult) const override;
|
2012-11-15 02:10:08 +04:00
|
|
|
nsresult CopyInnerTo(mozilla::dom::Element* aDest);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult PreHandleEvent(mozilla::EventChainPreVisitor& aVisitor) override;
|
2014-05-20 08:54:00 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
/*
|
|
|
|
* Helpers called by various users of Canvas
|
|
|
|
*/
|
|
|
|
|
2011-03-28 03:59:47 +04:00
|
|
|
already_AddRefed<CanvasLayer> GetCanvasLayer(nsDisplayListBuilder* aBuilder,
|
|
|
|
CanvasLayer *aOldLayer,
|
2010-07-16 01:07:46 +04:00
|
|
|
LayerManager *aManager);
|
2011-07-08 14:42:21 +04:00
|
|
|
// Should return true if the canvas layer should always be marked inactive.
|
|
|
|
// We should return true here if we can't do accelerated compositing with
|
|
|
|
// a non-BasicCanvasLayer.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ShouldForceInactiveLayer(LayerManager *aManager);
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2011-03-28 03:59:47 +04:00
|
|
|
// Call this whenever we need future changes to the canvas
|
|
|
|
// to trigger fresh invalidation requests. This needs to be called
|
|
|
|
// whenever we render the canvas contents to the screen, or whenever we
|
|
|
|
// take a snapshot of the canvas that needs to be "live" (e.g. -moz-element).
|
2010-05-18 08:04:22 +04:00
|
|
|
void MarkContextClean();
|
|
|
|
|
2013-04-13 11:05:33 +04:00
|
|
|
nsresult GetContext(const nsAString& aContextId, nsISupports** aContext);
|
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
protected:
|
2014-07-09 01:23:16 +04:00
|
|
|
virtual ~HTMLCanvasElement();
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-13 11:05:51 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
nsIntSize GetWidthHeight();
|
|
|
|
|
2013-06-07 22:42:12 +04:00
|
|
|
nsresult UpdateContext(JSContext* aCx, JS::Handle<JS::Value> options);
|
2013-07-24 15:17:00 +04:00
|
|
|
nsresult ParseParams(JSContext* aCx,
|
|
|
|
const nsAString& aType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
|
|
|
nsAString& aParams,
|
|
|
|
bool* usingCustomParseOptions);
|
2013-10-17 06:55:08 +04:00
|
|
|
nsresult ExtractData(nsAString& aType,
|
2010-09-18 05:14:59 +04:00
|
|
|
const nsAString& aOptions,
|
2013-10-17 06:55:08 +04:00
|
|
|
nsIInputStream** aStream);
|
2013-04-13 11:05:22 +04:00
|
|
|
nsresult ToDataURLImpl(JSContext* aCx,
|
|
|
|
const nsAString& aMimeType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
2010-05-18 08:04:22 +04:00
|
|
|
nsAString& aDataURL);
|
2010-09-18 05:14:59 +04:00
|
|
|
nsresult MozGetAsFileImpl(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsIDOMFile** aResult);
|
2012-08-31 21:45:45 +04:00
|
|
|
void CallPrintCallback();
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2014-11-11 02:16:50 +03:00
|
|
|
CanvasContextType mCurrentContextType;
|
2013-01-04 09:16:14 +04:00
|
|
|
nsRefPtr<HTMLCanvasElement> mOriginalCanvas;
|
2013-09-12 02:35:51 +04:00
|
|
|
nsRefPtr<PrintCallback> mPrintCallback;
|
2010-05-18 08:04:22 +04:00
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> mCurrentContext;
|
2013-09-12 02:35:51 +04:00
|
|
|
nsRefPtr<HTMLCanvasPrintState> mPrintState;
|
2013-04-13 11:03:57 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
public:
|
|
|
|
// Record whether this canvas should be write-only or not.
|
|
|
|
// We set this when script paints an image from a different origin.
|
|
|
|
// We also transitively set it when script paints a canvas which
|
|
|
|
// is itself write-only.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mWriteOnly;
|
2012-08-31 21:45:45 +04:00
|
|
|
|
|
|
|
bool IsPrintCallbackDone();
|
|
|
|
|
|
|
|
void HandlePrintCallback(nsPresContext::nsPresContextType aType);
|
|
|
|
|
|
|
|
nsresult DispatchPrintCallback(nsITimerCallback* aCallback);
|
|
|
|
|
|
|
|
void ResetPrintCallback();
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement* GetOriginalCanvas();
|
2010-05-18 08:04:22 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class HTMLCanvasPrintState final : public nsWrapperCache
|
2013-09-12 02:35:51 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
HTMLCanvasPrintState(HTMLCanvasElement* aCanvas,
|
|
|
|
nsICanvasRenderingContextInternal* aContext,
|
|
|
|
nsITimerCallback* aCallback);
|
|
|
|
|
|
|
|
nsISupports* Context() const;
|
|
|
|
|
|
|
|
void Done();
|
|
|
|
|
|
|
|
void NotifyDone();
|
|
|
|
|
|
|
|
bool mIsDone;
|
|
|
|
|
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(HTMLCanvasPrintState)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(HTMLCanvasPrintState)
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-09-12 02:35:51 +04:00
|
|
|
|
|
|
|
HTMLCanvasElement* GetParentObject()
|
|
|
|
{
|
|
|
|
return mCanvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~HTMLCanvasPrintState();
|
|
|
|
bool mPendingNotify;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsRefPtr<HTMLCanvasElement> mCanvas;
|
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> mContext;
|
|
|
|
nsCOMPtr<nsITimerCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_HTMLCanvasElement_h */
|