1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsICanvasRenderingContextInternal_h___
7 #define nsICanvasRenderingContextInternal_h___
9 #include "mozilla/gfx/2D.h"
10 #include "nsISupports.h"
11 #include "nsIInputStream.h"
12 #include "nsIDocShell.h"
13 #include "nsRefreshDriver.h"
14 #include "mozilla/dom/HTMLCanvasElement.h"
15 #include "GraphicsFilter.h"
16 #include "mozilla/RefPtr.h"
18 #define NS_ICANVASRENDERINGCONTEXTINTERNAL_IID \
19 { 0x3cc9e801, 0x1806, 0x4ff6, \
20 { 0x86, 0x14, 0xf9, 0xd0, 0xf4, 0xfb, 0x3b, 0x08 } }
24 class nsDisplayListBuilder
;
36 class nsICanvasRenderingContextInternal
:
38 public nsAPostRefreshObserver
41 typedef mozilla::layers::CanvasLayer CanvasLayer
;
42 typedef mozilla::layers::LayerManager LayerManager
;
44 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICANVASRENDERINGCONTEXTINTERNAL_IID
)
46 void SetCanvasElement(mozilla::dom::HTMLCanvasElement
* aParentCanvas
)
48 RemovePostRefreshObserver();
49 mCanvasElement
= aParentCanvas
;
50 AddPostRefreshObserverIfNecessary();
53 virtual nsIPresShell
*GetPresShell() {
55 return mCanvasElement
->OwnerDoc()->GetShell();
60 void RemovePostRefreshObserver()
63 mRefreshDriver
->RemovePostRefreshObserver(this);
64 mRefreshDriver
= nullptr;
68 void AddPostRefreshObserverIfNecessary()
70 if (!GetPresShell() ||
71 !GetPresShell()->GetPresContext() ||
72 !GetPresShell()->GetPresContext()->RefreshDriver()) {
75 mRefreshDriver
= GetPresShell()->GetPresContext()->RefreshDriver();
76 mRefreshDriver
->AddPostRefreshObserver(this);
79 mozilla::dom::HTMLCanvasElement
* GetParentObject() const
81 return mCanvasElement
;
86 virtual int32_t GetWidth() const = 0;
87 virtual int32_t GetHeight() const = 0;
90 // Sets the dimensions of the canvas, in pixels. Called
91 // whenever the size of the element changes.
92 NS_IMETHOD
SetDimensions(int32_t width
, int32_t height
) = 0;
94 NS_IMETHOD
InitializeWithSurface(nsIDocShell
*docShell
, gfxASurface
*surface
, int32_t width
, int32_t height
) = 0;
96 // Creates an image buffer. Returns null on failure.
97 virtual void GetImageBuffer(uint8_t** aImageBuffer
, int32_t* aFormat
) = 0;
99 // Gives you a stream containing the image represented by this context.
100 // The format is given in aMimeTime, for example "image/png".
102 // If the image format does not support transparency or aIncludeTransparency
103 // is false, alpha will be discarded and the result will be the image
104 // composited on black.
105 NS_IMETHOD
GetInputStream(const char *aMimeType
,
106 const char16_t
*aEncoderOptions
,
107 nsIInputStream
**aStream
) = 0;
109 // This gets an Azure SourceSurface for the canvas, this will be a snapshot
110 // of the canvas at the time it was called.
111 // If aPremultAlpha is provided, then it assumed the callee can handle
112 // un-premultiplied surfaces, and *aPremultAlpha will be set to false
113 // if one is returned.
114 virtual mozilla::TemporaryRef
<mozilla::gfx::SourceSurface
> GetSurfaceSnapshot(bool* aPremultAlpha
= nullptr) = 0;
116 // If this context is opaque, the backing store of the canvas should
117 // be created as opaque; all compositing operators should assume the
118 // dst alpha is always 1.0. If this is never called, the context
119 // defaults to false (not opaque).
120 NS_IMETHOD
SetIsOpaque(bool isOpaque
) = 0;
121 virtual bool GetIsOpaque() = 0;
123 // Invalidate this context and release any held resources, in preperation
124 // for possibly reinitializing with SetDimensions/InitializeWithSurface.
125 NS_IMETHOD
Reset() = 0;
127 // Return the CanvasLayer for this context, creating
128 // one for the given layer manager if not available.
129 virtual already_AddRefed
<CanvasLayer
> GetCanvasLayer(nsDisplayListBuilder
* aBuilder
,
130 CanvasLayer
*aOldLayer
,
131 LayerManager
*aManager
) = 0;
133 // Return true if the canvas should be forced to be "inactive" to ensure
134 // it can be drawn to the screen even if it's too large to be blitted by
135 // an accelerated CanvasLayer.
136 virtual bool ShouldForceInactiveLayer(LayerManager
*aManager
) { return false; }
138 virtual void MarkContextClean() = 0;
140 // Redraw the dirty rectangle of this canvas.
141 NS_IMETHOD
Redraw(const gfxRect
&dirty
) = 0;
143 NS_IMETHOD
SetContextOptions(JSContext
* aCx
, JS::Handle
<JS::Value
> aOptions
) { return NS_OK
; }
145 // return true and fills in the bounding rect if elementis a child and has a hit region.
146 virtual bool GetHitRegionRect(mozilla::dom::Element
* aElement
, nsRect
& aRect
) { return false; }
148 // Given a point, return hit region ID if it exists or an empty string if it doesn't
149 virtual nsString
GetHitRegion(const mozilla::gfx::Point
& aPoint
) { return nsString(); }
155 // If this context can be set to use Mozilla's Shmem segments as its backing
156 // store, this will set it to that state. Note that if you have drawn
157 // anything into this canvas before changing the shmem state, it will be
159 NS_IMETHOD
SetIsIPC(bool isIPC
) = 0;
162 nsRefPtr
<mozilla::dom::HTMLCanvasElement
> mCanvasElement
;
163 nsRefPtr
<nsRefreshDriver
> mRefreshDriver
;
166 NS_DEFINE_STATIC_IID_ACCESSOR(nsICanvasRenderingContextInternal
,
167 NS_ICANVASRENDERINGCONTEXTINTERNAL_IID
)
169 #endif /* nsICanvasRenderingContextInternal_h___ */