Merge remote branch 'origin/master' into pipe-video
[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 "main/state.h"
36 #include "main/texobj.h"
37 #include "main/context.h"
38 #include "swrast/swrast.h"
39 #include "drivers/common/meta.h"
40
41 #include "intel_screen.h"
42 #include "intel_context.h"
43 #include "intel_batchbuffer.h"
44 #include "intel_blit.h"
45 #include "intel_regions.h"
46 #include "intel_buffers.h"
47 #include "intel_pixel.h"
48 #include "intel_reg.h"
49
50
51 #define FILE_DEBUG_FLAG DEBUG_PIXEL
52
53
54 /* Unlike the other intel_pixel_* functions, the expectation here is
55 * that the incoming data is not in a PBO. With the XY_TEXT blit
56 * method, there's no benefit haveing it in a PBO, but we could
57 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
58 * PBO bitmaps. I think they are probably pretty rare though - I
59 * wonder if Xgl uses them?
60 */
61 static const GLubyte *map_pbo( struct gl_context *ctx,
62 GLsizei width, GLsizei height,
63 const struct gl_pixelstore_attrib *unpack,
64 const GLubyte *bitmap )
65 {
66 GLubyte *buf;
67
68 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
69 GL_COLOR_INDEX, GL_BITMAP,
70 (GLvoid *) bitmap)) {
71 _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
72 return NULL;
73 }
74
75 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
76 GL_READ_ONLY_ARB,
77 unpack->BufferObj);
78 if (!buf) {
79 _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
80 return NULL;
81 }
82
83 return ADD_POINTERS(buf, bitmap);
84 }
85
86 static GLboolean test_bit( const GLubyte *src, GLuint bit )
87 {
88 return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
89 }
90
91 static void set_bit( GLubyte *dest, GLuint bit )
92 {
93 dest[bit/8] |= 1 << (bit % 8);
94 }
95
96 /* Extract a rectangle's worth of data from the bitmap. Called
97 * per chunk of HW-sized bitmap.
98 */
99 static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
100 const struct gl_pixelstore_attrib *unpack,
101 const GLubyte *bitmap,
102 GLuint x, GLuint y,
103 GLuint w, GLuint h,
104 GLubyte *dest,
105 GLuint row_align,
106 GLboolean invert)
107 {
108 GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
109 GLuint mask = unpack->LsbFirst ? 0 : 7;
110 GLuint bit = 0;
111 GLint row, col;
112 GLint first, last;
113 GLint incr;
114 GLuint count = 0;
115
116 DBG("%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 * Returns the low Y value of the vertical range given, flipped according to
154 * whether the framebuffer is or not.
155 */
156 static INLINE int
157 y_flip(struct gl_framebuffer *fb, int y, int height)
158 {
159 if (fb->Name != 0)
160 return y;
161 else
162 return fb->Height - y - height;
163 }
164
165 /*
166 * Render a bitmap.
167 */
168 static GLboolean
169 do_blit_bitmap( struct gl_context *ctx,
170 GLint dstx, GLint dsty,
171 GLsizei width, GLsizei height,
172 const struct gl_pixelstore_attrib *unpack,
173 const GLubyte *bitmap )
174 {
175 struct intel_context *intel = intel_context(ctx);
176 struct intel_region *dst = intel_drawbuf_region(intel);
177 struct gl_framebuffer *fb = ctx->DrawBuffer;
178 GLfloat tmpColor[4];
179 GLubyte ubcolor[4];
180 GLuint color;
181 GLsizei bitmap_width = width;
182 GLsizei bitmap_height = height;
183 GLint px, py;
184 GLuint stipple[32];
185 GLint orig_dstx = dstx;
186 GLint orig_dsty = dsty;
187
188 /* Update draw buffer bounds */
189 _mesa_update_state(ctx);
190
191 if (ctx->Depth.Test) {
192 /* The blit path produces incorrect results when depth testing is on.
193 * It seems the blit Z coord is always 1.0 (the far plane) so fragments
194 * will likely be obscured by other, closer geometry.
195 */
196 return GL_FALSE;
197 }
198
199 if (!dst)
200 return GL_FALSE;
201
202 if (_mesa_is_bufferobj(unpack->BufferObj)) {
203 bitmap = map_pbo(ctx, width, height, unpack, bitmap);
204 if (bitmap == NULL)
205 return GL_TRUE; /* even though this is an error, we're done */
206 }
207
208 COPY_4V(tmpColor, ctx->Current.RasterColor);
209
210 if (NEED_SECONDARY_COLOR(ctx)) {
211 ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
212 }
213
214 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
215 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
216 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
217 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
218
219 if (dst->cpp == 2)
220 color = PACK_COLOR_565(ubcolor[0], ubcolor[1], ubcolor[2]);
221 else
222 color = PACK_COLOR_8888(ubcolor[3], ubcolor[0], ubcolor[1], ubcolor[2]);
223
224 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
225 return GL_FALSE;
226
227 intel_prepare_render(intel);
228
229 /* Clip to buffer bounds and scissor. */
230 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
231 fb->_Xmax, fb->_Ymax,
232 &dstx, &dsty, &width, &height))
233 goto out;
234
235 dsty = y_flip(fb, dsty, height);
236
237 #define DY 32
238 #define DX 32
239
240 /* Chop it all into chunks that can be digested by hardware: */
241 for (py = 0; py < height; py += DY) {
242 for (px = 0; px < width; px += DX) {
243 int h = MIN2(DY, height - py);
244 int w = MIN2(DX, width - px);
245 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
246 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
247 ctx->Color.LogicOp : GL_COPY;
248
249 assert(sz <= sizeof(stipple));
250 memset(stipple, 0, sz);
251
252 /* May need to adjust this when padding has been introduced in
253 * sz above:
254 *
255 * Have to translate destination coordinates back into source
256 * coordinates.
257 */
258 if (get_bitmap_rect(bitmap_width, bitmap_height, unpack,
259 bitmap,
260 -orig_dstx + (dstx + px),
261 -orig_dsty + y_flip(fb, dsty + py, h),
262 w, h,
263 (GLubyte *)stipple,
264 8,
265 fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0)
266 continue;
267
268 if (!intelEmitImmediateColorExpandBlit(intel,
269 dst->cpp,
270 (GLubyte *)stipple,
271 sz,
272 color,
273 dst->pitch,
274 dst->buffer,
275 0,
276 dst->tiling,
277 dstx + px,
278 dsty + py,
279 w, h,
280 logic_op)) {
281 return GL_FALSE;
282 }
283 }
284 }
285 out:
286
287 if (unlikely(INTEL_DEBUG & DEBUG_SYNC))
288 intel_batchbuffer_flush(intel->batch);
289
290 if (_mesa_is_bufferobj(unpack->BufferObj)) {
291 /* done with PBO so unmap it now */
292 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
293 unpack->BufferObj);
294 }
295
296 intel_check_front_buffer_rendering(intel);
297
298 return GL_TRUE;
299 }
300
301
302 /* There are a large number of possible ways to implement bitmap on
303 * this hardware, most of them have some sort of drawback. Here are a
304 * few that spring to mind:
305 *
306 * Blit:
307 * - XY_MONO_SRC_BLT_CMD
308 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
309 * - XY_TEXT_BLT
310 * - XY_TEXT_IMMEDIATE_BLT
311 * - blit per cliprect, subject to maximum immediate data size.
312 * - XY_COLOR_BLT
313 * - per pixel or run of pixels
314 * - XY_PIXEL_BLT
315 * - good for sparse bitmaps
316 *
317 * 3D engine:
318 * - Point per pixel
319 * - Translate bitmap to an alpha texture and render as a quad
320 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
321 */
322 void
323 intelBitmap(struct gl_context * ctx,
324 GLint x, GLint y,
325 GLsizei width, GLsizei height,
326 const struct gl_pixelstore_attrib *unpack,
327 const GLubyte * pixels)
328 {
329 struct intel_context *intel = intel_context(ctx);
330
331 if (do_blit_bitmap(ctx, x, y, width, height,
332 unpack, pixels))
333 return;
334
335 /* FIXME */
336 if (intel->gen == 6)
337 return _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
338
339 _mesa_meta_Bitmap(ctx, x, y, width, height, unpack, pixels);
340 }