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