intel_pixel_bitmap.c revision bfebeffc0045266d354a36968336337e099a9f27
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 (!dst)
208       return GL_FALSE;
209
210   if (unpack->BufferObj->Name) {
211      bitmap = map_pbo(ctx, width, height, unpack, bitmap);
212      if (bitmap == NULL)
213	 return GL_TRUE;	/* even though this is an error, we're done */
214   }
215
216   COPY_4V(tmpColor, ctx->Current.RasterColor);
217
218   if (NEED_SECONDARY_COLOR(ctx)) {
219       ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
220   }
221
222   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
223   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
224   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
225   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
226
227   color8888 = INTEL_PACKCOLOR8888(ubcolor[0], ubcolor[1], ubcolor[2], ubcolor[3]);
228   color565 = INTEL_PACKCOLOR565(ubcolor[0], ubcolor[1], ubcolor[2]);
229
230   if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
231      return GL_FALSE;
232
233   LOCK_HARDWARE(intel);
234
235   intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off);
236   if (num_cliprects != 0) {
237      GLuint i;
238      GLint orig_dstx = dstx;
239      GLint orig_dsty = dsty;
240
241      /* Clip to buffer bounds and scissor. */
242      if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
243				fb->_Xmax, fb->_Ymax,
244				&dstx, &dsty, &width, &height))
245            goto out;
246
247      dstx = x_off + dstx;
248      dsty = y_off + y_flip(fb, dsty, height);
249
250      for (i = 0; i < num_cliprects; i++) {
251	 int box_x, box_y, box_w, box_h;
252	 GLint px, py;
253	 GLuint stipple[32];
254
255	 box_x = dstx;
256	 box_y = dsty;
257	 box_w = width;
258	 box_h = height;
259
260	 /* Clip to drawable cliprect */
261         if (!_mesa_clip_to_region(cliprects[i].x1,
262				   cliprects[i].y1,
263				   cliprects[i].x2,
264				   cliprects[i].y2,
265				   &box_x, &box_y, &box_w, &box_h))
266	    continue;
267
268#define DY 32
269#define DX 32
270
271	 /* Then, finally, chop it all into chunks that can be
272	  * digested by hardware:
273	  */
274	 for (py = 0; py < box_h; py += DY) {
275	    for (px = 0; px < box_w; px += DX) {
276	       int h = MIN2(DY, box_h - py);
277	       int w = MIN2(DX, box_w - px);
278	       GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
279	       GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
280		  ctx->Color.LogicOp : GL_COPY;
281
282	       assert(sz <= sizeof(stipple));
283	       memset(stipple, 0, sz);
284
285	       /* May need to adjust this when padding has been introduced in
286		* sz above:
287		*
288		* Have to translate destination coordinates back into source
289		* coordinates.
290		*/
291	       if (get_bitmap_rect(bitmap_width, bitmap_height, unpack,
292				   bitmap,
293				   -orig_dstx + (box_x + px - x_off),
294				   -orig_dsty + y_flip(fb,
295						       box_y + py - y_off, h),
296				   w, h,
297				   (GLubyte *)stipple,
298				   8,
299				   fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0)
300		  continue;
301
302	       /*
303		*/
304	       intelEmitImmediateColorExpandBlit( intel,
305						  dst->cpp,
306						  (GLubyte *)stipple,
307						  sz,
308						  (dst->cpp == 2) ? color565 : color8888,
309						  dst->pitch,
310						  dst->buffer,
311						  0,
312						  dst->tiling,
313						  box_x + px,
314						  box_y + py,
315						  w, h,
316						  logic_op);
317	    }
318	 }
319      }
320   }
321out:
322   UNLOCK_HARDWARE(intel);
323
324   if (INTEL_DEBUG & DEBUG_SYNC)
325      intel_batchbuffer_flush(intel->batch);
326
327   if (unpack->BufferObj->Name) {
328      /* done with PBO so unmap it now */
329      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
330                              unpack->BufferObj);
331   }
332
333   return GL_TRUE;
334}
335
336static GLboolean
337intel_texture_bitmap(GLcontext * ctx,
338		     GLint dst_x, GLint dst_y,
339		     GLsizei width, GLsizei height,
340		     const struct gl_pixelstore_attrib *unpack,
341		     const GLubyte *bitmap)
342{
343   struct intel_context *intel = intel_context(ctx);
344   static const char *fp =
345      "!!ARBfp1.0\n"
346      "TEMP val;\n"
347      "PARAM color=program.local[0];\n"
348      "TEX val, fragment.texcoord[0], texture[0], 2D;\n"
349      "ADD val, val.wwww, {-.5, -.5, -.5, -.5};\n"
350      "KIL val;\n"
351      "MOV result.color, color;\n"
352      "END\n";
353   GLuint texname;
354   GLfloat vertices[4][4];
355   GLfloat texcoords[4][2];
356   GLint old_active_texture;
357   GLubyte *unpacked_bitmap;
358   GLubyte *a8_bitmap;
359   int x, y;
360
361   /* We need a fragment program for the KIL effect */
362   if (!ctx->Extensions.ARB_fragment_program ||
363       !ctx->Extensions.ARB_vertex_program) {
364      if (INTEL_DEBUG & DEBUG_FALLBACKS)
365	 fprintf(stderr,
366		 "glBitmap fallback: No fragment/vertex program support\n");
367      return GL_FALSE;
368   }
369
370   /* We're going to mess with texturing with no regard to existing texture
371    * state, so if there is some set up we have to bail.
372    */
373   if (ctx->Texture._EnabledUnits != 0) {
374      if (INTEL_DEBUG & DEBUG_FALLBACKS)
375	 fprintf(stderr, "glBitmap fallback: texturing enabled\n");
376      return GL_FALSE;
377   }
378
379   /* Can't do textured DrawPixels with a fragment program, unless we were
380    * to generate a new program that sampled our texture and put the results
381    * in the fragment color before the user's program started.
382    */
383   if (ctx->FragmentProgram.Enabled) {
384      if (INTEL_DEBUG & DEBUG_FALLBACKS)
385	 fprintf(stderr, "glBitmap fallback: fragment program enabled\n");
386      return GL_FALSE;
387   }
388
389   if (ctx->VertexProgram.Enabled) {
390      if (INTEL_DEBUG & DEBUG_FALLBACKS)
391	 fprintf(stderr, "glBitmap fallback: vertex program enabled\n");
392      return GL_FALSE;
393   }
394
395   /* Check that we can load in a texture this big. */
396   if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
397       height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
398      if (INTEL_DEBUG & DEBUG_FALLBACKS)
399	 fprintf(stderr, "glBitmap fallback: bitmap too large (%dx%d)\n",
400		 width, height);
401      return GL_FALSE;
402   }
403
404   /* Convert the A1 bitmap to an A8 format suitable for glTexImage */
405   if (unpack->BufferObj->Name) {
406      bitmap = map_pbo(ctx, width, height, unpack, bitmap);
407      if (bitmap == NULL)
408	 return GL_TRUE;	/* even though this is an error, we're done */
409   }
410   unpacked_bitmap = _mesa_unpack_bitmap(width, height, bitmap,
411					 unpack);
412   a8_bitmap = _mesa_calloc(width * height);
413   for (y = 0; y < height; y++) {
414      for (x = 0; x < width; x++) {
415	 if (test_msb_bit(unpacked_bitmap, ALIGN(width, 8) * y + x))
416	    a8_bitmap[y * width + x] = 0xff;
417      }
418   }
419   _mesa_free(unpacked_bitmap);
420   if (unpack->BufferObj->Name) {
421      /* done with PBO so unmap it now */
422      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
423                              unpack->BufferObj);
424   }
425
426   /* Save GL state before we start setting up our drawing */
427   _mesa_PushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT |
428		    GL_VIEWPORT_BIT);
429   _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT |
430			  GL_CLIENT_PIXEL_STORE_BIT);
431   old_active_texture = ctx->Texture.CurrentUnit;
432
433   _mesa_Disable(GL_POLYGON_STIPPLE);
434
435   /* Upload our bitmap data to an alpha texture */
436   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
437   _mesa_Enable(GL_TEXTURE_2D);
438   _mesa_GenTextures(1, &texname);
439   _mesa_BindTexture(GL_TEXTURE_2D, texname);
440   _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
441   _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
442
443   _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
444   _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
445   _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
446   _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
447   _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
448   _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 1);
449   _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0,
450		    GL_ALPHA, GL_UNSIGNED_BYTE, a8_bitmap);
451   _mesa_free(a8_bitmap);
452
453   intel_meta_set_fragment_program(intel, &intel->meta.bitmap_fp, fp);
454   _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
455				     ctx->Current.RasterColor);
456   intel_meta_set_passthrough_vertex_program(intel);
457   intel_meta_set_passthrough_transform(intel);
458
459   vertices[0][0] = dst_x;
460   vertices[0][1] = dst_y;
461   vertices[0][2] = ctx->Current.RasterPos[2];
462   vertices[0][3] = 1.0;
463   vertices[1][0] = dst_x + width;
464   vertices[1][1] = dst_y;
465   vertices[1][2] = ctx->Current.RasterPos[2];
466   vertices[1][3] = 1.0;
467   vertices[2][0] = dst_x + width;
468   vertices[2][1] = dst_y + height;
469   vertices[2][2] = ctx->Current.RasterPos[2];
470   vertices[2][3] = 1.0;
471   vertices[3][0] = dst_x;
472   vertices[3][1] = dst_y + height;
473   vertices[3][2] = ctx->Current.RasterPos[2];
474   vertices[3][3] = 1.0;
475
476   texcoords[0][0] = 0.0;
477   texcoords[0][1] = 0.0;
478   texcoords[1][0] = 1.0;
479   texcoords[1][1] = 0.0;
480   texcoords[2][0] = 1.0;
481   texcoords[2][1] = 1.0;
482   texcoords[3][0] = 0.0;
483   texcoords[3][1] = 1.0;
484
485   _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
486   _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
487   _mesa_Enable(GL_VERTEX_ARRAY);
488   _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
489   CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
490
491   intel_meta_restore_transform(intel);
492   intel_meta_restore_fragment_program(intel);
493   intel_meta_restore_vertex_program(intel);
494
495   _mesa_PopClientAttrib();
496   _mesa_Disable(GL_TEXTURE_2D); /* asserted that it was disabled at entry */
497   _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
498   _mesa_PopAttrib();
499
500   _mesa_DeleteTextures(1, &texname);
501
502   return GL_TRUE;
503}
504
505/* There are a large number of possible ways to implement bitmap on
506 * this hardware, most of them have some sort of drawback.  Here are a
507 * few that spring to mind:
508 *
509 * Blit:
510 *    - XY_MONO_SRC_BLT_CMD
511 *         - use XY_SETUP_CLIP_BLT for cliprect clipping.
512 *    - XY_TEXT_BLT
513 *    - XY_TEXT_IMMEDIATE_BLT
514 *         - blit per cliprect, subject to maximum immediate data size.
515 *    - XY_COLOR_BLT
516 *         - per pixel or run of pixels
517 *    - XY_PIXEL_BLT
518 *         - good for sparse bitmaps
519 *
520 * 3D engine:
521 *    - Point per pixel
522 *    - Translate bitmap to an alpha texture and render as a quad
523 *    - Chop bitmap up into 32x32 squares and render w/polygon stipple.
524 */
525void
526intelBitmap(GLcontext * ctx,
527	    GLint x, GLint y,
528	    GLsizei width, GLsizei height,
529	    const struct gl_pixelstore_attrib *unpack,
530	    const GLubyte * pixels)
531{
532   if (do_blit_bitmap(ctx, x, y, width, height,
533                          unpack, pixels))
534      return;
535
536   if (intel_texture_bitmap(ctx, x, y, width, height,
537			    unpack, pixels))
538      return;
539
540   if (INTEL_DEBUG & DEBUG_PIXEL)
541      _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
542
543   _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
544}
545