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/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 "swrast/swrast.h"
38 #include "intel_screen.h"
39 #include "intel_context.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_blit.h"
42 #include "intel_regions.h"
43 #include "intel_buffer_objects.h"
44 #include "intel_buffers.h"
45 #include "intel_pixel.h"
46 #include "intel_reg.h"
49 #define FILE_DEBUG_FLAG DEBUG_PIXEL
52 /* Unlike the other intel_pixel_* functions, the expectation here is
53 * that the incoming data is not in a PBO. With the XY_TEXT blit
54 * method, there's no benefit haveing it in a PBO, but we could
55 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
56 * PBO bitmaps. I think they are probably pretty rare though - I
57 * wonder if Xgl uses them?
59 static const GLubyte
*map_pbo( GLcontext
*ctx
,
60 GLsizei width
, GLsizei height
,
61 const struct gl_pixelstore_attrib
*unpack
,
62 const GLubyte
*bitmap
)
66 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
67 GL_COLOR_INDEX
, GL_BITMAP
,
69 _mesa_error(ctx
, GL_INVALID_OPERATION
,"glBitmap(invalid PBO access)");
73 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
77 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBitmap(PBO is mapped)");
81 return ADD_POINTERS(buf
, bitmap
);
84 static GLboolean
test_bit( const GLubyte
*src
,
87 return (src
[bit
/8] & (1<<(bit
% 8))) ? 1 : 0;
90 static void set_bit( GLubyte
*dest
,
93 dest
[bit
/8] |= 1 << (bit
% 8);
96 /* Extract a rectangle's worth of data from the bitmap. Called
99 static GLuint
get_bitmap_rect(GLsizei width
, GLsizei height
,
100 const struct gl_pixelstore_attrib
*unpack
,
101 const GLubyte
*bitmap
,
108 GLuint src_offset
= (x
+ unpack
->SkipPixels
) & 0x7;
109 GLuint mask
= unpack
->LsbFirst
? 0 : 7;
116 if (INTEL_DEBUG
& DEBUG_PIXEL
)
117 _mesa_printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
118 __FUNCTION__
, x
,y
,w
,h
,width
,height
,unpack
->SkipPixels
, src_offset
, mask
);
131 /* Require that dest be pre-zero'd.
133 for (row
= first
; row
!= (last
+incr
); row
+= incr
) {
134 const GLubyte
*rowsrc
= _mesa_image_address2d(unpack
, bitmap
,
136 GL_COLOR_INDEX
, GL_BITMAP
,
139 for (col
= 0; col
< w
; col
++, bit
++) {
140 if (test_bit(rowsrc
, (col
+ src_offset
) ^ mask
)) {
141 set_bit(dest
, bit
^ 7);
147 bit
= ALIGN(bit
, row_align
);
160 do_blit_bitmap( GLcontext
*ctx
,
161 GLint dstx
, GLint dsty
,
162 GLsizei width
, GLsizei height
,
163 const struct gl_pixelstore_attrib
*unpack
,
164 const GLubyte
*bitmap
)
166 struct intel_context
*intel
= intel_context(ctx
);
167 struct intel_region
*dst
= intel_drawbuf_region(intel
);
168 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
171 GLuint color8888
, color565
;
172 unsigned int num_cliprects
;
173 drm_clip_rect_t
*cliprects
;
176 /* Update draw buffer bounds */
177 _mesa_update_state(ctx
);
182 if (unpack
->BufferObj
->Name
) {
183 bitmap
= map_pbo(ctx
, width
, height
, unpack
, bitmap
);
185 return GL_TRUE
; /* even though this is an error, we're done */
188 COPY_4V(tmpColor
, ctx
->Current
.RasterColor
);
190 if (NEED_SECONDARY_COLOR(ctx
)) {
191 ADD_3V(tmpColor
, tmpColor
, ctx
->Current
.RasterSecondaryColor
);
194 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[0], tmpColor
[0]);
195 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[1], tmpColor
[1]);
196 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[2], tmpColor
[2]);
197 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[3], tmpColor
[3]);
199 color8888
= INTEL_PACKCOLOR8888(ubcolor
[0], ubcolor
[1], ubcolor
[2], ubcolor
[3]);
200 color565
= INTEL_PACKCOLOR565(ubcolor
[0], ubcolor
[1], ubcolor
[2]);
202 if (!intel_check_blit_fragment_ops(ctx
, tmpColor
[3] == 1.0F
))
205 LOCK_HARDWARE(intel
);
207 intel_get_cliprects(intel
, &cliprects
, &num_cliprects
, &x_off
, &y_off
);
208 if (num_cliprects
!= 0) {
209 drm_clip_rect_t dest_rect
;
210 GLint srcx
= 0, srcy
= 0;
212 GLint orig_dstx
= dstx
;
213 GLint orig_dsty
= dsty
;
215 /* Clip to buffer bounds and scissor. */
216 if (!_mesa_clip_to_region(fb
->_Xmin
, fb
->_Ymin
,
217 fb
->_Xmax
, fb
->_Ymax
,
218 &dstx
, &dsty
, &width
, &height
))
221 /* Convert from GL to hardware coordinates. Transform original points
222 * along with it so that we can look at cliprects in hw coordinates and
223 * map back to points in the source space.
226 /* bitmap to a system framebuffer */
228 dsty
= y_off
+ (fb
->Height
- dsty
- height
);
229 orig_dstx
= x_off
+ orig_dstx
;
230 orig_dsty
= y_off
+ (fb
->Height
- orig_dsty
- height
);
232 /* bitmap to a user framebuffer object */
235 orig_dstx
= x_off
+ orig_dstx
;
236 orig_dsty
= y_off
+ orig_dsty
;
241 dest_rect
.x2
= dstx
+ width
;
242 dest_rect
.y2
= dsty
+ height
;
244 for (i
= 0; i
< num_cliprects
; i
++) {
245 drm_clip_rect_t rect
;
250 if (!intel_intersect_cliprects(&rect
, &dest_rect
, &cliprects
[i
]))
253 /* Now go back to GL coordinates to figure out what subset of
254 * the bitmap we are uploading for this cliprect:
256 box_w
= rect
.x2
- rect
.x1
;
257 box_h
= rect
.y2
- rect
.y1
;
258 srcx
= rect
.x1
- orig_dstx
;
259 srcy
= rect
.y1
- orig_dsty
;
264 /* Then, finally, chop it all into chunks that can be
265 * digested by hardware:
267 for (py
= 0; py
< box_h
; py
+= DY
) {
268 for (px
= 0; px
< box_w
; px
+= DX
) {
269 int h
= MIN2(DY
, box_h
- py
);
270 int w
= MIN2(DX
, box_w
- px
);
271 GLuint sz
= ALIGN(ALIGN(w
,8) * h
, 64)/8;
272 GLenum logic_op
= ctx
->Color
.ColorLogicOpEnabled
?
273 ctx
->Color
.LogicOp
: GL_COPY
;
275 assert(sz
<= sizeof(stipple
));
276 memset(stipple
, 0, sz
);
278 /* May need to adjust this when padding has been introduced in
281 if (get_bitmap_rect(width
, height
, unpack
,
283 srcx
+ px
, srcy
+ py
, w
, h
,
286 fb
->Name
== 0 ? GL_TRUE
: GL_FALSE
) == 0)
291 intelEmitImmediateColorExpandBlit( intel
,
295 (dst
->cpp
== 2) ? color565
: color8888
,
309 UNLOCK_HARDWARE(intel
);
311 if (INTEL_DEBUG
& DEBUG_SYNC
)
312 intel_batchbuffer_flush(intel
->batch
);
314 if (unpack
->BufferObj
->Name
) {
315 /* done with PBO so unmap it now */
316 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
327 /* There are a large number of possible ways to implement bitmap on
328 * this hardware, most of them have some sort of drawback. Here are a
329 * few that spring to mind:
332 * - XY_MONO_SRC_BLT_CMD
333 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
335 * - XY_TEXT_IMMEDIATE_BLT
336 * - blit per cliprect, subject to maximum immediate data size.
338 * - per pixel or run of pixels
340 * - good for sparse bitmaps
344 * - Translate bitmap to an alpha texture and render as a quad
345 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
348 intelBitmap(GLcontext
* ctx
,
350 GLsizei width
, GLsizei height
,
351 const struct gl_pixelstore_attrib
*unpack
,
352 const GLubyte
* pixels
)
354 if (do_blit_bitmap(ctx
, x
, y
, width
, height
,
358 if (INTEL_DEBUG
& DEBUG_PIXEL
)
359 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__
);
361 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, pixels
);