intel_pixel_bitmap.c revision cb433d91c6e198b7c77f747f1a38803532bc9be9
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 "swrast/swrast.h"
36
37#include "intel_screen.h"
38#include "intel_context.h"
39#include "intel_batchbuffer.h"
40#include "intel_blit.h"
41#include "intel_regions.h"
42#include "intel_buffer_objects.h"
43#include "intel_buffers.h"
44#include "intel_pixel.h"
45#include "intel_reg.h"
46
47
48#define FILE_DEBUG_FLAG DEBUG_PIXEL
49
50
51/* Unlike the other intel_pixel_* functions, the expectation here is
52 * that the incoming data is not in a PBO.  With the XY_TEXT blit
53 * method, there's no benefit haveing it in a PBO, but we could
54 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
55 * PBO bitmaps.  I think they are probably pretty rare though - I
56 * wonder if Xgl uses them?
57 */
58static const GLubyte *map_pbo( GLcontext *ctx,
59			       GLsizei width, GLsizei height,
60			       const struct gl_pixelstore_attrib *unpack,
61			       const GLubyte *bitmap )
62{
63   GLubyte *buf;
64
65   if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
66				  GL_COLOR_INDEX, GL_BITMAP,
67				  (GLvoid *) bitmap)) {
68      _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
69      return NULL;
70   }
71
72   buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
73					   GL_READ_ONLY_ARB,
74					   unpack->BufferObj);
75   if (!buf) {
76      _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
77      return NULL;
78   }
79
80   return ADD_POINTERS(buf, bitmap);
81}
82
83static GLboolean test_bit( const GLubyte *src,
84			    GLuint bit )
85{
86   return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
87}
88
89static void set_bit( GLubyte *dest,
90			  GLuint bit )
91{
92   dest[bit/8] |= 1 << (bit % 8);
93}
94
95/* Extract a rectangle's worth of data from the bitmap.  Called
96 * per-cliprect.
97 */
98static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
99			      const struct gl_pixelstore_attrib *unpack,
100			      const GLubyte *bitmap,
101			      GLuint x, GLuint y,
102			      GLuint w, GLuint h,
103			      GLubyte *dest,
104			      GLuint row_align,
105			      GLboolean invert)
106{
107   GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
108   GLuint mask = unpack->LsbFirst ? 0 : 7;
109   GLuint bit = 0;
110   GLint row, col;
111   GLint first, last;
112   GLint incr;
113   GLuint count = 0;
114
115   if (INTEL_DEBUG & DEBUG_PIXEL)
116      _mesa_printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
117		   __FUNCTION__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask);
118
119   if (invert) {
120      first = h-1;
121      last = 0;
122      incr = -1;
123   }
124   else {
125      first = 0;
126      last = h-1;
127      incr = 1;
128   }
129
130   /* Require that dest be pre-zero'd.
131    */
132   for (row = first; row != (last+incr); row += incr) {
133      const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap,
134						    width, height,
135						    GL_COLOR_INDEX, GL_BITMAP,
136						    y + row, x);
137
138      for (col = 0; col < w; col++, bit++) {
139	 if (test_bit(rowsrc, (col + src_offset) ^ mask)) {
140	    set_bit(dest, bit ^ 7);
141	    count++;
142	 }
143      }
144
145      if (row_align)
146	 bit = ALIGN(bit, row_align);
147   }
148
149   return count;
150}
151
152
153
154
155/*
156 * Render a bitmap.
157 */
158static GLboolean
159do_blit_bitmap( GLcontext *ctx,
160		GLint dstx, GLint dsty,
161		GLsizei width, GLsizei height,
162		const struct gl_pixelstore_attrib *unpack,
163		const GLubyte *bitmap )
164{
165   struct intel_context *intel = intel_context(ctx);
166   struct intel_region *dst = intel_drawbuf_region(intel);
167   struct gl_framebuffer *fb = ctx->DrawBuffer;
168   GLfloat tmpColor[4];
169   GLubyte ubcolor[4];
170   GLuint color8888, color565;
171   unsigned int num_cliprects;
172   drm_clip_rect_t *cliprects;
173   int x_off, y_off;
174
175   if (!dst)
176       return GL_FALSE;
177
178   if (unpack->BufferObj->Name) {
179      bitmap = map_pbo(ctx, width, height, unpack, bitmap);
180      if (bitmap == NULL)
181	 return GL_TRUE;	/* even though this is an error, we're done */
182   }
183
184   COPY_4V(tmpColor, ctx->Current.RasterColor);
185
186   if (NEED_SECONDARY_COLOR(ctx)) {
187       ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
188   }
189
190   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
191   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
192   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
193   UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
194
195   color8888 = INTEL_PACKCOLOR8888(ubcolor[0], ubcolor[1], ubcolor[2], ubcolor[3]);
196   color565 = INTEL_PACKCOLOR565(ubcolor[0], ubcolor[1], ubcolor[2]);
197
198   if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
199      return GL_FALSE;
200
201   LOCK_HARDWARE(intel);
202
203   intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off);
204   if (num_cliprects != 0) {
205      drm_clip_rect_t dest_rect;
206      GLint srcx = 0, srcy = 0;
207      GLuint i;
208      GLint orig_dstx = dstx;
209      GLint orig_dsty = dsty;
210
211      /* Clip to buffer bounds and scissor. */
212      if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
213				fb->_Xmax, fb->_Ymax,
214				&dstx, &dsty, &width, &height))
215            goto out;
216
217      /* Convert from GL to hardware coordinates.  Transform original points
218       * along with it so that we can look at cliprects in hw coordinates and
219       * map back to points in the source space.
220       */
221      if (fb->Name == 0) {
222	 /* bitmap to a system framebuffer */
223	 dstx = x_off + dstx;
224	 dsty = y_off + (fb->Height - dsty - height);
225	 orig_dstx = x_off + orig_dstx;
226	 orig_dsty = y_off + (fb->Height - orig_dsty - height);
227      } else {
228	 /* bitmap to a user framebuffer object */
229	 dstx = x_off + dstx;
230	 dsty = y_off + dsty;
231	 orig_dstx = x_off + orig_dstx;
232	 orig_dsty = y_off + orig_dsty;
233      }
234
235      dest_rect.x1 = dstx;
236      dest_rect.y1 = dsty;
237      dest_rect.x2 = dstx + width;
238      dest_rect.y2 = dsty + height;
239
240      for (i = 0; i < num_cliprects; i++) {
241         drm_clip_rect_t rect;
242	 int box_w, box_h;
243	 GLint px, py;
244	 GLuint stipple[32];
245
246         if (!intel_intersect_cliprects(&rect, &dest_rect, &cliprects[i]))
247            continue;
248
249	 /* Now go back to GL coordinates to figure out what subset of
250	  * the bitmap we are uploading for this cliprect:
251	  */
252	 box_w = rect.x2 - rect.x1;
253	 box_h = rect.y2 - rect.y1;
254	 srcx = rect.x1 - orig_dstx;
255	 srcy = rect.y1 - orig_dsty;
256
257#define DY 32
258#define DX 32
259
260	 /* Then, finally, chop it all into chunks that can be
261	  * digested by hardware:
262	  */
263	 for (py = 0; py < box_h; py += DY) {
264	    for (px = 0; px < box_w; px += DX) {
265	       int h = MIN2(DY, box_h - py);
266	       int w = MIN2(DX, box_w - px);
267	       GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
268	       GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
269		  ctx->Color.LogicOp : GL_COPY;
270
271	       assert(sz <= sizeof(stipple));
272	       memset(stipple, 0, sz);
273
274	       /* May need to adjust this when padding has been introduced in
275		* sz above:
276		*/
277	       if (get_bitmap_rect(width, height, unpack,
278				   bitmap,
279				   srcx + px, srcy + py, w, h,
280				   (GLubyte *)stipple,
281				   8,
282				   fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0)
283		  continue;
284
285	       /*
286		*/
287	       intelEmitImmediateColorExpandBlit( intel,
288						  dst->cpp,
289						  (GLubyte *)stipple,
290						  sz,
291						  (dst->cpp == 2) ? color565 : color8888,
292						  dst->pitch,
293						  dst->buffer,
294						  0,
295						  dst->tiling,
296						  rect.x1 + px,
297						  rect.y2 - (py + h),
298						  w, h,
299						  logic_op);
300	    }
301	 }
302      }
303   }
304out:
305   UNLOCK_HARDWARE(intel);
306
307   if (INTEL_DEBUG & DEBUG_SYNC)
308      intel_batchbuffer_flush(intel->batch);
309
310   if (unpack->BufferObj->Name) {
311      /* done with PBO so unmap it now */
312      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
313                              unpack->BufferObj);
314   }
315
316   return GL_TRUE;
317}
318
319
320
321
322
323/* There are a large number of possible ways to implement bitmap on
324 * this hardware, most of them have some sort of drawback.  Here are a
325 * few that spring to mind:
326 *
327 * Blit:
328 *    - XY_MONO_SRC_BLT_CMD
329 *         - use XY_SETUP_CLIP_BLT for cliprect clipping.
330 *    - XY_TEXT_BLT
331 *    - XY_TEXT_IMMEDIATE_BLT
332 *         - blit per cliprect, subject to maximum immediate data size.
333 *    - XY_COLOR_BLT
334 *         - per pixel or run of pixels
335 *    - XY_PIXEL_BLT
336 *         - good for sparse bitmaps
337 *
338 * 3D engine:
339 *    - Point per pixel
340 *    - Translate bitmap to an alpha texture and render as a quad
341 *    - Chop bitmap up into 32x32 squares and render w/polygon stipple.
342 */
343void
344intelBitmap(GLcontext * ctx,
345	    GLint x, GLint y,
346	    GLsizei width, GLsizei height,
347	    const struct gl_pixelstore_attrib *unpack,
348	    const GLubyte * pixels)
349{
350   if (do_blit_bitmap(ctx, x, y, width, height,
351                          unpack, pixels))
352      return;
353
354   if (INTEL_DEBUG & DEBUG_PIXEL)
355      _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
356
357   _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
358}
359