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