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 #include "mozilla/UniquePtr.h" 11 12 namespace mozilla { 13 namespace gl { 14 15 class GLContext; 16 17 #ifdef DEBUG 18 bool IsContextCurrent(GLContext* gl); 19 #endif 20 21 // Wraps glEnable/Disable. 22 struct ScopedGLState final { 23 private: 24 GLContext* const mGL; 25 const GLenum mCapability; 26 bool mOldState; 27 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(); 35 }; 36 37 // Saves and restores with GetUserBoundFB and BindUserFB. 38 struct ScopedBindFramebuffer final { 39 private: 40 GLContext* const mGL; 41 GLuint mOldReadFB; 42 GLuint mOldDrawFB; 43 44 void Init(); 45 46 public: 47 explicit ScopedBindFramebuffer(GLContext* aGL); 48 ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB); 49 ~ScopedBindFramebuffer(); 50 }; 51 52 struct ScopedBindTextureUnit final { 53 private: 54 GLContext* const mGL; 55 GLenum mOldTexUnit; 56 57 public: 58 ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit); 59 ~ScopedBindTextureUnit(); 60 }; 61 62 struct ScopedTexture final { 63 private: 64 GLContext* const mGL; 65 GLuint mTexture; 66 67 public: 68 explicit ScopedTexture(GLContext* aGL); 69 ~ScopedTexture(); 70 Texturefinal71 GLuint Texture() const { return mTexture; } GLuintfinal72 operator GLuint() const { return mTexture; } 73 }; 74 75 struct ScopedFramebuffer final { 76 private: 77 GLContext* const mGL; 78 GLuint mFB; 79 80 public: 81 explicit ScopedFramebuffer(GLContext* aGL); 82 ~ScopedFramebuffer(); 83 FBfinal84 const auto& FB() const { return mFB; } GLuintfinal85 operator GLuint() const { return mFB; } 86 }; 87 88 struct ScopedRenderbuffer final { 89 private: 90 GLContext* const mGL; 91 GLuint mRB; 92 93 public: 94 explicit ScopedRenderbuffer(GLContext* aGL); 95 ~ScopedRenderbuffer(); 96 RBfinal97 GLuint RB() { return mRB; } GLuintfinal98 operator GLuint() const { return mRB; } 99 }; 100 101 struct ScopedBindTexture final { 102 private: 103 GLContext* const mGL; 104 const GLenum mTarget; 105 const GLuint mOldTex; 106 107 public: 108 ScopedBindTexture(GLContext* aGL, GLuint aNewTex, 109 GLenum aTarget = LOCAL_GL_TEXTURE_2D); 110 ~ScopedBindTexture(); 111 }; 112 113 struct ScopedBindRenderbuffer final { 114 private: 115 GLContext* const mGL; 116 GLuint mOldRB; 117 118 private: 119 void Init(); 120 121 public: 122 explicit ScopedBindRenderbuffer(GLContext* aGL); 123 ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB); 124 ~ScopedBindRenderbuffer(); 125 }; 126 127 struct ScopedFramebufferForTexture final { 128 private: 129 GLContext* const mGL; 130 bool mComplete; // True if the framebuffer we create is complete. 131 GLuint mFB; 132 133 public: 134 ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture, 135 GLenum aTarget = LOCAL_GL_TEXTURE_2D); 136 ~ScopedFramebufferForTexture(); 137 IsCompletefinal138 bool IsComplete() const { return mComplete; } 139 FBfinal140 GLuint FB() const { 141 MOZ_GL_ASSERT(mGL, IsComplete()); 142 return mFB; 143 } 144 }; 145 146 struct ScopedFramebufferForRenderbuffer final { 147 private: 148 GLContext* const mGL; 149 bool mComplete; // True if the framebuffer we create is complete. 150 GLuint mFB; 151 152 public: 153 ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB); 154 ~ScopedFramebufferForRenderbuffer(); 155 IsCompletefinal156 bool IsComplete() const { return mComplete; } FBfinal157 GLuint FB() const { 158 MOZ_GL_ASSERT(mGL, IsComplete()); 159 return mFB; 160 } 161 }; 162 163 struct ScopedViewportRect final { 164 private: 165 GLContext* const mGL; 166 GLint mSavedViewportRect[4]; 167 168 public: 169 ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, 170 GLsizei height); 171 ~ScopedViewportRect(); 172 }; 173 174 struct ScopedScissorRect final { 175 private: 176 GLContext* const mGL; 177 GLint mSavedScissorRect[4]; 178 179 public: 180 ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, 181 GLsizei height); 182 explicit ScopedScissorRect(GLContext* aGL); 183 ~ScopedScissorRect(); 184 }; 185 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; 198 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(); 206 207 private: 208 void WrapImpl(GLuint index); 209 }; 210 211 struct ScopedPackState final { 212 private: 213 GLContext* const mGL; 214 GLint mAlignment; 215 216 GLuint mPixelBuffer; 217 GLint mRowLength; 218 GLint mSkipPixels; 219 GLint mSkipRows; 220 221 public: 222 explicit ScopedPackState(GLContext* gl); 223 ~ScopedPackState(); 224 225 // Returns whether the stride was handled successfully. 226 bool SetForWidthAndStrideRGBA(GLsizei aWidth, GLsizei aStride); 227 }; 228 229 struct ResetUnpackState final { 230 private: 231 GLContext* const mGL; 232 GLuint mAlignment; 233 234 GLuint mPBO; 235 GLuint mRowLength; 236 GLuint mImageHeight; 237 GLuint mSkipPixels; 238 GLuint mSkipRows; 239 GLuint mSkipImages; 240 241 public: 242 explicit ResetUnpackState(GLContext* gl); 243 ~ResetUnpackState(); 244 }; 245 246 struct ScopedBindPBO final { 247 private: 248 GLContext* const mGL; 249 const GLenum mTarget; 250 const GLuint mPBO; 251 252 public: 253 ScopedBindPBO(GLContext* gl, GLenum target); 254 ~ScopedBindPBO(); 255 }; 256 257 } /* namespace gl */ 258 } /* namespace mozilla */ 259 260 #endif /* SCOPEDGLHELPERS_H_ */ 261