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 /* Does zoom apply to bitmaps?
196 if (!intel_check_blit_fragment_ops(ctx
, tmpColor
[3] == 1.0F
) ||
197 ctx
->Pixel
.ZoomX
!= 1.0F
||
198 ctx
->Pixel
.ZoomY
!= 1.0F
)
201 LOCK_HARDWARE(intel
);
203 if (intel
->driDrawable
->numClipRects
) {
204 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
205 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
206 drm_clip_rect_t dest_rect
;
207 GLint nbox
= dPriv
->numClipRects
;
208 GLint srcx
= 0, srcy
= 0;
209 GLint orig_screen_x1
, orig_screen_y2
;
213 orig_screen_x1
= dPriv
->x
+ dstx
;
214 orig_screen_y2
= dPriv
->y
+ (dPriv
->h
- dsty
);
216 /* Do scissoring in GL coordinates:
218 if (ctx
->Scissor
.Enabled
)
220 GLint x
= ctx
->Scissor
.X
;
221 GLint y
= ctx
->Scissor
.Y
;
222 GLuint w
= ctx
->Scissor
.Width
;
223 GLuint h
= ctx
->Scissor
.Height
;
225 if (!_mesa_clip_to_region(x
, y
, x
+w
-1, y
+h
-1, &dstx
, &dsty
, &width
, &height
))
229 /* Convert from GL to hardware coordinates:
231 dsty
= dPriv
->y
+ (dPriv
->h
- dsty
- height
);
232 dstx
= dPriv
->x
+ dstx
;
234 dest_rect
.x1
= dstx
< 0 ? 0 : dstx
;
235 dest_rect
.y1
= dsty
< 0 ? 0 : dsty
;
236 dest_rect
.x2
= dstx
+ width
< 0 ? 0 : dstx
+ width
;
237 dest_rect
.y2
= dsty
+ height
< 0 ? 0 : dsty
+ height
;
239 for (i
= 0; i
< nbox
; i
++) {
240 drm_clip_rect_t rect
;
245 if (!intel_intersect_cliprects(&rect
, &dest_rect
, &box
[i
]))
248 /* Now go back to GL coordinates to figure out what subset of
249 * the bitmap we are uploading for this cliprect:
251 box_w
= rect
.x2
- rect
.x1
;
252 box_h
= rect
.y2
- rect
.y1
;
253 srcx
= rect
.x1
- orig_screen_x1
;
254 srcy
= orig_screen_y2
- rect
.y2
;
260 /* Then, finally, chop it all into chunks that can be
261 * digested by hardware:
263 for (py
= 0; py
< box_h
; py
+= DY
) {
264 for (px
= 0; px
< box_w
; px
+= DX
) {
265 int h
= MIN2(DY
, box_h
- py
);
266 int w
= MIN2(DX
, box_w
- 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 if (get_bitmap_rect(width
, height
, unpack
,
279 srcx
+ px
, srcy
+ py
, w
, h
,
287 intelEmitImmediateColorExpandBlit( intel
,
291 (dst
->cpp
== 2) ? color565
: color8888
,
305 UNLOCK_HARDWARE(intel
);
308 if (unpack
->BufferObj
->Name
) {
309 /* done with PBO so unmap it now */
310 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
321 /* There are a large number of possible ways to implement bitmap on
322 * this hardware, most of them have some sort of drawback. Here are a
323 * few that spring to mind:
326 * - XY_MONO_SRC_BLT_CMD
327 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
329 * - XY_TEXT_IMMEDIATE_BLT
330 * - blit per cliprect, subject to maximum immediate data size.
332 * - per pixel or run of pixels
334 * - good for sparse bitmaps
338 * - Translate bitmap to an alpha texture and render as a quad
339 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
342 intelBitmap(GLcontext
* ctx
,
344 GLsizei width
, GLsizei height
,
345 const struct gl_pixelstore_attrib
*unpack
,
346 const GLubyte
* pixels
)
348 if (do_blit_bitmap(ctx
, x
, y
, width
, height
,
352 if (INTEL_DEBUG
& DEBUG_PIXEL
)
353 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__
);
355 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, pixels
);