1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/gl/ScopedGLHelpers.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,296 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "GLContext.h" 1.10 +#include "ScopedGLHelpers.h" 1.11 + 1.12 +namespace mozilla { 1.13 +namespace gl { 1.14 + 1.15 +/* ScopedGLState - Wraps glEnable/glDisable. **********************************/ 1.16 + 1.17 +// Use |newState = true| to enable, |false| to disable. 1.18 +ScopedGLState::ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState) 1.19 + : ScopedGLWrapper<ScopedGLState>(aGL) 1.20 + , mCapability(aCapability) 1.21 +{ 1.22 + mOldState = mGL->fIsEnabled(mCapability); 1.23 + 1.24 + // Early out if we're already in the right state. 1.25 + if (aNewState == mOldState) 1.26 + return; 1.27 + 1.28 + if (aNewState) { 1.29 + mGL->fEnable(mCapability); 1.30 + } else { 1.31 + mGL->fDisable(mCapability); 1.32 + } 1.33 +} 1.34 + 1.35 +ScopedGLState::ScopedGLState(GLContext* aGL, GLenum aCapability) 1.36 + : ScopedGLWrapper<ScopedGLState>(aGL) 1.37 + , mCapability(aCapability) 1.38 +{ 1.39 + mOldState = mGL->fIsEnabled(mCapability); 1.40 +} 1.41 + 1.42 +void 1.43 +ScopedGLState::UnwrapImpl() 1.44 +{ 1.45 + if (mOldState) { 1.46 + mGL->fEnable(mCapability); 1.47 + } else { 1.48 + mGL->fDisable(mCapability); 1.49 + } 1.50 +} 1.51 + 1.52 + 1.53 +/* ScopedBindFramebuffer - Saves and restores with GetUserBoundFB and BindUserFB. */ 1.54 + 1.55 +void 1.56 +ScopedBindFramebuffer::Init() 1.57 +{ 1.58 + mOldFB = mGL->GetFB(); 1.59 +} 1.60 + 1.61 +ScopedBindFramebuffer::ScopedBindFramebuffer(GLContext* aGL) 1.62 + : ScopedGLWrapper<ScopedBindFramebuffer>(aGL) 1.63 +{ 1.64 + Init(); 1.65 +} 1.66 + 1.67 +ScopedBindFramebuffer::ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB) 1.68 + : ScopedGLWrapper<ScopedBindFramebuffer>(aGL) 1.69 +{ 1.70 + Init(); 1.71 + mGL->BindFB(aNewFB); 1.72 +} 1.73 + 1.74 +void 1.75 +ScopedBindFramebuffer::UnwrapImpl() 1.76 +{ 1.77 + // Check that we're not falling out of scope after the current context changed. 1.78 + MOZ_ASSERT(mGL->IsCurrent()); 1.79 + 1.80 + mGL->BindFB(mOldFB); 1.81 +} 1.82 + 1.83 + 1.84 +/* ScopedBindTextureUnit ******************************************************/ 1.85 + 1.86 +ScopedBindTextureUnit::ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit) 1.87 + : ScopedGLWrapper<ScopedBindTextureUnit>(aGL) 1.88 +{ 1.89 + MOZ_ASSERT(aTexUnit >= LOCAL_GL_TEXTURE0); 1.90 + mGL->GetUIntegerv(LOCAL_GL_ACTIVE_TEXTURE, &mOldTexUnit); 1.91 + mGL->fActiveTexture(aTexUnit); 1.92 +} 1.93 + 1.94 +void 1.95 +ScopedBindTextureUnit::UnwrapImpl() { 1.96 + // Check that we're not falling out of scope after the current context changed. 1.97 + MOZ_ASSERT(mGL->IsCurrent()); 1.98 + 1.99 + mGL->fActiveTexture(mOldTexUnit); 1.100 +} 1.101 + 1.102 + 1.103 +/* ScopedTexture **************************************************************/ 1.104 + 1.105 +ScopedTexture::ScopedTexture(GLContext* aGL) 1.106 + : ScopedGLWrapper<ScopedTexture>(aGL) 1.107 +{ 1.108 + mGL->fGenTextures(1, &mTexture); 1.109 +} 1.110 + 1.111 +void 1.112 +ScopedTexture::UnwrapImpl() 1.113 +{ 1.114 + // Check that we're not falling out of scope after 1.115 + // the current context changed. 1.116 + MOZ_ASSERT(mGL->IsCurrent()); 1.117 + 1.118 + mGL->fDeleteTextures(1, &mTexture); 1.119 +} 1.120 + 1.121 +/* ScopedBindTexture **********************************************************/ 1.122 +void 1.123 +ScopedBindTexture::Init(GLenum aTarget) 1.124 +{ 1.125 + mTarget = aTarget; 1.126 + mOldTex = 0; 1.127 + GLenum bindingTarget = (aTarget == LOCAL_GL_TEXTURE_2D) ? LOCAL_GL_TEXTURE_BINDING_2D 1.128 + : (aTarget == LOCAL_GL_TEXTURE_RECTANGLE_ARB) ? LOCAL_GL_TEXTURE_BINDING_RECTANGLE_ARB 1.129 + : (aTarget == LOCAL_GL_TEXTURE_CUBE_MAP) ? LOCAL_GL_TEXTURE_BINDING_CUBE_MAP 1.130 + : (aTarget == LOCAL_GL_TEXTURE_EXTERNAL) ? LOCAL_GL_TEXTURE_BINDING_EXTERNAL 1.131 + : LOCAL_GL_NONE; 1.132 + MOZ_ASSERT(bindingTarget != LOCAL_GL_NONE); 1.133 + mGL->GetUIntegerv(bindingTarget, &mOldTex); 1.134 +} 1.135 + 1.136 +ScopedBindTexture::ScopedBindTexture(GLContext* aGL, GLuint aNewTex, GLenum aTarget) 1.137 + : ScopedGLWrapper<ScopedBindTexture>(aGL) 1.138 + { 1.139 + Init(aTarget); 1.140 + mGL->fBindTexture(aTarget, aNewTex); 1.141 + } 1.142 + 1.143 +void 1.144 +ScopedBindTexture::UnwrapImpl() 1.145 +{ 1.146 + // Check that we're not falling out of scope after the current context changed. 1.147 + MOZ_ASSERT(mGL->IsCurrent()); 1.148 + 1.149 + mGL->fBindTexture(mTarget, mOldTex); 1.150 +} 1.151 + 1.152 + 1.153 +/* ScopedBindRenderbuffer *****************************************************/ 1.154 + 1.155 +void 1.156 +ScopedBindRenderbuffer::Init() 1.157 +{ 1.158 + mOldRB = 0; 1.159 + mGL->GetUIntegerv(LOCAL_GL_RENDERBUFFER_BINDING, &mOldRB); 1.160 +} 1.161 + 1.162 +ScopedBindRenderbuffer::ScopedBindRenderbuffer(GLContext* aGL) 1.163 + : ScopedGLWrapper<ScopedBindRenderbuffer>(aGL) 1.164 +{ 1.165 + Init(); 1.166 +} 1.167 + 1.168 +ScopedBindRenderbuffer::ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB) 1.169 + : ScopedGLWrapper<ScopedBindRenderbuffer>(aGL) 1.170 +{ 1.171 + Init(); 1.172 + mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, aNewRB); 1.173 +} 1.174 + 1.175 +void 1.176 +ScopedBindRenderbuffer::UnwrapImpl() { 1.177 + // Check that we're not falling out of scope after the current context changed. 1.178 + MOZ_ASSERT(mGL->IsCurrent()); 1.179 + 1.180 + mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mOldRB); 1.181 +} 1.182 + 1.183 + 1.184 +/* ScopedFramebufferForTexture ************************************************/ 1.185 +ScopedFramebufferForTexture::ScopedFramebufferForTexture(GLContext* aGL, 1.186 + GLuint aTexture, 1.187 + GLenum aTarget) 1.188 + : ScopedGLWrapper<ScopedFramebufferForTexture>(aGL) 1.189 + , mComplete(false) 1.190 + , mFB(0) 1.191 +{ 1.192 + mGL->fGenFramebuffers(1, &mFB); 1.193 + ScopedBindFramebuffer autoFB(aGL, mFB); 1.194 + mGL->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER, 1.195 + LOCAL_GL_COLOR_ATTACHMENT0, 1.196 + aTarget, 1.197 + aTexture, 1.198 + 0); 1.199 + 1.200 + GLenum status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER); 1.201 + if (status == LOCAL_GL_FRAMEBUFFER_COMPLETE) { 1.202 + mComplete = true; 1.203 + } else { 1.204 + mGL->fDeleteFramebuffers(1, &mFB); 1.205 + mFB = 0; 1.206 + } 1.207 +} 1.208 + 1.209 +void ScopedFramebufferForTexture::UnwrapImpl() 1.210 +{ 1.211 + if (!mFB) 1.212 + return; 1.213 + 1.214 + mGL->fDeleteFramebuffers(1, &mFB); 1.215 + mFB = 0; 1.216 +} 1.217 + 1.218 + 1.219 +/* ScopedFramebufferForRenderbuffer *******************************************/ 1.220 + 1.221 + 1.222 +ScopedFramebufferForRenderbuffer::ScopedFramebufferForRenderbuffer(GLContext* aGL, 1.223 + GLuint aRB) 1.224 + : ScopedGLWrapper<ScopedFramebufferForRenderbuffer>(aGL) 1.225 + , mComplete(false) 1.226 + , mFB(0) 1.227 +{ 1.228 + mGL->fGenFramebuffers(1, &mFB); 1.229 + ScopedBindFramebuffer autoFB(aGL, mFB); 1.230 + mGL->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, 1.231 + LOCAL_GL_COLOR_ATTACHMENT0, 1.232 + LOCAL_GL_RENDERBUFFER, 1.233 + aRB); 1.234 + 1.235 + GLenum status = mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER); 1.236 + if (status == LOCAL_GL_FRAMEBUFFER_COMPLETE) { 1.237 + mComplete = true; 1.238 + } else { 1.239 + mGL->fDeleteFramebuffers(1, &mFB); 1.240 + mFB = 0; 1.241 + } 1.242 +} 1.243 + 1.244 +void 1.245 +ScopedFramebufferForRenderbuffer::UnwrapImpl() 1.246 +{ 1.247 + if (!mFB) 1.248 + return; 1.249 + 1.250 + mGL->fDeleteFramebuffers(1, &mFB); 1.251 + mFB = 0; 1.252 +} 1.253 + 1.254 +/* ScopedViewportRect *********************************************************/ 1.255 + 1.256 +ScopedViewportRect::ScopedViewportRect(GLContext* aGL, 1.257 + GLint x, GLint y, 1.258 + GLsizei width, GLsizei height) 1.259 + : ScopedGLWrapper<ScopedViewportRect>(aGL) 1.260 +{ 1.261 + mGL->fGetIntegerv(LOCAL_GL_VIEWPORT, mSavedViewportRect); 1.262 + mGL->fViewport(x, y, width, height); 1.263 +} 1.264 + 1.265 +void ScopedViewportRect::UnwrapImpl() 1.266 +{ 1.267 + mGL->fViewport(mSavedViewportRect[0], 1.268 + mSavedViewportRect[1], 1.269 + mSavedViewportRect[2], 1.270 + mSavedViewportRect[3]); 1.271 +} 1.272 + 1.273 +/* ScopedScissorRect **********************************************************/ 1.274 + 1.275 +ScopedScissorRect::ScopedScissorRect(GLContext* aGL, 1.276 + GLint x, GLint y, 1.277 + GLsizei width, GLsizei height) 1.278 + : ScopedGLWrapper<ScopedScissorRect>(aGL) 1.279 +{ 1.280 + mGL->fGetIntegerv(LOCAL_GL_SCISSOR_BOX, mSavedScissorRect); 1.281 + mGL->fScissor(x, y, width, height); 1.282 +} 1.283 + 1.284 +ScopedScissorRect::ScopedScissorRect(GLContext* aGL) 1.285 + : ScopedGLWrapper<ScopedScissorRect>(aGL) 1.286 +{ 1.287 + mGL->fGetIntegerv(LOCAL_GL_SCISSOR_BOX, mSavedScissorRect); 1.288 +} 1.289 + 1.290 +void ScopedScissorRect::UnwrapImpl() 1.291 +{ 1.292 + mGL->fScissor(mSavedScissorRect[0], 1.293 + mSavedScissorRect[1], 1.294 + mSavedScissorRect[2], 1.295 + mSavedScissorRect[3]); 1.296 +} 1.297 + 1.298 +} /* namespace gl */ 1.299 +} /* namespace mozilla */