1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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
);
21 // Wraps glEnable/Disable.
22 struct ScopedGLState final
{
25 const GLenum mCapability
;
29 // Use |newState = true| to enable, |false| to disable.
30 ScopedGLState(GLContext
* aGL
, GLenum aCapability
, bool aNewState
);
31 // variant that doesn't change state; simply records existing state to be
32 // restored by the destructor
33 ScopedGLState(GLContext
* aGL
, GLenum aCapability
);
37 // Saves and restores with GetUserBoundFB and BindUserFB.
38 struct ScopedBindFramebuffer final
{
47 explicit ScopedBindFramebuffer(GLContext
* aGL
);
48 ScopedBindFramebuffer(GLContext
* aGL
, GLuint aNewFB
);
49 ~ScopedBindFramebuffer();
52 struct ScopedBindTextureUnit final
{
58 ScopedBindTextureUnit(GLContext
* aGL
, GLenum aTexUnit
);
59 ~ScopedBindTextureUnit();
62 struct ScopedTexture final
{
68 explicit ScopedTexture(GLContext
* aGL
);
71 GLuint
Texture() const { return mTexture
; }
72 operator GLuint() const { return mTexture
; }
75 struct ScopedFramebuffer final
{
81 explicit ScopedFramebuffer(GLContext
* aGL
);
84 const auto& FB() const { return mFB
; }
85 operator GLuint() const { return mFB
; }
88 struct ScopedRenderbuffer final
{
94 explicit ScopedRenderbuffer(GLContext
* aGL
);
95 ~ScopedRenderbuffer();
97 GLuint
RB() { return mRB
; }
98 operator GLuint() const { return mRB
; }
101 struct ScopedBindTexture final
{
103 GLContext
* const mGL
;
104 const GLenum mTarget
;
105 const GLuint mOldTex
;
108 ScopedBindTexture(GLContext
* aGL
, GLuint aNewTex
,
109 GLenum aTarget
= LOCAL_GL_TEXTURE_2D
);
110 ~ScopedBindTexture();
113 struct ScopedBindRenderbuffer final
{
115 GLContext
* const mGL
;
122 explicit ScopedBindRenderbuffer(GLContext
* aGL
);
123 ScopedBindRenderbuffer(GLContext
* aGL
, GLuint aNewRB
);
124 ~ScopedBindRenderbuffer();
127 struct ScopedFramebufferForTexture final
{
129 GLContext
* const mGL
;
130 bool mComplete
; // True if the framebuffer we create is complete.
134 ScopedFramebufferForTexture(GLContext
* aGL
, GLuint aTexture
,
135 GLenum aTarget
= LOCAL_GL_TEXTURE_2D
);
136 ~ScopedFramebufferForTexture();
138 bool IsComplete() const { return mComplete
; }
141 MOZ_GL_ASSERT(mGL
, IsComplete());
146 struct ScopedFramebufferForRenderbuffer final
{
148 GLContext
* const mGL
;
149 bool mComplete
; // True if the framebuffer we create is complete.
153 ScopedFramebufferForRenderbuffer(GLContext
* aGL
, GLuint aRB
);
154 ~ScopedFramebufferForRenderbuffer();
156 bool IsComplete() const { return mComplete
; }
158 MOZ_GL_ASSERT(mGL
, IsComplete());
163 struct ScopedViewportRect final
{
165 GLContext
* const mGL
;
166 GLint mSavedViewportRect
[4];
169 ScopedViewportRect(GLContext
* aGL
, GLint x
, GLint y
, GLsizei width
,
171 ~ScopedViewportRect();
174 struct ScopedScissorRect final
{
176 GLContext
* const mGL
;
177 GLint mSavedScissorRect
[4];
180 ScopedScissorRect(GLContext
* aGL
, GLint x
, GLint y
, GLsizei width
,
182 explicit ScopedScissorRect(GLContext
* aGL
);
183 ~ScopedScissorRect();
186 struct ScopedVertexAttribPointer final
{
188 GLContext
* const mGL
;
190 GLint mAttribEnabled
;
194 GLint mAttribNormalized
;
195 GLint mAttribBufferBinding
;
196 void* mAttribPointer
;
200 ScopedVertexAttribPointer(GLContext
* aGL
, GLuint index
, GLint size
,
201 GLenum type
, realGLboolean normalized
,
202 GLsizei stride
, GLuint buffer
,
203 const GLvoid
* pointer
);
204 explicit ScopedVertexAttribPointer(GLContext
* aGL
, GLuint index
);
205 ~ScopedVertexAttribPointer();
208 void WrapImpl(GLuint index
);
211 struct ScopedPackState final
{
213 GLContext
* const mGL
;
222 explicit ScopedPackState(GLContext
* gl
);
225 // Returns whether the stride was handled successfully.
226 bool SetForWidthAndStrideRGBA(GLsizei aWidth
, GLsizei aStride
);
229 struct ResetUnpackState final
{
231 GLContext
* const mGL
;
242 explicit ResetUnpackState(GLContext
* gl
);
246 struct ScopedBindPBO final
{
248 GLContext
* const mGL
;
249 const GLenum mTarget
;
253 ScopedBindPBO(GLContext
* gl
, GLenum target
);
258 } /* namespace mozilla */
260 #endif /* SCOPEDGLHELPERS_H_ */