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