Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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 | #ifndef SCOPEDGLHELPERS_H_ |
michael@0 | 7 | #define SCOPEDGLHELPERS_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "GLContext.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace mozilla { |
michael@0 | 12 | namespace gl { |
michael@0 | 13 | |
michael@0 | 14 | //RAII via CRTP! |
michael@0 | 15 | template <class Derived> |
michael@0 | 16 | struct ScopedGLWrapper |
michael@0 | 17 | { |
michael@0 | 18 | private: |
michael@0 | 19 | bool mIsUnwrapped; |
michael@0 | 20 | |
michael@0 | 21 | protected: |
michael@0 | 22 | GLContext* const mGL; |
michael@0 | 23 | |
michael@0 | 24 | ScopedGLWrapper(GLContext* gl) |
michael@0 | 25 | : mIsUnwrapped(false) |
michael@0 | 26 | , mGL(gl) |
michael@0 | 27 | { |
michael@0 | 28 | MOZ_ASSERT(&ScopedGLWrapper<Derived>::Unwrap == &Derived::Unwrap); |
michael@0 | 29 | MOZ_ASSERT(&Derived::UnwrapImpl); |
michael@0 | 30 | MOZ_ASSERT(mGL->IsCurrent()); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | virtual ~ScopedGLWrapper() { |
michael@0 | 34 | if (!mIsUnwrapped) |
michael@0 | 35 | Unwrap(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | public: |
michael@0 | 39 | void Unwrap() { |
michael@0 | 40 | MOZ_ASSERT(!mIsUnwrapped); |
michael@0 | 41 | |
michael@0 | 42 | Derived* derived = static_cast<Derived*>(this); |
michael@0 | 43 | derived->UnwrapImpl(); |
michael@0 | 44 | |
michael@0 | 45 | mIsUnwrapped = true; |
michael@0 | 46 | } |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | // Wraps glEnable/Disable. |
michael@0 | 50 | struct ScopedGLState |
michael@0 | 51 | : public ScopedGLWrapper<ScopedGLState> |
michael@0 | 52 | { |
michael@0 | 53 | friend struct ScopedGLWrapper<ScopedGLState>; |
michael@0 | 54 | |
michael@0 | 55 | protected: |
michael@0 | 56 | const GLenum mCapability; |
michael@0 | 57 | bool mOldState; |
michael@0 | 58 | |
michael@0 | 59 | public: |
michael@0 | 60 | // Use |newState = true| to enable, |false| to disable. |
michael@0 | 61 | ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState); |
michael@0 | 62 | // variant that doesn't change state; simply records existing state to be |
michael@0 | 63 | // restored by the destructor |
michael@0 | 64 | ScopedGLState(GLContext* aGL, GLenum aCapability); |
michael@0 | 65 | |
michael@0 | 66 | protected: |
michael@0 | 67 | void UnwrapImpl(); |
michael@0 | 68 | }; |
michael@0 | 69 | |
michael@0 | 70 | // Saves and restores with GetUserBoundFB and BindUserFB. |
michael@0 | 71 | struct ScopedBindFramebuffer |
michael@0 | 72 | : public ScopedGLWrapper<ScopedBindFramebuffer> |
michael@0 | 73 | { |
michael@0 | 74 | friend struct ScopedGLWrapper<ScopedBindFramebuffer>; |
michael@0 | 75 | |
michael@0 | 76 | protected: |
michael@0 | 77 | GLuint mOldFB; |
michael@0 | 78 | |
michael@0 | 79 | private: |
michael@0 | 80 | void Init(); |
michael@0 | 81 | |
michael@0 | 82 | public: |
michael@0 | 83 | explicit ScopedBindFramebuffer(GLContext* aGL); |
michael@0 | 84 | ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB); |
michael@0 | 85 | |
michael@0 | 86 | protected: |
michael@0 | 87 | void UnwrapImpl(); |
michael@0 | 88 | }; |
michael@0 | 89 | |
michael@0 | 90 | struct ScopedBindTextureUnit |
michael@0 | 91 | : public ScopedGLWrapper<ScopedBindTextureUnit> |
michael@0 | 92 | { |
michael@0 | 93 | friend struct ScopedGLWrapper<ScopedBindTextureUnit>; |
michael@0 | 94 | |
michael@0 | 95 | protected: |
michael@0 | 96 | GLenum mOldTexUnit; |
michael@0 | 97 | |
michael@0 | 98 | public: |
michael@0 | 99 | ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit); |
michael@0 | 100 | |
michael@0 | 101 | protected: |
michael@0 | 102 | void UnwrapImpl(); |
michael@0 | 103 | }; |
michael@0 | 104 | |
michael@0 | 105 | |
michael@0 | 106 | struct ScopedTexture |
michael@0 | 107 | : public ScopedGLWrapper<ScopedTexture> |
michael@0 | 108 | { |
michael@0 | 109 | friend struct ScopedGLWrapper<ScopedTexture>; |
michael@0 | 110 | |
michael@0 | 111 | protected: |
michael@0 | 112 | GLuint mTexture; |
michael@0 | 113 | |
michael@0 | 114 | public: |
michael@0 | 115 | ScopedTexture(GLContext* aGL); |
michael@0 | 116 | GLuint Texture() { return mTexture; } |
michael@0 | 117 | |
michael@0 | 118 | protected: |
michael@0 | 119 | void UnwrapImpl(); |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | |
michael@0 | 123 | struct ScopedBindTexture |
michael@0 | 124 | : public ScopedGLWrapper<ScopedBindTexture> |
michael@0 | 125 | { |
michael@0 | 126 | friend struct ScopedGLWrapper<ScopedBindTexture>; |
michael@0 | 127 | |
michael@0 | 128 | protected: |
michael@0 | 129 | GLuint mOldTex; |
michael@0 | 130 | GLenum mTarget; |
michael@0 | 131 | |
michael@0 | 132 | private: |
michael@0 | 133 | void Init(GLenum aTarget); |
michael@0 | 134 | |
michael@0 | 135 | public: |
michael@0 | 136 | ScopedBindTexture(GLContext* aGL, GLuint aNewTex, |
michael@0 | 137 | GLenum aTarget = LOCAL_GL_TEXTURE_2D); |
michael@0 | 138 | |
michael@0 | 139 | protected: |
michael@0 | 140 | void UnwrapImpl(); |
michael@0 | 141 | }; |
michael@0 | 142 | |
michael@0 | 143 | |
michael@0 | 144 | struct ScopedBindRenderbuffer |
michael@0 | 145 | : public ScopedGLWrapper<ScopedBindRenderbuffer> |
michael@0 | 146 | { |
michael@0 | 147 | friend struct ScopedGLWrapper<ScopedBindRenderbuffer>; |
michael@0 | 148 | |
michael@0 | 149 | protected: |
michael@0 | 150 | GLuint mOldRB; |
michael@0 | 151 | |
michael@0 | 152 | private: |
michael@0 | 153 | void Init(); |
michael@0 | 154 | |
michael@0 | 155 | public: |
michael@0 | 156 | explicit ScopedBindRenderbuffer(GLContext* aGL); |
michael@0 | 157 | |
michael@0 | 158 | ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB); |
michael@0 | 159 | |
michael@0 | 160 | protected: |
michael@0 | 161 | void UnwrapImpl(); |
michael@0 | 162 | }; |
michael@0 | 163 | |
michael@0 | 164 | |
michael@0 | 165 | struct ScopedFramebufferForTexture |
michael@0 | 166 | : public ScopedGLWrapper<ScopedFramebufferForTexture> |
michael@0 | 167 | { |
michael@0 | 168 | friend struct ScopedGLWrapper<ScopedFramebufferForTexture>; |
michael@0 | 169 | |
michael@0 | 170 | protected: |
michael@0 | 171 | bool mComplete; // True if the framebuffer we create is complete. |
michael@0 | 172 | GLuint mFB; |
michael@0 | 173 | |
michael@0 | 174 | public: |
michael@0 | 175 | ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture, |
michael@0 | 176 | GLenum aTarget = LOCAL_GL_TEXTURE_2D); |
michael@0 | 177 | |
michael@0 | 178 | bool IsComplete() const { |
michael@0 | 179 | return mComplete; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | GLuint FB() const { |
michael@0 | 183 | MOZ_ASSERT(IsComplete()); |
michael@0 | 184 | return mFB; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | protected: |
michael@0 | 188 | void UnwrapImpl(); |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | struct ScopedFramebufferForRenderbuffer |
michael@0 | 192 | : public ScopedGLWrapper<ScopedFramebufferForRenderbuffer> |
michael@0 | 193 | { |
michael@0 | 194 | friend struct ScopedGLWrapper<ScopedFramebufferForRenderbuffer>; |
michael@0 | 195 | |
michael@0 | 196 | protected: |
michael@0 | 197 | bool mComplete; // True if the framebuffer we create is complete. |
michael@0 | 198 | GLuint mFB; |
michael@0 | 199 | |
michael@0 | 200 | public: |
michael@0 | 201 | ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB); |
michael@0 | 202 | |
michael@0 | 203 | bool IsComplete() const { |
michael@0 | 204 | return mComplete; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | GLuint FB() const { |
michael@0 | 208 | return mFB; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | protected: |
michael@0 | 212 | void UnwrapImpl(); |
michael@0 | 213 | }; |
michael@0 | 214 | |
michael@0 | 215 | struct ScopedViewportRect |
michael@0 | 216 | : public ScopedGLWrapper<ScopedViewportRect> |
michael@0 | 217 | { |
michael@0 | 218 | friend struct ScopedGLWrapper<ScopedViewportRect>; |
michael@0 | 219 | |
michael@0 | 220 | protected: |
michael@0 | 221 | GLint mSavedViewportRect[4]; |
michael@0 | 222 | |
michael@0 | 223 | public: |
michael@0 | 224 | ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height); |
michael@0 | 225 | |
michael@0 | 226 | protected: |
michael@0 | 227 | void UnwrapImpl(); |
michael@0 | 228 | }; |
michael@0 | 229 | |
michael@0 | 230 | struct ScopedScissorRect |
michael@0 | 231 | : public ScopedGLWrapper<ScopedScissorRect> |
michael@0 | 232 | { |
michael@0 | 233 | friend struct ScopedGLWrapper<ScopedScissorRect>; |
michael@0 | 234 | |
michael@0 | 235 | protected: |
michael@0 | 236 | GLint mSavedScissorRect[4]; |
michael@0 | 237 | |
michael@0 | 238 | public: |
michael@0 | 239 | ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height); |
michael@0 | 240 | explicit ScopedScissorRect(GLContext* aGL); |
michael@0 | 241 | |
michael@0 | 242 | protected: |
michael@0 | 243 | void UnwrapImpl(); |
michael@0 | 244 | }; |
michael@0 | 245 | |
michael@0 | 246 | } /* namespace gl */ |
michael@0 | 247 | } /* namespace mozilla */ |
michael@0 | 248 | |
michael@0 | 249 | #endif /* SCOPEDGLHELPERS_H_ */ |