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