1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #if !defined(mozilla_dom_HTMLCanvasElement_h)
7 #define mozilla_dom_HTMLCanvasElement_h
9 #include "mozilla/Attributes.h"
10 #include "mozilla/TypedEnum.h"
11 #include "nsIDOMHTMLCanvasElement.h"
12 #include "nsGenericHTMLElement.h"
13 #include "nsGkAtoms.h"
17 #include "mozilla/gfx/Rect.h"
19 class nsICanvasRenderingContextInternal
;
20 class nsITimerCallback
;
36 class HTMLCanvasPrintState
;
39 MOZ_BEGIN_ENUM_CLASS(CanvasContextType
, uint8_t)
43 MOZ_END_ENUM_CLASS(CanvasContextType
)
45 class HTMLCanvasElement MOZ_FINAL
: public nsGenericHTMLElement
,
46 public nsIDOMHTMLCanvasElement
49 DEFAULT_CANVAS_WIDTH
= 300,
50 DEFAULT_CANVAS_HEIGHT
= 150
53 typedef layers::CanvasLayer CanvasLayer
;
54 typedef layers::LayerManager LayerManager
;
57 explicit HTMLCanvasElement(already_AddRefed
<mozilla::dom::NodeInfo
>& aNodeInfo
);
59 NS_IMPL_FROMCONTENT_HTML_WITH_TAG(HTMLCanvasElement
, canvas
)
62 NS_DECL_ISUPPORTS_INHERITED
64 // nsIDOMHTMLCanvasElement
65 NS_DECL_NSIDOMHTMLCANVASELEMENT
68 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLCanvasElement
,
74 return GetUnsignedIntAttr(nsGkAtoms::height
, DEFAULT_CANVAS_HEIGHT
);
76 void SetHeight(uint32_t aHeight
, ErrorResult
& aRv
)
78 SetUnsignedIntAttr(nsGkAtoms::height
, aHeight
, aRv
);
82 return GetUnsignedIntAttr(nsGkAtoms::width
, DEFAULT_CANVAS_WIDTH
);
84 void SetWidth(uint32_t aWidth
, ErrorResult
& aRv
)
86 SetUnsignedIntAttr(nsGkAtoms::width
, aWidth
, aRv
);
88 already_AddRefed
<nsISupports
>
89 GetContext(JSContext
* aCx
, const nsAString
& aContextId
,
90 JS::Handle
<JS::Value
> aContextOptions
,
92 void ToDataURL(JSContext
* aCx
, const nsAString
& aType
,
93 JS::Handle
<JS::Value
> aParams
,
94 nsAString
& aDataURL
, ErrorResult
& aRv
)
96 aRv
= ToDataURL(aType
, aParams
, aCx
, aDataURL
);
98 void ToBlob(JSContext
* aCx
,
99 FileCallback
& aCallback
,
100 const nsAString
& aType
,
101 JS::Handle
<JS::Value
> aParams
,
104 bool MozOpaque() const
106 return GetBoolAttr(nsGkAtoms::moz_opaque
);
108 void SetMozOpaque(bool aValue
, ErrorResult
& aRv
)
110 SetHTMLBoolAttr(nsGkAtoms::moz_opaque
, aValue
, aRv
);
112 already_AddRefed
<File
> MozGetAsFile(const nsAString
& aName
,
113 const nsAString
& aType
,
115 already_AddRefed
<nsISupports
> MozGetIPCContext(const nsAString
& aContextId
,
118 nsCOMPtr
<nsISupports
> context
;
119 aRv
= MozGetIPCContext(aContextId
, getter_AddRefs(context
));
120 return context
.forget();
122 void MozFetchAsStream(nsIInputStreamCallback
* aCallback
,
123 const nsAString
& aType
, ErrorResult
& aRv
)
125 aRv
= MozFetchAsStream(aCallback
, aType
);
127 PrintCallback
* GetMozPrintCallback() const;
128 void SetMozPrintCallback(PrintCallback
* aCallback
);
131 * Get the size in pixels of this canvas element
136 * Determine whether the canvas is write-only.
141 * Force the canvas to be write-only.
146 * Notify that some canvas content has changed and the window may
147 * need to be updated. aDamageRect is in canvas coordinates.
149 void InvalidateCanvasContent(const mozilla::gfx::Rect
* aDamageRect
);
151 * Notify that we need to repaint the entire canvas, including updating of
154 void InvalidateCanvas();
157 * Get the number of contexts in this canvas, and request a context at
160 int32_t CountContexts ();
161 nsICanvasRenderingContextInternal
*GetContextAtIndex (int32_t index
);
164 * Returns true if the canvas context content is guaranteed to be opaque
165 * across its entire area.
169 virtual TemporaryRef
<gfx::SourceSurface
> GetSurfaceSnapshot(bool* aPremultAlpha
= nullptr);
171 virtual bool ParseAttribute(int32_t aNamespaceID
,
173 const nsAString
& aValue
,
174 nsAttrValue
& aResult
) MOZ_OVERRIDE
;
175 nsChangeHint
GetAttributeChangeHint(const nsIAtom
* aAttribute
, int32_t aModType
) const MOZ_OVERRIDE
;
177 // SetAttr override. C++ is stupid, so have to override both
178 // overloaded methods.
179 nsresult
SetAttr(int32_t aNameSpaceID
, nsIAtom
* aName
,
180 const nsAString
& aValue
, bool aNotify
)
182 return SetAttr(aNameSpaceID
, aName
, nullptr, aValue
, aNotify
);
184 virtual nsresult
SetAttr(int32_t aNameSpaceID
, nsIAtom
* aName
,
185 nsIAtom
* aPrefix
, const nsAString
& aValue
,
186 bool aNotify
) MOZ_OVERRIDE
;
188 virtual nsresult
UnsetAttr(int32_t aNameSpaceID
, nsIAtom
* aName
,
189 bool aNotify
) MOZ_OVERRIDE
;
191 virtual nsresult
Clone(mozilla::dom::NodeInfo
*aNodeInfo
, nsINode
**aResult
) const MOZ_OVERRIDE
;
192 nsresult
CopyInnerTo(mozilla::dom::Element
* aDest
);
194 virtual nsresult
PreHandleEvent(mozilla::EventChainPreVisitor
& aVisitor
) MOZ_OVERRIDE
;
197 * Helpers called by various users of Canvas
200 already_AddRefed
<CanvasLayer
> GetCanvasLayer(nsDisplayListBuilder
* aBuilder
,
201 CanvasLayer
*aOldLayer
,
202 LayerManager
*aManager
);
203 // Should return true if the canvas layer should always be marked inactive.
204 // We should return true here if we can't do accelerated compositing with
205 // a non-BasicCanvasLayer.
206 bool ShouldForceInactiveLayer(LayerManager
*aManager
);
208 // Call this whenever we need future changes to the canvas
209 // to trigger fresh invalidation requests. This needs to be called
210 // whenever we render the canvas contents to the screen, or whenever we
211 // take a snapshot of the canvas that needs to be "live" (e.g. -moz-element).
212 void MarkContextClean();
214 nsresult
GetContext(const nsAString
& aContextId
, nsISupports
** aContext
);
217 virtual ~HTMLCanvasElement();
219 virtual JSObject
* WrapNode(JSContext
* aCx
) MOZ_OVERRIDE
;
221 nsIntSize
GetWidthHeight();
223 nsresult
UpdateContext(JSContext
* aCx
, JS::Handle
<JS::Value
> options
);
224 nsresult
ParseParams(JSContext
* aCx
,
225 const nsAString
& aType
,
226 const JS::Value
& aEncoderOptions
,
228 bool* usingCustomParseOptions
);
229 nsresult
ExtractData(nsAString
& aType
,
230 const nsAString
& aOptions
,
231 nsIInputStream
** aStream
);
232 nsresult
ToDataURLImpl(JSContext
* aCx
,
233 const nsAString
& aMimeType
,
234 const JS::Value
& aEncoderOptions
,
235 nsAString
& aDataURL
);
236 nsresult
MozGetAsFileImpl(const nsAString
& aName
,
237 const nsAString
& aType
,
238 nsIDOMFile
** aResult
);
239 void CallPrintCallback();
241 CanvasContextType mCurrentContextType
;
242 nsRefPtr
<HTMLCanvasElement
> mOriginalCanvas
;
243 nsRefPtr
<PrintCallback
> mPrintCallback
;
244 nsCOMPtr
<nsICanvasRenderingContextInternal
> mCurrentContext
;
245 nsRefPtr
<HTMLCanvasPrintState
> mPrintState
;
248 // Record whether this canvas should be write-only or not.
249 // We set this when script paints an image from a different origin.
250 // We also transitively set it when script paints a canvas which
251 // is itself write-only.
254 bool IsPrintCallbackDone();
256 void HandlePrintCallback(nsPresContext::nsPresContextType aType
);
258 nsresult
DispatchPrintCallback(nsITimerCallback
* aCallback
);
260 void ResetPrintCallback();
262 HTMLCanvasElement
* GetOriginalCanvas();
265 class HTMLCanvasPrintState MOZ_FINAL
: public nsWrapperCache
268 HTMLCanvasPrintState(HTMLCanvasElement
* aCanvas
,
269 nsICanvasRenderingContextInternal
* aContext
,
270 nsITimerCallback
* aCallback
);
272 nsISupports
* Context() const;
280 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(HTMLCanvasPrintState
)
281 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(HTMLCanvasPrintState
)
283 virtual JSObject
* WrapObject(JSContext
*cx
) MOZ_OVERRIDE
;
285 HTMLCanvasElement
* GetParentObject()
291 ~HTMLCanvasPrintState();
295 nsRefPtr
<HTMLCanvasElement
> mCanvas
;
296 nsCOMPtr
<nsICanvasRenderingContextInternal
> mContext
;
297 nsCOMPtr
<nsITimerCallback
> mCallback
;
301 } // namespace mozilla
303 #endif /* mozilla_dom_HTMLCanvasElement_h */