intel: pixelzoom doesn't apply to glBitmap, so disable the fallback.
[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 "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 */
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 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
195 return GL_FALSE;
196
197 LOCK_HARDWARE(intel);
198
199 if (intel->driDrawable->numClipRects) {
200 __DRIdrawablePrivate *dPriv = intel->driDrawable;
201 drm_clip_rect_t *box = dPriv->pClipRects;
202 drm_clip_rect_t dest_rect;
203 GLint nbox = dPriv->numClipRects;
204 GLint srcx = 0, srcy = 0;
205 GLint orig_screen_x1, orig_screen_y2;
206 GLuint i;
207
208
209 orig_screen_x1 = dPriv->x + dstx;
210 orig_screen_y2 = dPriv->y + (dPriv->h - dsty);
211
212 /* Do scissoring in GL coordinates:
213 */
214 if (ctx->Scissor.Enabled)
215 {
216 GLint x = ctx->Scissor.X;
217 GLint y = ctx->Scissor.Y;
218 GLuint w = ctx->Scissor.Width;
219 GLuint h = ctx->Scissor.Height;
220
221 if (!_mesa_clip_to_region(x, y, x+w-1, y+h-1, &dstx, &dsty, &width, &height))
222 goto out;
223 }
224
225 /* Convert from GL to hardware coordinates:
226 */
227 dsty = dPriv->y + (dPriv->h - dsty - height);
228 dstx = dPriv->x + dstx;
229
230 dest_rect.x1 = dstx < 0 ? 0 : dstx;
231 dest_rect.y1 = dsty < 0 ? 0 : dsty;
232 dest_rect.x2 = dstx + width < 0 ? 0 : dstx + width;
233 dest_rect.y2 = dsty + height < 0 ? 0 : dsty + height;
234
235 for (i = 0; i < nbox; i++) {
236 drm_clip_rect_t rect;
237 int box_w, box_h;
238 GLint px, py;
239 GLuint stipple[32];
240
241 if (!intel_intersect_cliprects(&rect, &dest_rect, &box[i]))
242 continue;
243
244 /* Now go back to GL coordinates to figure out what subset of
245 * the bitmap we are uploading for this cliprect:
246 */
247 box_w = rect.x2 - rect.x1;
248 box_h = rect.y2 - rect.y1;
249 srcx = rect.x1 - orig_screen_x1;
250 srcy = orig_screen_y2 - rect.y2;
251
252
253 #define DY 32
254 #define DX 32
255
256 /* Then, finally, chop it all into chunks that can be
257 * digested by hardware:
258 */
259 for (py = 0; py < box_h; py += DY) {
260 for (px = 0; px < box_w; px += DX) {
261 int h = MIN2(DY, box_h - py);
262 int w = MIN2(DX, box_w - px);
263 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
264 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
265 ctx->Color.LogicOp : GL_COPY;
266
267 assert(sz <= sizeof(stipple));
268 memset(stipple, 0, sz);
269
270 /* May need to adjust this when padding has been introduced in
271 * sz above:
272 */
273 if (get_bitmap_rect(width, height, unpack,
274 bitmap,
275 srcx + px, srcy + py, w, h,
276 (GLubyte *)stipple,
277 8,
278 GL_TRUE) == 0)
279 continue;
280
281 /*
282 */
283 intelEmitImmediateColorExpandBlit( intel,
284 dst->cpp,
285 (GLubyte *)stipple,
286 sz,
287 (dst->cpp == 2) ? color565 : color8888,
288 dst->pitch,
289 dst->buffer,
290 0,
291 dst->tiling,
292 rect.x1 + px,
293 rect.y2 - (py + h),
294 w, h,
295 logic_op);
296 }
297 }
298 }
299 }
300 out:
301 UNLOCK_HARDWARE(intel);
302
303
304 if (unpack->BufferObj->Name) {
305 /* done with PBO so unmap it now */
306 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
307 unpack->BufferObj);
308 }
309
310 return GL_TRUE;
311 }
312
313
314
315
316
317 /* There are a large number of possible ways to implement bitmap on
318 * this hardware, most of them have some sort of drawback. Here are a
319 * few that spring to mind:
320 *
321 * Blit:
322 * - XY_MONO_SRC_BLT_CMD
323 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
324 * - XY_TEXT_BLT
325 * - XY_TEXT_IMMEDIATE_BLT
326 * - blit per cliprect, subject to maximum immediate data size.
327 * - XY_COLOR_BLT
328 * - per pixel or run of pixels
329 * - XY_PIXEL_BLT
330 * - good for sparse bitmaps
331 *
332 * 3D engine:
333 * - Point per pixel
334 * - Translate bitmap to an alpha texture and render as a quad
335 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
336 */
337 void
338 intelBitmap(GLcontext * ctx,
339 GLint x, GLint y,
340 GLsizei width, GLsizei height,
341 const struct gl_pixelstore_attrib *unpack,
342 const GLubyte * pixels)
343 {
344 if (do_blit_bitmap(ctx, x, y, width, height,
345 unpack, pixels))
346 return;
347
348 if (INTEL_DEBUG & DEBUG_PIXEL)
349 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
350
351 _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
352 }