Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / intel / intel_pixel_bitmap.c
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 "glheader.h"
29 #include "enums.h"
30 #include "image.h"
31 #include "colormac.h"
32 #include "mtypes.h"
33 #include "macros.h"
34 #include "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 */
58 static 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
83 static GLboolean test_bit( const GLubyte *src,
84 GLuint bit )
85 {
86 return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
87 }
88
89 static 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 */
98 static 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 */
158 static GLboolean
159 do_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 GLfloat tmpColor[4];
168 GLubyte ubcolor[4];
169 GLuint color8888, color565;
170
171 if (!dst)
172 return GL_FALSE;
173
174 if (unpack->BufferObj->Name) {
175 bitmap = map_pbo(ctx, width, height, unpack, bitmap);
176 if (bitmap == NULL)
177 return GL_TRUE; /* even though this is an error, we're done */
178 }
179
180 COPY_4V(tmpColor, ctx->Current.RasterColor);
181
182 if (NEED_SECONDARY_COLOR(ctx)) {
183 ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
184 }
185
186 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
187 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
188 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
189 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
190
191 color8888 = INTEL_PACKCOLOR8888(ubcolor[0], ubcolor[1], ubcolor[2], ubcolor[3]);
192 color565 = INTEL_PACKCOLOR565(ubcolor[0], ubcolor[1], ubcolor[2]);
193
194 /* Does zoom apply to bitmaps?
195 */
196 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F) ||
197 ctx->Pixel.ZoomX != 1.0F ||
198 ctx->Pixel.ZoomY != 1.0F)
199 return GL_FALSE;
200
201 LOCK_HARDWARE(intel);
202
203 if (intel->driDrawable->numClipRects) {
204 __DRIdrawablePrivate *dPriv = intel->driDrawable;
205 drm_clip_rect_t *box = dPriv->pClipRects;
206 drm_clip_rect_t dest_rect;
207 GLint nbox = dPriv->numClipRects;
208 GLint srcx = 0, srcy = 0;
209 GLint orig_screen_x1, orig_screen_y2;
210 GLuint i;
211
212
213 orig_screen_x1 = dPriv->x + dstx;
214 orig_screen_y2 = dPriv->y + (dPriv->h - dsty);
215
216 /* Do scissoring in GL coordinates:
217 */
218 if (ctx->Scissor.Enabled)
219 {
220 GLint x = ctx->Scissor.X;
221 GLint y = ctx->Scissor.Y;
222 GLuint w = ctx->Scissor.Width;
223 GLuint h = ctx->Scissor.Height;
224
225 if (!_mesa_clip_to_region(x, y, x+w-1, y+h-1, &dstx, &dsty, &width, &height))
226 goto out;
227 }
228
229 /* Convert from GL to hardware coordinates:
230 */
231 dsty = dPriv->y + (dPriv->h - dsty - height);
232 dstx = dPriv->x + dstx;
233
234 dest_rect.x1 = dstx < 0 ? 0 : dstx;
235 dest_rect.y1 = dsty < 0 ? 0 : dsty;
236 dest_rect.x2 = dstx + width < 0 ? 0 : dstx + width;
237 dest_rect.y2 = dsty + height < 0 ? 0 : dsty + height;
238
239 for (i = 0; i < nbox; i++) {
240 drm_clip_rect_t rect;
241 int box_w, box_h;
242 GLint px, py;
243 GLuint stipple[32];
244
245 if (!intel_intersect_cliprects(&rect, &dest_rect, &box[i]))
246 continue;
247
248 /* Now go back to GL coordinates to figure out what subset of
249 * the bitmap we are uploading for this cliprect:
250 */
251 box_w = rect.x2 - rect.x1;
252 box_h = rect.y2 - rect.y1;
253 srcx = rect.x1 - orig_screen_x1;
254 srcy = orig_screen_y2 - rect.y2;
255
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 GL_TRUE) == 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 }
304 out:
305 UNLOCK_HARDWARE(intel);
306
307
308 if (unpack->BufferObj->Name) {
309 /* done with PBO so unmap it now */
310 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
311 unpack->BufferObj);
312 }
313
314 return GL_TRUE;
315 }
316
317
318
319
320
321 /* There are a large number of possible ways to implement bitmap on
322 * this hardware, most of them have some sort of drawback. Here are a
323 * few that spring to mind:
324 *
325 * Blit:
326 * - XY_MONO_SRC_BLT_CMD
327 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
328 * - XY_TEXT_BLT
329 * - XY_TEXT_IMMEDIATE_BLT
330 * - blit per cliprect, subject to maximum immediate data size.
331 * - XY_COLOR_BLT
332 * - per pixel or run of pixels
333 * - XY_PIXEL_BLT
334 * - good for sparse bitmaps
335 *
336 * 3D engine:
337 * - Point per pixel
338 * - Translate bitmap to an alpha texture and render as a quad
339 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
340 */
341 void
342 intelBitmap(GLcontext * ctx,
343 GLint x, GLint y,
344 GLsizei width, GLsizei height,
345 const struct gl_pixelstore_attrib *unpack,
346 const GLubyte * pixels)
347 {
348 if (do_blit_bitmap(ctx, x, y, width, height,
349 unpack, pixels))
350 return;
351
352 if (INTEL_DEBUG & DEBUG_PIXEL)
353 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
354
355 _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
356 }