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