texgetimage.c revision 627b435dfe17698a1c69e9a259838fc6f2e6bd4e
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.7
4 *
5 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6 * Copyright (c) 2009 VMware, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27/**
28 * Code for glGetTexImage() and glGetCompressedTexImage().
29 */
30
31
32#include "glheader.h"
33#include "bufferobj.h"
34#include "enums.h"
35#include "context.h"
36#include "formats.h"
37#include "format_unpack.h"
38#include "image.h"
39#include "mfeatures.h"
40#include "mtypes.h"
41#include "pack.h"
42#include "pbo.h"
43#include "texcompress.h"
44#include "texgetimage.h"
45#include "teximage.h"
46
47
48
49/**
50 * Can the given type represent negative values?
51 */
52static inline GLboolean
53type_needs_clamping(GLenum type)
54{
55   switch (type) {
56   case GL_BYTE:
57   case GL_SHORT:
58   case GL_INT:
59   case GL_FLOAT:
60   case GL_HALF_FLOAT_ARB:
61   case GL_UNSIGNED_INT_10F_11F_11F_REV:
62   case GL_UNSIGNED_INT_5_9_9_9_REV:
63      return GL_FALSE;
64   default:
65      return GL_TRUE;
66   }
67}
68
69
70/**
71 * glGetTexImage for depth/Z pixels.
72 */
73static void
74get_tex_depth(struct gl_context *ctx, GLuint dimensions,
75              GLenum format, GLenum type, GLvoid *pixels,
76              struct gl_texture_image *texImage)
77{
78   const GLint width = texImage->Width;
79   const GLint height = texImage->Height;
80   const GLint depth = texImage->Depth;
81   GLint img, row;
82   GLfloat *depthRow = (GLfloat *) malloc(width * sizeof(GLfloat));
83
84   if (!depthRow) {
85      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
86      return;
87   }
88
89   for (img = 0; img < depth; img++) {
90      GLubyte *srcMap;
91      GLint srcRowStride;
92
93      /* map src texture buffer */
94      ctx->Driver.MapTextureImage(ctx, texImage, img,
95                                  0, 0, width, height, GL_MAP_READ_BIT,
96                                  &srcMap, &srcRowStride);
97
98      if (srcMap) {
99         for (row = 0; row < height; row++) {
100            void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
101                                             width, height, format, type,
102                                             img, row, 0);
103            const GLubyte *src = srcMap + row * srcRowStride;
104            _mesa_unpack_float_z_row(texImage->TexFormat, width, src, depthRow);
105            _mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
106         }
107
108         ctx->Driver.UnmapTextureImage(ctx, texImage, img);
109      }
110      else {
111         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
112         break;
113      }
114   }
115
116   free(depthRow);
117}
118
119
120/**
121 * glGetTexImage for depth/stencil pixels.
122 */
123static void
124get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
125                      GLenum format, GLenum type, GLvoid *pixels,
126                      struct gl_texture_image *texImage)
127{
128   const GLint width = texImage->Width;
129   const GLint height = texImage->Height;
130   const GLint depth = texImage->Depth;
131   GLint img, row;
132
133   for (img = 0; img < depth; img++) {
134      GLubyte *srcMap;
135      GLint rowstride;
136
137      /* map src texture buffer */
138      ctx->Driver.MapTextureImage(ctx, texImage, img,
139                                  0, 0, width, height, GL_MAP_READ_BIT,
140                                  &srcMap, &rowstride);
141
142      if (srcMap) {
143         for (row = 0; row < height; row++) {
144            const GLubyte *src = srcMap + row * rowstride;
145            void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
146                                             width, height, format, type,
147                                             img, row, 0);
148            /* XXX Z24_S8 vs. S8_Z24??? */
149            memcpy(dest, src, width * sizeof(GLuint));
150            if (ctx->Pack.SwapBytes) {
151               _mesa_swap4((GLuint *) dest, width);
152            }
153         }
154
155         ctx->Driver.UnmapTextureImage(ctx, texImage, img);
156      }
157      else {
158         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
159         break;
160      }
161   }
162}
163
164
165/**
166 * glGetTexImage for YCbCr pixels.
167 */
168static void
169get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
170              GLenum format, GLenum type, GLvoid *pixels,
171              struct gl_texture_image *texImage)
172{
173   const GLint width = texImage->Width;
174   const GLint height = texImage->Height;
175   const GLint depth = texImage->Depth;
176   GLint img, row;
177
178   for (img = 0; img < depth; img++) {
179      GLubyte *srcMap;
180      GLint rowstride;
181
182      /* map src texture buffer */
183      ctx->Driver.MapTextureImage(ctx, texImage, img,
184                                  0, 0, width, height, GL_MAP_READ_BIT,
185                                  &srcMap, &rowstride);
186
187      if (srcMap) {
188         for (row = 0; row < height; row++) {
189            const GLubyte *src = srcMap + row * rowstride;
190            void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
191                                             width, height, format, type,
192                                             img, row, 0);
193            memcpy(dest, src, width * sizeof(GLushort));
194
195            /* check for byte swapping */
196            if ((texImage->TexFormat == MESA_FORMAT_YCBCR
197                 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
198                (texImage->TexFormat == MESA_FORMAT_YCBCR_REV
199                 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
200               if (!ctx->Pack.SwapBytes)
201                  _mesa_swap2((GLushort *) dest, width);
202            }
203            else if (ctx->Pack.SwapBytes) {
204               _mesa_swap2((GLushort *) dest, width);
205            }
206         }
207
208         ctx->Driver.UnmapTextureImage(ctx, texImage, img);
209      }
210      else {
211         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
212         break;
213      }
214   }
215}
216
217
218/**
219 * Get a color texture image with decompression.
220 */
221static void
222get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
223                        GLenum format, GLenum type, GLvoid *pixels,
224                        struct gl_texture_image *texImage,
225                        GLbitfield transferOps)
226{
227   /* don't want to apply sRGB -> RGB conversion here so override the format */
228   const gl_format texFormat =
229      _mesa_get_srgb_format_linear(texImage->TexFormat);
230   const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
231   const GLuint width = texImage->Width;
232   const GLuint height = texImage->Height;
233   const GLuint depth = texImage->Depth;
234   GLfloat *tempImage, *srcRow;
235   GLuint row;
236
237   /* Decompress into temp float buffer, then pack into user buffer */
238   tempImage = (GLfloat *) malloc(width * height * depth
239                                  * 4 * sizeof(GLfloat));
240   if (!tempImage) {
241      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
242      return;
243   }
244
245   /* Decompress the texture image - results in 'tempImage' */
246   {
247      GLubyte *srcMap;
248      GLint srcRowStride;
249      GLuint bytes, bw, bh;
250
251      bytes = _mesa_get_format_bytes(texFormat);
252      _mesa_get_format_block_size(texFormat, &bw, &bh);
253
254      ctx->Driver.MapTextureImage(ctx, texImage, 0,
255                                  0, 0, width, height,
256                                  GL_MAP_READ_BIT,
257                                  &srcMap, &srcRowStride);
258      if (srcMap) {
259         /* XXX This line is a bit of a hack to work around the
260          * mismatch of compressed row strides as returned by
261          * MapTextureImage() vs. what the texture decompression code
262          * uses.  This will be fixed in the future.
263          */
264         srcRowStride = srcRowStride * bh / bytes;
265
266         _mesa_decompress_image(texFormat, width, height,
267                                srcMap, srcRowStride, tempImage);
268
269         ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
270      }
271      else {
272         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
273      }
274   }
275
276   if (baseFormat == GL_LUMINANCE ||
277       baseFormat == GL_LUMINANCE_ALPHA) {
278      /* Set green and blue to zero since the pack function here will
279       * compute L=R+G+B.
280       */
281      GLuint i;
282      for (i = 0; i < width * height; i++) {
283         tempImage[i * 4 + GCOMP] = tempImage[i * 4 + BCOMP] = 0.0f;
284      }
285   }
286
287   srcRow = tempImage;
288   for (row = 0; row < height; row++) {
289      void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
290                                       width, height, format, type,
291                                       0, row, 0);
292
293      _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) srcRow,
294                                 format, type, dest, &ctx->Pack, transferOps);
295      srcRow += width * 4;
296   }
297
298   free(tempImage);
299}
300
301
302/**
303 * Get an uncompressed color texture image.
304 */
305static void
306get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
307                          GLenum format, GLenum type, GLvoid *pixels,
308                          struct gl_texture_image *texImage,
309                          GLbitfield transferOps)
310{
311   /* don't want to apply sRGB -> RGB conversion here so override the format */
312   const gl_format texFormat =
313      _mesa_get_srgb_format_linear(texImage->TexFormat);
314   const GLuint width = texImage->Width;
315   GLuint height = texImage->Height;
316   GLuint depth = texImage->Depth;
317   GLuint img, row;
318   GLfloat (*rgba)[4];
319   GLuint (*rgba_uint)[4];
320   GLboolean is_integer = _mesa_is_format_integer_color(texImage->TexFormat);
321
322   /* Allocate buffer for one row of texels */
323   rgba = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
324   rgba_uint = (GLuint (*)[4]) rgba;
325   if (!rgba) {
326      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
327      return;
328   }
329
330   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
331      depth = height;
332      height = 1;
333   }
334
335   for (img = 0; img < depth; img++) {
336      GLubyte *srcMap;
337      GLint rowstride;
338
339      /* map src texture buffer */
340      ctx->Driver.MapTextureImage(ctx, texImage, img,
341                                  0, 0, width, height, GL_MAP_READ_BIT,
342                                  &srcMap, &rowstride);
343      if (srcMap) {
344         for (row = 0; row < height; row++) {
345            const GLubyte *src = srcMap + row * rowstride;
346            void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
347                                             width, height, format, type,
348                                             img, row, 0);
349
350	    if (is_integer) {
351	       _mesa_unpack_uint_rgba_row(texFormat, width, src, rgba_uint);
352
353	       if (texImage->_BaseFormat == GL_ALPHA) {
354		  GLuint col;
355		  for (col = 0; col < width; col++) {
356		     rgba_uint[col][RCOMP] = 0;
357		     rgba_uint[col][GCOMP] = 0;
358		     rgba_uint[col][BCOMP] = 0;
359		  }
360	       }
361	       else if (texImage->_BaseFormat == GL_LUMINANCE) {
362		  GLuint col;
363		  for (col = 0; col < width; col++) {
364		     rgba_uint[col][GCOMP] = 0;
365		     rgba_uint[col][BCOMP] = 0;
366		     rgba_uint[col][ACOMP] = 1;
367		  }
368	       }
369	       else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
370		  GLuint col;
371		  for (col = 0; col < width; col++) {
372		     rgba_uint[col][GCOMP] = 0;
373		     rgba_uint[col][BCOMP] = 0;
374		  }
375	       }
376	       else if (texImage->_BaseFormat == GL_INTENSITY) {
377		  GLuint col;
378		  for (col = 0; col < width; col++) {
379		     rgba_uint[col][GCOMP] = 0;
380		     rgba_uint[col][BCOMP] = 0;
381		     rgba_uint[col][ACOMP] = 1;
382		  }
383	       }
384
385	       _mesa_pack_rgba_span_int(ctx, width, rgba_uint,
386					format, type, dest);
387	    } else {
388	       _mesa_unpack_rgba_row(texFormat, width, src, rgba);
389
390	       if (texImage->_BaseFormat == GL_ALPHA) {
391		  GLuint col;
392		  for (col = 0; col < width; col++) {
393		     rgba[col][RCOMP] = 0.0F;
394		     rgba[col][GCOMP] = 0.0F;
395		     rgba[col][BCOMP] = 0.0F;
396		  }
397	       }
398	       else if (texImage->_BaseFormat == GL_LUMINANCE) {
399		  GLuint col;
400		  for (col = 0; col < width; col++) {
401		     rgba[col][GCOMP] = 0.0F;
402		     rgba[col][BCOMP] = 0.0F;
403		     rgba[col][ACOMP] = 1.0F;
404		  }
405	       }
406	       else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
407		  GLuint col;
408		  for (col = 0; col < width; col++) {
409		     rgba[col][GCOMP] = 0.0F;
410		     rgba[col][BCOMP] = 0.0F;
411		  }
412	       }
413	       else if (texImage->_BaseFormat == GL_INTENSITY) {
414		  GLuint col;
415		  for (col = 0; col < width; col++) {
416		     rgba[col][GCOMP] = 0.0F;
417		     rgba[col][BCOMP] = 0.0F;
418		     rgba[col][ACOMP] = 1.0F;
419		  }
420	       }
421
422	       _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
423					  format, type, dest,
424					  &ctx->Pack, transferOps);
425	    }
426	 }
427
428         /* Unmap the src texture buffer */
429         ctx->Driver.UnmapTextureImage(ctx, texImage, img);
430      }
431      else {
432         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
433         break;
434      }
435   }
436
437   free(rgba);
438}
439
440
441/**
442 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
443 * Compressed textures are handled here as well.
444 */
445static void
446get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
447             GLenum format, GLenum type, GLvoid *pixels,
448             struct gl_texture_image *texImage)
449{
450   const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
451   GLbitfield transferOps = 0x0;
452
453   /* In general, clamping does not apply to glGetTexImage, except when
454    * the returned type of the image can't hold negative values.
455    */
456   if (type_needs_clamping(type)) {
457      /* the returned image type can't have negative values */
458      if (dataType == GL_FLOAT ||
459          dataType == GL_SIGNED_NORMALIZED ||
460          format == GL_LUMINANCE ||
461          format == GL_LUMINANCE_ALPHA) {
462         transferOps |= IMAGE_CLAMP_BIT;
463      }
464   }
465   /* This applies to RGB, RGBA textures. if the format is either LUMINANCE
466    * or LUMINANCE ALPHA, luminance (L) is computed as L=R+G+B .we need to
467    * clamp the sum to [0,1].
468    */
469   else if ((format == GL_LUMINANCE ||
470            format == GL_LUMINANCE_ALPHA) &&
471            dataType == GL_UNSIGNED_NORMALIZED) {
472      transferOps |= IMAGE_CLAMP_BIT;
473   }
474
475   if (_mesa_is_format_compressed(texImage->TexFormat)) {
476      get_tex_rgba_compressed(ctx, dimensions, format, type,
477                              pixels, texImage, transferOps);
478   }
479   else {
480      get_tex_rgba_uncompressed(ctx, dimensions, format, type,
481                                pixels, texImage, transferOps);
482   }
483}
484
485
486/**
487 * Try to do glGetTexImage() with simple memcpy().
488 * \return GL_TRUE if done, GL_FALSE otherwise
489 */
490static GLboolean
491get_tex_memcpy(struct gl_context *ctx, GLenum format, GLenum type,
492               GLvoid *pixels,
493               struct gl_texture_image *texImage)
494{
495   const GLenum target = texImage->TexObject->Target;
496   GLboolean memCopy = GL_FALSE;
497
498   /*
499    * Check if the src/dst formats are compatible.
500    * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
501    * so we don't have to worry about those.
502    * XXX more format combinations could be supported here.
503    */
504   if (target == GL_TEXTURE_1D ||
505       target == GL_TEXTURE_2D ||
506       target == GL_TEXTURE_RECTANGLE ||
507       _mesa_is_cube_face(target)) {
508      if ((texImage->TexFormat == MESA_FORMAT_ARGB8888 ||
509             texImage->TexFormat == MESA_FORMAT_SARGB8) &&
510          format == GL_BGRA &&
511          (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
512          !ctx->Pack.SwapBytes &&
513          _mesa_little_endian()) {
514         memCopy = GL_TRUE;
515      }
516      else if ((texImage->TexFormat == MESA_FORMAT_AL88 ||
517                  texImage->TexFormat == MESA_FORMAT_SLA8) &&
518               format == GL_LUMINANCE_ALPHA &&
519               type == GL_UNSIGNED_BYTE &&
520               !ctx->Pack.SwapBytes &&
521               _mesa_little_endian()) {
522         memCopy = GL_TRUE;
523      }
524      else if ((texImage->TexFormat == MESA_FORMAT_L8 ||
525                  texImage->TexFormat == MESA_FORMAT_SL8) &&
526               format == GL_LUMINANCE &&
527               type == GL_UNSIGNED_BYTE) {
528         memCopy = GL_TRUE;
529      }
530      else if (texImage->TexFormat == MESA_FORMAT_L16 &&
531               format == GL_LUMINANCE &&
532               type == GL_UNSIGNED_SHORT) {
533         memCopy = GL_TRUE;
534      }
535      else if (texImage->TexFormat == MESA_FORMAT_A8 &&
536               format == GL_ALPHA &&
537               type == GL_UNSIGNED_BYTE) {
538         memCopy = GL_TRUE;
539      }
540      else if (texImage->TexFormat == MESA_FORMAT_A16 &&
541               format == GL_ALPHA &&
542               type == GL_UNSIGNED_SHORT) {
543         memCopy = GL_TRUE;
544      }
545   }
546
547   if (memCopy) {
548      const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
549      const GLuint bytesPerRow = texImage->Width * bpp;
550      GLubyte *dst =
551         _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
552                               texImage->Height, format, type, 0, 0);
553      const GLint dstRowStride =
554         _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
555      GLubyte *src;
556      GLint srcRowStride;
557
558      /* map src texture buffer */
559      ctx->Driver.MapTextureImage(ctx, texImage, 0,
560                                  0, 0, texImage->Width, texImage->Height,
561                                  GL_MAP_READ_BIT, &src, &srcRowStride);
562
563      if (src) {
564         if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
565            memcpy(dst, src, bytesPerRow * texImage->Height);
566         }
567         else {
568            GLuint row;
569            for (row = 0; row < texImage->Height; row++) {
570               memcpy(dst, src, bytesPerRow);
571               dst += dstRowStride;
572               src += srcRowStride;
573            }
574         }
575
576         /* unmap src texture buffer */
577         ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
578      }
579      else {
580         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
581      }
582   }
583
584   return memCopy;
585}
586
587
588/**
589 * This is the software fallback for Driver.GetTexImage().
590 * All error checking will have been done before this routine is called.
591 * We'll call ctx->Driver.MapTextureImage() to access the data, then
592 * unmap with ctx->Driver.UnmapTextureImage().
593 */
594void
595_mesa_get_teximage(struct gl_context *ctx,
596                   GLenum format, GLenum type, GLvoid *pixels,
597                   struct gl_texture_image *texImage)
598{
599   GLuint dimensions;
600
601   switch (texImage->TexObject->Target) {
602   case GL_TEXTURE_1D:
603      dimensions = 1;
604      break;
605   case GL_TEXTURE_3D:
606      dimensions = 3;
607      break;
608   default:
609      dimensions = 2;
610   }
611
612   /* map dest buffer, if PBO */
613   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
614      /* Packing texture image into a PBO.
615       * Map the (potentially) VRAM-based buffer into our process space so
616       * we can write into it with the code below.
617       * A hardware driver might use a sophisticated blit to move the
618       * texture data to the PBO if the PBO is in VRAM along with the texture.
619       */
620      GLubyte *buf = (GLubyte *)
621         ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
622				    GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
623      if (!buf) {
624         /* out of memory or other unexpected error */
625         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
626         return;
627      }
628      /* <pixels> was an offset into the PBO.
629       * Now make it a real, client-side pointer inside the mapped region.
630       */
631      pixels = ADD_POINTERS(buf, pixels);
632   }
633
634   if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
635      /* all done */
636   }
637   else if (format == GL_DEPTH_COMPONENT) {
638      get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
639   }
640   else if (format == GL_DEPTH_STENCIL_EXT) {
641      get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
642   }
643   else if (format == GL_YCBCR_MESA) {
644      get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
645   }
646   else {
647      get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
648   }
649
650   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
651      ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
652   }
653}
654
655
656
657/**
658 * This is the software fallback for Driver.GetCompressedTexImage().
659 * All error checking will have been done before this routine is called.
660 */
661void
662_mesa_get_compressed_teximage(struct gl_context *ctx,
663                              struct gl_texture_image *texImage,
664                              GLvoid *img)
665{
666   const GLuint row_stride =
667      _mesa_format_row_stride(texImage->TexFormat, texImage->Width);
668   GLuint i;
669   GLubyte *src;
670   GLint srcRowStride;
671
672   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
673      /* pack texture image into a PBO */
674      GLubyte *buf = (GLubyte *)
675         ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
676				    GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
677      if (!buf) {
678         /* out of memory or other unexpected error */
679         _mesa_error(ctx, GL_OUT_OF_MEMORY,
680                     "glGetCompresssedTexImage(map PBO failed)");
681         return;
682      }
683      img = ADD_POINTERS(buf, img);
684   }
685
686   /* map src texture buffer */
687   ctx->Driver.MapTextureImage(ctx, texImage, 0,
688                               0, 0, texImage->Width, texImage->Height,
689                               GL_MAP_READ_BIT, &src, &srcRowStride);
690
691   if (src) {
692      /* no pixelstore or pixel transfer, but respect stride */
693
694      if (row_stride == srcRowStride) {
695         const GLuint size = _mesa_format_image_size(texImage->TexFormat,
696                                                     texImage->Width,
697                                                     texImage->Height,
698                                                     texImage->Depth);
699         memcpy(img, src, size);
700      }
701      else {
702         GLuint bw, bh;
703         _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
704         for (i = 0; i < (texImage->Height + bh - 1) / bh; i++) {
705            memcpy((GLubyte *)img + i * row_stride,
706                   (GLubyte *)src + i * srcRowStride,
707                   row_stride);
708         }
709      }
710
711      ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
712   }
713   else {
714      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
715   }
716
717   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
718      ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
719   }
720}
721
722
723
724/**
725 * Do error checking for a glGetTexImage() call.
726 * \return GL_TRUE if any error, GL_FALSE if no errors.
727 */
728static GLboolean
729getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
730                        GLenum format, GLenum type, GLsizei clientMemSize,
731                        GLvoid *pixels )
732{
733   struct gl_texture_object *texObj;
734   struct gl_texture_image *texImage;
735   const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
736   const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
737   GLenum baseFormat, err;
738
739   if (maxLevels == 0) {
740      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
741      return GL_TRUE;
742   }
743
744   if (level < 0 || level >= maxLevels) {
745      _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
746      return GL_TRUE;
747   }
748
749   if (_mesa_sizeof_packed_type(type) <= 0) {
750      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
751      return GL_TRUE;
752   }
753
754   if (_mesa_components_in_format(format) <= 0 ||
755       format == GL_STENCIL_INDEX ||
756       format == GL_COLOR_INDEX) {
757      _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
758      return GL_TRUE;
759   }
760
761   if (!ctx->Extensions.ARB_depth_texture && _mesa_is_depth_format(format)) {
762      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
763      return GL_TRUE;
764   }
765
766   if (!ctx->Extensions.MESA_ycbcr_texture && _mesa_is_ycbcr_format(format)) {
767      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
768      return GL_TRUE;
769   }
770
771   if (!ctx->Extensions.EXT_packed_depth_stencil
772       && _mesa_is_depthstencil_format(format)) {
773      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
774      return GL_TRUE;
775   }
776
777   if (!ctx->Extensions.ATI_envmap_bumpmap
778       && _mesa_is_dudv_format(format)) {
779      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
780      return;
781   }
782
783   err = _mesa_error_check_format_and_type(ctx, format, type);
784   if (err != GL_NO_ERROR) {
785      _mesa_error(ctx, err, "glGetTexImage(format/type)");
786      return GL_TRUE;
787   }
788
789   texObj = _mesa_get_current_tex_object(ctx, target);
790
791   if (!texObj || _mesa_is_proxy_texture(target)) {
792      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
793      return GL_TRUE;
794   }
795
796   texImage = _mesa_select_tex_image(ctx, texObj, target, level);
797   if (!texImage) {
798      /* non-existant texture image */
799      return GL_TRUE;
800   }
801
802   baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
803
804   /* Make sure the requested image format is compatible with the
805    * texture's format.
806    */
807   if (_mesa_is_color_format(format)
808       && !_mesa_is_color_format(baseFormat)) {
809      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
810      return GL_TRUE;
811   }
812   else if (_mesa_is_depth_format(format)
813            && !_mesa_is_depth_format(baseFormat)
814            && !_mesa_is_depthstencil_format(baseFormat)) {
815      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
816      return GL_TRUE;
817   }
818   else if (_mesa_is_ycbcr_format(format)
819            && !_mesa_is_ycbcr_format(baseFormat)) {
820      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
821      return GL_TRUE;
822   }
823   else if (_mesa_is_depthstencil_format(format)
824            && !_mesa_is_depthstencil_format(baseFormat)) {
825      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
826      return GL_TRUE;
827   }
828   else if (_mesa_is_dudv_format(format)
829            && !_mesa_is_dudv_format(baseFormat)) {
830      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
831      return GL_TRUE;
832   }
833
834   if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
835                                  texImage->Height, texImage->Depth,
836                                  format, type, clientMemSize, pixels)) {
837      if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
838         _mesa_error(ctx, GL_INVALID_OPERATION,
839                     "glGetTexImage(out of bounds PBO access)");
840      } else {
841         _mesa_error(ctx, GL_INVALID_OPERATION,
842                     "glGetnTexImageARB(out of bounds access:"
843                     " bufSize (%d) is too small)", clientMemSize);
844      }
845      return GL_TRUE;
846   }
847
848   if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
849      /* PBO should not be mapped */
850      if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
851         _mesa_error(ctx, GL_INVALID_OPERATION,
852                     "glGetTexImage(PBO is mapped)");
853         return GL_TRUE;
854      }
855   }
856
857   return GL_FALSE;
858}
859
860
861
862/**
863 * Get texture image.  Called by glGetTexImage.
864 *
865 * \param target texture target.
866 * \param level image level.
867 * \param format pixel data format for returned image.
868 * \param type pixel data type for returned image.
869 * \param bufSize size of the pixels data buffer.
870 * \param pixels returned pixel data.
871 */
872void GLAPIENTRY
873_mesa_GetnTexImageARB( GLenum target, GLint level, GLenum format,
874                       GLenum type, GLsizei bufSize, GLvoid *pixels )
875{
876   struct gl_texture_object *texObj;
877   struct gl_texture_image *texImage;
878   GET_CURRENT_CONTEXT(ctx);
879   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
880
881   if (getteximage_error_check(ctx, target, level, format, type,
882                               bufSize, pixels)) {
883      return;
884   }
885
886   if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
887      /* not an error, do nothing */
888      return;
889   }
890
891   texObj = _mesa_get_current_tex_object(ctx, target);
892   texImage = _mesa_select_tex_image(ctx, texObj, target, level);
893
894   if (_mesa_is_zero_size_texture(texImage))
895      return;
896
897   if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
898      _mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
899                  " dstFmt=0x%x, dstType=0x%x\n",
900                  texObj->Name,
901                  _mesa_get_format_name(texImage->TexFormat),
902                  texImage->Width, texImage->Height,
903                  format, type);
904   }
905
906   _mesa_lock_texture(ctx, texObj);
907   {
908      ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
909   }
910   _mesa_unlock_texture(ctx, texObj);
911}
912
913
914void GLAPIENTRY
915_mesa_GetTexImage( GLenum target, GLint level, GLenum format,
916                   GLenum type, GLvoid *pixels )
917{
918   _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
919}
920
921
922/**
923 * Do error checking for a glGetCompressedTexImage() call.
924 * \return GL_TRUE if any error, GL_FALSE if no errors.
925 */
926static GLboolean
927getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
928                                  GLint level, GLsizei clientMemSize, GLvoid *img)
929{
930   struct gl_texture_object *texObj;
931   struct gl_texture_image *texImage;
932   const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
933   GLuint compressedSize;
934
935   if (maxLevels == 0) {
936      _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
937                  target);
938      return GL_TRUE;
939   }
940
941   if (level < 0 || level >= maxLevels) {
942      _mesa_error(ctx, GL_INVALID_VALUE,
943                  "glGetCompressedTexImageARB(bad level = %d)", level);
944      return GL_TRUE;
945   }
946
947   if (_mesa_is_proxy_texture(target)) {
948      _mesa_error(ctx, GL_INVALID_ENUM,
949                  "glGetCompressedTexImageARB(bad target = %s)",
950                  _mesa_lookup_enum_by_nr(target));
951      return GL_TRUE;
952   }
953
954   texObj = _mesa_get_current_tex_object(ctx, target);
955   if (!texObj) {
956      _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
957      return GL_TRUE;
958   }
959
960   texImage = _mesa_select_tex_image(ctx, texObj, target, level);
961
962   if (!texImage) {
963      /* probably invalid mipmap level */
964      _mesa_error(ctx, GL_INVALID_VALUE,
965                  "glGetCompressedTexImageARB(level)");
966      return GL_TRUE;
967   }
968
969   if (!_mesa_is_format_compressed(texImage->TexFormat)) {
970      _mesa_error(ctx, GL_INVALID_OPERATION,
971                  "glGetCompressedTexImageARB(texture is not compressed)");
972      return GL_TRUE;
973   }
974
975   compressedSize = _mesa_format_image_size(texImage->TexFormat,
976                                            texImage->Width,
977                                            texImage->Height,
978                                            texImage->Depth);
979
980   if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
981      /* do bounds checking on writing to client memory */
982      if (clientMemSize < compressedSize) {
983         _mesa_error(ctx, GL_INVALID_OPERATION,
984                     "glGetnCompressedTexImageARB(out of bounds access:"
985                     " bufSize (%d) is too small)", clientMemSize);
986         return GL_TRUE;
987      }
988   } else {
989      /* do bounds checking on PBO write */
990      if ((const GLubyte *) img + compressedSize >
991          (const GLubyte *) ctx->Pack.BufferObj->Size) {
992         _mesa_error(ctx, GL_INVALID_OPERATION,
993                     "glGetCompressedTexImage(out of bounds PBO access)");
994         return GL_TRUE;
995      }
996
997      /* make sure PBO is not mapped */
998      if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
999         _mesa_error(ctx, GL_INVALID_OPERATION,
1000                     "glGetCompressedTexImage(PBO is mapped)");
1001         return GL_TRUE;
1002      }
1003   }
1004
1005   return GL_FALSE;
1006}
1007
1008
1009void GLAPIENTRY
1010_mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1011                                GLvoid *img)
1012{
1013   struct gl_texture_object *texObj;
1014   struct gl_texture_image *texImage;
1015   GET_CURRENT_CONTEXT(ctx);
1016   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1017
1018   if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
1019      return;
1020   }
1021
1022   if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
1023      /* not an error, do nothing */
1024      return;
1025   }
1026
1027   texObj = _mesa_get_current_tex_object(ctx, target);
1028   texImage = _mesa_select_tex_image(ctx, texObj, target, level);
1029
1030   if (_mesa_is_zero_size_texture(texImage))
1031      return;
1032
1033   if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1034      _mesa_debug(ctx,
1035                  "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1036                  texObj->Name,
1037                  _mesa_get_format_name(texImage->TexFormat),
1038                  texImage->Width, texImage->Height);
1039   }
1040
1041   _mesa_lock_texture(ctx, texObj);
1042   {
1043      ctx->Driver.GetCompressedTexImage(ctx, texImage, img);
1044   }
1045   _mesa_unlock_texture(ctx, texObj);
1046}
1047
1048void GLAPIENTRY
1049_mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
1050{
1051   _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
1052}
1053