gfx/gl/ScopedGLHelpers.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     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/. */
     6 #ifndef SCOPEDGLHELPERS_H_
     7 #define SCOPEDGLHELPERS_H_
     9 #include "GLContext.h"
    11 namespace mozilla {
    12 namespace gl {
    14 //RAII via CRTP!
    15 template <class Derived>
    16 struct ScopedGLWrapper
    17 {
    18 private:
    19     bool mIsUnwrapped;
    21 protected:
    22     GLContext* const mGL;
    24     ScopedGLWrapper(GLContext* gl)
    25         : mIsUnwrapped(false)
    26         , mGL(gl)
    27     {
    28         MOZ_ASSERT(&ScopedGLWrapper<Derived>::Unwrap == &Derived::Unwrap);
    29         MOZ_ASSERT(&Derived::UnwrapImpl);
    30         MOZ_ASSERT(mGL->IsCurrent());
    31     }
    33     virtual ~ScopedGLWrapper() {
    34         if (!mIsUnwrapped)
    35             Unwrap();
    36     }
    38 public:
    39     void Unwrap() {
    40         MOZ_ASSERT(!mIsUnwrapped);
    42         Derived* derived = static_cast<Derived*>(this);
    43         derived->UnwrapImpl();
    45         mIsUnwrapped = true;
    46     }
    47 };
    49 // Wraps glEnable/Disable.
    50 struct ScopedGLState
    51     : public ScopedGLWrapper<ScopedGLState>
    52 {
    53     friend struct ScopedGLWrapper<ScopedGLState>;
    55 protected:
    56     const GLenum mCapability;
    57     bool mOldState;
    59 public:
    60     // Use |newState = true| to enable, |false| to disable.
    61     ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState);
    62     // variant that doesn't change state; simply records existing state to be
    63     // restored by the destructor
    64     ScopedGLState(GLContext* aGL, GLenum aCapability);
    66 protected:
    67     void UnwrapImpl();
    68 };
    70 // Saves and restores with GetUserBoundFB and BindUserFB.
    71 struct ScopedBindFramebuffer
    72     : public ScopedGLWrapper<ScopedBindFramebuffer>
    73 {
    74     friend struct ScopedGLWrapper<ScopedBindFramebuffer>;
    76 protected:
    77     GLuint mOldFB;
    79 private:
    80     void Init();
    82 public:
    83     explicit ScopedBindFramebuffer(GLContext* aGL);
    84     ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB);
    86 protected:
    87     void UnwrapImpl();
    88 };
    90 struct ScopedBindTextureUnit
    91     : public ScopedGLWrapper<ScopedBindTextureUnit>
    92 {
    93     friend struct ScopedGLWrapper<ScopedBindTextureUnit>;
    95 protected:
    96     GLenum mOldTexUnit;
    98 public:
    99     ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit);
   101 protected:
   102     void UnwrapImpl();
   103 };
   106 struct ScopedTexture
   107     : public ScopedGLWrapper<ScopedTexture>
   108 {
   109     friend struct ScopedGLWrapper<ScopedTexture>;
   111 protected:
   112     GLuint mTexture;
   114 public:
   115     ScopedTexture(GLContext* aGL);
   116     GLuint Texture() { return mTexture; }
   118 protected:
   119     void UnwrapImpl();
   120 };
   123 struct ScopedBindTexture
   124     : public ScopedGLWrapper<ScopedBindTexture>
   125 {
   126     friend struct ScopedGLWrapper<ScopedBindTexture>;
   128 protected:
   129     GLuint mOldTex;
   130     GLenum mTarget;
   132 private:
   133     void Init(GLenum aTarget);
   135 public:
   136     ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
   137                       GLenum aTarget = LOCAL_GL_TEXTURE_2D);
   139 protected:
   140     void UnwrapImpl();
   141 };
   144 struct ScopedBindRenderbuffer
   145     : public ScopedGLWrapper<ScopedBindRenderbuffer>
   146 {
   147     friend struct ScopedGLWrapper<ScopedBindRenderbuffer>;
   149 protected:
   150     GLuint mOldRB;
   152 private:
   153     void Init();
   155 public:
   156     explicit ScopedBindRenderbuffer(GLContext* aGL);
   158     ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB);
   160 protected:
   161     void UnwrapImpl();
   162 };
   165 struct ScopedFramebufferForTexture
   166     : public ScopedGLWrapper<ScopedFramebufferForTexture>
   167 {
   168     friend struct ScopedGLWrapper<ScopedFramebufferForTexture>;
   170 protected:
   171     bool mComplete; // True if the framebuffer we create is complete.
   172     GLuint mFB;
   174 public:
   175     ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture,
   176                                 GLenum aTarget = LOCAL_GL_TEXTURE_2D);
   178     bool IsComplete() const {
   179         return mComplete;
   180     }
   182     GLuint FB() const {
   183         MOZ_ASSERT(IsComplete());
   184         return mFB;
   185     }
   187 protected:
   188     void UnwrapImpl();
   189 };
   191 struct ScopedFramebufferForRenderbuffer
   192     : public ScopedGLWrapper<ScopedFramebufferForRenderbuffer>
   193 {
   194     friend struct ScopedGLWrapper<ScopedFramebufferForRenderbuffer>;
   196 protected:
   197     bool mComplete; // True if the framebuffer we create is complete.
   198     GLuint mFB;
   200 public:
   201     ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB);
   203     bool IsComplete() const {
   204         return mComplete;
   205     }
   207     GLuint FB() const {
   208         return mFB;
   209     }
   211 protected:
   212     void UnwrapImpl();
   213 };
   215 struct ScopedViewportRect
   216     : public ScopedGLWrapper<ScopedViewportRect>
   217 {
   218     friend struct ScopedGLWrapper<ScopedViewportRect>;
   220 protected:
   221     GLint mSavedViewportRect[4];
   223 public:
   224     ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
   226 protected:
   227     void UnwrapImpl();
   228 };
   230 struct ScopedScissorRect
   231     : public ScopedGLWrapper<ScopedScissorRect>
   232 {
   233     friend struct ScopedGLWrapper<ScopedScissorRect>;
   235 protected:
   236     GLint mSavedScissorRect[4];
   238 public:
   239     ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
   240     explicit ScopedScissorRect(GLContext* aGL);
   242 protected:
   243     void UnwrapImpl();
   244 };
   246 } /* namespace gl */
   247 } /* namespace mozilla */
   249 #endif /* SCOPEDGLHELPERS_H_ */

mercurial