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