Bumping manifests a=b2g-bump
[gecko.git] / gfx / gl / ScopedGLHelpers.h
blob0da3303b62e56005c49a21417c3be36368637c56
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/. */
6 #ifndef SCOPEDGLHELPERS_H_
7 #define SCOPEDGLHELPERS_H_
9 #include "GLDefs.h"
10 #include "mozilla/UniquePtr.h"
12 namespace mozilla {
13 namespace gl {
15 class GLContext;
17 #ifdef DEBUG
18 bool IsContextCurrent(GLContext* gl);
19 #endif
21 //RAII via CRTP!
22 template <class Derived>
23 struct ScopedGLWrapper
25 private:
26 bool mIsUnwrapped;
28 protected:
29 GLContext* const mGL;
31 explicit ScopedGLWrapper(GLContext* gl)
32 : mIsUnwrapped(false)
33 , mGL(gl)
35 MOZ_ASSERT(&ScopedGLWrapper<Derived>::Unwrap == &Derived::Unwrap);
36 MOZ_ASSERT(&Derived::UnwrapImpl);
37 MOZ_ASSERT(IsContextCurrent(mGL));
40 virtual ~ScopedGLWrapper() {
41 if (!mIsUnwrapped)
42 Unwrap();
45 public:
46 void Unwrap() {
47 MOZ_ASSERT(!mIsUnwrapped);
49 Derived* derived = static_cast<Derived*>(this);
50 derived->UnwrapImpl();
52 mIsUnwrapped = true;
56 // Wraps glEnable/Disable.
57 struct ScopedGLState
58 : public ScopedGLWrapper<ScopedGLState>
60 friend struct ScopedGLWrapper<ScopedGLState>;
62 protected:
63 const GLenum mCapability;
64 bool mOldState;
66 public:
67 // Use |newState = true| to enable, |false| to disable.
68 ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState);
69 // variant that doesn't change state; simply records existing state to be
70 // restored by the destructor
71 ScopedGLState(GLContext* aGL, GLenum aCapability);
73 protected:
74 void UnwrapImpl();
77 // Saves and restores with GetUserBoundFB and BindUserFB.
78 struct ScopedBindFramebuffer
79 : public ScopedGLWrapper<ScopedBindFramebuffer>
81 friend struct ScopedGLWrapper<ScopedBindFramebuffer>;
83 protected:
84 GLuint mOldReadFB;
85 GLuint mOldDrawFB;
87 private:
88 void Init();
90 public:
91 explicit ScopedBindFramebuffer(GLContext* aGL);
92 ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB);
94 protected:
95 void UnwrapImpl();
98 struct ScopedBindTextureUnit
99 : public ScopedGLWrapper<ScopedBindTextureUnit>
101 friend struct ScopedGLWrapper<ScopedBindTextureUnit>;
103 protected:
104 GLenum mOldTexUnit;
106 public:
107 ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit);
109 protected:
110 void UnwrapImpl();
114 struct ScopedTexture
115 : public ScopedGLWrapper<ScopedTexture>
117 friend struct ScopedGLWrapper<ScopedTexture>;
119 protected:
120 GLuint mTexture;
122 public:
123 explicit ScopedTexture(GLContext* aGL);
124 GLuint Texture() { return mTexture; }
126 protected:
127 void UnwrapImpl();
131 struct ScopedFramebuffer
132 : public ScopedGLWrapper<ScopedFramebuffer>
134 friend struct ScopedGLWrapper<ScopedFramebuffer>;
136 protected:
137 GLuint mFB;
139 public:
140 explicit ScopedFramebuffer(GLContext* aGL);
141 GLuint FB() { return mFB; }
143 protected:
144 void UnwrapImpl();
148 struct ScopedRenderbuffer
149 : public ScopedGLWrapper<ScopedRenderbuffer>
151 friend struct ScopedGLWrapper<ScopedRenderbuffer>;
153 protected:
154 GLuint mRB;
156 public:
157 explicit ScopedRenderbuffer(GLContext* aGL);
158 GLuint RB() { return mRB; }
160 protected:
161 void UnwrapImpl();
165 struct ScopedBindTexture
166 : public ScopedGLWrapper<ScopedBindTexture>
168 friend struct ScopedGLWrapper<ScopedBindTexture>;
170 protected:
171 GLuint mOldTex;
172 GLenum mTarget;
174 private:
175 void Init(GLenum aTarget);
177 public:
178 ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
179 GLenum aTarget = LOCAL_GL_TEXTURE_2D);
181 protected:
182 void UnwrapImpl();
186 struct ScopedBindRenderbuffer
187 : public ScopedGLWrapper<ScopedBindRenderbuffer>
189 friend struct ScopedGLWrapper<ScopedBindRenderbuffer>;
191 protected:
192 GLuint mOldRB;
194 private:
195 void Init();
197 public:
198 explicit ScopedBindRenderbuffer(GLContext* aGL);
200 ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB);
202 protected:
203 void UnwrapImpl();
207 struct ScopedFramebufferForTexture
208 : public ScopedGLWrapper<ScopedFramebufferForTexture>
210 friend struct ScopedGLWrapper<ScopedFramebufferForTexture>;
212 protected:
213 bool mComplete; // True if the framebuffer we create is complete.
214 GLuint mFB;
216 public:
217 ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture,
218 GLenum aTarget = LOCAL_GL_TEXTURE_2D);
220 bool IsComplete() const {
221 return mComplete;
224 GLuint FB() const {
225 MOZ_ASSERT(IsComplete());
226 return mFB;
229 protected:
230 void UnwrapImpl();
233 struct ScopedFramebufferForRenderbuffer
234 : public ScopedGLWrapper<ScopedFramebufferForRenderbuffer>
236 friend struct ScopedGLWrapper<ScopedFramebufferForRenderbuffer>;
238 protected:
239 bool mComplete; // True if the framebuffer we create is complete.
240 GLuint mFB;
242 public:
243 ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB);
245 bool IsComplete() const {
246 return mComplete;
249 GLuint FB() const {
250 return mFB;
253 protected:
254 void UnwrapImpl();
257 struct ScopedViewportRect
258 : public ScopedGLWrapper<ScopedViewportRect>
260 friend struct ScopedGLWrapper<ScopedViewportRect>;
262 protected:
263 GLint mSavedViewportRect[4];
265 public:
266 ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
268 protected:
269 void UnwrapImpl();
272 struct ScopedScissorRect
273 : public ScopedGLWrapper<ScopedScissorRect>
275 friend struct ScopedGLWrapper<ScopedScissorRect>;
277 protected:
278 GLint mSavedScissorRect[4];
280 public:
281 ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
282 explicit ScopedScissorRect(GLContext* aGL);
284 protected:
285 void UnwrapImpl();
288 struct ScopedVertexAttribPointer
289 : public ScopedGLWrapper<ScopedVertexAttribPointer>
291 friend struct ScopedGLWrapper<ScopedVertexAttribPointer>;
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;
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);
309 protected:
310 void WrapImpl(GLuint index);
311 void UnwrapImpl();
314 struct ScopedGLDrawState {
315 explicit ScopedGLDrawState(GLContext* gl);
316 ~ScopedGLDrawState();
318 GLuint boundProgram;
319 GLuint boundBuffer;
321 ScopedGLState blend;
322 ScopedGLState cullFace;
323 ScopedGLState depthTest;
324 ScopedGLState dither;
325 ScopedGLState polyOffsFill;
326 ScopedGLState sampleAToC;
327 ScopedGLState sampleCover;
328 ScopedGLState scissor;
329 ScopedGLState stencil;
331 GLuint maxAttrib;
332 UniquePtr<GLint[]> attrib_enabled;
333 GLint attrib0_size;
334 GLint attrib0_stride;
335 GLint attrib0_type;
336 GLint attrib0_normalized;
337 GLint attrib0_bufferBinding;
338 void* attrib0_pointer;
340 realGLboolean colorMask[4];
341 GLint viewport[4];
342 GLint scissorBox[4];
343 GLContext* const mGL;
344 GLuint packAlign;
347 struct ScopedPackAlignment
348 : public ScopedGLWrapper<ScopedPackAlignment>
350 friend struct ScopedGLWrapper<ScopedPackAlignment>;
352 protected:
353 GLint mOldVal;
355 public:
356 ScopedPackAlignment(GLContext* aGL, GLint scopedVal);
358 protected:
359 void UnwrapImpl();
361 } /* namespace gl */
362 } /* namespace mozilla */
364 #endif /* SCOPEDGLHELPERS_H_ */