2 * Copyright 2006 VMware, Inc.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portionsalloc
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.
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"
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"
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 #include "intel_reg.h"
53 #define FILE_DEBUG_FLAG DEBUG_PIXEL
56 /* Unlike the other intel_pixel_* functions, the expectation here is
57 * that the incoming data is not in a PBO. With the XY_TEXT blit
58 * method, there's no benefit haveing it in a PBO, but we could
59 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
60 * PBO bitmaps. I think they are probably pretty rare though - I
61 * wonder if Xgl uses them?
63 static const GLubyte
*map_pbo( struct gl_context
*ctx
,
64 GLsizei width
, GLsizei height
,
65 const struct gl_pixelstore_attrib
*unpack
,
66 const GLubyte
*bitmap
)
70 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
71 GL_COLOR_INDEX
, GL_BITMAP
,
72 INT_MAX
, (const GLvoid
*) bitmap
)) {
73 _mesa_error(ctx
, GL_INVALID_OPERATION
,"glBitmap(invalid PBO access)");
77 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0, unpack
->BufferObj
->Size
,
82 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBitmap(PBO is mapped)");
86 return ADD_POINTERS(buf
, bitmap
);
89 static bool test_bit( const GLubyte
*src
, GLuint bit
)
91 return (src
[bit
/8] & (1<<(bit
% 8))) ? 1 : 0;
94 static void set_bit( GLubyte
*dest
, GLuint bit
)
96 dest
[bit
/8] |= 1 << (bit
% 8);
99 /* Extract a rectangle's worth of data from the bitmap. Called
100 * per chunk of HW-sized bitmap.
102 static GLuint
get_bitmap_rect(GLsizei width
, GLsizei height
,
103 const struct gl_pixelstore_attrib
*unpack
,
104 const GLubyte
*bitmap
,
111 GLuint src_offset
= (x
+ unpack
->SkipPixels
) & 0x7;
112 GLuint mask
= unpack
->LsbFirst
? 0 : 7;
119 DBG("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
120 __func__
, x
,y
,w
,h
,width
,height
,unpack
->SkipPixels
, src_offset
, mask
);
133 /* Require that dest be pre-zero'd.
135 for (row
= first
; row
!= (last
+incr
); row
+= incr
) {
136 const GLubyte
*rowsrc
= _mesa_image_address2d(unpack
, bitmap
,
138 GL_COLOR_INDEX
, GL_BITMAP
,
141 for (col
= 0; col
< w
; col
++, bit
++) {
142 if (test_bit(rowsrc
, (col
+ src_offset
) ^ mask
)) {
143 set_bit(dest
, bit
^ 7);
149 bit
= ALIGN(bit
, row_align
);
156 * Returns the low Y value of the vertical range given, flipped according to
157 * whether the framebuffer is or not.
160 y_flip(struct gl_framebuffer
*fb
, int y
, int height
)
162 if (_mesa_is_user_fbo(fb
))
165 return fb
->Height
- y
- height
;
172 do_blit_bitmap( struct gl_context
*ctx
,
173 GLint dstx
, GLint dsty
,
174 GLsizei width
, GLsizei height
,
175 const struct gl_pixelstore_attrib
*unpack
,
176 const GLubyte
*bitmap
)
178 struct brw_context
*brw
= brw_context(ctx
);
179 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
180 struct intel_renderbuffer
*irb
;
184 GLsizei bitmap_width
= width
;
185 GLsizei bitmap_height
= height
;
188 GLint orig_dstx
= dstx
;
189 GLint orig_dsty
= dsty
;
191 /* Update draw buffer bounds */
192 _mesa_update_state(ctx
);
194 if (ctx
->Depth
.Test
) {
195 /* The blit path produces incorrect results when depth testing is on.
196 * It seems the blit Z coord is always 1.0 (the far plane) so fragments
197 * will likely be obscured by other, closer geometry.
202 intel_prepare_render(brw
);
204 if (fb
->_NumColorDrawBuffers
!= 1) {
205 perf_debug("accelerated glBitmap() only supports rendering to a "
206 "single color buffer\n");
210 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
212 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
213 bitmap
= map_pbo(ctx
, width
, height
, unpack
, bitmap
);
215 return true; /* even though this is an error, we're done */
218 COPY_4V(tmpColor
, ctx
->Current
.RasterColor
);
220 if (_mesa_need_secondary_color(ctx
)) {
221 ADD_3V(tmpColor
, tmpColor
, ctx
->Current
.RasterSecondaryColor
);
224 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[0], tmpColor
[0]);
225 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[1], tmpColor
[1]);
226 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[2], tmpColor
[2]);
227 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[3], tmpColor
[3]);
229 switch (_mesa_get_render_format(ctx
, intel_rb_format(irb
))) {
230 case MESA_FORMAT_B8G8R8A8_UNORM
:
231 case MESA_FORMAT_B8G8R8X8_UNORM
:
232 color
= PACK_COLOR_8888(ubcolor
[3], ubcolor
[0], ubcolor
[1], ubcolor
[2]);
234 case MESA_FORMAT_B5G6R5_UNORM
:
235 color
= PACK_COLOR_565(ubcolor
[0], ubcolor
[1], ubcolor
[2]);
238 perf_debug("Unsupported format %s in accelerated glBitmap()\n",
239 _mesa_get_format_name(irb
->mt
->format
));
243 if (!intel_check_blit_fragment_ops(ctx
, tmpColor
[3] == 1.0F
))
246 /* Clip to buffer bounds and scissor. */
247 if (!_mesa_clip_to_region(fb
->_Xmin
, fb
->_Ymin
,
248 fb
->_Xmax
, fb
->_Ymax
,
249 &dstx
, &dsty
, &width
, &height
))
252 dsty
= y_flip(fb
, dsty
, height
);
257 /* The blitter has no idea about fast color clears, so we need to resolve
258 * the miptree before we do anything.
260 intel_miptree_resolve_color(brw
, irb
->mt
);
262 /* Chop it all into chunks that can be digested by hardware: */
263 for (py
= 0; py
< height
; py
+= DY
) {
264 for (px
= 0; px
< width
; px
+= DX
) {
265 int h
= MIN2(DY
, height
- py
);
266 int w
= MIN2(DX
, width
- px
);
267 GLuint sz
= ALIGN(ALIGN(w
,8) * h
, 64)/8;
268 GLenum logic_op
= ctx
->Color
.ColorLogicOpEnabled
?
269 ctx
->Color
.LogicOp
: GL_COPY
;
271 assert(sz
<= sizeof(stipple
));
272 memset(stipple
, 0, sz
);
274 /* May need to adjust this when padding has been introduced in
277 * Have to translate destination coordinates back into source
280 int count
= get_bitmap_rect(bitmap_width
, bitmap_height
, unpack
,
282 -orig_dstx
+ (dstx
+ px
),
283 -orig_dsty
+ y_flip(fb
, dsty
+ py
, h
),
287 _mesa_is_winsys_fbo(fb
));
291 if (!intelEmitImmediateColorExpandBlit(brw
,
307 if (ctx
->Query
.CurrentOcclusionObject
)
308 ctx
->Query
.CurrentOcclusionObject
->Result
+= count
;
313 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
))
314 intel_batchbuffer_flush(brw
);
316 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
317 /* done with PBO so unmap it now */
318 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
, MAP_INTERNAL
);
325 /* There are a large number of possible ways to implement bitmap on
326 * this hardware, most of them have some sort of drawback. Here are a
327 * few that spring to mind:
330 * - XY_MONO_SRC_BLT_CMD
331 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
333 * - XY_TEXT_IMMEDIATE_BLT
334 * - blit per cliprect, subject to maximum immediate data size.
336 * - per pixel or run of pixels
338 * - good for sparse bitmaps
342 * - Translate bitmap to an alpha texture and render as a quad
343 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
346 intelBitmap(struct gl_context
* ctx
,
348 GLsizei width
, GLsizei height
,
349 const struct gl_pixelstore_attrib
*unpack
,
350 const GLubyte
* pixels
)
352 if (!_mesa_check_conditional_render(ctx
))
355 if (do_blit_bitmap(ctx
, x
, y
, width
, height
,
359 _mesa_meta_Bitmap(ctx
, x
, y
, width
, height
, unpack
, pixels
);