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_
10 #include "mozilla/UniquePtr.h"
18 bool IsContextCurrent(GLContext
* gl
);
22 template <class Derived
>
23 struct ScopedGLWrapper
31 explicit ScopedGLWrapper(GLContext
* gl
)
35 MOZ_ASSERT(&ScopedGLWrapper
<Derived
>::Unwrap
== &Derived::Unwrap
);
36 MOZ_ASSERT(&Derived::UnwrapImpl
);
37 MOZ_ASSERT(IsContextCurrent(mGL
));
40 virtual ~ScopedGLWrapper() {
47 MOZ_ASSERT(!mIsUnwrapped
);
49 Derived
* derived
= static_cast<Derived
*>(this);
50 derived
->UnwrapImpl();
56 // Wraps glEnable/Disable.
58 : public ScopedGLWrapper
<ScopedGLState
>
60 friend struct ScopedGLWrapper
<ScopedGLState
>;
63 const GLenum mCapability
;
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
);
77 // Saves and restores with GetUserBoundFB and BindUserFB.
78 struct ScopedBindFramebuffer
79 : public ScopedGLWrapper
<ScopedBindFramebuffer
>
81 friend struct ScopedGLWrapper
<ScopedBindFramebuffer
>;
91 explicit ScopedBindFramebuffer(GLContext
* aGL
);
92 ScopedBindFramebuffer(GLContext
* aGL
, GLuint aNewFB
);
98 struct ScopedBindTextureUnit
99 : public ScopedGLWrapper
<ScopedBindTextureUnit
>
101 friend struct ScopedGLWrapper
<ScopedBindTextureUnit
>;
107 ScopedBindTextureUnit(GLContext
* aGL
, GLenum aTexUnit
);
115 : public ScopedGLWrapper
<ScopedTexture
>
117 friend struct ScopedGLWrapper
<ScopedTexture
>;
123 explicit ScopedTexture(GLContext
* aGL
);
124 GLuint
Texture() { return mTexture
; }
131 struct ScopedFramebuffer
132 : public ScopedGLWrapper
<ScopedFramebuffer
>
134 friend struct ScopedGLWrapper
<ScopedFramebuffer
>;
140 explicit ScopedFramebuffer(GLContext
* aGL
);
141 GLuint
FB() { return mFB
; }
148 struct ScopedRenderbuffer
149 : public ScopedGLWrapper
<ScopedRenderbuffer
>
151 friend struct ScopedGLWrapper
<ScopedRenderbuffer
>;
157 explicit ScopedRenderbuffer(GLContext
* aGL
);
158 GLuint
RB() { return mRB
; }
165 struct ScopedBindTexture
166 : public ScopedGLWrapper
<ScopedBindTexture
>
168 friend struct ScopedGLWrapper
<ScopedBindTexture
>;
175 void Init(GLenum aTarget
);
178 ScopedBindTexture(GLContext
* aGL
, GLuint aNewTex
,
179 GLenum aTarget
= LOCAL_GL_TEXTURE_2D
);
186 struct ScopedBindRenderbuffer
187 : public ScopedGLWrapper
<ScopedBindRenderbuffer
>
189 friend struct ScopedGLWrapper
<ScopedBindRenderbuffer
>;
198 explicit ScopedBindRenderbuffer(GLContext
* aGL
);
200 ScopedBindRenderbuffer(GLContext
* aGL
, GLuint aNewRB
);
207 struct ScopedFramebufferForTexture
208 : public ScopedGLWrapper
<ScopedFramebufferForTexture
>
210 friend struct ScopedGLWrapper
<ScopedFramebufferForTexture
>;
213 bool mComplete
; // True if the framebuffer we create is complete.
217 ScopedFramebufferForTexture(GLContext
* aGL
, GLuint aTexture
,
218 GLenum aTarget
= LOCAL_GL_TEXTURE_2D
);
220 bool IsComplete() const {
225 MOZ_ASSERT(IsComplete());
233 struct ScopedFramebufferForRenderbuffer
234 : public ScopedGLWrapper
<ScopedFramebufferForRenderbuffer
>
236 friend struct ScopedGLWrapper
<ScopedFramebufferForRenderbuffer
>;
239 bool mComplete
; // True if the framebuffer we create is complete.
243 ScopedFramebufferForRenderbuffer(GLContext
* aGL
, GLuint aRB
);
245 bool IsComplete() const {
257 struct ScopedViewportRect
258 : public ScopedGLWrapper
<ScopedViewportRect
>
260 friend struct ScopedGLWrapper
<ScopedViewportRect
>;
263 GLint mSavedViewportRect
[4];
266 ScopedViewportRect(GLContext
* aGL
, GLint x
, GLint y
, GLsizei width
, GLsizei height
);
272 struct ScopedScissorRect
273 : public ScopedGLWrapper
<ScopedScissorRect
>
275 friend struct ScopedGLWrapper
<ScopedScissorRect
>;
278 GLint mSavedScissorRect
[4];
281 ScopedScissorRect(GLContext
* aGL
, GLint x
, GLint y
, GLsizei width
, GLsizei height
);
282 explicit ScopedScissorRect(GLContext
* aGL
);
288 struct ScopedVertexAttribPointer
289 : public ScopedGLWrapper
<ScopedVertexAttribPointer
>
291 friend struct ScopedGLWrapper
<ScopedVertexAttribPointer
>;
295 GLint mAttribEnabled
;
299 GLint mAttribNormalized
;
300 GLint mAttribBufferBinding
;
301 void* mAttribPointer
;
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
);
310 void WrapImpl(GLuint index
);
314 struct ScopedGLDrawState
{
315 explicit ScopedGLDrawState(GLContext
* gl
);
316 ~ScopedGLDrawState();
322 ScopedGLState cullFace
;
323 ScopedGLState depthTest
;
324 ScopedGLState dither
;
325 ScopedGLState polyOffsFill
;
326 ScopedGLState sampleAToC
;
327 ScopedGLState sampleCover
;
328 ScopedGLState scissor
;
329 ScopedGLState stencil
;
332 UniquePtr
<GLint
[]> attrib_enabled
;
334 GLint attrib0_stride
;
336 GLint attrib0_normalized
;
337 GLint attrib0_bufferBinding
;
338 void* attrib0_pointer
;
340 realGLboolean colorMask
[4];
343 GLContext
* const mGL
;
347 struct ScopedPackAlignment
348 : public ScopedGLWrapper
<ScopedPackAlignment
>
350 friend struct ScopedGLWrapper
<ScopedPackAlignment
>;
356 ScopedPackAlignment(GLContext
* aGL
, GLint scopedVal
);
362 } /* namespace mozilla */
364 #endif /* SCOPEDGLHELPERS_H_ */