/work/obj-fuzz/dist/include/mozilla/layers/CanvasClient.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
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 | | |
7 | | #ifndef MOZILLA_GFX_CANVASCLIENT_H |
8 | | #define MOZILLA_GFX_CANVASCLIENT_H |
9 | | |
10 | | #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc |
11 | | #include "mozilla/Attributes.h" // for override |
12 | | #include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed |
13 | | #include "mozilla/layers/CompositableClient.h" // for CompositableClient |
14 | | #include "mozilla/layers/CompositorTypes.h" // for TextureInfo, etc |
15 | | #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor |
16 | | #include "mozilla/layers/TextureClient.h" // for TextureClient, etc |
17 | | #include "mozilla/layers/PersistentBufferProvider.h" |
18 | | |
19 | | #include "mozilla/MaybeOneOf.h" |
20 | | |
21 | | #include "mozilla/mozalloc.h" // for operator delete |
22 | | |
23 | | #include "mozilla/gfx/Point.h" // for IntSize |
24 | | #include "mozilla/gfx/Types.h" // for SurfaceFormat |
25 | | |
26 | | namespace mozilla { |
27 | | namespace layers { |
28 | | |
29 | | class AsyncCanvasRenderer; |
30 | | class ShareableCanvasRenderer; |
31 | | class CompositableForwarder; |
32 | | class ShadowableLayer; |
33 | | class SharedSurfaceTextureClient; |
34 | | |
35 | | /** |
36 | | * Compositable client for 2d and webgl canvas. |
37 | | */ |
38 | | class CanvasClient : public CompositableClient |
39 | | { |
40 | | public: |
41 | | typedef MaybeOneOf<ShareableCanvasRenderer*, AsyncCanvasRenderer*> Renderer; |
42 | | |
43 | | /** |
44 | | * Creates, configures, and returns a new canvas client. If necessary, a |
45 | | * message will be sent to the compositor to create a corresponding image |
46 | | * host. |
47 | | */ |
48 | | enum CanvasClientType { |
49 | | CanvasClientSurface, |
50 | | CanvasClientGLContext, |
51 | | CanvasClientTypeShSurf, |
52 | | CanvasClientAsync, // webgl on workers |
53 | | }; |
54 | | static already_AddRefed<CanvasClient> CreateCanvasClient(CanvasClientType aType, |
55 | | CompositableForwarder* aFwd, |
56 | | TextureFlags aFlags); |
57 | | |
58 | | CanvasClient(CompositableForwarder* aFwd, TextureFlags aFlags) |
59 | | : CompositableClient(aFwd, aFlags) |
60 | | , mFrameID(0) |
61 | 0 | { |
62 | 0 | mTextureFlags = aFlags; |
63 | 0 | } |
64 | | |
65 | 0 | virtual ~CanvasClient() {} |
66 | | |
67 | 0 | virtual void Clear() {}; |
68 | | |
69 | | virtual void Update(gfx::IntSize aSize, ShareableCanvasRenderer* aCanvasRenderer) = 0; |
70 | | |
71 | | virtual bool AddTextureClient(TextureClient* aTexture) override |
72 | 0 | { |
73 | 0 | ++mFrameID; |
74 | 0 | return CompositableClient::AddTextureClient(aTexture); |
75 | 0 | } |
76 | | |
77 | 0 | virtual void UpdateAsync(AsyncCanvasRenderer* aRenderer) {} |
78 | | |
79 | 0 | virtual void UpdateFromTexture(TextureClient* aTexture) {} |
80 | | |
81 | 0 | virtual void Updated() { } |
82 | | |
83 | | protected: |
84 | | int32_t mFrameID; |
85 | | }; |
86 | | |
87 | | // Used for 2D canvases and WebGL canvas on non-GL systems where readback is requried. |
88 | | class CanvasClient2D : public CanvasClient |
89 | | { |
90 | | public: |
91 | | CanvasClient2D(CompositableForwarder* aLayerForwarder, |
92 | | TextureFlags aFlags) |
93 | | : CanvasClient(aLayerForwarder, aFlags) |
94 | 0 | { |
95 | 0 | } |
96 | | |
97 | | TextureInfo GetTextureInfo() const override |
98 | 0 | { |
99 | 0 | return TextureInfo(CompositableType::IMAGE, mTextureFlags); |
100 | 0 | } |
101 | | |
102 | | virtual void Clear() override |
103 | 0 | { |
104 | 0 | mBackBuffer = mFrontBuffer = mBufferProviderTexture = nullptr; |
105 | 0 | } |
106 | | |
107 | | virtual void Update(gfx::IntSize aSize, ShareableCanvasRenderer* aCanvasRenderer) override; |
108 | | |
109 | | virtual void UpdateFromTexture(TextureClient* aBuffer) override; |
110 | | |
111 | | virtual bool AddTextureClient(TextureClient* aTexture) override |
112 | 0 | { |
113 | 0 | return CanvasClient::AddTextureClient(aTexture); |
114 | 0 | } |
115 | | |
116 | | virtual void OnDetach() override |
117 | 0 | { |
118 | 0 | mBackBuffer = mFrontBuffer = nullptr; |
119 | 0 | } |
120 | | |
121 | | private: |
122 | | already_AddRefed<TextureClient> |
123 | | CreateTextureClientForCanvas(gfx::SurfaceFormat aFormat, |
124 | | gfx::IntSize aSize, |
125 | | TextureFlags aFlags, |
126 | | ShareableCanvasRenderer* aCanvasRenderer); |
127 | | |
128 | | RefPtr<TextureClient> mBackBuffer; |
129 | | RefPtr<TextureClient> mFrontBuffer; |
130 | | // We store this texture separately to make sure it is not written into |
131 | | // in Update() if for some silly reason we end up alternating between |
132 | | // UpdateFromTexture and Update. |
133 | | // This code is begging for a cleanup. The situation described above should |
134 | | // not be made possible. |
135 | | RefPtr<TextureClient> mBufferProviderTexture; |
136 | | }; |
137 | | |
138 | | // Used for GL canvases where we don't need to do any readback, i.e., with a |
139 | | // GL backend. |
140 | | class CanvasClientSharedSurface : public CanvasClient |
141 | | { |
142 | | private: |
143 | | RefPtr<SharedSurfaceTextureClient> mShSurfClient; |
144 | | RefPtr<TextureClient> mReadbackClient; |
145 | | RefPtr<TextureClient> mFront; |
146 | | RefPtr<TextureClient> mNewFront; |
147 | | |
148 | | void ClearSurfaces(); |
149 | | |
150 | | public: |
151 | | CanvasClientSharedSurface(CompositableForwarder* aLayerForwarder, |
152 | | TextureFlags aFlags); |
153 | | |
154 | | ~CanvasClientSharedSurface(); |
155 | | |
156 | 0 | virtual TextureInfo GetTextureInfo() const override { |
157 | 0 | return TextureInfo(CompositableType::IMAGE); |
158 | 0 | } |
159 | | |
160 | 0 | virtual void Clear() override { |
161 | 0 | ClearSurfaces(); |
162 | 0 | } |
163 | | |
164 | | virtual void Update(gfx::IntSize aSize, |
165 | | ShareableCanvasRenderer* aCanvasRenderer) override; |
166 | | void UpdateRenderer(gfx::IntSize aSize, Renderer& aRenderer); |
167 | | |
168 | | virtual void UpdateAsync(AsyncCanvasRenderer* aRenderer) override; |
169 | | |
170 | | virtual void Updated() override; |
171 | | |
172 | | virtual void OnDetach() override; |
173 | | }; |
174 | | |
175 | | /** |
176 | | * Used for OMT<canvas> uploads using the image bridge protocol. |
177 | | * Actual CanvasClient is on the ImageBridgeChild thread, so we |
178 | | * only forward its AsyncID here |
179 | | */ |
180 | | class CanvasClientBridge final : public CanvasClient |
181 | | { |
182 | | public: |
183 | | CanvasClientBridge(CompositableForwarder* aLayerForwarder, |
184 | | TextureFlags aFlags) |
185 | | : CanvasClient(aLayerForwarder, aFlags) |
186 | | , mLayer(nullptr) |
187 | 0 | { |
188 | 0 | } |
189 | | |
190 | | TextureInfo GetTextureInfo() const override |
191 | 0 | { |
192 | 0 | return TextureInfo(CompositableType::IMAGE); |
193 | 0 | } |
194 | | |
195 | | virtual void Update(gfx::IntSize aSize, ShareableCanvasRenderer* aCanvasRenderer) override |
196 | 0 | { |
197 | 0 | } |
198 | | |
199 | | virtual void UpdateAsync(AsyncCanvasRenderer* aRenderer) override; |
200 | | |
201 | | void SetLayer(ShadowableLayer* aLayer) |
202 | 0 | { |
203 | 0 | mLayer = aLayer; |
204 | 0 | } |
205 | | |
206 | | protected: |
207 | | CompositableHandle mAsyncHandle; |
208 | | ShadowableLayer* mLayer; |
209 | | }; |
210 | | |
211 | | } // namespace layers |
212 | | } // namespace mozilla |
213 | | |
214 | | #endif |