[Mesa-dev] [PATCH 1/3] mesa: handle MapTextureImage() failures in glTexImage code

Brian Paul brianp at vmware.com
Sat Nov 26 08:23:36 PST 2011


---
 src/mesa/main/texstore.c |  199 ++++++++++++++++++++++++++++------------------
 1 files changed, 123 insertions(+), 76 deletions(-)

diff --git a/src/mesa/main/texstore.c b/src/mesa/main/texstore.c
index aae6b4b..d17a49b 100644
--- a/src/mesa/main/texstore.c
+++ b/src/mesa/main/texstore.c
@@ -4528,16 +4528,20 @@ _mesa_store_teximage1d(struct gl_context *ctx, GLenum target, GLint level,
                                0, 0, width, 1,
                                rwMode,
                                &dstMap, &dstRowStride);
+   if (dstMap) {
+      success = _mesa_texstore(ctx, 1, texImage->_BaseFormat,
+                               texImage->TexFormat,
+                               0, 0, 0,  /* dstX/Y/Zoffset */
+                               0, /* dstRowStride */
+                               &dstMap,
+                               width, 1, 1,
+                               format, type, pixels, packing);
 
-   success = _mesa_texstore(ctx, 1, texImage->_BaseFormat,
-                            texImage->TexFormat,
-                            0, 0, 0,  /* dstX/Y/Zoffset */
-                            0, /* dstRowStride */
-                            &dstMap,
-                            width, 1, 1,
-                            format, type, pixels, packing);
-
-   ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+      ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+   }
+   else {
+      success = GL_FALSE;
+   }
 
    if (!success)
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
@@ -4600,15 +4604,19 @@ _mesa_store_teximage2d(struct gl_context *ctx, GLenum target, GLint level,
                                      0, 0, width, 1,
                                      rwMode,
                                      &dstMap, &dstRowStride);
-         assert(dstMap);
-         success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
-                                  texImage->TexFormat,
-                                  0, 0, 0,  /* dstX/Y/Zoffset */
-                                  dstRowStride,
-                                  &dstMap,
-                                  width, 1, 1,
-                                  format, type, pixels, packing);
-         ctx->Driver.UnmapTextureImage(ctx, texImage, y);
+         if (dstMap) {
+            success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
+                                     texImage->TexFormat,
+                                     0, 0, 0,  /* dstX/Y/Zoffset */
+                                     dstRowStride,
+                                     &dstMap,
+                                     width, 1, 1,
+                                     format, type, pixels, packing);
+            ctx->Driver.UnmapTextureImage(ctx, texImage, y);
+         }
+         else {
+            success = GL_FALSE;
+         }
 
          if (!success)
             break;
@@ -4621,16 +4629,20 @@ _mesa_store_teximage2d(struct gl_context *ctx, GLenum target, GLint level,
                                   0, 0, width, height,
                                   rwMode,
                                   &dstMap, &dstRowStride);
-      assert(dstMap);
-      success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
-                               texImage->TexFormat,
-                               0, 0, 0,  /* dstX/Y/Zoffset */
-                               dstRowStride,
-                               &dstMap,
-                               width, height, 1,
-                               format, type, pixels, packing);
+      if (dstMap) {
+         success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
+                                  texImage->TexFormat,
+                                  0, 0, 0,  /* dstX/Y/Zoffset */
+                                  dstRowStride,
+                                  &dstMap,
+                                  width, height, 1,
+                                  format, type, pixels, packing);
 
-      ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+         ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+      }
+      else {
+         success = GL_FALSE;
+      }
    }
 
    if (!success)
@@ -4655,7 +4667,7 @@ _mesa_store_teximage3d(struct gl_context *ctx, GLenum target, GLint level,
                        struct gl_texture_image *texImage)
 {
    const GLbitfield rwMode = get_read_write_mode(format, texImage->TexFormat);
-   GLboolean success;
+   GLboolean success = GL_TRUE;
    GLint slice;
    GLubyte **sliceMaps;
    GLint dstRowStride;
@@ -4687,7 +4699,7 @@ _mesa_store_teximage3d(struct gl_context *ctx, GLenum target, GLint level,
       height = 1;
    }
 
-   sliceMaps = (GLubyte **) malloc(depth * sizeof(GLubyte *));
+   sliceMaps = (GLubyte **) calloc(depth, sizeof(GLubyte *));
 
    /* Map dest texture buffer slices */
    for (slice = 0; slice < depth; slice++) {
@@ -4695,19 +4707,27 @@ _mesa_store_teximage3d(struct gl_context *ctx, GLenum target, GLint level,
                                   0, 0, width, height,
                                   rwMode,
                                   &sliceMaps[slice], &dstRowStride);
+      if (!sliceMaps[slice]) {
+         success = GL_FALSE;
+         break;
+      }
    }
 
-   success = _mesa_texstore(ctx, 3, texImage->_BaseFormat,
-                            texImage->TexFormat,
-                            0, 0, 0,  /* dstX/Y/Zoffset */
-                            dstRowStride,
-                            sliceMaps,
-                            width, height, depth,
-                            format, type, pixels, packing);
+   if (success) {
+      success = _mesa_texstore(ctx, 3, texImage->_BaseFormat,
+                               texImage->TexFormat,
+                               0, 0, 0,  /* dstX/Y/Zoffset */
+                               dstRowStride,
+                               sliceMaps,
+                               width, height, depth,
+                               format, type, pixels, packing);
+   }
 
    /* Unmap dest texture buffer slices */
    for (slice = 0; slice < depth; slice++) {
-      ctx->Driver.UnmapTextureImage(ctx, texImage, slice);
+      if (sliceMaps[slice]) {
+         ctx->Driver.UnmapTextureImage(ctx, texImage, slice);
+      }
    }
 
    if (!success)
@@ -4750,15 +4770,20 @@ _mesa_store_texsubimage1d(struct gl_context *ctx, GLenum target, GLint level,
                                rwMode,
                                &dstMap, &dstRowStride);
 
-   success = _mesa_texstore(ctx, 1, texImage->_BaseFormat,
-                            texImage->TexFormat,
-                            0, 0, 0,  /* dstX/Y/Zoffset */
-                            dstRowStride,
-                            &dstMap,
-                            width, 1, 1,
-                            format, type, pixels, packing);
+   if (dstMap) {
+      success = _mesa_texstore(ctx, 1, texImage->_BaseFormat,
+                               texImage->TexFormat,
+                               0, 0, 0,  /* dstX/Y/Zoffset */
+                               dstRowStride,
+                               &dstMap,
+                               width, 1, 1,
+                               format, type, pixels, packing);
 
-   ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+      ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+   }
+   else {
+      success = GL_FALSE;
+   }
 
    if (!success)
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage1D");
@@ -4798,15 +4823,20 @@ _mesa_store_texsubimage2d(struct gl_context *ctx, GLenum target, GLint level,
                                rwMode,
                                &dstMap, &dstRowStride);
 
-   success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
-                            texImage->TexFormat,
-                            0, 0, 0,  /* dstX/Y/Zoffset */
-                            dstRowStride,
-                            &dstMap,
-                            width, height, 1,
-                            format, type, pixels, packing);
+   if (dstMap) {
+      success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
+                               texImage->TexFormat,
+                               0, 0, 0,  /* dstX/Y/Zoffset */
+                               dstRowStride,
+                               &dstMap,
+                               width, height, 1,
+                               format, type, pixels, packing);
 
