2013-11-26 03:25:25 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* 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_
|
|
|
|
|
2014-10-08 04:01:51 +00:00
|
|
|
#include "GLDefs.h"
|
2014-07-11 00:43:01 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2013-11-26 03:25:25 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
2013-11-29 16:31:46 +00:00
|
|
|
|
2014-10-08 04:01:51 +00:00
|
|
|
class GLContext;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool IsContextCurrent(GLContext* gl);
|
|
|
|
#endif
|
|
|
|
|
2013-11-26 03:25:25 +00:00
|
|
|
//RAII via CRTP!
|
|
|
|
template <class Derived>
|
|
|
|
struct ScopedGLWrapper
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool mIsUnwrapped;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLContext* const mGL;
|
|
|
|
|
2014-08-08 01:17:30 +00:00
|
|
|
explicit ScopedGLWrapper(GLContext* gl)
|
2013-11-26 03:25:25 +00:00
|
|
|
: mIsUnwrapped(false)
|
|
|
|
, mGL(gl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(&ScopedGLWrapper<Derived>::Unwrap == &Derived::Unwrap);
|
|
|
|
MOZ_ASSERT(&Derived::UnwrapImpl);
|
2014-10-08 04:01:51 +00:00
|
|
|
MOZ_ASSERT(IsContextCurrent(mGL));
|
2013-11-26 03:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ScopedGLWrapper() {
|
|
|
|
if (!mIsUnwrapped)
|
|
|
|
Unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void Unwrap() {
|
|
|
|
MOZ_ASSERT(!mIsUnwrapped);
|
|
|
|
|
|
|
|
Derived* derived = static_cast<Derived*>(this);
|
|
|
|
derived->UnwrapImpl();
|
|
|
|
|
|
|
|
mIsUnwrapped = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wraps glEnable/Disable.
|
|
|
|
struct ScopedGLState
|
|
|
|
: public ScopedGLWrapper<ScopedGLState>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedGLState>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const GLenum mCapability;
|
|
|
|
bool mOldState;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Use |newState = true| to enable, |false| to disable.
|
|
|
|
ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState);
|
2014-01-07 20:02:18 +00:00
|
|
|
// variant that doesn't change state; simply records existing state to be
|
|
|
|
// restored by the destructor
|
|
|
|
ScopedGLState(GLContext* aGL, GLenum aCapability);
|
2013-11-26 03:25:25 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Saves and restores with GetUserBoundFB and BindUserFB.
|
|
|
|
struct ScopedBindFramebuffer
|
|
|
|
: public ScopedGLWrapper<ScopedBindFramebuffer>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedBindFramebuffer>;
|
|
|
|
|
|
|
|
protected:
|
2014-05-14 00:14:53 +00:00
|
|
|
GLuint mOldReadFB;
|
|
|
|
GLuint mOldDrawFB;
|
2013-11-26 03:25:25 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ScopedBindFramebuffer(GLContext* aGL);
|
|
|
|
ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ScopedBindTextureUnit
|
|
|
|
: public ScopedGLWrapper<ScopedBindTextureUnit>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedBindTextureUnit>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLenum mOldTexUnit;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ScopedTexture
|
|
|
|
: public ScopedGLWrapper<ScopedTexture>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedTexture>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLuint mTexture;
|
|
|
|
|
|
|
|
public:
|
2014-08-08 01:17:30 +00:00
|
|
|
explicit ScopedTexture(GLContext* aGL);
|
2013-11-26 03:25:25 +00:00
|
|
|
GLuint Texture() { return mTexture; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-30 21:30:21 +00:00
|
|
|
struct ScopedFramebuffer
|
|
|
|
: public ScopedGLWrapper<ScopedFramebuffer>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedFramebuffer>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLuint mFB;
|
|
|
|
|
|
|
|
public:
|
2014-08-08 01:17:30 +00:00
|
|
|
explicit ScopedFramebuffer(GLContext* aGL);
|
2014-04-30 21:30:21 +00:00
|
|
|
GLuint FB() { return mFB; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ScopedRenderbuffer
|
|
|
|
: public ScopedGLWrapper<ScopedRenderbuffer>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedRenderbuffer>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLuint mRB;
|
|
|
|
|
|
|
|
public:
|
2014-08-08 01:17:30 +00:00
|
|
|
explicit ScopedRenderbuffer(GLContext* aGL);
|
2014-04-30 21:30:21 +00:00
|
|
|
GLuint RB() { return mRB; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-11-26 03:25:25 +00:00
|
|
|
struct ScopedBindTexture
|
|
|
|
: public ScopedGLWrapper<ScopedBindTexture>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedBindTexture>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLuint mOldTex;
|
|
|
|
GLenum mTarget;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Init(GLenum aTarget);
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
|
|
|
|
GLenum aTarget = LOCAL_GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ScopedBindRenderbuffer
|
|
|
|
: public ScopedGLWrapper<ScopedBindRenderbuffer>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedBindRenderbuffer>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLuint mOldRB;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ScopedBindRenderbuffer(GLContext* aGL);
|
|
|
|
|
|
|
|
ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ScopedFramebufferForTexture
|
|
|
|
: public ScopedGLWrapper<ScopedFramebufferForTexture>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedFramebufferForTexture>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool mComplete; // True if the framebuffer we create is complete.
|
|
|
|
GLuint mFB;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture,
|
|
|
|
GLenum aTarget = LOCAL_GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
bool IsComplete() const {
|
|
|
|
return mComplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint FB() const {
|
|
|
|
MOZ_ASSERT(IsComplete());
|
|
|
|
return mFB;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ScopedFramebufferForRenderbuffer
|
|
|
|
: public ScopedGLWrapper<ScopedFramebufferForRenderbuffer>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedFramebufferForRenderbuffer>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool mComplete; // True if the framebuffer we create is complete.
|
|
|
|
GLuint mFB;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB);
|
|
|
|
|
|
|
|
bool IsComplete() const {
|
|
|
|
return mComplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint FB() const {
|
|
|
|
return mFB;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
2014-01-07 20:02:18 +00:00
|
|
|
struct ScopedViewportRect
|
|
|
|
: public ScopedGLWrapper<ScopedViewportRect>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedViewportRect>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLint mSavedViewportRect[4];
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
2014-01-07 20:02:18 +00:00
|
|
|
struct ScopedScissorRect
|
|
|
|
: public ScopedGLWrapper<ScopedScissorRect>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedScissorRect>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLint mSavedScissorRect[4];
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width, GLsizei height);
|
|
|
|
explicit ScopedScissorRect(GLContext* aGL);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
2014-05-29 14:19:48 +00:00
|
|
|
struct ScopedVertexAttribPointer
|
|
|
|
: public ScopedGLWrapper<ScopedVertexAttribPointer>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedVertexAttribPointer>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
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);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void WrapImpl(GLuint index);
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
|
|
|
|
2014-07-08 18:52:00 +00:00
|
|
|
struct ScopedGLDrawState {
|
2014-08-08 01:17:30 +00:00
|
|
|
explicit ScopedGLDrawState(GLContext* gl);
|
2014-07-08 18:52:00 +00:00
|
|
|
~ScopedGLDrawState();
|
|
|
|
|
|
|
|
GLuint boundProgram;
|
|
|
|
GLuint boundBuffer;
|
|
|
|
|
|
|
|
ScopedGLState blend;
|
|
|
|
ScopedGLState cullFace;
|
|
|
|
ScopedGLState depthTest;
|
|
|
|
ScopedGLState dither;
|
|
|
|
ScopedGLState polyOffsFill;
|
|
|
|
ScopedGLState sampleAToC;
|
|
|
|
ScopedGLState sampleCover;
|
|
|
|
ScopedGLState scissor;
|
|
|
|
ScopedGLState stencil;
|
|
|
|
|
|
|
|
GLuint maxAttrib;
|
2014-07-11 00:43:01 +00:00
|
|
|
UniquePtr<GLint[]> attrib_enabled;
|
2014-07-08 18:52:00 +00:00
|
|
|
GLint attrib0_size;
|
|
|
|
GLint attrib0_stride;
|
|
|
|
GLint attrib0_type;
|
|
|
|
GLint attrib0_normalized;
|
|
|
|
GLint attrib0_bufferBinding;
|
|
|
|
void* attrib0_pointer;
|
|
|
|
|
|
|
|
realGLboolean colorMask[4];
|
|
|
|
GLint viewport[4];
|
|
|
|
GLint scissorBox[4];
|
|
|
|
GLContext* const mGL;
|
|
|
|
GLuint packAlign;
|
|
|
|
};
|
2014-10-08 04:01:51 +00:00
|
|
|
|
|
|
|
struct ScopedPackAlignment
|
|
|
|
: public ScopedGLWrapper<ScopedPackAlignment>
|
|
|
|
{
|
|
|
|
friend struct ScopedGLWrapper<ScopedPackAlignment>;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GLint mOldVal;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedPackAlignment(GLContext* aGL, GLint scopedVal);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void UnwrapImpl();
|
|
|
|
};
|
2013-11-26 03:25:25 +00:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* SCOPEDGLHELPERS_H_ */
|