Refactor PBO validate/map code.
authorBrian <brian.paul@tungstengraphics.com>
Fri, 21 Mar 2008 20:20:07 +0000 (14:20 -0600)
committerBrian <brian.paul@tungstengraphics.com>
Fri, 21 Mar 2008 20:20:07 +0000 (14:20 -0600)
We always need to do PBO validation, so do that in core Mesa before calling driv
er routine.
cherry-picked from Mesa/master.

src/mesa/main/bufferobj.c
src/mesa/main/bufferobj.h
src/mesa/main/drawpix.c
src/mesa/state_tracker/st_cb_bitmap.c
src/mesa/state_tracker/st_cb_drawpixels.c
src/mesa/state_tracker/st_cb_readpixels.c
src/mesa/swrast/s_bitmap.c
src/mesa/swrast/s_drawpix.c
src/mesa/swrast/s_readpix.c

index 71e571353f27b8ebc9ff2d7b6e65f305975c1ab4..e762eb3b634e56db15a7bcc4648b99155678d663 100644 (file)
@@ -475,31 +475,17 @@ _mesa_validate_pbo_access(GLuint dimensions,
  * If not sourcing from a PBO, just return the bitmap pointer.
  * This is a helper function for (some) drivers.
  * Return NULL if error.
- * If non-null return, must call validate_and_map_bitmap_pbo() when done.
+ * If non-null return, must call _mesa_unmap_bitmap_pbo() when done.
  */
 const GLubyte *
-_mesa_validate_and_map_bitmap_pbo(GLcontext *ctx,
-                                  GLsizei width, GLsizei height,
-                                  const struct gl_pixelstore_attrib *unpack,
-                                  const GLubyte *bitmap)
+_mesa_map_bitmap_pbo(GLcontext *ctx,
+                     const struct gl_pixelstore_attrib *unpack,
+                     const GLubyte *bitmap)
 {
    const GLubyte *buf;
 
    if (unpack->BufferObj->Name) {
       /* unpack from PBO */
-      if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
-                                     GL_COLOR_INDEX, GL_BITMAP,
-                                     (GLvoid *) bitmap)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
-         return NULL;
-      }
-
-      if (unpack->BufferObj->Pointer) {
-         /* buffer is already mapped - that's an error */
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
-         return NULL;
-      }
-
       buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
                                               GL_READ_ONLY_ARB,
                                               unpack->BufferObj);
