gfx/gl/ScopedGLHelpers.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "GLContext.h"
michael@0 7 #include "ScopedGLHelpers.h"
michael@0 8
michael@0 9 namespace mozilla {
michael@0 10 namespace gl {
michael@0 11
michael@0 12 /* ScopedGLState - Wraps glEnable/glDisable. **********************************/
michael@0 13
michael@0 14 // Use |newState = true| to enable, |false| to disable.
michael@0 15 ScopedGLState::ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState)
michael@0 16 : ScopedGLWrapper<ScopedGLState>(aGL)
michael@0 17 , mCapability(aCapability)
michael@0 18 {
michael@0 19 mOldState = mGL->fIsEnabled(mCapability);
michael@0 20
michael@0 21 // Early out if we're already in the right state.
michael@0 22 if (aNewState == mOldState)
michael@0 23 return;
michael@0 24
michael@0 25 if (aNewState) {
michael@0 26 mGL->fEnable(mCapability);
michael@0 27 } else {
michael@0 28 mGL->fDisable(mCapability);
michael@0 29 }
michael@0 30 }
michael@0 31
michael@0 32 ScopedGLState::ScopedGLState(GLContext* aGL, GLenum aCapability)
michael@0 33 : ScopedGLWrapper<ScopedGLState>(aGL)
michael@0 34 , mCapability(aCapability)
michael@0 35 {
michael@0 36 mOldState = mGL->fIsEnabled(mCapability);
michael@0 37 }
michael@0 38
michael@0 39 void
michael@0 40 ScopedGLState::UnwrapImpl()
michael@0 41 {
michael@0 42 if (mOldState) {
michael@0 43 mGL->fEnable(mCapability);
michael@0 44 } else {
michael@0 45 mGL->fDisable(mCapability);
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49
michael@0 50 /* ScopedBindFramebuffer - Saves and restores with GetUserBoundFB and BindUserFB. */
michael@0 51
michael@0 52 void
michael@0 53 ScopedBindFramebuffer::Init()
michael@0 54 {
michael@0 55 mOldFB = mGL->GetFB();
michael@0 56 }
michael@0 57
michael@0 58 ScopedBindFramebuffer::ScopedBindFramebuffer(GLContext* aGL)
michael@0 59 : ScopedGLWrapper<ScopedBindFramebuffer>(aGL)
michael@0 60 {
michael@0 61 Init();
michael@0 62 }
michael@0 63
michael@0 64 ScopedBindFramebuffer::ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB)
michael@0 65 : ScopedGLWrapper<ScopedBindFramebuffer>(aGL)
michael@0 66 {
michael@0 67 Init();
michael@0 68 mGL->BindFB(aNewFB);
michael@0 69 }
michael@0 70
michael@0 71 void
michael@0 72 ScopedBindFramebuffer::UnwrapImpl()
michael@0 73 {
michael@0 74 // Check that we're not falling out of scope after the current context changed.
michael@0 75 MOZ_ASSERT(mGL->IsCurrent());
michael@0 76
michael@0 77 mGL->BindFB(mOldFB);
michael@0 78 }
michael@0 79
michael@0 80
michael@0 81 /* ScopedBindTextureUnit ******************************************************/
michael@0 82
michael@0 83 ScopedBindTextureUnit::ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit)
michael@0 84 : ScopedGLWrapper<ScopedBindTextureUnit>(aGL)
michael@0 85 {
michael@0 86 MOZ_ASSERT(aTexUnit >= LOCAL_GL_TEXTURE0);
michael@0 87 mGL->GetUIntegerv(LOCAL_GL_ACTIVE_TEXTURE, &mOldTexUnit);
michael@0 88 mGL->fActiveTexture(aTexUnit);
michael@0 89 }
michael@0 90
michael@0 91 void
michael@0 92 ScopedBindTextureUnit::UnwrapImpl() {
michael@0 93 // Check that we're not falling out of scope after the current context changed.
michael@0 94 MOZ_ASSERT(mGL->IsCurrent());
michael@0 95
michael@0 96 mGL->fActiveTexture(mOldTexUnit);
michael@0 97 }
michael@0 98
michael@0 99
michael@0 100 /* ScopedTexture **************************************************************/
michael@0 101
michael@0 102 ScopedTexture::ScopedTexture(GLContext* aGL)
michael@0 103 : ScopedGLWrapper<ScopedTexture>(aGL)
michael@0 104 {
michael@0 105 mGL->fGenTextures(1, &mTexture);
michael@0 106 }
michael@0 107
michael@0 108 void
michael@0 109 ScopedTexture::UnwrapImpl()
michael@0 110 {
michael@0 111 // Check that we're not falling out of scope after
michael@0 112 // the current context changed.
michael@0 113 MOZ_ASSERT(mGL->IsCurrent());
michael@0 114
michael@0 115 mGL->fDeleteTextures(1, &mTexture);
michael@0 116 }
michael@0 117
michael@0 118 /* ScopedBindTexture **********************************************************/
michael@0 119 void
michael@0 120 ScopedBindTexture::Init(GLenum aTarget)
michael@0 121 {
michael@0 122 mTarget = aTarget;
michael@0 123 mOldTex = 0;
michael@0 124 GLenum bindingTarget = (aTarget == LOCAL_GL_TEXTURE_2D) ? LOCAL_GL_TEXTURE_BINDING_2D
michael@0 125 : (aTarget == LOCAL_GL_TEXTURE_RECTANGLE_ARB) ? LOCAL_GL_TEXTURE_BINDING_RECTANGLE_ARB
michael@0 126 : (aTarget == LOCAL_GL_TEXTURE_CUBE_MAP) ? LOCAL_GL_TEXTURE_BINDING_CUBE_MAP
michael@0 127 : (aTarget == LOCAL_GL_TEXTURE_EXTERNAL) ? LOCAL_GL_TEXTURE_BINDING_EXTERNAL
michael@0 128 : LOCAL_GL_NONE;
michael@0 129 MOZ_ASSERT(bindingTarget != LOCAL_GL_NONE);
michael@0 130 mGL->GetUIntegerv(bindingTarget, &mOldTex);
michael@0 131 }
michael@0 132
michael@0 133 ScopedBindTexture::ScopedBindTexture(GLContext* aGL, GLuint aNewTex, GLenum aTarget)
michael@0 134 : ScopedGLWrapper<ScopedBindTexture>(aGL)
michael@0 135 {
michael@0 136 Init(aTarget);
michael@0 137 mGL->fBindTexture(aTarget, aNewTex);
michael@0 138 }
michael@0 139
michael@0 140 void
michael@0 141 ScopedBindTexture::UnwrapImpl()
michael@0 142 {
michael@0 143 // Check that we're not falling out of scope after the current context changed.
michael@0 144 MOZ_ASSERT(mGL->IsCurrent());
michael@0 145
michael@0 146 mGL->fBindTexture(mTarget, mOldTex);
michael@0 147 }
michael@0 148
michael@0 149
michael@0 150 /* ScopedBindRenderbuffer *****************************************************/
michael@0 151
michael@0 152 void
michael@0 153 ScopedBindRenderbuffer::Init()
michael@0 154 {
michael@0 155 mOldRB = 0;
michael@0 156 mGL->GetUIntegerv(LOCAL_GL_RENDERBUFFER_BINDING, &mOldRB);
michael@0 157 }
michael@0 158
michael@0 159 ScopedBindRenderbuffer::ScopedBindRenderbuffer(GLContext* aGL)
michael@0 160 : ScopedGLWrapper<ScopedBindRenderbuffer>(aGL)
michael@0 161 {
michael@0 162 Init();
michael@0 163 }
michael@0 164
michael@0 165 ScopedBindRenderbuffer::ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB)
michael@0 166 : ScopedGLWrapper<ScopedBindRenderbuffer>(aGL)
michael@0 167 {
michael@0 168 Init();
michael@0 169 mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, aNewRB);
michael@0 170 }
michael@0 171
michael@0 172 void
michael@0 173 ScopedBindRenderbuffer::UnwrapImpl() {
michael@0 174 // Check that we're not falling out of scope after the current context changed.
michael@0 175 MOZ_ASSERT(mGL->IsCurrent());
michael@0 176
michael@0 177 mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mOldRB);
michael@0 178 }
michael@0 179
michael@0 180
michael@0 181 /* ScopedFramebufferForTexture ************************************************/
michael@0 182 ScopedFramebufferForTexture::ScopedFramebufferForTexture(GLContext* aGL,
michael@0 183 GLuint aTexture,
michael@0 184 GLenum aTarget)
michael@0 185 : ScopedGLWrapper<ScopedFramebufferForTexture>(aGL)
michael@0 186 , mComplete(false)
michael@0 187 , mFB(0)
michael@0 188 {
michael@0 189 mGL->fGenFramebuffers(1, &mFB);
michael@0 190 ScopedBindFramebuffer autoFB(aGL, mFB);
michael@0 191 mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
michael@0 192 LOCAL_GL_COLOR_ATTACHMENT0,
michael@0 193 aTarget,
michael@0 194 aTexture,
michael@0 195 0);
michael@0 196
michael@0 197 GLenum status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
michael@0 198 if (status == LOCAL_GL_FRAMEBUFFER_COMPLETE) {
michael@0 199 mComplete = true;
michael@0 200 } else {
michael@0 201 mGL->fDeleteFramebuffers(1, &mFB);
michael@0 202 mFB = 0;
michael@0 203 }
michael@0 204 }
michael@0 205
michael@0 206 void ScopedFramebufferForTexture::UnwrapImpl()
michael@0 207 {
michael@0 208 if (!mFB)
michael@0 209 return;
michael@0 210
michael@0 211 mGL->fDeleteFramebuffers(1, &mFB);
michael@0 212 mFB = 0;
michael@0 213 }
michael@0 214
michael@0 215
michael@0 216 /* ScopedFramebufferForRenderbuffer *******************************************/
michael@0 217
michael@0 218
michael@0 219 ScopedFramebufferForRenderbuffer::ScopedFramebufferForRenderbuffer(GLContext* aGL,
michael@0 220 GLuint aRB)
michael@0 221 : ScopedGLWrapper<ScopedFramebufferForRenderbuffer>(aGL)
michael@0 222 , mComplete(false)
michael@0 223 , mFB(0)
michael@0 224 {
michael@0 225 mGL->fGenFramebuffers(1, &mFB);
michael@0 226 ScopedBindFramebuffer autoFB(aGL, mFB);
michael@0 227 mGL->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
michael@0 228 LOCAL_GL_COLOR_ATTACHMENT0,
michael@0 229 LOCAL_GL_RENDERBUFFER,
michael@0 230 aRB);
michael@0 231
michael@0 232 GLenum status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
michael@0 233 if (status == LOCAL_GL_FRAMEBUFFER_COMPLETE) {
michael@0 234 mComplete = true;
michael@0 235 } else {
michael@0 236 mGL->fDeleteFramebuffers(1, &mFB);
michael@0 237 mFB = 0;
michael@0 238 }
michael@0 239 }
michael@0 240
michael@0 241 void
michael@0 242 ScopedFramebufferForRenderbuffer::UnwrapImpl()
michael@0 243 {
michael@0 244 if (!mFB)
michael@0 245 return;
michael@0 246
michael@0 247 mGL->fDeleteFramebuffers(1, &mFB);
michael@0 248 mFB = 0;
michael@0 249 }
michael@0 250
michael@0 251 /* ScopedViewportRect *********************************************************/
michael@0 252
michael@0 253 ScopedViewportRect::ScopedViewportRect(GLContext* aGL,
michael@0 254 GLint x, GLint y,
michael@0 255 GLsizei width, GLsizei height)
michael@0 256 : ScopedGLWrapper<ScopedViewportRect>(aGL)
michael@0 257 {
michael@0 258 mGL->fGetIntegerv(LOCAL_GL_VIEWPORT, mSavedViewportRect);
michael@0 259 mGL->fViewport(x, y, width, height);
michael@0 260 }
michael@0 261
michael@0 262 void ScopedViewportRect::UnwrapImpl()
michael@0 263 {
michael@0 264 mGL->fViewport(mSavedViewportRect[0],
michael@0 265 mSavedViewportRect[1],
michael@0 266 mSavedViewportRect[2],
michael@0 267 mSavedViewportRect[3]);
michael@0 268 }
michael@0 269
michael@0 270 /* ScopedScissorRect **********************************************************/
michael@0 271
michael@0 272 ScopedScissorRect::ScopedScissorRect(GLContext* aGL,
michael@0 273 GLint x, GLint y,
michael@0 274 GLsizei width, GLsizei height)
michael@0 275 : ScopedGLWrapper<ScopedScissorRect>(aGL)
michael@0 276 {
michael@0 277 mGL->fGetIntegerv(LOCAL_GL_SCISSOR_BOX, mSavedScissorRect);
michael@0 278 mGL->fScissor(x, y, width, height);
michael@0 279 }
michael@0 280
michael@0 281 ScopedScissorRect::ScopedScissorRect(GLContext* aGL)
michael@0 282 : ScopedGLWrapper<ScopedScissorRect>(aGL)
michael@0 283 {
michael@0 284 mGL->fGetIntegerv(LOCAL_GL_SCISSOR_BOX, mSavedScissorRect);
michael@0 285 }
michael@0 286
michael@0 287 void ScopedScissorRect::UnwrapImpl()
michael@0 288 {
michael@0 289 mGL->fScissor(mSavedScissorRect[0],
michael@0 290 mSavedScissorRect[1],
michael@0 291 mSavedScissorRect[2],
michael@0 292 mSavedScissorRect[3]);
michael@0 293 }
michael@0 294
michael@0 295 } /* namespace gl */
michael@0 296 } /* namespace mozilla */

mercurial