Bug 1250710 - Update scoped pack/unpack helpers. - r=jrmuizel
authorJeff Gilbert <jgilbert@mozilla.com>
Tue, 14 Jun 2016 10:07:55 -0700
changeset 335774 f99eeb83698292a803c379b6067df6bbc358080f
parent 335773 057ec3ca368097a2594d34cef36e7151353297c9
child 335775 4423ce67bfb77ef8085afbb1f5159e4e0732d566
push idunknown
push userunknown
push dateunknown
reviewersjrmuizel
bugs1250710
milestone50.0a1
Bug 1250710 - Update scoped pack/unpack helpers. - r=jrmuizel MozReview-Commit-ID: 1WHVAAb4V00
gfx/gl/ScopedGLHelpers.cpp
gfx/gl/ScopedGLHelpers.h
--- a/gfx/gl/ScopedGLHelpers.cpp
+++ b/gfx/gl/ScopedGLHelpers.cpp
@@ -513,66 +513,103 @@ ScopedGLDrawState::~ScopedGLDrawState()
                               attrib0_pointer);
 
     mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, boundBuffer);
 
     mGL->fUseProgram(boundProgram);
 }
 
 ////////////////////////////////////////////////////////////////////////
-// ScopedPackAlignment
+// ScopedPackState
 
-ScopedPackAlignment::ScopedPackAlignment(GLContext* gl, GLint scopedVal)
-    : ScopedGLWrapper<ScopedPackAlignment>(gl)
+static bool
+HasPBOState(const GLContext* gl)
+{
+    return (!gl->IsGLES() || gl->Version() >= 300);
+}
+
+ScopedPackState::ScopedPackState(GLContext* gl)
+    : ScopedGLWrapper<ScopedPackState>(gl)
 {
-    MOZ_ASSERT(scopedVal == 1 ||
-               scopedVal == 2 ||
-               scopedVal == 4 ||
-               scopedVal == 8);
+    mGL->fGetIntegerv(LOCAL_GL_PACK_ALIGNMENT, &mAlignment);
 
-    gl->fGetIntegerv(LOCAL_GL_PACK_ALIGNMENT, &mOldVal);
+    if (mAlignment != 4) mGL->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, 4);
+
+    if (!HasPBOState(mGL))
+        return;
 
-    if (scopedVal != mOldVal) {
-        gl->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, scopedVal);
-    } else {
-      // Don't try to re-set it during unwrap.
-        mOldVal = 0;
-    }
+    mGL->fGetIntegerv(LOCAL_GL_PIXEL_PACK_BUFFER_BINDING, (GLint*)&mPixelBuffer);
+    mGL->fGetIntegerv(LOCAL_GL_PACK_ROW_LENGTH, &mRowLength);
+    mGL->fGetIntegerv(LOCAL_GL_PACK_SKIP_PIXELS, &mSkipPixels);
+    mGL->fGetIntegerv(LOCAL_GL_PACK_SKIP_ROWS, &mSkipRows);
+
+    if (mPixelBuffer != 0) mGL->fBindBuffer(LOCAL_GL_PIXEL_PACK_BUFFER, 0);
+    if (mRowLength != 0)   mGL->fPixelStorei(LOCAL_GL_PACK_ROW_LENGTH, 0);
+    if (mSkipPixels != 0)  mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_PIXELS, 0);
+    if (mSkipRows != 0)    mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_ROWS, 0);
 }
 
 void
-ScopedPackAlignment::UnwrapImpl() {
+ScopedPackState::UnwrapImpl()
+{
     // Check that we're not falling out of scope after the current context changed.
     MOZ_ASSERT(mGL->IsCurrent());
 
-    if (mOldVal) {
-        mGL->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, mOldVal);
-    }
+    mGL->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, mAlignment);
+
+    if (!HasPBOState(mGL))
+        return;
+
+    mGL->fBindBuffer(LOCAL_GL_PIXEL_PACK_BUFFER, mPixelBuffer);
+    mGL->fPixelStorei(LOCAL_GL_PACK_ROW_LENGTH, mRowLength);
+    mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_PIXELS, mSkipPixels);
+    mGL->fPixelStorei(LOCAL_GL_PACK_SKIP_ROWS, mSkipRows);
 }
 
 ////////////////////////////////////////////////////////////////////////
-// ScopedUnpackAlignment
+// ScopedUnpackState
 
