2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-11-26 07:25:25 +04:00
|
|
|
/* 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 08:01:51 +04:00
|
|
|
#include "GLDefs.h"
|
2014-07-11 04:43:01 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
2013-11-29 20:31:46 +04:00
|
|
|
|
2014-10-08 08:01:51 +04:00
|
|
|
class GLContext;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool IsContextCurrent(GLContext* gl);
|
|
|
|
#endif
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
// Wraps glEnable/Disable.
|
|
|
|
struct ScopedGLState final {
|
2013-11-26 07:25:25 +04:00
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
|
|
|
const GLenum mCapability;
|
|
|
|
bool mOldState;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
public:
|
|
|
|
// Use |newState = true| to enable, |false| to disable.
|
|
|
|
ScopedGLState(GLContext* aGL, GLenum aCapability, bool aNewState);
|
2014-01-08 00:02:18 +04:00
|
|
|
// variant that doesn't change state; simply records existing state to be
|
|
|
|
// restored by the destructor
|
|
|
|
ScopedGLState(GLContext* aGL, GLenum aCapability);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedGLState();
|
2013-11-26 07:25:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Saves and restores with GetUserBoundFB and BindUserFB.
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedBindFramebuffer final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2014-05-14 04:14:53 +04:00
|
|
|
GLuint mOldReadFB;
|
|
|
|
GLuint mOldDrawFB;
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ScopedBindFramebuffer(GLContext* aGL);
|
|
|
|
ScopedBindFramebuffer(GLContext* aGL, GLuint aNewFB);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedBindFramebuffer();
|
2013-11-26 07:25:25 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedBindTextureUnit final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2013-11-26 07:25:25 +04:00
|
|
|
GLenum mOldTexUnit;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedBindTextureUnit(GLContext* aGL, GLenum aTexUnit);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedBindTextureUnit();
|
2013-11-26 07:25:25 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedTexture final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2013-11-26 07:25:25 +04:00
|
|
|
GLuint mTexture;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ScopedTexture(GLContext* aGL);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedTexture();
|
2013-11-26 07:25:25 +04:00
|
|
|
|
2014-08-08 05:17:30 +04:00
|
|
|
GLuint Texture() const { return mTexture; }
|
|
|
|
operator GLuint() const { return mTexture; }
|
2013-11-26 07:25:25 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedFramebuffer final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2014-05-01 01:30:21 +04:00
|
|
|
GLuint mFB;
|
|
|
|
|
|
|
|
public:
|
2014-08-08 05:17:30 +04:00
|
|
|
explicit ScopedFramebuffer(GLContext* aGL);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedFramebuffer();
|
2014-05-01 01:30:21 +04:00
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
const auto& FB() const { return mFB; }
|
|
|
|
operator GLuint() const { return mFB; }
|
2014-05-01 01:30:21 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedRenderbuffer final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2014-05-01 01:30:21 +04:00
|
|
|
GLuint mRB;
|
|
|
|
|
|
|
|
public:
|
2014-08-08 05:17:30 +04:00
|
|
|
explicit ScopedRenderbuffer(GLContext* aGL);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedRenderbuffer();
|
2014-05-01 01:30:21 +04:00
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
GLuint RB() { return mRB; }
|
|
|
|
operator GLuint() const { return mRB; }
|
2014-05-01 01:30:21 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedBindTexture final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2013-11-26 07:25:25 +04:00
|
|
|
const GLenum mTarget;
|
|
|
|
const GLuint mOldTex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedBindTexture(GLContext* aGL, GLuint aNewTex,
|
2015-11-25 07:15:29 +03:00
|
|
|
GLenum aTarget = LOCAL_GL_TEXTURE_2D);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedBindTexture();
|
2013-11-26 07:25:25 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedBindRenderbuffer final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2013-11-26 07:25:25 +04:00
|
|
|
GLuint mOldRB;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ScopedBindRenderbuffer(GLContext* aGL);
|
|
|
|
ScopedBindRenderbuffer(GLContext* aGL, GLuint aNewRB);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedBindRenderbuffer();
|
2013-11-26 07:25:25 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedFramebufferForTexture final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2013-11-26 07:25:25 +04:00
|
|
|
bool mComplete; // True if the framebuffer we create is complete.
|
|
|
|
GLuint mFB;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedFramebufferForTexture(GLContext* aGL, GLuint aTexture,
|
|
|
|
GLenum aTarget = LOCAL_GL_TEXTURE_2D);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedFramebufferForTexture();
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
bool IsComplete() const { return mComplete; }
|
|
|
|
|
|
|
|
GLuint FB() const {
|
2018-11-27 07:23:19 +03:00
|
|
|
MOZ_GL_ASSERT(mGL, IsComplete());
|
2013-11-26 07:25:25 +04:00
|
|
|
return mFB;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedFramebufferForRenderbuffer final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2013-11-26 07:25:25 +04:00
|
|
|
bool mComplete; // True if the framebuffer we create is complete.
|
|
|
|
GLuint mFB;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedFramebufferForRenderbuffer(GLContext* aGL, GLuint aRB);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedFramebufferForRenderbuffer();
|
2013-11-26 07:25:25 +04:00
|
|
|
|
|
|
|
bool IsComplete() const { return mComplete; }
|
2019-06-17 09:40:27 +03:00
|
|
|
GLuint FB() const {
|
|
|
|
MOZ_GL_ASSERT(mGL, IsComplete());
|
|
|
|
return mFB;
|
|
|
|
}
|
2013-11-26 07:25:25 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedViewportRect final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2014-01-08 00:02:18 +04:00
|
|
|
GLint mSavedViewportRect[4];
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedViewportRect(GLContext* aGL, GLint x, GLint y, GLsizei width,
|
|
|
|
GLsizei height);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedViewportRect();
|
2014-01-08 00:02:18 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedScissorRect final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2014-01-08 00:02:18 +04:00
|
|
|
GLint mSavedScissorRect[4];
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedScissorRect(GLContext* aGL, GLint x, GLint y, GLsizei width,
|
|
|
|
GLsizei height);
|
|
|
|
explicit ScopedScissorRect(GLContext* aGL);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedScissorRect();
|
2014-01-08 00:02:18 +04:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedVertexAttribPointer final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2014-05-29 18:19:48 +04:00
|
|
|
GLuint mAttribIndex;
|
|
|
|
GLint mAttribEnabled;
|
|
|
|
GLint mAttribSize;
|
|
|
|
GLint mAttribStride;
|
|
|
|
GLint mAttribType;
|
|
|
|
GLint mAttribNormalized;
|
|
|
|
GLint mAttribBufferBinding;
|
|
|
|
void* mAttribPointer;
|
|
|
|
GLuint mBoundBuffer;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-29 18:19:48 +04:00
|
|
|
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);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedVertexAttribPointer();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
private:
|
2014-05-29 18:19:48 +04:00
|
|
|
void WrapImpl(GLuint index);
|
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedPackState final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2016-06-14 20:07:55 +03:00
|
|
|
GLint mAlignment;
|
|
|
|
|
|
|
|
GLuint mPixelBuffer;
|
|
|
|
GLint mRowLength;
|
|
|
|
GLint mSkipPixels;
|
|
|
|
GLint mSkipRows;
|
2014-10-08 08:01:51 +04:00
|
|
|
|
|
|
|
public:
|
2016-06-14 20:07:55 +03:00
|
|
|
explicit ScopedPackState(GLContext* gl);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedPackState();
|
2014-10-08 08:01:51 +04:00
|
|
|
|
2019-03-29 23:18:53 +03:00
|
|
|
// Returns whether the stride was handled successfully.
|
|
|
|
bool SetForWidthAndStrideRGBA(GLsizei aWidth, GLsizei aStride);
|
2014-10-08 08:01:51 +04:00
|
|
|
};
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ResetUnpackState final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2017-04-12 19:22:28 +03:00
|
|
|
GLuint mAlignment;
|
|
|
|
|
|
|
|
GLuint mPBO;
|
|
|
|
GLuint mRowLength;
|
|
|
|
GLuint mImageHeight;
|
|
|
|
GLuint mSkipPixels;
|
|
|
|
GLuint mSkipRows;
|
|
|
|
GLuint mSkipImages;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ResetUnpackState(GLContext* gl);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ResetUnpackState();
|
2017-04-12 19:22:28 +03:00
|
|
|
};
|
|
|
|
|
2019-06-17 09:40:27 +03:00
|
|
|
struct ScopedBindPBO final {
|
|
|
|
private:
|
|
|
|
GLContext* const mGL;
|
2016-12-30 02:17:23 +03:00
|
|
|
const GLenum mTarget;
|
|
|
|
const GLuint mPBO;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedBindPBO(GLContext* gl, GLenum target);
|
2019-06-17 09:40:27 +03:00
|
|
|
~ScopedBindPBO();
|
2016-12-30 02:17:23 +03:00
|
|
|
};
|
|
|
|
|
2013-11-26 07:25:25 +04:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* SCOPEDGLHELPERS_H_ */
|