@@ -518,7 +504,7 @@ _mesa_validate_and_map_bitmap_pbo(GLcontext *ctx,
 
 
 /**
- * Counterpart to validate_and_map_bitmap_pbo()
+ * Counterpart to _mesa_map_bitmap_pbo()
  * This is a helper function for (some) drivers.
  */
 void
@@ -533,33 +519,17 @@ _mesa_unmap_bitmap_pbo(GLcontext *ctx,
 
 
 /**
- * \sa _mesa_validate_and_map_bitmap_pbo
+ * \sa _mesa_map_bitmap_pbo
  */
 const GLvoid *
-_mesa_validate_and_map_drawpix_pbo(GLcontext *ctx,
-                                   GLsizei width, GLsizei height,
-                                   GLenum format, GLenum type,
-                                   const struct gl_pixelstore_attrib *unpack,
-                                   const GLvoid *pixels)
+_mesa_map_drawpix_pbo(GLcontext *ctx,
+                      const struct gl_pixelstore_attrib *unpack,
+                      const GLvoid *pixels)
 {
    const GLvoid *buf;
 
    if (unpack->BufferObj->Name) {
       /* unpack from PBO */
-
-      if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
-                                     format, type, pixels)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glDrawPixels(invalid PBO access)");
-         return NULL;
-      }
-
-      if (unpack->BufferObj->Pointer) {
-         /* buffer is already mapped - that's an error */
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(PBO is mapped)");
-         return NULL;
-      }
-
       buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
                                               GL_READ_ONLY_ARB,
                                               unpack->BufferObj);
@@ -592,36 +562,19 @@ _mesa_unmap_drapix_pbo(GLcontext *ctx,
 
 
 /**
- * When doing glReadPixels into a PBO, this function will check for errors
- * and map the buffer.
+ * If PBO is bound, map the buffer, return dest pointer in mapped buffer.
  * Call _mesa_unmap_readpix_pbo() when finished
  * \return NULL if error
  */
 void *
-_mesa_validate_and_map_readpix_pbo(GLcontext *ctx,
-                                   GLint x, GLint y,
-                                   GLsizei width, GLsizei height,
-                                   GLenum format, GLenum type,
-                                   const struct gl_pixelstore_attrib *pack,
-                                   GLvoid *dest)
+_mesa_map_readpix_pbo(GLcontext *ctx,
+                      const struct gl_pixelstore_attrib *pack,
+                      GLvoid *dest)
 {
    void *buf;
 
    if (pack->BufferObj->Name) {
       /* pack into PBO */
-      if (!_mesa_validate_pbo_access(2, pack, width, height, 1,
-                                     format, type, dest)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glReadPixels(invalid PBO access)");
-         return NULL;
-      }
-
-      if (pack->BufferObj->Pointer) {
-         /* buffer is already mapped - that's an error */
-         _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
-         return NULL;
-      }
-
       buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
                                               GL_WRITE_ONLY_ARB,
                                               pack->BufferObj);
@@ -640,7 +593,7 @@ _mesa_validate_and_map_readpix_pbo(GLcontext *ctx,
 
 
 /**
- * Counterpart to validate_and_map_readpix_pbo()
+ * Counterpart to _mesa_map_readpix_pbo()
  */
 void
 _mesa_unmap_readpix_pbo(GLcontext *ctx,
@@ -652,6 +605,7 @@ _mesa_unmap_readpix_pbo(GLcontext *ctx,
 }
 
 
+
 /**
  * Return the gl_buffer_object for the given ID.
  * Always return NULL for ID 0.
index efb3b4711e1bd6373a8ccbdb15b96b408a37d92c..46525f08ae39a6b5390e93114b7cbd72e0fd59d7 100644 (file)
@@ -87,21 +87,18 @@ _mesa_validate_pbo_access(GLuint dimensions,
                           GLenum format, GLenum type, const GLvoid *ptr);
 
 extern const GLubyte *
-_mesa_validate_and_map_bitmap_pbo(GLcontext *ctx,
-                                  GLsizei width, GLsizei height,
-                                  const struct gl_pixelstore_attrib *unpack,
-                                  const GLubyte *bitmap);
+_mesa_map_bitmap_pbo(GLcontext *ctx,
+                     const struct gl_pixelstore_attrib *unpack,
+                     const GLubyte *bitmap);
 
 extern void
 _mesa_unmap_bitmap_pbo(GLcontext *ctx,
                        const struct gl_pixelstore_attrib *unpack);
 
 extern const GLvoid *
-_mesa_validate_and_map_drawpix_pbo(GLcontext *ctx,
-                                   GLsizei width, GLsizei height,
-                                   GLenum format, GLenum type,
-                                   const struct gl_pixelstore_attrib *unpack,
-                                   const GLvoid *pixels);
+_mesa_map_drawpix_pbo(GLcontext *ctx,
+                      const struct gl_pixelstore_attrib *unpack,
+                      const GLvoid *pixels);
 
 extern void
 _mesa_unmap_drapix_pbo(GLcontext *ctx,
@@ -109,12 +106,9 @@ _mesa_unmap_drapix_pbo(GLcontext *ctx,
 
 
 extern void *
-_mesa_validate_and_map_readpix_pbo(GLcontext *ctx,
-                                   GLint x, GLint y,
-                                   GLsizei width, GLsizei height,
-                                   GLenum format, GLenum type,
-                                   const struct gl_pixelstore_attrib *pack,
-                                   GLvoid *dest);
+_mesa_map_readpix_pbo(GLcontext *ctx,
+                      const struct gl_pixelstore_attrib *pack,
+                      GLvoid *dest);
 
 extern void
 _mesa_unmap_readpix_pbo(GLcontext *ctx,
index 5df55ef0c9ea51fce1f7c30955c36fd0867f1022..0f64f1c1c0c02cbc10d6b27f804e0088f1fbb4f7 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "glheader.h"
 #include "imports.h"
+#include "bufferobj.h"
 #include "context.h"
 #include "drawpix.h"
 #include "feedback.h"
@@ -183,6 +184,23 @@ _mesa_DrawPixels( GLsizei width, GLsizei height,
       /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
       GLint x = IROUND(ctx->Current.RasterPos[0]);
       GLint y = IROUND(ctx->Current.RasterPos[1]);
+
+      if (ctx->Unpack.BufferObj->Name) {
+         /* unpack from PBO */
+         if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
+                                        format, type, pixels)) {
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glDrawPixels(invalid PBO access)");
+            return;
+         }
+         if (ctx->Unpack.BufferObj->Pointer) {
+            /* buffer is mapped - that's an error */
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glDrawPixels(PBO is mapped)");
+            return;
+         }
+      }
+
       ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
                             &ctx->Unpack, pixels);
    }
@@ -303,6 +321,21 @@ _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
       return;
    }
 