-ScopedUnpackAlignment::ScopedUnpackAlignment(GLContext* gl, GLint scopedVal)
-    : ScopedGLWrapper<ScopedUnpackAlignment>(gl)
+ScopedUnpackState::ScopedUnpackState(GLContext* gl)
+    : ScopedGLWrapper<ScopedUnpackState>(gl)
 {
-    MOZ_ASSERT(scopedVal == 1 ||
-               scopedVal == 2 ||
-               scopedVal == 4 ||
-               scopedVal == 8);
+    mGL->fGetIntegerv(LOCAL_GL_UNPACK_ALIGNMENT, &mAlignment);
+
+    if (mAlignment != 4) mGL->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
+
+    if (!HasPBOState(mGL))
+        return;
 
-    gl->fGetIntegerv(LOCAL_GL_UNPACK_ALIGNMENT, &mOldVal);
+    mGL->fGetIntegerv(LOCAL_GL_PIXEL_UNPACK_BUFFER_BINDING, (GLint*)&mPixelBuffer);
+    mGL->fGetIntegerv(LOCAL_GL_UNPACK_IMAGE_HEIGHT, &mImageHeight);
+    mGL->fGetIntegerv(LOCAL_GL_UNPACK_ROW_LENGTH, &mRowLength);
+    mGL->fGetIntegerv(LOCAL_GL_UNPACK_SKIP_IMAGES, &mSkipImages);
+    mGL->fGetIntegerv(LOCAL_GL_UNPACK_SKIP_PIXELS, &mSkipPixels);
+    mGL->fGetIntegerv(LOCAL_GL_UNPACK_SKIP_ROWS, &mSkipRows);
 
-    if (scopedVal != mOldVal) {
-        gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, scopedVal);
-    }
+    if (mPixelBuffer != 0) mGL->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
+    if (mImageHeight != 0) mGL->fPixelStorei(LOCAL_GL_UNPACK_IMAGE_HEIGHT, 0);
+    if (mRowLength != 0)   mGL->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, 0);
+    if (mSkipImages != 0)  mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES, 0);
+    if (mSkipPixels != 0)  mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_PIXELS, 0);
+    if (mSkipRows != 0)    mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_ROWS, 0);
 }
 
 void
-ScopedUnpackAlignment::UnwrapImpl() {
+ScopedUnpackState::UnwrapImpl()
+{
     // Check that we're not falling out of scope after the current context changed.
     MOZ_ASSERT(mGL->IsCurrent());
 
-    mGL->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, mOldVal);
+    mGL->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, mAlignment);
+
+    if (!HasPBOState(mGL))
+        return;
+
+    mGL->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, mPixelBuffer);
+    mGL->fPixelStorei(LOCAL_GL_UNPACK_IMAGE_HEIGHT, mImageHeight);
+    mGL->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, mRowLength);
+    mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES, mSkipImages);
+    mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_PIXELS, mSkipPixels);
+    mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_ROWS, mSkipRows);
 }
 
 } /* namespace gl */
 } /* namespace mozilla */
--- a/gfx/gl/ScopedGLHelpers.h
+++ b/gfx/gl/ScopedGLHelpers.h
@@ -337,42 +337,54 @@ struct ScopedGLDrawState {
 
     realGLboolean colorMask[4];
     GLint viewport[4];
     GLint scissorBox[4];
     GLContext* const mGL;
     GLuint packAlign;
 };
 
-struct ScopedPackAlignment
-    : public ScopedGLWrapper<ScopedPackAlignment>
+struct ScopedPackState
+    : public ScopedGLWrapper<ScopedPackState>
 {
-    friend struct ScopedGLWrapper<ScopedPackAlignment>;
+    friend struct ScopedGLWrapper<ScopedPackState>;
 
 protected:
-    GLint mOldVal;
+    GLint mAlignment;
+
+    GLuint mPixelBuffer;
+    GLint mRowLength;
+    GLint mSkipPixels;
+    GLint mSkipRows;
 
 public:
-    ScopedPackAlignment(GLContext* aGL, GLint scopedVal);
+    explicit ScopedPackState(GLContext* gl);
 
 protected:
     void UnwrapImpl();
 };
 
 
-struct ScopedUnpackAlignment
-    : public ScopedGLWrapper<ScopedUnpackAlignment>
+struct ScopedUnpackState
+    : public ScopedGLWrapper<ScopedUnpackState>
 {
-    friend struct ScopedGLWrapper<ScopedUnpackAlignment>;
+    friend struct ScopedGLWrapper<ScopedUnpackState>;
 
 protected:
-    GLint mOldVal;
+    GLint mAlignment;
+
+    GLuint mPixelBuffer;
+    GLint mImageHeight;
+    GLint mRowLength;
+    GLint mSkipImages;
+    GLint mSkipPixels;
+    GLint mSkipRows;
 
 public:
-    ScopedUnpackAlignment(GLContext* gl, GLint scopedVal);
+    explicit ScopedUnpackState(GLContext* gl);
 
 protected:
     void UnwrapImpl();
 };
 
 } /* namespace gl */
 } /* namespace mozilla */