intel_pixel_bitmap.c revision d11981e0d722282894b5012d37b2960ee3fbfac2
1/**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
16 * 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
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "main/glheader.h"
29#include "main/enums.h"
30#include "main/image.h"
31#include "main/colormac.h"
32#include "main/mtypes.h"
33#include "main/macros.h"
34#include "main/bufferobj.h"
35#include "main/pixelstore.h"
36#include "main/state.h"
37#include "main/teximage.h"
38#include "main/texenv.h"
39#include "main/texobj.h"
40#include "main/texstate.h"
41#include "main/texparam.h"
42#include "main/varray.h"
43#include "main/attrib.h"
44#include "main/enable.h"
45#include "shader/arbprogram.h"
46#include "glapi/dispatch.h"
47#include "swrast/swrast.h"
48
49#include "intel_screen.h"
50#include "intel_context.h"
51#include "intel_batchbuffer.h"
52#include "intel_blit.h"
53#include "intel_regions.h"
54#include "intel_buffer_objects.h"
55#include "intel_buffers.h"
56#include "intel_pixel.h"
57#include "intel_reg.h"
58
59
60#define FILE_DEBUG_FLAG DEBUG_PIXEL
61
62
63/* Unlike the other intel_pixel_* functions, the expectation here is
64 * that the incoming data is not in a PBO.  With the XY_TEXT blit
65 * method, there's no benefit haveing it in a PBO, but we could
66 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
67 * PBO bitmaps.  I think they are probably pretty rare though - I
68 * wonder if Xgl uses them?
69 */
70static const GLubyte *map_pbo( GLcontext *ctx,
71			       GLsizei width, GLsizei height,
72			       const struct gl_pixelstore_attrib *unpack,
73			       const GLubyte *bitmap )
74{
75   GLubyte *buf;
76
77   if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
78				  GL_COLOR_INDEX, GL_BITMAP,
79				  (GLvoid *) bitmap)) {
80      _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
81      return NULL;
82   }
83
84   buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
85					   GL_READ_ONLY_ARB,
86					   unpack->BufferObj);
87   if (!buf) {
88      _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
89      return NULL;
90   }
91
92   return ADD_POINTERS(buf, bitmap);
93}
94
95static GLboolean test_bit( const GLubyte *src,
96			    GLuint bit )
97{
98   return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
99}
100
101static GLboolean test_msb_bit(const GLubyte *src, GLuint bit)
102{
103   return (src[bit/8] & (1<<(7 - (bit % 8)))) ? 1 : 0;
104}
105
106static void set_bit( GLubyte *dest,
107			  GLuint bit )
108{
109   dest[bit/8] |= 1 << (bit % 8);
110}
111
112/* Extract a rectangle's worth of data from the bitmap.  Called
113 * per-cliprect.
114 */
115static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
116			      const struct gl_pixelstore_attrib *unpack,
117			      const GLubyte *bitmap,
118			      GLuint x, GLuint y,
119			      GLuint w, GLuint h,
120			      GLubyte *dest,
121			      GLuint row_align,
122			      GLboolean invert)
123{
124   GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
125   GLuint mask = unpack->LsbFirst ? 0 : 7;
126   GLuint bit = 0;
127   GLint row, col;
128   GLint first, last;
129   GLint incr;
130   GLuint count = 0;
131
132   if (INTEL_DEBUG & DEBUG_PIXEL)
133      _mesa_printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
134		   __FUNCTION__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask);
135
136   if (invert) {
137      first = h-1;
138      last = 0;
139      incr = -1;
140   }
141   else {
142      first = 0;
143      last = h-1;
144      incr = 1;
145   }
146
147   /* Require that dest be pre-zero'd.
148    */
149   for (row = first; row != (last+incr); row += incr) {
150      const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap,
151						    width, height,
152						    GL_COLOR_INDEX, GL_BITMAP,
153						    y + row, x);
154
155      for (col = 0; col < w; col++, bit++) {
156	 if (test_bit(rowsrc, (col + src_offset) ^ mask)) {
157	    set_bit(dest, bit ^ 7);
158	    count++;
159	 }
160      }
161
162      if (row_align)
163	 bit = ALIGN(bit, row_align);
164   }
165
166   return count;
167}
168
169/**
170 * Returns the low Y value of the vertical range given, flipped according to
171 * whether the framebuffer is or not.
172 */
173static inline int
174y_flip(struct gl_framebuffer *fb, int y, int height)
175{
176   if (fb->Name != 0)
177      return y;
178   else
179      return fb->Height - y - height;
180}
181
182/*
183 * Render a bitmap.
184 */
185static GLboolean
186do_blit_bitmap( GLcontext *ctx,
187		GLint dstx, GLint dsty,
188		GLsizei width, GLsizei height,
189		const struct gl_pixelstore_attrib *unpack,
190		const GLubyte *bitmap )
191{
192   struct intel_context *intel = intel_context(ctx);
193   struct intel_region *dst = intel_drawbuf_region(intel);
194   struct gl_framebuffer *fb = ctx->DrawBuffer;
195   GLfloat tmpColor[4];
196   GLubyte ubcolor[4];
197   GLuint color8888, color565;
198   unsigned int num_cliprects;
199   drm_clip_rect_t *cliprects;
200   int x_off, y_off;
201   GLsizei bitmap_width = width;
202   GLsizei bitmap_height = height;
203
204   /* Update draw buffer bounds */
205   _mesa_update_state(ctx);
206
207   if (ctx->Depth.Test) {
208      /* The blit path produces incorrect results when depth testing is on.
209       * It seems the blit Z coord is always 1.0 (the far plane) so fragments
210       * will likely be obscured by other, closer geometry.
211       */
212      return GL_FALSE;
213   }
214
215   if (!dst)
216       return GL_FALSE;
217
218   if (unpack->BufferObj->Name) {
219      bitmap = map_pbo(ctx, width, height, unpack, bitmap);
220      if (bitmap == NULL)
221	 return GL_TRUE;	/* even though this is an error, we're done */
222   }
223
224   COPY_4V(tmpColor, ctx->Current.RasterColor);
225
226   if (NEED_SECONDARY_COLOR(ctx)) {
227       ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
228   }
229
230   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
231   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
232   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
233   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
234
235   color8888 = INTEL_PACKCOLOR8888(ubcolor[0], ubcolor[1], ubcolor[2], ubcolor[3]);
236   color565 = INTEL_PACKCOLOR565(ubcolor[0], ubcolor[1], ubcolor[2]);
237
238   if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
239      return GL_FALSE;
240
241   LOCK_HARDWARE(intel);
242
243   intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off);
244   if (num_cliprects != 0) {
245      GLuint i;
246      GLint orig_dstx = dstx;
247      GLint orig_dsty = dsty;
248
249      /* Clip to buffer bounds and scissor. */
250      if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
251				fb->_Xmax, fb->_Ymax,
252				&dstx, &dsty, &width, &height))
253            goto out;
254
255      dstx = x_off + dstx;
256      dsty = y_off + y_flip(fb, dsty, height);
257
258      for (i = 0; i < num_cliprects; i++) {
259	 int box_x, box_y, box_w, box_h;
260	 GLint px, py;
261	 GLuint stipple[32];
262
263	 box_x = dstx;
264	 box_y = dsty;
265	 box_w = width;
266	 box_h = height;
267
268	 /* Clip to drawable cliprect */
269         if (!_mesa_clip_to_region(cliprects[i].x1,
270				   cliprects[i].y1,
271				   cliprects[i].x2,
272				   cliprects[i].y2,
273				   &box_x, &box_y, &box_w, &box_h))
274	    continue;
275
276#define DY 32
277#define DX 32
278
279	 /* Then, finally, chop it all into chunks that can be
280	  * digested by hardware:
281	  */
282	 for (py = 0; py < box_h; py += DY) {
283	    for (px = 0; px < box_w; px += DX) {
284	       int h = MIN2(DY, box_h - py);
285	       int w = MIN2(DX, box_w - px);
286	       GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
287	       GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
288		  ctx->Color.LogicOp : GL_COPY;
289
290	       assert(sz <= sizeof(stipple));
291	       memset(stipple, 0, sz);
292
293	       /* May need to adjust this when padding has been introduced in
294		* sz above:
295		*
296		* Have to translate destination coordinates back into source
297		* coordinates.
298		*/
299	       if (get_bitmap_rect(bitmap_width, bitmap_height, unpack,
300				   bitmap,
301				   -orig_dstx + (box_x + px - x_off),
302				   -orig_dsty + y_flip(fb,
303						       box_y + py - y_off, h),
304				   w, h,
305				   (GLubyte *)stipple,
306				   8,
307				   fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0)
308		  continue;
309
310	       /*
311		*/
312	       intelEmitImmediateColorExpandBlit( intel,
313						  dst->cpp,
314						  (GLubyte *)stipple,
315						  sz,
316						  (dst->cpp == 2) ? color565 : color8888,
317						  dst->pitch,
318						  dst->buffer,
319						  0,
320						  dst->tiling,
321						  box_x + px,
322						  box_y + py,
323						  w, h,
324						  logic_op);
325	    }
326	 }
327      }
328   }
329out:
330   UNLOCK_HARDWARE(intel);
331
332   if (INTEL_DEBUG & DEBUG_SYNC)
333      intel_batchbuffer_flush(intel->batch);
334
335   if (unpack->BufferObj->Name) {
336      /* done with PBO so unmap it now */
337      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
338                              unpack->BufferObj);
339   }
340
341   return GL_TRUE;
342}
343
344static GLboolean
345intel_texture_bitmap(GLcontext * ctx,
346		     GLint dst_x, GLint dst_y,
347		     GLsizei width, GLsizei height,
348		     const struct gl_pixelstore_attrib *unpack,
349		     const GLubyte *bitmap)
350{
351   struct intel_context *intel = intel_context(ctx);
352   static const char *fp =
353      "!!ARBfp1.0\n"
354      "TEMP val;\n"
355      "PARAM color=program.local[0];\n"
356      "TEX val, fragment.texcoord[0], texture[0], 2D;\n"
357      "ADD val, val.wwww, {-.5, -.5, -.5, -.5};\n"
358      "KIL val;\n"
359      "MOV result.color, color;\n"
360      "END\n";
361   GLuint texname;
362   GLfloat vertices[4][4];
363   GLfloat texcoords[4][2];
364   GLint old_active_texture;
365   GLubyte *unpacked_bitmap;
366   GLubyte *a8_bitmap;
367   int x, y;
368   GLfloat dst_z;
369
370   /* We need a fragment program for the KIL effect */
371   if (!ctx->Extensions.ARB_fragment_program ||
372       !ctx->Extensions.ARB_vertex_program) {
373      if (INTEL_DEBUG & DEBUG_FALLBACKS)
374	 fprintf(stderr,
375		 "glBitmap fallback: No fragment/vertex program support\n");
376      return GL_FALSE;
377   }
378
379   /* We're going to mess with texturing with no regard to existing texture
380    * state, so if there is some set up we have to bail.
381    */
382   if (ctx->Texture._EnabledUnits != 0) {
383      if (INTEL_DEBUG & DEBUG_FALLBACKS)
384	 fprintf(stderr, "glBitmap fallback: texturing enabled\n");
385      return GL_FALSE;
386   }
387
388   /* Can't do textured DrawPixels with a fragment program, unless we were
389    * to generate a new program that sampled our texture and put the results
390    * in the fragment color before the user's program started.
391    */
392   if (ctx->FragmentProgram.Enabled) {
393      if (INTEL_DEBUG & DEBUG_FALLBACKS)
394	 fprintf(stderr, "glBitmap fallback: fragment program enabled\n");
395      return GL_FALSE;
396   }
397
398   if (ctx->VertexProgram.Enabled) {
399      if (INTEL_DEBUG & DEBUG_FALLBACKS)
400	 fprintf(stderr, "glBitmap fallback: vertex program enabled\n");
401      return GL_FALSE;
402   }
403
404   /* Check that we can load in a texture this big. */
405   if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
406       height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
407      if (INTEL_DEBUG & DEBUG_FALLBACKS)
408	 fprintf(stderr, "glBitmap fallback: bitmap too large (%dx%d)\n",
409		 width, height);
410      return GL_FALSE;
411   }
412
413   /* Convert the A1 bitmap to an A8 format suitable for glTexImage */
414   if (unpack->BufferObj->Name) {
415      bitmap = map_pbo(ctx, width, height, unpack, bitmap);
416      if (bitmap == NULL)
417	 return GL_TRUE;	/* even though this is an error, we're done */
418   }
419   unpacked_bitmap = _mesa_unpack_bitmap(width, height, bitmap,
420					 unpack);
421   a8_bitmap = _mesa_calloc(width * height);
422   for (y = 0; y < height; y++) {
423      for (x = 0; x < width; x++) {
424	 if (test_msb_bit(unpacked_bitmap, ALIGN(width, 8) * y + x))
425	    a8_bitmap[y * width + x] = 0xff;
426      }
427   }
428   _mesa_free(unpacked_bitmap);
429   if (unpack->BufferObj->Name) {
430      /* done with PBO so unmap it now */
431      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
432                              unpack->BufferObj);
433   }
434
435   /* Save GL state before we start setting up our drawing */
436   _mesa_PushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT |
437		    GL_VIEWPORT_BIT);
438   _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT |
439			  GL_CLIENT_PIXEL_STORE_BIT);
440   old_active_texture = ctx->Texture.CurrentUnit;
441
442   _mesa_Disable(GL_POLYGON_STIPPLE);
443
444   /* Upload our bitmap data to an alpha texture */
445   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
446   _mesa_Enable(GL_TEXTURE_2D);
447   _mesa_GenTextures(1, &texname);
448   _mesa_BindTexture(GL_TEXTURE_2D, texname);
449   _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
450   _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
451
452   _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
453   _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
454   _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
455   _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
456   _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
457   _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 1);
458   _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0,
459		    GL_ALPHA, GL_UNSIGNED_BYTE, a8_bitmap);
460   _mesa_free(a8_bitmap);
461
462   intel_meta_set_fragment_program(intel, &intel->meta.bitmap_fp, fp);
463   _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
464				     ctx->Current.RasterColor);
465   intel_meta_set_passthrough_vertex_program(intel);
466   intel_meta_set_passthrough_transform(intel);
467
468   /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */
469   dst_z = -1.0 + 2.0 * ctx->Current.RasterPos[2];
470
471   vertices[0][0] = dst_x;
472   vertices[0][1] = dst_y;
473   vertices[0][2] = dst_z;
474   vertices[0][3] = 1.0;
475   vertices[1][0] = dst_x + width;
476   vertices[1][1] = dst_y;
477   vertices[1][2] = dst_z;
478   vertices[1][3] = 1.0;
479   vertices[2][0] = dst_x + width;
480   vertices[2][1] = dst_y + height;
481   vertices[2][2] = dst_z;
482   vertices[2][3] = 1.0;
483   vertices[3][0] = dst_x;
484   vertices[3][1] = dst_y + height;
485   vertices[3][2] = dst_z;
486   vertices[3][3] = 1.0;
487
488   texcoords[0][0] = 0.0;
489   texcoords[0][1] = 0.0;
490   texcoords[1][0] = 1.0;
491   texcoords[1][1] = 0.0;
492   texcoords[2][0] = 1.0;
493   texcoords[2][1] = 1.0;
494   texcoords[3][0] = 0.0;
495   texcoords[3][1] = 1.0;
496
497   _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
498   _mesa_ClientActiveTextureARB(GL_TEXTURE0);
499   _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
500   _mesa_Enable(GL_VERTEX_ARRAY);
501   _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
502   CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
503
504   intel_meta_restore_transform(intel);
505   intel_meta_restore_fragment_program(intel);
506   intel_meta_restore_vertex_program(intel);
507
508   _mesa_PopClientAttrib();
509   _mesa_Disable(GL_TEXTURE_2D); /* asserted that it was disabled at entry */
510   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
511   _mesa_PopAttrib();
512
513   _mesa_DeleteTextures(1, &texname);
514
515   return GL_TRUE;
516}
517
518/* There are a large number of possible ways to implement bitmap on
519 * this hardware, most of them have some sort of drawback.  Here are a
520 * few that spring to mind:
521 *
522 * Blit:
523 *    - XY_MONO_SRC_BLT_CMD
524 *         - use XY_SETUP_CLIP_BLT for cliprect clipping.
525 *    - XY_TEXT_BLT
526 *    - XY_TEXT_IMMEDIATE_BLT
527 *         - blit per cliprect, subject to maximum immediate data size.
528 *    - XY_COLOR_BLT
529 *         - per pixel or run of pixels
530 *    - XY_PIXEL_BLT
531 *         - good for sparse bitmaps
532 *
533 * 3D engine:
534 *    - Point per pixel
535 *    - Translate bitmap to an alpha texture and render as a quad
536 *    - Chop bitmap up into 32x32 squares and render w/polygon stipple.
537 */
538void
539intelBitmap(GLcontext * ctx,
540	    GLint x, GLint y,
541	    GLsizei width, GLsizei height,
542	    const struct gl_pixelstore_attrib *unpack,
543	    const GLubyte * pixels)
544{
545   if (do_blit_bitmap(ctx, x, y, width, height,
546                          unpack, pixels))
547      return;
548
549   if (intel_texture_bitmap(ctx, x, y, width, height,
550			    unpack, pixels))
551      return;
552
553   if (INTEL_DEBUG & DEBUG_PIXEL)
554      _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
555
556   _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
557}
558