|
1 /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */ |
|
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 SHARED_SURFACE_GL_H_ |
|
7 #define SHARED_SURFACE_GL_H_ |
|
8 |
|
9 #include "ScopedGLHelpers.h" |
|
10 #include "SharedSurface.h" |
|
11 #include "SurfaceFactory.h" |
|
12 #include "SurfaceTypes.h" |
|
13 #include "GLContextTypes.h" |
|
14 #include "nsAutoPtr.h" |
|
15 #include "gfxTypes.h" |
|
16 #include "mozilla/Mutex.h" |
|
17 |
|
18 #include <queue> |
|
19 |
|
20 // Forwards: |
|
21 namespace mozilla { |
|
22 namespace gl { |
|
23 class GLContext; |
|
24 } |
|
25 namespace gfx { |
|
26 class DataSourceSurface; |
|
27 } |
|
28 } |
|
29 |
|
30 namespace mozilla { |
|
31 namespace gl { |
|
32 |
|
33 class SurfaceFactory_GL; |
|
34 |
|
35 class SharedSurface_GL |
|
36 : public gfx::SharedSurface |
|
37 { |
|
38 protected: |
|
39 typedef class gfx::SharedSurface SharedSurface; |
|
40 typedef gfx::SharedSurfaceType SharedSurfaceType; |
|
41 typedef gfx::APITypeT APITypeT; |
|
42 typedef gfx::AttachmentType AttachmentType; |
|
43 |
|
44 GLContext* const mGL; |
|
45 |
|
46 SharedSurface_GL(SharedSurfaceType type, |
|
47 AttachmentType attachType, |
|
48 GLContext* gl, |
|
49 const gfx::IntSize& size, |
|
50 bool hasAlpha) |
|
51 : SharedSurface(type, APITypeT::OpenGL, attachType, size, hasAlpha) |
|
52 , mGL(gl) |
|
53 {} |
|
54 |
|
55 public: |
|
56 static void ProdCopy(SharedSurface_GL* src, SharedSurface_GL* dest, |
|
57 SurfaceFactory_GL* factory); |
|
58 |
|
59 static SharedSurface_GL* Cast(SharedSurface* surf) { |
|
60 MOZ_ASSERT(surf->APIType() == APITypeT::OpenGL); |
|
61 |
|
62 return (SharedSurface_GL*)surf; |
|
63 } |
|
64 |
|
65 // For use when AttachType is correct. |
|
66 virtual GLuint ProdTexture() { |
|
67 MOZ_ASSERT(AttachType() == AttachmentType::GLTexture); |
|
68 MOZ_CRASH("Did you forget to override this function?"); |
|
69 } |
|
70 |
|
71 virtual GLenum ProdTextureTarget() const { |
|
72 return LOCAL_GL_TEXTURE_2D; |
|
73 } |
|
74 |
|
75 virtual GLuint ProdRenderbuffer() { |
|
76 MOZ_ASSERT(AttachType() == AttachmentType::GLRenderbuffer); |
|
77 MOZ_CRASH("Did you forget to override this function?"); |
|
78 } |
|
79 |
|
80 virtual bool ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, |
|
81 GLenum format, GLenum type, GLvoid *pixels) { |
|
82 return false; |
|
83 } |
|
84 |
|
85 virtual void LockProd() MOZ_OVERRIDE; |
|
86 virtual void UnlockProd() MOZ_OVERRIDE; |
|
87 |
|
88 GLContext* GL() const { |
|
89 return mGL; |
|
90 } |
|
91 }; |
|
92 |
|
93 class SurfaceFactory_GL |
|
94 : public gfx::SurfaceFactory |
|
95 { |
|
96 protected: |
|
97 typedef struct gfx::SurfaceCaps SurfaceCaps; |
|
98 typedef class gfx::SurfaceFactory SurfaceFactory; |
|
99 typedef class gfx::SharedSurface SharedSurface; |
|
100 typedef gfx::SharedSurfaceType SharedSurfaceType; |
|
101 |
|
102 GLContext* const mGL; |
|
103 const GLFormats mFormats; |
|
104 |
|
105 SurfaceCaps mDrawCaps; |
|
106 SurfaceCaps mReadCaps; |
|
107 |
|
108 // This uses ChooseBufferBits to pick drawBits/readBits. |
|
109 SurfaceFactory_GL(GLContext* gl, |
|
110 SharedSurfaceType type, |
|
111 const SurfaceCaps& caps); |
|
112 |
|
113 virtual void ChooseBufferBits(const SurfaceCaps& caps, |
|
114 SurfaceCaps& drawCaps, |
|
115 SurfaceCaps& readCaps) const; |
|
116 |
|
117 public: |
|
118 GLContext* GL() const { |
|
119 return mGL; |
|
120 } |
|
121 |
|
122 const GLFormats& Formats() const { |
|
123 return mFormats; |
|
124 } |
|
125 |
|
126 const SurfaceCaps& DrawCaps() const { |
|
127 return mDrawCaps; |
|
128 } |
|
129 |
|
130 const SurfaceCaps& ReadCaps() const { |
|
131 return mReadCaps; |
|
132 } |
|
133 }; |
|
134 |
|
135 // For readback and bootstrapping: |
|
136 class SharedSurface_Basic |
|
137 : public SharedSurface_GL |
|
138 { |
|
139 public: |
|
140 static SharedSurface_Basic* Create(GLContext* gl, |
|
141 const GLFormats& formats, |
|
142 const gfx::IntSize& size, |
|
143 bool hasAlpha); |
|
144 |
|
145 static SharedSurface_Basic* Cast(SharedSurface* surf) { |
|
146 MOZ_ASSERT(surf->Type() == SharedSurfaceType::Basic); |
|
147 |
|
148 return (SharedSurface_Basic*)surf; |
|
149 } |
|
150 |
|
151 protected: |
|
152 const GLuint mTex; |
|
153 GLuint mFB; |
|
154 |
|
155 RefPtr<gfx::DataSourceSurface> mData; |
|
156 |
|
157 SharedSurface_Basic(GLContext* gl, |
|
158 const gfx::IntSize& size, |
|
159 bool hasAlpha, |
|
160 gfx::SurfaceFormat format, |
|
161 GLuint tex); |
|
162 |
|
163 public: |
|
164 virtual ~SharedSurface_Basic(); |
|
165 |
|
166 virtual void LockProdImpl() MOZ_OVERRIDE {} |
|
167 virtual void UnlockProdImpl() MOZ_OVERRIDE {} |
|
168 |
|
169 |
|
170 virtual void Fence() MOZ_OVERRIDE; |
|
171 |
|
172 virtual bool WaitSync() MOZ_OVERRIDE { |
|
173 // Since we already store the data in Fence, we're always done already. |
|
174 return true; |
|
175 } |
|
176 |
|
177 virtual GLuint ProdTexture() MOZ_OVERRIDE { |
|
178 return mTex; |
|
179 } |
|
180 |
|
181 // Implementation-specific functions below: |
|
182 gfx::DataSourceSurface* GetData() { |
|
183 return mData; |
|
184 } |
|
185 }; |
|
186 |
|
187 class SurfaceFactory_Basic |
|
188 : public SurfaceFactory_GL |
|
189 { |
|
190 public: |
|
191 SurfaceFactory_Basic(GLContext* gl, const SurfaceCaps& caps) |
|
192 : SurfaceFactory_GL(gl, SharedSurfaceType::Basic, caps) |
|
193 {} |
|
194 |
|
195 virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE { |
|
196 bool hasAlpha = mReadCaps.alpha; |
|
197 return SharedSurface_Basic::Create(mGL, mFormats, size, hasAlpha); |
|
198 } |
|
199 }; |
|
200 |
|
201 |
|
202 // Using shared GL textures: |
|
203 class SharedSurface_GLTexture |
|
204 : public SharedSurface_GL |
|
205 { |
|
206 public: |
|
207 static SharedSurface_GLTexture* Create(GLContext* prodGL, |
|
208 GLContext* consGL, |
|
209 const GLFormats& formats, |
|
210 const gfx::IntSize& size, |
|
211 bool hasAlpha, |
|
212 GLuint texture = 0); |
|
213 |
|
214 static SharedSurface_GLTexture* Cast(SharedSurface* surf) { |
|
215 MOZ_ASSERT(surf->Type() == SharedSurfaceType::GLTextureShare); |
|
216 |
|
217 return (SharedSurface_GLTexture*)surf; |
|
218 } |
|
219 |
|
220 protected: |
|
221 GLContext* mConsGL; |
|
222 const GLuint mTex; |
|
223 const bool mOwnsTex; |
|
224 GLsync mSync; |
|
225 mutable Mutex mMutex; |
|
226 |
|
227 SharedSurface_GLTexture(GLContext* prodGL, |
|
228 GLContext* consGL, |
|
229 const gfx::IntSize& size, |
|
230 bool hasAlpha, |
|
231 GLuint tex, |
|
232 bool ownsTex) |
|
233 : SharedSurface_GL(SharedSurfaceType::GLTextureShare, |
|
234 AttachmentType::GLTexture, |
|
235 prodGL, |
|
236 size, |
|
237 hasAlpha) |
|
238 , mConsGL(consGL) |
|
239 , mTex(tex) |
|
240 , mOwnsTex(ownsTex) |
|
241 , mSync(0) |
|
242 , mMutex("SharedSurface_GLTexture mutex") |
|
243 { |
|
244 } |
|
245 |
|
246 public: |
|
247 virtual ~SharedSurface_GLTexture(); |
|
248 |
|
249 virtual void LockProdImpl() MOZ_OVERRIDE {} |
|
250 virtual void UnlockProdImpl() MOZ_OVERRIDE {} |
|
251 |
|
252 |
|
253 virtual void Fence() MOZ_OVERRIDE; |
|
254 virtual bool WaitSync() MOZ_OVERRIDE; |
|
255 |
|
256 |
|
257 virtual GLuint ProdTexture() MOZ_OVERRIDE { |
|
258 return mTex; |
|
259 } |
|
260 |
|
261 // Custom: |
|
262 |
|
263 GLuint ConsTexture(GLContext* consGL); |
|
264 |
|
265 GLenum ConsTextureTarget() const { |
|
266 return ProdTextureTarget(); |
|
267 } |
|
268 }; |
|
269 |
|
270 class SurfaceFactory_GLTexture |
|
271 : public SurfaceFactory_GL |
|
272 { |
|
273 protected: |
|
274 GLContext* const mConsGL; |
|
275 |
|
276 public: |
|
277 // If we don't know `consGL` at construction time, use `nullptr`, and call |
|
278 // `SetConsumerGL()` on each `SharedSurface_GLTexture` before calling its |
|
279 // `WaitSync()`. |
|
280 SurfaceFactory_GLTexture(GLContext* prodGL, |
|
281 GLContext* consGL, |
|
282 const SurfaceCaps& caps) |
|
283 : SurfaceFactory_GL(prodGL, SharedSurfaceType::GLTextureShare, caps) |
|
284 , mConsGL(consGL) |
|
285 { |
|
286 MOZ_ASSERT(consGL != prodGL); |
|
287 } |
|
288 |
|
289 virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE { |
|
290 bool hasAlpha = mReadCaps.alpha; |
|
291 return SharedSurface_GLTexture::Create(mGL, mConsGL, mFormats, size, hasAlpha); |
|
292 } |
|
293 }; |
|
294 |
|
295 } /* namespace gfx */ |
|
296 } /* namespace mozilla */ |
|
297 |
|
298 #endif /* SHARED_SURFACE_GL_H_ */ |