Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / dom / html / HTMLCanvasElement.h
blobdd96c044d70ad10eef8c4abb7a5dbc554cce1f45
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"
14 #include "nsSize.h"
15 #include "nsError.h"
17 #include "mozilla/gfx/Rect.h"
19 class nsICanvasRenderingContextInternal;
20 class nsITimerCallback;
22 namespace mozilla {
24 namespace layers {
25 class CanvasLayer;
26 class LayerManager;
28 namespace gfx {
29 class SourceSurface;
32 namespace dom {
34 class File;
35 class FileCallback;
36 class HTMLCanvasPrintState;
37 class PrintCallback;
39 MOZ_BEGIN_ENUM_CLASS(CanvasContextType, uint8_t)
40 Canvas2D,
41 WebGL1,
42 WebGL2
43 MOZ_END_ENUM_CLASS(CanvasContextType)
45 class HTMLCanvasElement MOZ_FINAL : public nsGenericHTMLElement,
46 public nsIDOMHTMLCanvasElement
48 enum {
49 DEFAULT_CANVAS_WIDTH = 300,
50 DEFAULT_CANVAS_HEIGHT = 150
53 typedef layers::CanvasLayer CanvasLayer;
54 typedef layers::LayerManager LayerManager;
56 public:
57 explicit HTMLCanvasElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
59 NS_IMPL_FROMCONTENT_HTML_WITH_TAG(HTMLCanvasElement, canvas)
61 // nsISupports
62 NS_DECL_ISUPPORTS_INHERITED
64 // nsIDOMHTMLCanvasElement
65 NS_DECL_NSIDOMHTMLCANVASELEMENT
67 // CC
68 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLCanvasElement,
69 nsGenericHTMLElement)
71 // WebIDL
72 uint32_t Height()
74 return GetUnsignedIntAttr(nsGkAtoms::height, DEFAULT_CANVAS_HEIGHT);
76 void SetHeight(uint32_t aHeight, ErrorResult& aRv)
78 SetUnsignedIntAttr(nsGkAtoms::height, aHeight, aRv);
80 uint32_t Width()
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,
91 ErrorResult& aRv);
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,
102 ErrorResult& aRv);
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,
114 ErrorResult& aRv);
115 already_AddRefed<nsISupports> MozGetIPCContext(const nsAString& aContextId,
116 ErrorResult& aRv)
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
133 nsIntSize GetSize();
136 * Determine whether the canvas is write-only.
138 bool IsWriteOnly();
141 * Force the canvas to be write-only.
143 void SetWriteOnly();
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
152 * the layer tree.
154 void InvalidateCanvas();
157 * Get the number of contexts in this canvas, and request a context at
158 * an index.
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.
167 bool GetIsOpaque();
169 virtual TemporaryRef<gfx::SourceSurface> GetSurfaceSnapshot(bool* aPremultAlpha = nullptr);
171 virtual bool ParseAttribute(int32_t aNamespaceID,
172 nsIAtom* aAttribute,
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);
216 protected:
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,
227 nsAString& aParams,
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;
247 public:
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.
252 bool mWriteOnly;
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
267 public:
268 HTMLCanvasPrintState(HTMLCanvasElement* aCanvas,
269 nsICanvasRenderingContextInternal* aContext,
270 nsITimerCallback* aCallback);
272 nsISupports* Context() const;
274 void Done();
276 void NotifyDone();
278 bool mIsDone;
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()
287 return mCanvas;
290 private:
291 ~HTMLCanvasPrintState();
292 bool mPendingNotify;
294 protected:
295 nsRefPtr<HTMLCanvasElement> mCanvas;
296 nsCOMPtr<nsICanvasRenderingContextInternal> mContext;
297 nsCOMPtr<nsITimerCallback> mCallback;
300 } // namespace dom
301 } // namespace mozilla
303 #endif /* mozilla_dom_HTMLCanvasElement_h */