ScopedGLHelpers.h (5776B)
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/. */ 5 6 #ifndef SCOPEDGLHELPERS_H_ 7 #define SCOPEDGLHELPERS_H_ 8 9 #include "GLDefs.h" 10 11 namespace mozilla { 12 namespace gl { 13 14 class GLContext; 15 16 #ifdef DEBUG 17 bool IsContextCurrent(GLContext* gl); 18 #endif 19 20 // Wraps glEnable/Disable. 21 struct ScopedGLState final { 22 private: 23 GLContext* const mGL; 24 const GLenum mCapability; 25 bool mOldState; 26 27 public: 28 // Use |newState = true| to enable, |false| to disable. 29 ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState); 30 // variant that doesn't change state; simply records existing state to be 31 // restored by the destructor 32 ScopedGLState(GLContext* aGL, GLenum aCapability); 33 ~ScopedGLState(); 34 }; 35 36 // Saves and restores with GetUserBoundFB and BindUserFB. 37 struct ScopedBindFramebuffer final { 38 private: 39 GLContext* const mGL; 40 GLuint mOldReadFB; 41 GLuint mOldDrawFB; 42 43 void Init(); 44 45 public: 46 explicit ScopedBindFramebuffer(GLContext* aGL); 47 ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB); 48 ~ScopedBindFramebuffer(); 49 }; 50 51 struct ScopedBindTextureUnit final { 52 private: 53 GLContext* const mGL; 54 GLenum mOldTexUnit; 55 56 public: 57 ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit); 58 ~ScopedBindTextureUnit(); 59 }; 60 61 struct ScopedTexture final { 62 private: 63 GLContext* const mGL; 64 GLuint mTexture; 65 66 public: 67 explicit ScopedTexture(GLContext* aGL); 68 ~ScopedTexture(); 69 70 GLuint Texture() const { return mTexture; } 71 operator GLuint() const { return mTexture; } 72 }; 73 74 struct ScopedFramebuffer final { 75 private: 76 GLContext* const mGL; 77 GLuint mFB; 78 79 public: 80 explicit ScopedFramebuffer(GLContext* aGL); 81 ~ScopedFramebuffer(); 82 83 const auto& FB() const { return mFB; } 84 operator GLuint() const { return mFB; } 85 }; 86 87 struct ScopedRenderbuffer final { 88 private: 89 GLContext* const mGL; 90 GLuint mRB; 91 92 public: 93 explicit ScopedRenderbuffer(GLContext* aGL); 94 ~ScopedRenderbuffer(); 95 96 GLuint RB() { return mRB; } 97 operator GLuint() const { return mRB; } 98 }; 99 100 struct ScopedBindTexture final { 101 private: 102 GLContext* const mGL; 103 const GLenum mTarget; 104 const GLuint mOldTex; 105 106 public: 107 ScopedBindTexture(GLContext* aGL, GLuint aNewTex, 108 GLenum aTarget = LOCAL_GL_TEXTURE_2D); 109 ~ScopedBindTexture(); 110 }; 111 112 struct ScopedBindRenderbuffer final { 113 private: 114 GLContext* const mGL; 115 GLuint mOldRB; 116 117 private: 118 void Init(); 119 120 public: 121 explicit ScopedBindRenderbuffer(GLContext* aGL); 122 ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB); 123 ~ScopedBindRenderbuffer(); 124 }; 125 126 struct ScopedFramebufferForTexture final { 127 private: 128 GLContext* const mGL; 129 bool mComplete; // True if the framebuffer we create is complete. 130 GLuint mFB; 131 132 public: 133 ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture, 134 GLenum aTarget = LOCAL_GL_TEXTURE_2D); 135 ~ScopedFramebufferForTexture(); 136 137 bool IsComplete() const { return mComplete; } 138 139 GLuint FB() const { 140 MOZ_GL_ASSERT(mGL, IsComplete()); 141 return mFB; 142 } 143 }; 144 145 struct ScopedFramebufferForRenderbuffer final { 146 private: 147 GLContext* const mGL; 148 bool mComplete; // True if the framebuffer we create is complete. 149 GLuint mFB; 150 151 public: 152 ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB); 153 ~ScopedFramebufferForRenderbuffer(); 154 155 bool IsComplete() const { return mComplete; } 156 GLuint FB() const { 157 MOZ_GL_ASSERT(mGL, IsComplete()); 158 return mFB; 159 } 160 }; 161 162 struct ScopedViewportRect final { 163 private: 164 GLContext* const mGL; 165 GLint mSavedViewportRect[4]; 166 167 public: 168 ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, 169 GLsizei height); 170 ~ScopedViewportRect(); 171 }; 172 173 struct ScopedScissorRect final { 174 private: 175 GLContext* const mGL; 176 GLint mSavedScissorRect[4]; 177 178 public: 179 ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, 180 GLsizei height); 181 explicit ScopedScissorRect(GLContext* aGL); 182 ~ScopedScissorRect(); 183 }; 184 185 struct ScopedVertexAttribPointer final { 186 private: 187 GLContext* const mGL; 188 GLuint mAttribIndex; 189 GLint mAttribEnabled; 190 GLint mAttribSize; 191 GLint mAttribStride; 192 GLint mAttribType; 193 GLint mAttribNormalized; 194 GLint mAttribBufferBinding; 195 void* mAttribPointer; 196 GLuint mBoundBuffer; 197 198 public: 199 ScopedVertexAttribPointer(GLContext* aGL, GLuint index, GLint size, 200 GLenum type, realGLboolean normalized, 201 GLsizei stride, GLuint buffer, 202 const GLvoid* pointer); 203 explicit ScopedVertexAttribPointer(GLContext* aGL, GLuint index); 204 ~ScopedVertexAttribPointer(); 205 206 private: 207 void WrapImpl(GLuint index); 208 }; 209 210 struct ScopedPackState final { 211 private: 212 GLContext* const mGL; 213 GLint mAlignment; 214 215 GLuint mPixelBuffer; 216 GLint mRowLength; 217 GLint mSkipPixels; 218 GLint mSkipRows; 219 220 public: 221 explicit ScopedPackState(GLContext* gl); 222 ~ScopedPackState(); 223 224 // Returns whether the stride was handled successfully. 225 bool SetForWidthAndStrideRGBA(GLsizei aWidth, GLsizei aStride); 226 }; 227 228 struct ResetUnpackState final { 229 private: 230 GLContext* const mGL; 231 GLuint mAlignment; 232 233 GLuint mPBO; 234 GLuint mRowLength; 235 GLuint mImageHeight; 236 GLuint mSkipPixels; 237 GLuint mSkipRows; 238 GLuint mSkipImages; 239 240 public: 241 explicit ResetUnpackState(GLContext* gl); 242 ~ResetUnpackState(); 243 }; 244 245 struct ScopedBindPBO final { 246 private: 247 GLContext* const mGL; 248 const GLenum mTarget; 249 const GLuint mPBO; 250 251 public: 252 ScopedBindPBO(GLContext* gl, GLenum target); 253 ~ScopedBindPBO(); 254 }; 255 256 } /* namespace gl */ 257 } /* namespace mozilla */ 258 259 #endif /* SCOPEDGLHELPERS_H_ */