summaryrefslogtreecommitdiffstats
path: root/gfx/gl/ScopedGLHelpers.h
blob: 6e3b9867bc48eb1f6d6b8fdfda5d8f0d92dc01c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef SCOPEDGLHELPERS_H_
#define SCOPEDGLHELPERS_H_

#include "GLDefs.h"
#include "mozilla/UniquePtr.h"

namespace mozilla {
namespace gl {

class GLContext;

#ifdef DEBUG
bool IsContextCurrent(GLContext* gl);
#endif

// Wraps glEnable/Disable.
struct ScopedGLState final {
 private:
  GLContext* const mGL;
  const GLenum mCapability;
  bool mOldState;

 public:
  // Use |newState = true| to enable, |false| to disable.
  ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState);
  // variant that doesn't change state; simply records existing state to be
  // restored by the destructor
  ScopedGLState(GLContext* aGL, GLenum aCapability);
  ~ScopedGLState();
};

// Saves and restores with GetUserBoundFB and BindUserFB.
struct ScopedBindFramebuffer final {
 private:
  GLContext* const mGL;
  GLuint mOldReadFB;
  GLuint mOldDrawFB;

  void Init();

 public:
  explicit ScopedBindFramebuffer(GLContext* aGL);
  ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB);
  ~ScopedBindFramebuffer();
};

struct ScopedBindTextureUnit final {
 private:
  GLContext* const mGL;
  GLenum mOldTexUnit;

 public:
  ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit);
  ~ScopedBindTextureUnit();
};

struct ScopedTexture final {
 private:
  GLContext* const mGL;
  GLuint mTexture;

 public:
  explicit ScopedTexture(GLContext* aGL);
  ~ScopedTexture();

  GLuint Texture() const { return mTexture; }
  operator GLuint() const { return mTexture; }
};

struct ScopedFramebuffer final {
 private:
  GLContext* const mGL;
  GLuint mFB;

 public:
  explicit ScopedFramebuffer(GLContext* aGL);
  ~ScopedFramebuffer();

  const auto& FB() const { return mFB; }
  operator GLuint() const { return mFB; }
};

struct ScopedRenderbuffer final {
 private:
  GLContext* const mGL;
  GLuint mRB;

 public:
  explicit ScopedRenderbuffer(GLContext* aGL);
  ~ScopedRenderbuffer();

  GLuint RB() { return mRB; }
  operator GLuint() const { return mRB; }
};

struct ScopedBindTexture final {
 private:
  GLContext* const mGL;
  const GLenum mTarget;
  const GLuint mOldTex;

 public:
  ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
                    GLenum aTarget = LOCAL_GL_TEXTURE_2D);
  ~ScopedBindTexture();
};

struct ScopedBindRenderbuffer final {
 private:
  GLContext* const mGL;
  GLuint mOldRB;

 private:
  void Init();

 public:
  explicit ScopedBindRenderbuffer(GLContext* aGL);
  ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB);
  ~ScopedBindRenderbuffer();
};

struct ScopedFramebufferForTexture final {
 private:
  GLContext* const mGL;
  bool mComplete;  // True if the framebuffer we create is complete.
  GLuint mFB;

 public:
  ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture,
                              GLenum aTarget = LOCAL_GL_TEXTURE_2D);
  ~ScopedFramebufferForTexture();

  bool IsComplete() const { return mComplete; }

  GLuint FB() const {
    MOZ_GL_ASSERT(mGL, IsComplete());
    return mFB;
  }
};

struct ScopedFramebufferForRenderbuffer final {
 private:
  GLContext* const mGL;
  bool mComplete;  // True if the framebuffer we create is complete.
  GLuint mFB;

 public:
  ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB);
  ~ScopedFramebufferForRenderbuffer();

  bool IsComplete() const { return mComplete; }
  GLuint FB() const {
    MOZ_GL_ASSERT(mGL, IsComplete());
    return mFB;
  }
};

struct ScopedViewportRect final {
 private:
  GLContext* const mGL;
  GLint mSavedViewportRect[4];

 public:
  ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width,
                     GLsizei height);
  ~ScopedViewportRect();
};

struct ScopedScissorRect final {
 private:
  GLContext* const mGL;
  GLint mSavedScissorRect[4];

 public:
  ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width,
                    GLsizei height);
  explicit ScopedScissorRect(GLContext* aGL);
  ~ScopedScissorRect();
};

struct ScopedVertexAttribPointer final {
 private:
  GLContext* const mGL;
  GLuint mAttribIndex;
  GLint mAttribEnabled;
  GLint mAttribSize;
  GLint mAttribStride;
  GLint mAttribType;
  GLint mAttribNormalized;
  GLint mAttribBufferBinding;
  void* mAttribPointer;
  GLuint mBoundBuffer;

 public:
  ScopedVertexAttribPointer(GLContext* aGL, GLuint index, GLint size,
                            GLenum type, realGLboolean normalized,
                            GLsizei stride, GLuint buffer,
                            const GLvoid* pointer);
  explicit ScopedVertexAttribPointer(GLContext* aGL, GLuint index);
  ~ScopedVertexAttribPointer();

 private:
  void WrapImpl(GLuint index);
};

struct ScopedPackState final {
 private:
  GLContext* const mGL;
  GLint mAlignment;

  GLuint mPixelBuffer;
  GLint mRowLength;
  GLint mSkipPixels;
  GLint mSkipRows;

 public:
  explicit ScopedPackState(GLContext* gl);
  ~ScopedPackState();

  // Returns whether the stride was handled successfully.
  bool SetForWidthAndStrideRGBA(GLsizei aWidth, GLsizei aStride);
};

struct ResetUnpackState final {
 private:
  GLContext* const mGL;
  GLuint mAlignment;

  GLuint mPBO;
  GLuint mRowLength;
  GLuint mImageHeight;
  GLuint mSkipPixels;
  GLuint mSkipRows;
  GLuint mSkipImages;

 public:
  explicit ResetUnpackState(GLContext* gl);
  ~ResetUnpackState();
};

struct ScopedBindPBO final {
 private:
  GLContext* const mGL;
  const GLenum mTarget;
  const GLuint mPBO;

 public:
  ScopedBindPBO(GLContext* gl, GLenum target);
  ~ScopedBindPBO();
};

} /* namespace gl */
} /* namespace mozilla */

#endif /* SCOPEDGLHELPERS_H_ */