1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
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.
26 **************************************************************************/
28 #include "main/glheader.h"
29 #include "main/arbprogram.h"
30 #include "main/enums.h"
31 #include "main/image.h"
32 #include "main/colormac.h"
33 #include "main/mtypes.h"
34 #include "main/macros.h"
35 #include "main/bufferobj.h"
36 #include "main/polygon.h"
37 #include "main/pixelstore.h"
38 #include "main/polygon.h"
39 #include "main/state.h"
40 #include "main/teximage.h"
41 #include "main/texobj.h"
42 #include "main/texstate.h"
43 #include "main/texparam.h"
44 #include "main/varray.h"
45 #include "main/attrib.h"
46 #include "main/enable.h"
47 #include "main/viewport.h"
48 #include "main/context.h"
49 #include "swrast/swrast.h"
51 #include "intel_screen.h"
52 #include "intel_context.h"
53 #include "intel_batchbuffer.h"
54 #include "intel_blit.h"
55 #include "intel_regions.h"
56 #include "intel_buffers.h"
57 #include "intel_pixel.h"
58 #include "intel_reg.h"
61 #define FILE_DEBUG_FLAG DEBUG_PIXEL
64 /* Unlike the other intel_pixel_* functions, the expectation here is
65 * that the incoming data is not in a PBO. With the XY_TEXT blit
66 * method, there's no benefit haveing it in a PBO, but we could
67 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
68 * PBO bitmaps. I think they are probably pretty rare though - I
69 * wonder if Xgl uses them?
71 static const GLubyte
*map_pbo( GLcontext
*ctx
,
72 GLsizei width
, GLsizei height
,
73 const struct gl_pixelstore_attrib
*unpack
,
74 const GLubyte
*bitmap
)
78 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
79 GL_COLOR_INDEX
, GL_BITMAP
,
81 _mesa_error(ctx
, GL_INVALID_OPERATION
,"glBitmap(invalid PBO access)");
85 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
89 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBitmap(PBO is mapped)");
93 return ADD_POINTERS(buf
, bitmap
);
96 static GLboolean
test_bit( const GLubyte
*src
, GLuint bit
)
98 return (src
[bit
/8] & (1<<(bit
% 8))) ? 1 : 0;
101 static void set_bit( GLubyte
*dest
, GLuint bit
)
103 dest
[bit
/8] |= 1 << (bit
% 8);
106 /* Extract a rectangle's worth of data from the bitmap. Called
107 * per chunk of HW-sized bitmap.
109 static GLuint
get_bitmap_rect(GLsizei width
, GLsizei height
,
110 const struct gl_pixelstore_attrib
*unpack
,
111 const GLubyte
*bitmap
,
118 GLuint src_offset
= (x
+ unpack
->SkipPixels
) & 0x7;
119 GLuint mask
= unpack
->LsbFirst
? 0 : 7;
126 if (INTEL_DEBUG
& DEBUG_PIXEL
)
127 printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
128 __FUNCTION__
, x
,y
,w
,h
,width
,height
,unpack
->SkipPixels
, src_offset
, mask
);
141 /* Require that dest be pre-zero'd.
143 for (row
= first
; row
!= (last
+incr
); row
+= incr
) {
144 const GLubyte
*rowsrc
= _mesa_image_address2d(unpack
, bitmap
,
146 GL_COLOR_INDEX
, GL_BITMAP
,
149 for (col
= 0; col
< w
; col
++, bit
++) {
150 if (test_bit(rowsrc
, (col
+ src_offset
) ^ mask
)) {
151 set_bit(dest
, bit
^ 7);
157 bit
= ALIGN(bit
, row_align
);
164 * Returns the low Y value of the vertical range given, flipped according to
165 * whether the framebuffer is or not.
168 y_flip(struct gl_framebuffer
*fb
, int y
, int height
)
173 return fb
->Height
- y
- height
;
180 do_blit_bitmap( GLcontext
*ctx
,
181 GLint dstx
, GLint dsty
,
182 GLsizei width
, GLsizei height
,
183 const struct gl_pixelstore_attrib
*unpack
,
184 const GLubyte
*bitmap
)
186 struct intel_context
*intel
= intel_context(ctx
);
187 struct intel_region
*dst
= intel_drawbuf_region(intel
);
188 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
192 GLsizei bitmap_width
= width
;
193 GLsizei bitmap_height
= height
;
196 GLint orig_dstx
= dstx
;
197 GLint orig_dsty
= dsty
;
199 /* Update draw buffer bounds */
200 _mesa_update_state(ctx
);
202 if (ctx
->Depth
.Test
) {
203 /* The blit path produces incorrect results when depth testing is on.
204 * It seems the blit Z coord is always 1.0 (the far plane) so fragments
205 * will likely be obscured by other, closer geometry.
213 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
214 bitmap
= map_pbo(ctx
, width
, height
, unpack
, bitmap
);
216 return GL_TRUE
; /* even though this is an error, we're done */
219 COPY_4V(tmpColor
, ctx
->Current
.RasterColor
);
221 if (NEED_SECONDARY_COLOR(ctx
)) {
222 ADD_3V(tmpColor
, tmpColor
, ctx
->Current
.RasterSecondaryColor
);
225 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[0], tmpColor
[0]);
226 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[1], tmpColor
[1]);
227 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[2], tmpColor
[2]);
228 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[3], tmpColor
[3]);
231 color
= PACK_COLOR_565(ubcolor
[0], ubcolor
[1], ubcolor
[2]);
233 color
= PACK_COLOR_8888(ubcolor
[3], ubcolor
[0], ubcolor
[1], ubcolor
[2]);
235 if (!intel_check_blit_fragment_ops(ctx
, tmpColor
[3] == 1.0F
))
238 intel_prepare_render(intel
);
240 /* Clip to buffer bounds and scissor. */
241 if (!_mesa_clip_to_region(fb
->_Xmin
, fb
->_Ymin
,
242 fb
->_Xmax
, fb
->_Ymax
,
243 &dstx
, &dsty
, &width
, &height
))
246 dsty
= y_flip(fb
, dsty
, height
);
251 /* Chop it all into chunks that can be digested by hardware: */
252 for (py
= 0; py
< height
; py
+= DY
) {
253 for (px
= 0; px
< width
; px
+= DX
) {
254 int h
= MIN2(DY
, height
- py
);
255 int w
= MIN2(DX
, width
- px
);
256 GLuint sz
= ALIGN(ALIGN(w
,8) * h
, 64)/8;
257 GLenum logic_op
= ctx
->Color
.ColorLogicOpEnabled
?
258 ctx
->Color
.LogicOp
: GL_COPY
;
260 assert(sz
<= sizeof(stipple
));
261 memset(stipple
, 0, sz
);
263 /* May need to adjust this when padding has been introduced in
266 * Have to translate destination coordinates back into source
269 if (get_bitmap_rect(bitmap_width
, bitmap_height
, unpack
,
271 -orig_dstx
+ (dstx
+ px
),
272 -orig_dsty
+ y_flip(fb
, dsty
+ py
, h
),
276 fb
->Name
== 0 ? GL_TRUE
: GL_FALSE
) == 0)
279 if (!intelEmitImmediateColorExpandBlit(intel
,
298 if (INTEL_DEBUG
& DEBUG_SYNC
)
299 intel_batchbuffer_flush(intel
->batch
);
301 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
302 /* done with PBO so unmap it now */
303 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
307 intel_check_front_buffer_rendering(intel
);
312 /* There are a large number of possible ways to implement bitmap on
313 * this hardware, most of them have some sort of drawback. Here are a
314 * few that spring to mind:
317 * - XY_MONO_SRC_BLT_CMD
318 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
320 * - XY_TEXT_IMMEDIATE_BLT
321 * - blit per cliprect, subject to maximum immediate data size.
323 * - per pixel or run of pixels
325 * - good for sparse bitmaps
329 * - Translate bitmap to an alpha texture and render as a quad
330 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
333 intelBitmap(GLcontext
* ctx
,
335 GLsizei width
, GLsizei height
,
336 const struct gl_pixelstore_attrib
*unpack
,
337 const GLubyte
* pixels
)
339 if (do_blit_bitmap(ctx
, x
, y
, width
, height
,
343 _mesa_meta_Bitmap(ctx
, x
, y
, width
, height
, unpack
, pixels
);