Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mesa / drivers / dri / i965 / intel_pixel_bitmap.c
1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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/blend.h"
29 #include "main/glheader.h"
30 #include "main/enums.h"
31 #include "main/image.h"
32 #include "main/colormac.h"
33 #include "main/condrender.h"
34 #include "main/mtypes.h"
35 #include "main/macros.h"
36 #include "main/pbo.h"
37 #include "main/bufferobj.h"
38 #include "main/state.h"
39 #include "main/texobj.h"
40 #include "main/context.h"
41 #include "main/fbobject.h"
42 #include "swrast/swrast.h"
43 #include "drivers/common/meta.h"
44
45 #include "brw_context.h"
46 #include "intel_screen.h"
47 #include "intel_batchbuffer.h"
48 #include "intel_blit.h"
49 #include "intel_fbo.h"
50 #include "intel_image.h"
51 #include "intel_buffers.h"
52 #include "intel_pixel.h"
53 #include "intel_reg.h"
54
55
56 #define FILE_DEBUG_FLAG DEBUG_PIXEL
57
58
59 /* Unlike the other intel_pixel_* functions, the expectation here is
60 * that the incoming data is not in a PBO. With the XY_TEXT blit
61 * method, there's no benefit haveing it in a PBO, but we could
62 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
63 * PBO bitmaps. I think they are probably pretty rare though - I
64 * wonder if Xgl uses them?
65 */
66 static const GLubyte *map_pbo( struct gl_context *ctx,
67 GLsizei width, GLsizei height,
68 const struct gl_pixelstore_attrib *unpack,
69 const GLubyte *bitmap )
70 {
71 GLubyte *buf;
72
73 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
74 GL_COLOR_INDEX, GL_BITMAP,
75 INT_MAX, (const GLvoid *) bitmap)) {
76 _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
77 return NULL;
78 }
79
80 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0, unpack->BufferObj->Size,
81 GL_MAP_READ_BIT,
82 unpack->BufferObj,
83 MAP_INTERNAL);
84 if (!buf) {
85 _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
86 return NULL;
87 }
88
89 return ADD_POINTERS(buf, bitmap);
90 }
91
92 static bool test_bit( const GLubyte *src, GLuint bit )
93 {
94 return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
95 }
96
97 static void set_bit( GLubyte *dest, GLuint bit )
98 {
99 dest[bit/8] |= 1 << (bit % 8);
100 }
101
102 /* Extract a rectangle's worth of data from the bitmap. Called
103 * per chunk of HW-sized bitmap.
104 */
105 static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
106 const struct gl_pixelstore_attrib *unpack,
107 const GLubyte *bitmap,
108 GLuint x, GLuint y,
109 GLuint w, GLuint h,
110 GLubyte *dest,
111 GLuint row_align,
112 bool invert)
113 {
114 GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
115 GLuint mask = unpack->LsbFirst ? 0 : 7;
116 GLuint bit = 0;
117 GLint row, col;
118 GLint first, last;
119 GLint incr;
120 GLuint count = 0;
121
122 DBG("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
123 __func__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask);
124
125 if (invert) {
126 first = h-1;
127 last = 0;
128 incr = -1;
129 }
130 else {
131 first = 0;
132 last = h-1;
133 incr = 1;
134 }
135
136 /* Require that dest be pre-zero'd.
137 */
138 for (row = first; row != (last+incr); row += incr) {
139 const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap,
140 width, height,
141 GL_COLOR_INDEX, GL_BITMAP,
142 y + row, x);
143
144 for (col = 0; col < w; col++, bit++) {
145 if (test_bit(rowsrc, (col + src_offset) ^ mask)) {
146 set_bit(dest, bit ^ 7);
147 count++;
148 }
149 }
150
151 if (row_align)
152 bit = ALIGN(bit, row_align);
153 }
154
155 return count;
156 }
157
158 /**
159 * Returns the low Y value of the vertical range given, flipped according to
160 * whether the framebuffer is or not.
161 */
162 static inline int
163 y_flip(struct gl_framebuffer *fb, int y, int height)
164 {
165 if (_mesa_is_user_fbo(fb))
166 return y;
167 else
168 return fb->Height - y - height;
169 }
170
171 /*
172 * Render a bitmap.
173 */
174 static bool
175 do_blit_bitmap( struct gl_context *ctx,
176 GLint dstx, GLint dsty,
177 GLsizei width, GLsizei height,
178 const struct gl_pixelstore_attrib *unpack,
179 const GLubyte *bitmap )
180 {
181 struct brw_context *brw = brw_context(ctx);
182 struct gl_framebuffer *fb = ctx->DrawBuffer;
183 struct intel_renderbuffer *irb;
184 GLfloat tmpColor[4];
185 GLubyte ubcolor[4];
186 GLuint color;
187 GLsizei bitmap_width = width;
188 GLsizei bitmap_height = height;
189 GLint px, py;
190 GLuint stipple[32];
191 GLint orig_dstx = dstx;
192 GLint orig_dsty = dsty;
193
194 /* Update draw buffer bounds */
195 _mesa_update_state(ctx);
196
197 if (ctx->Depth.Test) {
198 /* The blit path produces incorrect results when depth testing is on.
199 * It seems the blit Z coord is always 1.0 (the far plane) so fragments
200 * will likely be obscured by other, closer geometry.
201 */
202 return false;
203 }
204
205 intel_prepare_render(brw);
206
207 if (fb->_NumColorDrawBuffers != 1) {
208 perf_debug("accelerated glBitmap() only supports rendering to a "
209 "single color buffer\n");
210 return false;
211 }
212
213 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]);
214
215 if (_mesa_is_bufferobj(unpack->BufferObj)) {
216 bitmap = map_pbo(ctx, width, height, unpack, bitmap);
217 if (bitmap == NULL)
218 return true; /* even though this is an error, we're done */
219 }
220
221 COPY_4V(tmpColor, ctx->Current.RasterColor);
222
223 if (_mesa_need_secondary_color(ctx)) {
224 ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
225 }
226
227 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
228 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
229 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
230 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
231
232 switch (_mesa_get_render_format(ctx, intel_rb_format(irb))) {
233 case MESA_FORMAT_B8G8R8A8_UNORM:
234 case MESA_FORMAT_B8G8R8X8_UNORM:
235 color = PACK_COLOR_8888(ubcolor[3], ubcolor[0], ubcolor[1], ubcolor[2]);
236 break;
237 case MESA_FORMAT_B5G6R5_UNORM:
238 color = PACK_COLOR_565(ubcolor[0], ubcolor[1], ubcolor[2]);
239 break;
240 default:
241 perf_debug("Unsupported format %s in accelerated glBitmap()\n",
242 _mesa_get_format_name(irb->mt->format));
243 return false;
244 }
245
246 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
247 return false;
248
249 /* Clip to buffer bounds and scissor. */
250 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
251 fb->_Xmax, fb->_Ymax,
252 &dstx, &dsty, &width, &height))
253 goto out;
254
255 dsty = y_flip(fb, dsty, height);
256
257 #define DY 32
258 #define DX 32
259
260 /* The blitter has no idea about fast color clears, so we need to resolve
261 * the miptree before we do anything.
262 */
263 intel_miptree_resolve_color(brw, irb->mt);
264
265 /* Chop it all into chunks that can be digested by hardware: */
266 for (py = 0; py < height; py += DY) {
267 for (px = 0; px < width; px += DX) {
268 int h = MIN2(DY, height - py);
269 int w = MIN2(DX, width - px);
270 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
271 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
272 ctx->Color.LogicOp : GL_COPY;
273
274 assert(sz <= sizeof(stipple));
275 memset(stipple, 0, sz);
276
277 /* May need to adjust this when padding has been introduced in
278 * sz above:
279 *
280 * Have to translate destination coordinates back into source
281 * coordinates.
282 */
283 int count = get_bitmap_rect(bitmap_width, bitmap_height, unpack,
284 bitmap,
285 -orig_dstx + (dstx + px),
286 -orig_dsty + y_flip(fb, dsty + py, h),
287 w, h,
288 (GLubyte *)stipple,
289 8,
290 _mesa_is_winsys_fbo(fb));
291 if (count == 0)
292 continue;
293
294 if (!intelEmitImmediateColorExpandBlit(brw,
295 irb->mt->cpp,
296 (GLubyte *)stipple,
297 sz,
298 color,
299 irb->mt->pitch,
300 irb->mt->bo,
301 0,
302 irb->mt->tiling,
303 dstx + px,
304 dsty + py,
305 w, h,
306 logic_op)) {
307 return false;
308 }
309
310 if (ctx->Query.CurrentOcclusionObject)
311 ctx->Query.CurrentOcclusionObject->Result += count;
312 }
313 }
314 out:
315
316 if (unlikely(INTEL_DEBUG & DEBUG_SYNC))
317 intel_batchbuffer_flush(brw);
318
319 if (_mesa_is_bufferobj(unpack->BufferObj)) {
320 /* done with PBO so unmap it now */
321 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj, MAP_INTERNAL);
322 }
323
324 return true;
325 }
326
327
328 /* There are a large number of possible ways to implement bitmap on
329 * this hardware, most of them have some sort of drawback. Here are a
330 * few that spring to mind:
331 *
332 * Blit:
333 * - XY_MONO_SRC_BLT_CMD
334 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
335 * - XY_TEXT_BLT
336 * - XY_TEXT_IMMEDIATE_BLT
337 * - blit per cliprect, subject to maximum immediate data size.
338 * - XY_COLOR_BLT
339 * - per pixel or run of pixels
340 * - XY_PIXEL_BLT
341 * - good for sparse bitmaps
342 *
343 * 3D engine:
344 * - Point per pixel
345 * - Translate bitmap to an alpha texture and render as a quad
346 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
347 */
348 void
349 intelBitmap(struct gl_context * ctx,
350 GLint x, GLint y,
351 GLsizei width, GLsizei height,
352 const struct gl_pixelstore_attrib *unpack,
353 const GLubyte * pixels)
354 {
355 if (!_mesa_check_conditional_render(ctx))
356 return;
357
358 if (do_blit_bitmap(ctx, x, y, width, height,
359 unpack, pixels))
360 return;
361
362 _mesa_meta_Bitmap(ctx, x, y, width, height, unpack, pixels);
363 }