intel_pixel_bitmap.c revision ef1e1261df02203da4c7ebf708b0edf1b9cd16c2
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#include "drivers/common/meta.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_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 chunk of HW-sized bitmap.
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      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   GLsizei bitmap_width = width;
194   GLsizei bitmap_height = height;
195   GLint px, py;
196   GLuint stipple[32];
197   GLint orig_dstx = dstx;
198   GLint orig_dsty = dsty;
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 = PACK_COLOR_565(ubcolor[0], ubcolor[1], ubcolor[2]);
233   else
234      color = PACK_COLOR_8888(ubcolor[3], ubcolor[0], ubcolor[1], ubcolor[2]);
235
236   if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
237      return GL_FALSE;
238
239   intel_prepare_render(intel);
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   dsty = y_flip(fb, dsty, height);
248
249#define DY 32
250#define DX 32
251
252   /* Chop it all into chunks that can be digested by hardware: */
253   for (py = 0; py < height; py += DY) {
254      for (px = 0; px < width; px += DX) {
255	 int h = MIN2(DY, height - py);
256	 int w = MIN2(DX, width - px);
257	 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
258	 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
259	    ctx->Color.LogicOp : GL_COPY;
260
261	 assert(sz <= sizeof(stipple));
262	 memset(stipple, 0, sz);
263
264	 /* May need to adjust this when padding has been introduced in
265	  * sz above:
266	  *
267	  * Have to translate destination coordinates back into source
268	  * coordinates.
269	  */
270	 if (get_bitmap_rect(bitmap_width, bitmap_height, unpack,
271			     bitmap,
272			     -orig_dstx + (dstx + px),
273			     -orig_dsty + y_flip(fb, dsty + py, h),
274			     w, h,
275			     (GLubyte *)stipple,
276			     8,
277			     fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0)
278	    continue;
279
280	 if (!intelEmitImmediateColorExpandBlit(intel,
281						dst->cpp,
282						(GLubyte *)stipple,
283						sz,
284						color,
285						dst->pitch,
286						dst->buffer,
287						0,
288						dst->tiling,
289						dstx + px,
290						dsty + py,
291						w, h,
292						logic_op)) {
293	    return GL_FALSE;
294	 }
295      }
296   }
297out:
298
299   if (INTEL_DEBUG & DEBUG_SYNC)
300      intel_batchbuffer_flush(intel->batch);
301
302   if (_mesa_is_bufferobj(unpack->BufferObj)) {
303      /* done with PBO so unmap it now */
304      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
305                              unpack->BufferObj);
306   }
307
308   intel_check_front_buffer_rendering(intel);
309
310   return GL_TRUE;
311}
312
313/* There are a large number of possible ways to implement bitmap on
314 * this hardware, most of them have some sort of drawback.  Here are a
315 * few that spring to mind:
316 *
317 * Blit:
318 *    - XY_MONO_SRC_BLT_CMD
319 *         - use XY_SETUP_CLIP_BLT for cliprect clipping.
320 *    - XY_TEXT_BLT
321 *    - XY_TEXT_IMMEDIATE_BLT
322 *         - blit per cliprect, subject to maximum immediate data size.
323 *    - XY_COLOR_BLT
324 *         - per pixel or run of pixels
325 *    - XY_PIXEL_BLT
326 *         - good for sparse bitmaps
327 *
328 * 3D engine:
329 *    - Point per pixel
330 *    - Translate bitmap to an alpha texture and render as a quad
331 *    - Chop bitmap up into 32x32 squares and render w/polygon stipple.
332 */
333void
334intelBitmap(GLcontext * ctx,
335	    GLint x, GLint y,
336	    GLsizei width, GLsizei height,
337	    const struct gl_pixelstore_attrib *unpack,
338	    const GLubyte * pixels)
339{
340   if (do_blit_bitmap(ctx, x, y, width, height,
341                          unpack, pixels))
342      return;
343
344   _mesa_meta_Bitmap(ctx, x, y, width, height, unpack, pixels);
345}
346