Bug 1890689 accumulate input in LargerReceiverBlockSizeThanDesiredBuffering GTest...
[gecko.git] / gfx / gl / ScopedGLHelpers.h
blob6e3b9867bc48eb1f6d6b8fdfda5d8f0d92dc01c4
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_
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 // Wraps glEnable/Disable.
22 struct ScopedGLState final {
23 private:
24 GLContext* const mGL;
25 const GLenum mCapability;
26 bool mOldState;
28 public:
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);
34 ~ScopedGLState();
37 // Saves and restores with GetUserBoundFB and BindUserFB.
38 struct ScopedBindFramebuffer final {
39 private:
40 GLContext* const mGL;
41 GLuint mOldReadFB;
42 GLuint mOldDrawFB;
44 void Init();
46 public:
47 explicit ScopedBindFramebuffer(GLContext* aGL);
48 ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB);
49 ~ScopedBindFramebuffer();
52 struct ScopedBindTextureUnit final {
53 private:
54 GLContext* const mGL;
55 GLenum mOldTexUnit;
57 public:
58 ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit);
59 ~ScopedBindTextureUnit();
62 struct ScopedTexture final {
63 private:
64 GLContext* const mGL;
65 GLuint mTexture;
67 public:
68 explicit ScopedTexture(GLContext* aGL);
69 ~ScopedTexture();
71 GLuint Texture() const { return mTexture; }
72 operator GLuint() const { return mTexture; }
75 struct ScopedFramebuffer final {
76 private:
77 GLContext* const mGL;
78 GLuint mFB;
80 public:
81 explicit ScopedFramebuffer(GLContext* aGL);
82 ~ScopedFramebuffer();
84 const auto& FB() const { return mFB; }
85 operator GLuint() const { return mFB; }
88 struct ScopedRenderbuffer final {
89 private:
90 GLContext* const mGL;
91 GLuint mRB;
93 public:
94 explicit ScopedRenderbuffer(GLContext* aGL);
95 ~ScopedRenderbuffer();
97 GLuint RB() { return mRB; }
98 operator GLuint() const { return mRB; }
101 struct ScopedBindTexture final {
102 private:
103 GLContext* const mGL;
104 const GLenum mTarget;
105 const GLuint mOldTex;
107 public:
108 ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
109 GLenum aTarget = LOCAL_GL_TEXTURE_2D);
110 ~ScopedBindTexture();
113 struct ScopedBindRenderbuffer final {
114 private:
115 GLContext* const mGL;
116 GLuint mOldRB;
118 private:
119 void Init();
121 public:
122 explicit ScopedBindRenderbuffer(GLContext* aGL);
123 ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB);
124 ~ScopedBindRenderbuffer();
127 struct ScopedFramebufferForTexture final {
128 private:
129 GLContext* const mGL;
130 bool mComplete; // True if the framebuffer we create is complete.
131 GLuint mFB;
133 public:
134 ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture,
135 GLenum aTarget = LOCAL_GL_TEXTURE_2D);
136 ~ScopedFramebufferForTexture();
138 bool IsComplete() const { return mComplete; }
140 GLuint FB() const {
141 MOZ_GL_ASSERT(mGL, IsComplete());
142 return mFB;
146 struct ScopedFramebufferForRenderbuffer final {
147 private:
148 GLContext* const mGL;
149 bool mComplete; // True if the framebuffer we create is complete.
150 GLuint mFB;
152 public:
153 ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB);
154 ~ScopedFramebufferForRenderbuffer();
156 bool IsComplete() const { return mComplete; }
157 GLuint FB() const {
158 MOZ_GL_ASSERT(mGL, IsComplete());
159 return mFB;
163 struct ScopedViewportRect final {
164 private:
165 GLContext* const mGL;
166 GLint mSavedViewportRect[4];
168 public:
169 ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width,
170 GLsizei height);
171 ~ScopedViewportRect();
174 struct ScopedScissorRect final {
175 private:
176 GLContext* const mGL;
177 GLint mSavedScissorRect[4];
179 public:
180 ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width,
181 GLsizei height);
182 explicit ScopedScissorRect(GLContext* aGL);
183 ~ScopedScissorRect();
186 struct ScopedVertexAttribPointer final {
187 private:
188 GLContext* const mGL;
189 GLuint mAttribIndex;
190 GLint mAttribEnabled;
191 GLint mAttribSize;
192 GLint mAttribStride;
193 GLint mAttribType;
194 GLint mAttribNormalized;
195 GLint mAttribBufferBinding;
196 void* mAttribPointer;
197 GLuint mBoundBuffer;
199 public:
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();
207 private:
208 void WrapImpl(GLuint index);
211 struct ScopedPackState final {
212 private:
213 GLContext* const mGL;
214 GLint mAlignment;
216 GLuint mPixelBuffer;
217 GLint mRowLength;
218 GLint mSkipPixels;
219 GLint mSkipRows;
221 public:
222 explicit ScopedPackState(GLContext* gl);
223 ~ScopedPackState();
225 // Returns whether the stride was handled successfully.
226 bool SetForWidthAndStrideRGBA(GLsizei aWidth, GLsizei aStride);
229 struct ResetUnpackState final {
230 private:
231 GLContext* const mGL;
232 GLuint mAlignment;
234 GLuint mPBO;
235 GLuint mRowLength;
236 GLuint mImageHeight;
237 GLuint mSkipPixels;
238 GLuint mSkipRows;
239 GLuint mSkipImages;
241 public:
242 explicit ResetUnpackState(GLContext* gl);
243 ~ResetUnpackState();
246 struct ScopedBindPBO final {
247 private:
248 GLContext* const mGL;
249 const GLenum mTarget;
250 const GLuint mPBO;
252 public:
253 ScopedBindPBO(GLContext* gl, GLenum target);
254 ~ScopedBindPBO();
257 } /* namespace gl */
258 } /* namespace mozilla */
260 #endif /* SCOPEDGLHELPERS_H_ */