gfx/gl/ScopedGLHelpers.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/gl/ScopedGLHelpers.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,249 @@
     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 +#ifndef SCOPEDGLHELPERS_H_
    1.10 +#define SCOPEDGLHELPERS_H_
    1.11 +
    1.12 +#include "GLContext.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace gl {
    1.16 +
    1.17 +//RAII via CRTP!
    1.18 +template <class Derived>
    1.19 +struct ScopedGLWrapper
    1.20 +{
    1.21 +private:
    1.22 +    bool mIsUnwrapped;
    1.23 +
    1.24 +protected:
    1.25 +    GLContext* const mGL;
    1.26 +
    1.27 +    ScopedGLWrapper(GLContext* gl)
    1.28 +        : mIsUnwrapped(false)
    1.29 +        , mGL(gl)
    1.30 +    {
    1.31 +        MOZ_ASSERT(&ScopedGLWrapper<Derived>::Unwrap == &Derived::Unwrap);
    1.32 +        MOZ_ASSERT(&Derived::UnwrapImpl);
    1.33 +        MOZ_ASSERT(mGL->IsCurrent());
    1.34 +    }
    1.35 +
    1.36 +    virtual ~ScopedGLWrapper() {
    1.37 +        if (!mIsUnwrapped)
    1.38 +            Unwrap();
    1.39 +    }
    1.40 +
    1.41 +public:
    1.42 +    void Unwrap() {
    1.43 +        MOZ_ASSERT(!mIsUnwrapped);
    1.44 +
    1.45 +        Derived* derived = static_cast<Derived*>(this);
    1.46 +        derived->UnwrapImpl();
    1.47 +
    1.48 +        mIsUnwrapped = true;
    1.49 +    }
    1.50 +};
    1.51 +
    1.52 +// Wraps glEnable/Disable.
    1.53 +struct ScopedGLState
    1.54 +    : public ScopedGLWrapper<ScopedGLState>
    1.55 +{
    1.56 +    friend struct ScopedGLWrapper<ScopedGLState>;
    1.57 +
    1.58 +protected:
    1.59 +    const GLenum mCapability;
    1.60 +    bool mOldState;
    1.61 +
    1.62 +public:
    1.63 +    // Use |newState = true| to enable, |false| to disable.
    1.64 +    ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState);
    1.65 +    // variant that doesn't change state; simply records existing state to be
    1.66 +    // restored by the destructor
    1.67 +    ScopedGLState(GLContext* aGL, GLenum aCapability);
    1.68 +
    1.69 +protected:
    1.70 +    void UnwrapImpl();
    1.71 +};
    1.72 +
    1.73 +// Saves and restores with GetUserBoundFB and BindUserFB.
    1.74 +struct ScopedBindFramebuffer
    1.75 +    : public ScopedGLWrapper<ScopedBindFramebuffer>
    1.76 +{
    1.77 +    friend struct ScopedGLWrapper<ScopedBindFramebuffer>;
    1.78 +
    1.79 +protected:
    1.80 +    GLuint mOldFB;
    1.81 +
    1.82 +private:
    1.83 +    void Init();
    1.84 +
    1.85 +public:
    1.86 +    explicit ScopedBindFramebuffer(GLContext* aGL);
    1.87 +    ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB);
    1.88 +
    1.89 +protected:
    1.90 +    void UnwrapImpl();
    1.91 +};
    1.92 +
    1.93 +struct ScopedBindTextureUnit
    1.94 +    : public ScopedGLWrapper<ScopedBindTextureUnit>
    1.95 +{
    1.96 +    friend struct ScopedGLWrapper<ScopedBindTextureUnit>;
    1.97 +
    1.98 +protected:
    1.99 +    GLenum mOldTexUnit;
   1.100 +
   1.101 +public:
   1.102 +    ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit);
   1.103 +
   1.104 +protected:
   1.105 +    void UnwrapImpl();
   1.106 +};
   1.107 +
   1.108 +
   1.109 +struct ScopedTexture
   1.110 +    : public ScopedGLWrapper<ScopedTexture>
   1.111 +{
   1.112 +    friend struct ScopedGLWrapper<ScopedTexture>;
   1.113 +
   1.114 +protected:
   1.115 +    GLuint mTexture;
   1.116 +
   1.117 +public:
   1.118 +    ScopedTexture(GLContext* aGL);
   1.119 +    GLuint Texture() { return mTexture; }
   1.120 +
   1.121 +protected:
   1.122 +    void UnwrapImpl();
   1.123 +};
   1.124 +
   1.125 +
   1.126 +struct ScopedBindTexture
   1.127 +    : public ScopedGLWrapper<ScopedBindTexture>
   1.128 +{
   1.129 +    friend struct ScopedGLWrapper<ScopedBindTexture>;
   1.130 +
   1.131 +protected:
   1.132 +    GLuint mOldTex;
   1.133 +    GLenum mTarget;
   1.134 +
   1.135 +private:
   1.136 +    void Init(GLenum aTarget);
   1.137 +
   1.138 +public:
   1.139 +    ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
   1.140 +                      GLenum aTarget = LOCAL_GL_TEXTURE_2D);
   1.141 +
   1.142 +protected:
   1.143 +    void UnwrapImpl();
   1.144 +};
   1.145 +
   1.146 +
   1.147 +struct ScopedBindRenderbuffer
   1.148 +    : public ScopedGLWrapper<ScopedBindRenderbuffer>
   1.149 +{
   1.150 +    friend struct ScopedGLWrapper<ScopedBindRenderbuffer>;
   1.151 +
   1.152 +protected:
   1.153 +    GLuint mOldRB;
   1.154 +
   1.155 +private:
   1.156 +    void Init();
   1.157 +
   1.158 +public:
   1.159 +    explicit ScopedBindRenderbuffer(GLContext* aGL);
   1.160 +
   1.161 +    ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB);
   1.162 +
   1.163 +protected:
   1.164 +    void UnwrapImpl();
   1.165 +};
   1.166 +
   1.167 +
   1.168 +struct ScopedFramebufferForTexture
   1.169 +    : public ScopedGLWrapper<ScopedFramebufferForTexture>
   1.170 +{
   1.171 +    friend struct ScopedGLWrapper<ScopedFramebufferForTexture>;
   1.172 +
   1.173 +protected:
   1.174 +    bool mComplete; // True if the framebuffer we create is complete.
   1.175 +    GLuint mFB;
   1.176 +
   1.177 +public:
   1.178 +    ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture,
   1.179 +                                GLenum aTarget = LOCAL_GL_TEXTURE_2D);
   1.180 +
   1.181 +    bool IsComplete() const {
   1.182 +        return mComplete;
   1.183 +    }
   1.184 +
   1.185 +    GLuint FB() const {
   1.186 +        MOZ_ASSERT(IsComplete());
   1.187 +        return mFB;
   1.188 +    }
   1.189 +
   1.190 +protected:
   1.191 +    void UnwrapImpl();
   1.192 +};
   1.193 +
   1.194 +struct ScopedFramebufferForRenderbuffer
   1.195 +    : public ScopedGLWrapper<ScopedFramebufferForRenderbuffer>
   1.196 +{
   1.197 +    friend struct ScopedGLWrapper<ScopedFramebufferForRenderbuffer>;
   1.198 +
   1.199 +protected:
   1.200 +    bool mComplete; // True if the framebuffer we create is complete.
   1.201 +    GLuint mFB;
   1.202 +
   1.203 +public:
   1.204 +    ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB);
   1.205 +
   1.206 +    bool IsComplete() const {
   1.207 +        return mComplete;
   1.208 +    }
   1.209 +
   1.210 +    GLuint FB() const {
   1.211 +        return mFB;
   1.212 +    }
   1.213 +
   1.214 +protected:
   1.215 +    void UnwrapImpl();
   1.216 +};
   1.217 +
   1.218 +struct ScopedViewportRect
   1.219 +    : public ScopedGLWrapper<ScopedViewportRect>
   1.220 +{
   1.221 +    friend struct ScopedGLWrapper<ScopedViewportRect>;
   1.222 +
   1.223 +protected:
   1.224 +    GLint mSavedViewportRect[4];
   1.225 +
   1.226 +public:
   1.227 +    ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
   1.228 +
   1.229 +protected:
   1.230 +    void UnwrapImpl();
   1.231 +};
   1.232 +
   1.233 +struct ScopedScissorRect
   1.234 +    : public ScopedGLWrapper<ScopedScissorRect>
   1.235 +{
   1.236 +    friend struct ScopedGLWrapper<ScopedScissorRect>;
   1.237 +
   1.238 +protected:
   1.239 +    GLint mSavedScissorRect[4];
   1.240 +
   1.241 +public:
   1.242 +    ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
   1.243 +    explicit ScopedScissorRect(GLContext* aGL);
   1.244 +
   1.245 +protected:
   1.246 +    void UnwrapImpl();
   1.247 +};
   1.248 +
   1.249 +} /* namespace gl */
   1.250 +} /* namespace mozilla */
   1.251 +
   1.252 +#endif /* SCOPEDGLHELPERS_H_ */

mercurial