/work/obj-fuzz/dist/include/ScopedGLHelpers.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 SCOPEDGLHELPERS_H_ |
7 | | #define SCOPEDGLHELPERS_H_ |
8 | | |
9 | | #include "GLDefs.h" |
10 | | #include "mozilla/UniquePtr.h" |
11 | | |
12 | | namespace mozilla { |
13 | | namespace gl { |
14 | | |
15 | | class GLContext; |
16 | | |
17 | | #ifdef DEBUG |
18 | | bool IsContextCurrent(GLContext* gl); |
19 | | #endif |
20 | | |
21 | | //RAII via CRTP! |
22 | | template <class Derived> |
23 | | struct ScopedGLWrapper |
24 | | { |
25 | | private: |
26 | | bool mIsUnwrapped; |
27 | | |
28 | | protected: |
29 | | GLContext* const mGL; |
30 | | |
31 | | explicit ScopedGLWrapper(GLContext* gl) |
32 | | : mIsUnwrapped(false) |
33 | | , mGL(gl) |
34 | | { |
35 | | MOZ_ASSERT(&ScopedGLWrapper<Derived>::Unwrap == &Derived::Unwrap); |
36 | | MOZ_ASSERT(&Derived::UnwrapImpl); |
37 | | MOZ_ASSERT(IsContextCurrent(mGL)); |
38 | | } |
39 | | |
40 | | virtual ~ScopedGLWrapper() { |
41 | | if (!mIsUnwrapped) |
42 | | Unwrap(); |
43 | | } |
44 | | |
45 | | public: |
46 | | void Unwrap() { |
47 | | MOZ_ASSERT(!mIsUnwrapped); |
48 | | MOZ_ASSERT(IsContextCurrent(mGL)); |
49 | | |
50 | | Derived* derived = static_cast<Derived*>(this); |
51 | | derived->UnwrapImpl(); |
52 | | |
53 | | mIsUnwrapped = true; |
54 | | } |
55 | | }; |
56 | | |
57 | | // Wraps glEnable/Disable. |
58 | | struct ScopedGLState |
59 | | : public ScopedGLWrapper<ScopedGLState> |
60 | | { |
61 | | friend struct ScopedGLWrapper<ScopedGLState>; |
62 | | |
63 | | protected: |
64 | | const GLenum mCapability; |
65 | | bool mOldState; |
66 | | |
67 | | public: |
68 | | // Use |newState = true| to enable, |false| to disable. |
69 | | ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState); |
70 | | // variant that doesn't change state; simply records existing state to be |
71 | | // restored by the destructor |
72 | | ScopedGLState(GLContext* aGL, GLenum aCapability); |
73 | | |
74 | | protected: |
75 | | void UnwrapImpl(); |
76 | | }; |
77 | | |
78 | | // Saves and restores with GetUserBoundFB and BindUserFB. |
79 | | struct ScopedBindFramebuffer |
80 | | : public ScopedGLWrapper<ScopedBindFramebuffer> |
81 | | { |
82 | | friend struct ScopedGLWrapper<ScopedBindFramebuffer>; |
83 | | |
84 | | protected: |
85 | | GLuint mOldReadFB; |
86 | | GLuint mOldDrawFB; |
87 | | |
88 | | private: |
89 | | void Init(); |
90 | | |
91 | | public: |
92 | | explicit ScopedBindFramebuffer(GLContext* aGL); |
93 | | ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB); |
94 | | |
95 | | protected: |
96 | | void UnwrapImpl(); |
97 | | }; |
98 | | |
99 | | struct ScopedBindTextureUnit |
100 | | : public ScopedGLWrapper<ScopedBindTextureUnit> |
101 | | { |
102 | | friend struct ScopedGLWrapper<ScopedBindTextureUnit>; |
103 | | |
104 | | protected: |
105 | | GLenum mOldTexUnit; |
106 | | |
107 | | public: |
108 | | ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit); |
109 | | |
110 | | protected: |
111 | | void UnwrapImpl(); |
112 | | }; |
113 | | |
114 | | |
115 | | struct ScopedTexture |
116 | | : public ScopedGLWrapper<ScopedTexture> |
117 | | { |
118 | | friend struct ScopedGLWrapper<ScopedTexture>; |
119 | | |
120 | | protected: |
121 | | GLuint mTexture; |
122 | | |
123 | | public: |
124 | | explicit ScopedTexture(GLContext* aGL); |
125 | | |
126 | | GLuint Texture() const { return mTexture; } |
127 | | operator GLuint() const { return mTexture; } |
128 | | |
129 | | protected: |
130 | | void UnwrapImpl(); |
131 | | }; |
132 | | |
133 | | |
134 | | struct ScopedFramebuffer |
135 | | : public ScopedGLWrapper<ScopedFramebuffer> |
136 | | { |
137 | | friend struct ScopedGLWrapper<ScopedFramebuffer>; |
138 | | |
139 | | protected: |
140 | | GLuint mFB; |
141 | | |
142 | | public: |
143 | | explicit ScopedFramebuffer(GLContext* aGL); |
144 | | GLuint FB() { return mFB; } |
145 | | |
146 | | protected: |
147 | | void UnwrapImpl(); |
148 | | }; |
149 | | |
150 | | |
151 | | struct ScopedRenderbuffer |
152 | | : public ScopedGLWrapper<ScopedRenderbuffer> |
153 | | { |
154 | | friend struct ScopedGLWrapper<ScopedRenderbuffer>; |
155 | | |
156 | | protected: |
157 | | GLuint mRB; |
158 | | |
159 | | public: |
160 | | explicit ScopedRenderbuffer(GLContext* aGL); |
161 | | GLuint RB() { return mRB; } |
162 | | |
163 | | protected: |
164 | | void UnwrapImpl(); |
165 | | }; |
166 | | |
167 | | |
168 | | struct ScopedBindTexture |
169 | | : public ScopedGLWrapper<ScopedBindTexture> |
170 | | { |
171 | | friend struct ScopedGLWrapper<ScopedBindTexture>; |
172 | | |
173 | | protected: |
174 | | const GLenum mTarget; |
175 | | const GLuint mOldTex; |
176 | | |
177 | | public: |
178 | | ScopedBindTexture(GLContext* aGL, GLuint aNewTex, |
179 | | GLenum aTarget = LOCAL_GL_TEXTURE_2D); |
180 | | |
181 | | protected: |
182 | | void UnwrapImpl(); |
183 | | }; |
184 | | |
185 | | |
186 | | struct ScopedBindRenderbuffer |
187 | | : public ScopedGLWrapper<ScopedBindRenderbuffer> |
188 | | { |
189 | | friend struct ScopedGLWrapper<ScopedBindRenderbuffer>; |
190 | | |
191 | | protected: |
192 | | GLuint mOldRB; |
193 | | |
194 | | private: |
195 | | void Init(); |
196 | | |
197 | | public: |
198 | | explicit ScopedBindRenderbuffer(GLContext* aGL); |
199 | | |
200 | | ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB); |
201 | | |
202 | | protected: |
203 | | void UnwrapImpl(); |
204 | | }; |
205 | | |
206 | | |
207 | | struct ScopedFramebufferForTexture |
208 | | : public ScopedGLWrapper<ScopedFramebufferForTexture> |
209 | | { |
210 | | friend struct ScopedGLWrapper<ScopedFramebufferForTexture>; |
211 | | |
212 | | protected: |
213 | | bool mComplete; // True if the framebuffer we create is complete. |
214 | | GLuint mFB; |
215 | | |
216 | | public: |
217 | | ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture, |
218 | | GLenum aTarget = LOCAL_GL_TEXTURE_2D); |
219 | | |
220 | 0 | bool IsComplete() const { |
221 | 0 | return mComplete; |
222 | 0 | } |
223 | | |
224 | | GLuint FB() const { |
225 | | MOZ_ASSERT(IsComplete()); |
226 | | return mFB; |
227 | | } |
228 | | |
229 | | protected: |
230 | | void UnwrapImpl(); |
231 | | }; |
232 | | |
233 | | struct ScopedFramebufferForRenderbuffer |
234 | | : public ScopedGLWrapper<ScopedFramebufferForRenderbuffer> |
235 | | { |
236 | | friend struct ScopedGLWrapper<ScopedFramebufferForRenderbuffer>; |
237 | | |
238 | | protected: |
239 | | bool mComplete; // True if the framebuffer we create is complete. |
240 | | GLuint mFB; |
241 | | |
242 | | public: |
243 | | ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB); |
244 | | |
245 | | bool IsComplete() const { |
246 | | return mComplete; |
247 | | } |
248 | | |
249 | | GLuint FB() const { |
250 | | return mFB; |
251 | | } |
252 | | |
253 | | protected: |
254 | | void UnwrapImpl(); |
255 | | }; |
256 | | |
257 | | struct ScopedViewportRect |
258 | | : public ScopedGLWrapper<ScopedViewportRect> |
259 | | { |
260 | | friend struct ScopedGLWrapper<ScopedViewportRect>; |
261 | | |
262 | | protected: |
263 | | GLint mSavedViewportRect[4]; |
264 | | |
265 | | public: |
266 | | ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height); |
267 | | |
268 | | protected: |
269 | | void UnwrapImpl(); |
270 | | }; |
271 | | |
272 | | struct ScopedScissorRect |
273 | | : public ScopedGLWrapper<ScopedScissorRect> |
274 | | { |
275 | | friend struct ScopedGLWrapper<ScopedScissorRect>; |
276 | | |
277 | | protected: |
278 | | GLint mSavedScissorRect[4]; |
279 | | |
280 | | public: |
281 | | ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height); |
282 | | explicit ScopedScissorRect(GLContext* aGL); |
283 | | |
284 | | protected: |
285 | | void UnwrapImpl(); |
286 | | }; |
287 | | |
288 | | struct ScopedVertexAttribPointer |
289 | | : public ScopedGLWrapper<ScopedVertexAttribPointer> |
290 | | { |
291 | | friend struct ScopedGLWrapper<ScopedVertexAttribPointer>; |
292 | | |
293 | | protected: |
294 | | GLuint mAttribIndex; |
295 | | GLint mAttribEnabled; |
296 | | GLint mAttribSize; |
297 | | GLint mAttribStride; |
298 | | GLint mAttribType; |
299 | | GLint mAttribNormalized; |
300 | | GLint mAttribBufferBinding; |
301 | | void* mAttribPointer; |
302 | | GLuint mBoundBuffer; |
303 | | |
304 | | public: |
305 | | ScopedVertexAttribPointer(GLContext* aGL, GLuint index, GLint size, GLenum type, realGLboolean normalized, |
306 | | GLsizei stride, GLuint buffer, const GLvoid* pointer); |
307 | | explicit ScopedVertexAttribPointer(GLContext* aGL, GLuint index); |
308 | | |
309 | | protected: |
310 | | void WrapImpl(GLuint index); |
311 | | void UnwrapImpl(); |
312 | | }; |
313 | | |
314 | | struct ScopedPackState |
315 | | : public ScopedGLWrapper<ScopedPackState> |
316 | | { |
317 | | friend struct ScopedGLWrapper<ScopedPackState>; |
318 | | |
319 | | protected: |
320 | | GLint mAlignment; |
321 | | |
322 | | GLuint mPixelBuffer; |
323 | | GLint mRowLength; |
324 | | GLint mSkipPixels; |
325 | | GLint mSkipRows; |
326 | | |
327 | | public: |
328 | | explicit ScopedPackState(GLContext* gl); |
329 | | |
330 | | protected: |
331 | | void UnwrapImpl(); |
332 | | }; |
333 | | |
334 | | struct ResetUnpackState |
335 | | : public ScopedGLWrapper<ResetUnpackState> |
336 | | { |
337 | | friend struct ScopedGLWrapper<ResetUnpackState>; |
338 | | |
339 | | protected: |
340 | | GLuint mAlignment; |
341 | | |
342 | | GLuint mPBO; |
343 | | GLuint mRowLength; |
344 | | GLuint mImageHeight; |
345 | | GLuint mSkipPixels; |
346 | | GLuint mSkipRows; |
347 | | GLuint mSkipImages; |
348 | | |
349 | | public: |
350 | | explicit ResetUnpackState(GLContext* gl); |
351 | | |
352 | | protected: |
353 | | void UnwrapImpl(); |
354 | | }; |
355 | | |
356 | | struct ScopedBindPBO final |
357 | | : public ScopedGLWrapper<ScopedBindPBO> |
358 | | { |
359 | | friend struct ScopedGLWrapper<ScopedBindPBO>; |
360 | | |
361 | | protected: |
362 | | const GLenum mTarget; |
363 | | const GLuint mPBO; |
364 | | |
365 | | public: |
366 | | ScopedBindPBO(GLContext* gl, GLenum target); |
367 | | |
368 | | protected: |
369 | | void UnwrapImpl(); |
370 | | }; |
371 | | |
372 | | } /* namespace gl */ |
373 | | } /* namespace mozilla */ |
374 | | |
375 | | #endif /* SCOPEDGLHELPERS_H_ */ |