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