-   ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+      ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+   }
+   else {
+      success = GL_FALSE;
+   }
 
    if (!success)
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage2D");
@@ -4829,7 +4859,7 @@ _mesa_store_texsubimage3d(struct gl_context *ctx, GLenum target, GLint level,
                           struct gl_texture_image *texImage)
 {
    const GLbitfield rwMode = get_read_write_mode(format, texImage->TexFormat);
-   GLboolean success;
+   GLboolean success = GL_TRUE;
    GLint slice;
    GLubyte **sliceMaps;
    GLint dstRowStride;
@@ -4841,7 +4871,7 @@ _mesa_store_texsubimage3d(struct gl_context *ctx, GLenum target, GLint level,
    if (!pixels)
       return;
 
-   sliceMaps = (GLubyte **) malloc(depth * sizeof(GLubyte *));
+   sliceMaps = (GLubyte **) calloc(depth, sizeof(GLubyte *));
 
    /* Map dest texture buffer slices */
    for (slice = 0; slice < depth; slice++) {
@@ -4849,19 +4879,27 @@ _mesa_store_texsubimage3d(struct gl_context *ctx, GLenum target, GLint level,
                                   xoffset, yoffset, width, height,
                                   rwMode,
                                   &sliceMaps[slice], &dstRowStride);
+      if (!sliceMaps[slice]) {
+         success = GL_FALSE;
+         break;
+      }
    }
 
-   success = _mesa_texstore(ctx, 3, texImage->_BaseFormat,
-                            texImage->TexFormat,
-                            0, 0, 0,
-                            dstRowStride,
-                            sliceMaps,
-                            width, height, depth,
-                            format, type, pixels, packing);
+   if (success) {
+      success = _mesa_texstore(ctx, 3, texImage->_BaseFormat,
+                               texImage->TexFormat,
+                               0, 0, 0,
+                               dstRowStride,
+                               sliceMaps,
+                               width, height, depth,
+                               format, type, pixels, packing);
+   }
 
    /* Unmap dest texture buffer slices */
    for (slice = 0; slice < depth; slice++) {
-      ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
+      if (sliceMaps[slice]) {
+         ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
+      }
    }
 
    if (!success)
@@ -4940,11 +4978,15 @@ _mesa_store_compressed_teximage2d(struct gl_context *ctx,
                                0, 0, width, height,
                                GL_MAP_WRITE_BIT,
                                &dstMap, &dstRowStride);
+   if (dstMap) {
+      /* copy the data */
+      memcpy(dstMap, data, imageSize);
 
-   /* copy the data */
-   memcpy(dstMap, data, imageSize);
-
-   ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+      ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+   }
+   else {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
+   }
 
    _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
 }
@@ -5044,17 +5086,22 @@ _mesa_store_compressed_texsubimage2d(struct gl_context *ctx, GLenum target,
                                GL_MAP_WRITE_BIT,
                                &dstMap, &dstRowStride);
 
-   bytesPerRow = srcRowStride;  /* bytes per row of blocks */
-   rows = height / bh;  /* rows in blocks */
+   if (dstMap) {
+      bytesPerRow = srcRowStride;  /* bytes per row of blocks */
+      rows = height / bh;  /* rows in blocks */
 
-   /* copy rows of blocks */
-   for (i = 0; i < rows; i++) {
-      memcpy(dstMap, src, bytesPerRow);
-      dstMap += dstRowStride;
-      src += srcRowStride;
-   }
+      /* copy rows of blocks */
+      for (i = 0; i < rows; i++) {
+         memcpy(dstMap, src, bytesPerRow);
+         dstMap += dstRowStride;
+         src += srcRowStride;
+      }
 
-   ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+      ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
+   }
+   else {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage2D");
+   }
 
    _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
 }
-- 
1.7.3.4



More information about the mesa-dev mailing list