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