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 "swrast/swrast.h"
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_blit.h"
41 #include "intel_regions.h"
42 #include "intel_buffer_objects.h"
43 #include "intel_buffers.h"
44 #include "intel_pixel.h"
45 #include "intel_reg.h"
48 #define FILE_DEBUG_FLAG DEBUG_PIXEL
51 /* Unlike the other intel_pixel_* functions, the expectation here is
52 * that the incoming data is not in a PBO. With the XY_TEXT blit
53 * method, there's no benefit haveing it in a PBO, but we could
54 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
55 * PBO bitmaps. I think they are probably pretty rare though - I
56 * wonder if Xgl uses them?
58 static const GLubyte
*map_pbo( GLcontext
*ctx
,
59 GLsizei width
, GLsizei height
,
60 const struct gl_pixelstore_attrib
*unpack
,
61 const GLubyte
*bitmap
)
65 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
66 GL_COLOR_INDEX
, GL_BITMAP
,
68 _mesa_error(ctx
, GL_INVALID_OPERATION
,"glBitmap(invalid PBO access)");
72 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
76 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBitmap(PBO is mapped)");
80 return ADD_POINTERS(buf
, bitmap
);
83 static GLboolean
test_bit( const GLubyte
*src
,
86 return (src
[bit
/8] & (1<<(bit
% 8))) ? 1 : 0;
89 static void set_bit( GLubyte
*dest
,
92 dest
[bit
/8] |= 1 << (bit
% 8);
95 /* Extract a rectangle's worth of data from the bitmap. Called
98 static GLuint
get_bitmap_rect(GLsizei width
, GLsizei height
,
99 const struct gl_pixelstore_attrib
*unpack
,
100 const GLubyte
*bitmap
,
107 GLuint src_offset
= (x
+ unpack
->SkipPixels
) & 0x7;
108 GLuint mask
= unpack
->LsbFirst
? 0 : 7;
115 if (INTEL_DEBUG
& DEBUG_PIXEL
)
116 _mesa_printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
117 __FUNCTION__
, x
,y
,w
,h
,width
,height
,unpack
->SkipPixels
, src_offset
, mask
);
130 /* Require that dest be pre-zero'd.
132 for (row
= first
; row
!= (last
+incr
); row
+= incr
) {
133 const GLubyte
*rowsrc
= _mesa_image_address2d(unpack
, bitmap
,
135 GL_COLOR_INDEX
, GL_BITMAP
,
138 for (col
= 0; col
< w
; col
++, bit
++) {
139 if (test_bit(rowsrc
, (col
+ src_offset
) ^ mask
)) {
140 set_bit(dest
, bit
^ 7);
146 bit
= ALIGN(bit
, row_align
);
159 do_blit_bitmap( GLcontext
*ctx
,
160 GLint dstx
, GLint dsty
,
161 GLsizei width
, GLsizei height
,
162 const struct gl_pixelstore_attrib
*unpack
,
163 const GLubyte
*bitmap
)
165 struct intel_context
*intel
= intel_context(ctx
);
166 struct intel_region
*dst
= intel_drawbuf_region(intel
);
169 GLuint color8888
, color565
;
174 if (unpack
->BufferObj
->Name
) {
175 bitmap
= map_pbo(ctx
, width
, height
, unpack
, bitmap
);
177 return GL_TRUE
; /* even though this is an error, we're done */
180 COPY_4V(tmpColor
, ctx
->Current
.RasterColor
);
182 if (NEED_SECONDARY_COLOR(ctx
)) {
183 ADD_3V(tmpColor
, tmpColor
, ctx
->Current
.RasterSecondaryColor
);
186 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[0], tmpColor
[0]);
187 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[1], tmpColor
[1]);
188 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[2], tmpColor
[2]);
189 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[3], tmpColor
[3]);
191 color8888
= INTEL_PACKCOLOR8888(ubcolor
[0], ubcolor
[1], ubcolor
[2], ubcolor
[3]);
192 color565
= INTEL_PACKCOLOR565(ubcolor
[0], ubcolor
[1], ubcolor
[2]);
194 if (!intel_check_blit_fragment_ops(ctx
, tmpColor
[3] == 1.0F
))
197 LOCK_HARDWARE(intel
);
199 if (intel
->driDrawable
->numClipRects
) {
200 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
201 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
202 drm_clip_rect_t dest_rect
;
203 GLint nbox
= dPriv
->numClipRects
;
204 GLint srcx
= 0, srcy
= 0;
205 GLint orig_screen_x1
, orig_screen_y2
;
209 orig_screen_x1
= dPriv
->x
+ dstx
;
210 orig_screen_y2
= dPriv
->y
+ (dPriv
->h
- dsty
);
212 /* Do scissoring in GL coordinates:
214 if (ctx
->Scissor
.Enabled
)
216 GLint x
= ctx
->Scissor
.X
;
217 GLint y
= ctx
->Scissor
.Y
;
218 GLuint w
= ctx
->Scissor
.Width
;
219 GLuint h
= ctx
->Scissor
.Height
;
221 if (!_mesa_clip_to_region(x
, y
, x
+w
-1, y
+h
-1, &dstx
, &dsty
, &width
, &height
))
225 /* Convert from GL to hardware coordinates:
227 dsty
= dPriv
->y
+ (dPriv
->h
- dsty
- height
);
228 dstx
= dPriv
->x
+ dstx
;
230 dest_rect
.x1
= dstx
< 0 ? 0 : dstx
;
231 dest_rect
.y1
= dsty
< 0 ? 0 : dsty
;
232 dest_rect
.x2
= dstx
+ width
< 0 ? 0 : dstx
+ width
;
233 dest_rect
.y2
= dsty
+ height
< 0 ? 0 : dsty
+ height
;
235 for (i
= 0; i
< nbox
; i
++) {
236 drm_clip_rect_t rect
;
241 if (!intel_intersect_cliprects(&rect
, &dest_rect
, &box
[i
]))
244 /* Now go back to GL coordinates to figure out what subset of
245 * the bitmap we are uploading for this cliprect:
247 box_w
= rect
.x2
- rect
.x1
;
248 box_h
= rect
.y2
- rect
.y1
;
249 srcx
= rect
.x1
- orig_screen_x1
;
250 srcy
= orig_screen_y2
- rect
.y2
;
256 /* Then, finally, chop it all into chunks that can be
257 * digested by hardware:
259 for (py
= 0; py
< box_h
; py
+= DY
) {
260 for (px
= 0; px
< box_w
; px
+= DX
) {
261 int h
= MIN2(DY
, box_h
- py
);
262 int w
= MIN2(DX
, box_w
- px
);
263 GLuint sz
= ALIGN(ALIGN(w
,8) * h
, 64)/8;
264 GLenum logic_op
= ctx
->Color
.ColorLogicOpEnabled
?
265 ctx
->Color
.LogicOp
: GL_COPY
;
267 assert(sz
<= sizeof(stipple
));
268 memset(stipple
, 0, sz
);
270 /* May need to adjust this when padding has been introduced in
273 if (get_bitmap_rect(width
, height
, unpack
,
275 srcx
+ px
, srcy
+ py
, w
, h
,
283 intelEmitImmediateColorExpandBlit( intel
,
287 (dst
->cpp
== 2) ? color565
: color8888
,
301 UNLOCK_HARDWARE(intel
);
304 if (unpack
->BufferObj
->Name
) {
305 /* done with PBO so unmap it now */
306 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
317 /* There are a large number of possible ways to implement bitmap on
318 * this hardware, most of them have some sort of drawback. Here are a
319 * few that spring to mind:
322 * - XY_MONO_SRC_BLT_CMD
323 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
325 * - XY_TEXT_IMMEDIATE_BLT
326 * - blit per cliprect, subject to maximum immediate data size.
328 * - per pixel or run of pixels
330 * - good for sparse bitmaps
334 * - Translate bitmap to an alpha texture and render as a quad
335 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
338 intelBitmap(GLcontext
* ctx
,
340 GLsizei width
, GLsizei height
,
341 const struct gl_pixelstore_attrib
*unpack
,
342 const GLubyte
* pixels
)
344 if (do_blit_bitmap(ctx
, x
, y
, width
, height
,
348 if (INTEL_DEBUG
& DEBUG_PIXEL
)
349 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__
);
351 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, pixels
);