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