Line data Source code
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/. */
5 :
6 : #ifndef nsICanvasRenderingContextInternal_h___
7 : #define nsICanvasRenderingContextInternal_h___
8 :
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 "mozilla/dom/OffscreenCanvas.h"
16 : #include "mozilla/RefPtr.h"
17 : #include "mozilla/UniquePtr.h"
18 : #include "mozilla/NotNull.h"
19 :
20 : #define NS_ICANVASRENDERINGCONTEXTINTERNAL_IID \
21 : { 0xb84f2fed, 0x9d4b, 0x430b, \
22 : { 0xbd, 0xfb, 0x85, 0x57, 0x8a, 0xc2, 0xb4, 0x4b } }
23 :
24 : class nsDisplayListBuilder;
25 :
26 : namespace mozilla {
27 : namespace layers {
28 : class CanvasLayer;
29 : class Layer;
30 : class LayerManager;
31 : } // namespace layers
32 : namespace gfx {
33 : class SourceSurface;
34 : } // namespace gfx
35 : } // namespace mozilla
36 :
37 0 : class nsICanvasRenderingContextInternal :
38 : public nsISupports,
39 : public nsAPostRefreshObserver
40 : {
41 : public:
42 : typedef mozilla::layers::CanvasLayer CanvasLayer;
43 : typedef mozilla::layers::Layer Layer;
44 : typedef mozilla::layers::LayerManager LayerManager;
45 :
46 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICANVASRENDERINGCONTEXTINTERNAL_IID)
47 :
48 0 : void SetCanvasElement(mozilla::dom::HTMLCanvasElement* parentCanvas)
49 : {
50 0 : RemovePostRefreshObserver();
51 0 : mCanvasElement = parentCanvas;
52 0 : AddPostRefreshObserverIfNecessary();
53 0 : }
54 :
55 0 : virtual nsIPresShell *GetPresShell() {
56 0 : if (mCanvasElement) {
57 0 : return mCanvasElement->OwnerDoc()->GetShell();
58 : }
59 0 : return nullptr;
60 : }
61 :
62 0 : void RemovePostRefreshObserver()
63 : {
64 0 : if (mRefreshDriver) {
65 0 : mRefreshDriver->RemovePostRefreshObserver(this);
66 0 : mRefreshDriver = nullptr;
67 : }
68 0 : }
69 :
70 0 : void AddPostRefreshObserverIfNecessary()
71 : {
72 0 : if (!GetPresShell() ||
73 0 : !GetPresShell()->GetPresContext() ||
74 0 : !GetPresShell()->GetPresContext()->RefreshDriver()) {
75 0 : return;
76 : }
77 0 : mRefreshDriver = GetPresShell()->GetPresContext()->RefreshDriver();
78 0 : mRefreshDriver->AddPostRefreshObserver(this);
79 : }
80 :
81 0 : mozilla::dom::HTMLCanvasElement* GetParentObject() const
82 : {
83 0 : return mCanvasElement;
84 : }
85 :
86 0 : void SetOffscreenCanvas(mozilla::dom::OffscreenCanvas* aOffscreenCanvas)
87 : {
88 0 : mOffscreenCanvas = aOffscreenCanvas;
89 0 : }
90 :
91 : // Dimensions of the canvas, in pixels.
92 : virtual int32_t GetWidth() const = 0;
93 : virtual int32_t GetHeight() const = 0;
94 :
95 : // Sets the dimensions of the canvas, in pixels. Called
96 : // whenever the size of the element changes.
97 : NS_IMETHOD SetDimensions(int32_t width, int32_t height) = 0;
98 :
99 : // Initializes with an nsIDocShell and DrawTarget. The size is taken from the
100 : // DrawTarget.
101 : NS_IMETHOD InitializeWithDrawTarget(nsIDocShell *aDocShell,
102 : mozilla::NotNull<mozilla::gfx::DrawTarget*> aTarget) = 0;
103 :
104 : // Creates an image buffer. Returns null on failure.
105 : virtual mozilla::UniquePtr<uint8_t[]> GetImageBuffer(int32_t* format) = 0;
106 :
107 : // Gives you a stream containing the image represented by this context.
108 : // The format is given in mimeTime, for example "image/png".
109 : //
110 : // If the image format does not support transparency or includeTransparency
111 : // is false, alpha will be discarded and the result will be the image
112 : // composited on black.
113 : NS_IMETHOD GetInputStream(const char *mimeType,
114 : const char16_t *encoderOptions,
115 : nsIInputStream **stream) = 0;
116 :
117 : // This gets an Azure SourceSurface for the canvas, this will be a snapshot
118 : // of the canvas at the time it was called.
119 : // If premultAlpha is provided, then it assumed the callee can handle
120 : // un-premultiplied surfaces, and *premultAlpha will be set to false
121 : // if one is returned.
122 : virtual already_AddRefed<mozilla::gfx::SourceSurface>
123 : GetSurfaceSnapshot(gfxAlphaType* out_alphaType = nullptr) = 0;
124 :
125 : // If this context is opaque, the backing store of the canvas should
126 : // be created as opaque; all compositing operators should assume the
127 : // dst alpha is always 1.0. If this is never called, the context
128 : // defaults to false (not opaque).
129 : virtual void SetIsOpaque(bool isOpaque) = 0;
130 : virtual bool GetIsOpaque() = 0;
131 :
132 : // Invalidate this context and release any held resources, in preperation
133 : // for possibly reinitializing with SetDimensions/InitializeWithSurface.
134 : NS_IMETHOD Reset() = 0;
135 :
136 : // Return the CanvasLayer for this context, creating
137 : // one for the given layer manager if not available.
138 : virtual already_AddRefed<Layer> GetCanvasLayer(nsDisplayListBuilder* builder,
139 : Layer *oldLayer,
140 : LayerManager *manager,
141 : bool aMirror = false) = 0;
142 :
143 : // Return true if the canvas should be forced to be "inactive" to ensure
144 : // it can be drawn to the screen even if it's too large to be blitted by
145 : // an accelerated CanvasLayer.
146 0 : virtual bool ShouldForceInactiveLayer(LayerManager *manager) { return false; }
147 :
148 : virtual void MarkContextClean() = 0;
149 :
150 : // Called when a frame is captured.
151 : virtual void MarkContextCleanForFrameCapture() = 0;
152 :
153 : // Whether the context is clean or has been invalidated since the last frame
154 : // was captured.
155 : virtual bool IsContextCleanForFrameCapture() = 0;
156 :
157 : // Redraw the dirty rectangle of this canvas.
158 : NS_IMETHOD Redraw(const gfxRect &dirty) = 0;
159 :
160 0 : NS_IMETHOD SetContextOptions(JSContext* cx, JS::Handle<JS::Value> options,
161 : mozilla::ErrorResult& aRvForDictionaryInit)
162 : {
163 0 : return NS_OK;
164 : }
165 :
166 : // return true and fills in the bounding rect if elementis a child and has a hit region.
167 0 : virtual bool GetHitRegionRect(mozilla::dom::Element* element, nsRect& rect) { return false; }
168 :
169 : // Given a point, return hit region ID if it exists or an empty string if it doesn't
170 0 : virtual nsString GetHitRegion(const mozilla::gfx::Point& point) { return nsString(); }
171 :
172 0 : virtual void OnVisibilityChange() {}
173 :
174 0 : virtual void OnMemoryPressure() {}
175 :
176 : //
177 : // shmem support
178 : //
179 :
180 : // If this context can be set to use Mozilla's Shmem segments as its backing
181 : // store, this will set it to that state. Note that if you have drawn
182 : // anything into this canvas before changing the shmem state, it will be
183 : // lost.
184 : NS_IMETHOD SetIsIPC(bool isIPC) = 0;
185 :
186 : protected:
187 : RefPtr<mozilla::dom::HTMLCanvasElement> mCanvasElement;
188 : RefPtr<mozilla::dom::OffscreenCanvas> mOffscreenCanvas;
189 : RefPtr<nsRefreshDriver> mRefreshDriver;
190 : };
191 :
192 : NS_DEFINE_STATIC_IID_ACCESSOR(nsICanvasRenderingContextInternal,
193 : NS_ICANVASRENDERINGCONTEXTINTERNAL_IID)
194 :
195 : #endif /* nsICanvasRenderingContextInternal_h___ */
|