+   if (ctx->Pack.BufferObj->Name) {
+      if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
+                                     format, type, pixels)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glReadPixels(invalid PBO access)");
+         return;
+      }
+
+      if (ctx->Pack.BufferObj->Pointer) {
+         /* buffer is mapped - that's an error */
+         _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
+         return;
+      }
+   }
+
    ctx->Driver.ReadPixels(ctx, x, y, width, height,
                          format, type, &ctx->Pack, pixels);
 }
@@ -346,6 +379,23 @@ _mesa_Bitmap( GLsizei width, GLsizei height,
       /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
       GLint x = IFLOOR(ctx->Current.RasterPos[0] - xorig);
       GLint y = IFLOOR(ctx->Current.RasterPos[1] - yorig);
+
+      if (ctx->Unpack.BufferObj->Name) {
+         /* unpack from PBO */
+         if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
+                                        GL_COLOR_INDEX, GL_BITMAP,
+                                        (GLvoid *) bitmap)) {
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glBitmap(invalid PBO access)");
+            return;
+         }
+         if (ctx->Unpack.BufferObj->Pointer) {
+            /* buffer is mapped - that's an error */
+            _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
+            return;
+         }
+      }
+
       ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
    }
 #if _HAVE_FULL_GL
index 4e23db0edcce90275e03919a5cabbca022929bab..acc22d4323ccbe4034ddd73ebf4c4bd5fcb7af60 100644 (file)
@@ -225,8 +225,7 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
    }
 
    /* PBO source... */
-   bitmap = _mesa_validate_and_map_bitmap_pbo(ctx, width, height,
-                                              unpack, bitmap);
+   bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap);
    if (!bitmap) {
       return NULL;
    }
index c0f8e5ffdd74d29d0344619185ec4b732e688ed8..2ebaf8a2c31ab37c9e2ae9fa10ca4d1acab3c176 100644 (file)
@@ -335,9 +335,7 @@ make_texture(struct st_context *st,
    assert(pipeFormat);
    cpp = st_sizeof_format(pipeFormat);
 
-   pixels = _mesa_validate_and_map_drawpix_pbo(ctx, width, height,
-                                               format, type,
-                                               unpack, pixels);
+   pixels = _mesa_map_drawpix_pbo(ctx, unpack, pixels);
    if (!pixels)
       return NULL;
 
index b22e846a15719f8953944325323e1432c2eb8d5f..4cf9adcd28a44bbd24324e3fe7a3808e842feef1 100644 (file)
@@ -155,9 +155,7 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
       return;
    }
 
-   dest = _mesa_validate_and_map_readpix_pbo(ctx, x, y, width, height,
-                                             format, type,
-                                             &clippedPacking, dest);
+   dest = _mesa_map_readpix_pbo(ctx, &clippedPacking, dest);
    if (!dest)
       return;
 
index 17f639fd5578ee3c0c93008fa5c5d53970942f15..f3dda12e252dcf21a183e3cd1a4522e54d509b3d 100644 (file)
@@ -57,11 +57,9 @@ _swrast_Bitmap( GLcontext *ctx, GLint px, GLint py,
 
    ASSERT(ctx->RenderMode == GL_RENDER);
 
-   bitmap = _mesa_validate_and_map_bitmap_pbo(ctx, width, height,
-                                              unpack, bitmap);
-   if (!bitmap) {
-      return NULL;
-   }
+   bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap);
+   if (!bitmap)
+      return;
 
    RENDER_START(swrast,ctx);
 
index 2cf3501274ffcd679f23f57720d378f2fa2bf4e2..fb04d9f746a45ec9a884b5803096b7afe2127c99 100644 (file)
@@ -834,8 +834,7 @@ _swrast_DrawPixels( GLcontext *ctx,
    if (swrast->NewState)
       _swrast_validate_derived( ctx );
 
-   pixels = _mesa_validate_and_map_drawpix_pbo(ctx, x, y, width, height,
-                                               format, type, unpack, pixels);
+   pixels = _mesa_map_drawpix_pbo(ctx, unpack, pixels);
    if (!pixels)
       return;
 
index f4f882ae8409f9c5e3726c9e28491e3656af6221..2f155d0b705b0fc21696c00fa6f1c7bb064791b4 100644 (file)
@@ -572,9 +572,7 @@ _swrast_ReadPixels( GLcontext *ctx,
       goto end;
    }
 
-   pixels = _mesa_validate_and_map_readpix_pbo(ctx, x, y, width, height,
-                                               format, type,
-                                               &clippedPacking, pixels);
+   pixels = _mesa_map_readpix_pbo(ctx, &clippedPacking, pixels);
    if (!pixels)
       return;