2 * Copyright 2000 Compaq Computer Inc. and VA Linux Systems, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
26 * Gareth Hughes <gareth@valinux.com>
28 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgapixel.c,v 1.9 2002/11/05 17:46:08 tsi Exp $ */
33 #include "mgacontext.h"
38 #include "swrast/swrast.h"
41 #define IS_AGP_MEM( mmesa, p ) \
42 ((unsigned long)mmesa->mgaScreen->buffers.map <= ((unsigned long)p) && \
43 (unsigned long)mmesa->mgaScreen->buffers.map + \
44 (unsigned long)mmesa->mgaScreen->buffers.size > ((unsigned long)p))
45 #define AGP_OFFSET( mmesa, p ) \
46 (((unsigned long)p) - (unsigned long)mmesa->mgaScreen->buffers.map)
49 #if defined(MESA_packed_depth_stencil)
51 check_depth_stencil_24_8( const GLcontext
*ctx
, GLenum type
,
52 const struct gl_pixelstore_attrib
*packing
,
53 const void *pixels
, GLint sz
,
56 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
58 return ( type
== GL_UNSIGNED_INT_24_8_MESA
&&
59 ctx
->Visual
->DepthBits
== 24 &&
60 ctx
->Visual
->StencilBits
== 8 &&
61 mmesa
->mgaScreen
->cpp
== 4 &&
63 !ctx
->Pixel
.IndexShift
&&
64 !ctx
->Pixel
.IndexOffset
&&
65 !ctx
->Pixel
.MapStencilFlag
&&
66 ctx
->Pixel
.DepthBias
== 0.0 &&
67 ctx
->Pixel
.DepthScale
== 1.0 &&
68 !packing
->SwapBytes
&&
76 check_depth( const GLcontext
*ctx
, GLenum type
,
77 const struct gl_pixelstore_attrib
*packing
,
78 const void *pixels
, GLint sz
, GLint pitch
)
80 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
82 if ( IS_AGP_MEM( mmesa
, pixels
) &&
83 !( ( type
== GL_UNSIGNED_INT
&& mmesa
->mgaScreen
->cpp
== 4 ) ||
84 ( type
== GL_UNSIGNED_SHORT
&& mmesa
->mgaScreen
->cpp
== 2 ) ) )
87 return ( ctx
->Pixel
.DepthBias
== 0.0 &&
88 ctx
->Pixel
.DepthScale
== 1.0 &&
89 !packing
->SwapBytes
&&
96 check_color( const GLcontext
*ctx
, GLenum type
, GLenum format
,
97 const struct gl_pixelstore_attrib
*packing
,
98 const void *pixels
, GLint sz
, GLint pitch
)
100 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
101 GLuint cpp
= mmesa
->mgaScreen
->cpp
;
103 /* Can't do conversions on agp reads/draws.
105 if ( IS_AGP_MEM( mmesa
, pixels
) &&
106 !( pitch
% 32 == 0 && pitch
< 4096 &&
107 ( ( type
== GL_UNSIGNED_BYTE
&&
108 cpp
== 4 && format
== GL_BGRA
) ||
109 ( type
== GL_UNSIGNED_INT_8_8_8_8
&&
110 cpp
== 4 && format
== GL_BGRA
) ||
111 ( type
== GL_UNSIGNED_SHORT_5_6_5_REV
&&
112 cpp
== 2 && format
== GL_RGB
) ) ) )
115 return (!ctx
->_ImageTransferState
&&
116 !packing
->SwapBytes
&&
121 check_color_per_fragment_ops( const GLcontext
*ctx
)
123 return (!( ctx
->Color
.AlphaEnabled
||
126 ctx
->Scissor
.Enabled
||
127 ctx
->Stencil
.Enabled
||
128 !ctx
->Color
.ColorMask
[0] ||
129 !ctx
->Color
.ColorMask
[1] ||
130 !ctx
->Color
.ColorMask
[2] ||
131 !ctx
->Color
.ColorMask
[3] ||
132 ctx
->Color
.ColorLogicOpEnabled
||
133 ctx
->Texture
._EnabledUnits
||
134 ctx
->Depth
.OcclusionTest
136 ctx
->Current
.RasterPosValid
&&
137 ctx
->Pixel
.ZoomX
== 1.0F
&&
138 (ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
));
142 check_depth_per_fragment_ops( const GLcontext
*ctx
)
144 return ( ctx
->Current
.RasterPosValid
&&
145 ctx
->Color
.ColorMask
[RCOMP
] == 0 &&
146 ctx
->Color
.ColorMask
[BCOMP
] == 0 &&
147 ctx
->Color
.ColorMask
[GCOMP
] == 0 &&
148 ctx
->Color
.ColorMask
[ACOMP
] == 0 &&
149 ctx
->Pixel
.ZoomX
== 1.0F
&&
150 ( ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
) );
153 /* In addition to the requirements for depth:
155 #if defined(MESA_packed_depth_stencil)
157 check_stencil_per_fragment_ops( const GLcontext
*ctx
)
159 return ( !ctx
->Pixel
.IndexShift
&&
160 !ctx
->Pixel
.IndexOffset
);
166 clip_pixelrect( const GLcontext
*ctx
,
167 const GLframebuffer
*buffer
,
169 GLsizei
*width
, GLsizei
*height
,
170 GLint
*skipPixels
, GLint
*skipRows
,
173 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
175 *width
= MIN2(*width
, MAX_WIDTH
); /* redundant? */
178 if (*x
< buffer
->_Xmin
) {
179 *skipPixels
+= (buffer
->_Xmin
- *x
);
180 *width
-= (buffer
->_Xmin
- *x
);
185 if (*x
+ *width
> buffer
->_Xmax
)
186 *width
-= (*x
+ *width
- buffer
->_Xmax
- 1);
191 /* bottom clipping */
192 if (*y
< buffer
->_Ymin
) {
193 *skipRows
+= (buffer
->_Ymin
- *y
);
194 *height
-= (buffer
->_Ymin
- *y
);
199 if (*y
+ *height
> buffer
->_Ymax
)
200 *height
-= (*y
+ *height
- buffer
->_Ymax
- 1);
205 *size
= ((*y
+ *height
- 1) * mmesa
->mgaScreen
->frontPitch
+
206 (*x
+ *width
- 1) * mmesa
->mgaScreen
->cpp
);
212 mgaTryReadPixels( GLcontext
*ctx
,
213 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
214 GLenum format
, GLenum type
,
215 const struct gl_pixelstore_attrib
*pack
,
218 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
219 GLint size
, skipPixels
, skipRows
;
220 GLint pitch
= pack
->RowLength
? pack
->RowLength
: width
;
228 GLint source_pitch
, dest_pitch
;
229 GLint delta_sx
, delta_sy
;
230 GLint delta_dx
, delta_dy
;
231 GLint blit_height
, ydir
;
234 if (!clip_pixelrect(ctx
, ctx
->ReadBuffer
,
235 &x
, &y
, &width
, &height
,
236 &skipPixels
, &skipRows
, &size
)) {
240 /* Only accelerate reading to agp buffers.
242 if ( !IS_AGP_MEM(mmesa
, (char *)pixels
) ||
243 !IS_AGP_MEM(mmesa
, (char *)pixels
+ size
) )
247 #if defined(MESA_packed_depth_stencil)
248 case GL_DEPTH_STENCIL_MESA
:
249 ok
= check_depth_stencil_24_8(ctx
, type
, pack
, pixels
, size
, pitch
);
251 source
= mmesa
->mgaScreen
->depthOffset
;
255 case GL_DEPTH_COMPONENT
:
256 ok
= check_depth(ctx
, type
, pack
, pixels
, size
, pitch
);
258 /* Can't accelerate at this depth -- planemask does the wrong
259 * thing; it doesn't clear the low order bits in the
260 * destination, instead it leaves them untouched.
262 * Could get the acclerator to solid fill the destination with
263 * zeros first... Or get the cpu to do it...
265 if (ctx
->Visual
.depthBits
== 24)
269 source
= mmesa
->mgaScreen
->depthOffset
;
274 ok
= check_color(ctx
, type
, format
, pack
, pixels
, size
, pitch
);
276 source
= (mmesa
->draw_buffer
== MGA_FRONT
?
277 mmesa
->mgaScreen
->frontOffset
:
278 mmesa
->mgaScreen
->backOffset
);
290 LOCK_HARDWARE( mmesa
);
294 __DRIdrawablePrivate
*dPriv
= mmesa
->driDrawable
;
295 int nbox
, retcode
, i
;
297 UPDATE_LOCK( mmesa
, DRM_LOCK_FLUSH
| DRM_LOCK_QUIESCENT
);
299 if (mmesa
->dirty_cliprects
& MGA_FRONT
)
300 mgaUpdateRects( mmesa
, MGA_FRONT
);
302 nbox
= dPriv
->numClipRects
;
304 y
= dPriv
->h
- y
- height
;
308 dest
= ((mmesa
->mgaScreen
->agp
.handle
+ AGP_OFFSET(mmesa
, pixels
)) |
309 DO_dstmap_sys
| DO_dstacc_agp
);
310 source_pitch
= mmesa
->mgaScreen
->frontPitch
/ mmesa
->mgaScreen
->cpp
;
316 blit_height
= 2*y
+ height
;
319 if (0) fprintf(stderr
, "XX doing readpixel blit src_pitch %d dst_pitch %d\n",
320 source_pitch
, dest_pitch
);
324 for (i
= 0 ; i
< nbox
; )
326 int nr
= MIN2(i
+ MGA_NR_SAREA_CLIPRECTS
, dPriv
->numClipRects
);
327 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
328 drm_clip_rect_t
*b
= mmesa
->sarea
->boxes
;
331 for ( ; i
< nr
; i
++) {
332 GLint bx
= box
[i
].x1
;
333 GLint by
= box
[i
].y1
;
334 GLint bw
= box
[i
].x2
- bx
;
335 GLint bh
= box
[i
].y2
- by
;
337 if (bx
< x
) bw
-= x
- bx
, bx
= x
;
338 if (by
< y
) bh
-= y
- by
, by
= y
;
339 if (bx
+ bw
> x
+ width
) bw
= x
+ width
- bx
;
340 if (by
+ bh
> y
+ height
) bh
= y
+ height
- by
;
341 if (bw
<= 0) continue;
342 if (bh
<= 0) continue;
352 mmesa
->sarea
->nbox
= n
;
354 if (n
&& (retcode
= drmCommandWrite( mmesa
->driFd
, DRM_MGA_BLIT
,
355 &blit
, sizeof(drmMGABlit
)))) {
356 fprintf(stderr
, "blit ioctl failed, retcode = %d\n", retcode
);
357 UNLOCK_HARDWARE( mmesa
);
362 UPDATE_LOCK( mmesa
, DRM_LOCK_FLUSH
| DRM_LOCK_QUIESCENT
);
366 UNLOCK_HARDWARE( mmesa
);
372 mgaDDReadPixels( GLcontext
*ctx
,
373 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
374 GLenum format
, GLenum type
,
375 const struct gl_pixelstore_attrib
*pack
,
378 if (!mgaTryReadPixels( ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
))
379 _swrast_ReadPixels( ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
);
385 static void do_draw_pix( GLcontext
*ctx
,
386 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
389 GLuint dest
, GLuint planemask
)
392 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
394 __DRIdrawablePrivate
*dPriv
= mmesa
->driDrawable
;
395 drm_clip_rect_t pbox
= dPriv
->pClipRects
;
396 int nbox
= dPriv
->numClipRects
;
399 y
= dPriv
->h
- y
- height
;
404 blit
.planemask
= planemask
;
405 blit
.source
= ((mmesa
->mgaScreen
->agp
.handle
+ AGP_OFFSET(mmesa
, pixels
))
406 | SO_srcmap_sys
| SO_srcacc_agp
);
407 blit
.dest_pitch
= mmesa
->mgaScreen
->frontPitch
/ mmesa
->mgaScreen
->cpp
;
408 blit
.source_pitch
= pitch
;
413 if (ctx
->Pixel
.ZoomY
== -1) {
414 blit
.height
= height
;
417 blit
.height
= height
;
421 if (0) fprintf(stderr
,
422 "doing drawpixel blit src_pitch %d dst_pitch %d\n",
423 blit
.source_pitch
, blit
.dest_pitch
);
425 for (i
= 0 ; i
< nbox
; )
427 int nr
= MIN2(i
+ MGA_NR_SAREA_CLIPRECTS
, dPriv
->numClipRects
);
428 drm_clip_rect_t
*box
= mmesa
->pClipRects
;
429 drm_clip_rect_t
*b
= mmesa
->sarea
->boxes
;
432 for ( ; i
< nr
; i
++) {
433 GLint bx
= box
[i
].x1
;
434 GLint by
= box
[i
].y1
;
435 GLint bw
= box
[i
].x2
- bx
;
436 GLint bh
= box
[i
].y2
- by
;
438 if (bx
< x
) bw
-= x
- bx
, bx
= x
;
439 if (by
< y
) bh
-= y
- by
, by
= y
;
440 if (bx
+ bw
> x
+ width
) bw
= x
+ width
- bx
;
441 if (by
+ bh
> y
+ height
) bh
= y
+ height
- by
;
442 if (bw
<= 0) continue;
443 if (bh
<= 0) continue;
453 mmesa
->sarea
->nbox
= n
;
455 if (n
&& (retcode
= drmCommandWrite( mmesa
->driFd
, DRM_MGA_BLIT
,
456 &blit
, sizeof(drmMGABlit
)))) {
457 fprintf(stderr
, "blit ioctl failed, retcode = %d\n", retcode
);
458 UNLOCK_HARDWARE( mmesa
);
469 mgaTryDrawPixels( GLcontext
*ctx
,
470 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
471 GLenum format
, GLenum type
,
472 const struct gl_pixelstore_attrib
*unpack
,
473 const GLvoid
*pixels
)
475 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
476 GLint size
, skipPixels
, skipRows
;
477 GLint pitch
= unpack
->RowLength
? unpack
->RowLength
: width
;
478 GLuint dest
, planemask
;
479 GLuint cpp
= mmesa
->mgaScreen
->cpp
;
481 if (!clip_pixelrect(ctx
, ctx
->DrawBuffer
,
482 &x
, &y
, &width
, &height
,
483 &skipPixels
, &skipRows
, &size
)) {
489 #if defined(MESA_packed_depth_stencil)
490 case GL_DEPTH_STENCIL_MESA
:
491 dest
= mmesa
->mgaScreen
->depthOffset
;
493 if (!check_depth_stencil_24_8(ctx
, type
, unpack
, pixels
, size
, pitch
) ||
494 !check_depth_per_fragment_ops(ctx
) ||
495 !check_stencil_per_fragment_ops(ctx
))
500 case GL_DEPTH_COMPONENT
:
501 dest
= mmesa
->mgaScreen
->depthOffset
;
503 if (ctx
->Visual
.depthBits
== 24)
508 if (!check_depth(ctx
, type
, unpack
, pixels
, size
, pitch
) ||
509 !check_depth_per_fragment_ops(ctx
))
515 dest
= (mmesa
->draw_buffer
== MGA_FRONT
?
516 mmesa
->mgaScreen
->frontOffset
:
517 mmesa
->mgaScreen
->backOffset
);
519 planemask
= mgaPackColor(cpp
,
520 ctx
->Color
.ColorMask
[RCOMP
],
521 ctx
->Color
.ColorMask
[GCOMP
],
522 ctx
->Color
.ColorMask
[BCOMP
],
523 ctx
->Color
.ColorMask
[ACOMP
]);
526 planemask
|= planemask
<< 16;
528 if (!check_color(ctx
, type
, format
, unpack
, pixels
, size
, pitch
)) {
531 if (!check_color_per_fragment_ops(ctx
)) {
540 LOCK_HARDWARE_QUIESCENT( mmesa
);
542 if (mmesa
->dirty_cliprects
& MGA_FRONT
)
543 mgaUpdateRects( mmesa
, MGA_FRONT
);
545 if ( IS_AGP_MEM(mmesa
, (char *)pixels
) &&
546 IS_AGP_MEM(mmesa
, (char *)pixels
+ size
) )
548 do_draw_pix( ctx
, x
, y
, width
, height
, pitch
, pixels
,
550 UPDATE_LOCK( mmesa
, DRM_LOCK_FLUSH
| DRM_LOCK_QUIESCENT
);
554 /* Pixels is in regular memory -- get dma buffers and perform
555 * upload through them.
557 /* drmBufPtr buf = mgaGetBufferLocked(mmesa); */
558 GLuint bufferpitch
= (width
*cpp
+31)&~31;
560 char *address
= 0; /* mmesa->mgaScreen->agp.map; */
563 /* GLuint rows = MIN2( height, MGA_DMA_BUF_SZ / bufferpitch ); */
564 GLuint rows
= height
;
567 if (0) fprintf(stderr
, "trying to upload %d rows (pitch %d)\n",
570 /* The texture conversion code is so slow that there is only
571 * negligble speedup when the buffers/images don't exactly
576 if (!_mesa_convert_texsubimage2d( MESA_FORMAT_RGB565
,
578 bufferpitch
, format
, type
,
579 unpack
, pixels
, address
)) {
580 /* mgaReleaseBufLocked( mmesa, buf ); */
581 UNLOCK_HARDWARE(mmesa
);
585 if (!_mesa_convert_texsubimage2d( MESA_FORMAT_ARGB8888
,
587 bufferpitch
, format
, type
,
588 unpack
, pixels
, address
)) {
589 /* mgaReleaseBufLocked( mmesa, buf ); */
590 UNLOCK_HARDWARE(mmesa
);
595 MEMCPY( address
, pixels
, rows
*bufferpitch
);
598 do_draw_pix( ctx
, x
, y
, width
, rows
,
599 bufferpitch
/cpp
, address
, dest
, planemask
);
601 /* Fix me -- use multiple buffers to avoid flush.
603 UPDATE_LOCK( mmesa
, DRM_LOCK_FLUSH
| DRM_LOCK_QUIESCENT
);
605 pixels
= (void *)((char *) pixels
+ rows
* pitch
);
610 /* mgaReleaseBufLocked( mmesa, buf ); */
613 UNLOCK_HARDWARE( mmesa
);
614 mmesa
->dirty
|= MGA_UPLOAD_CLIPRECTS
;
620 mgaDDDrawPixels( GLcontext
*ctx
,
621 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
622 GLenum format
, GLenum type
,
623 const struct gl_pixelstore_attrib
*unpack
,
624 const GLvoid
*pixels
)
626 if (!mgaTryDrawPixels( ctx
, x
, y
, width
, height
, format
, type
,
628 _swrast_DrawPixels( ctx
, x
, y
, width
, height
, format
, type
,
634 /* Stub functions - not a real allocator, always returns pointer to
635 * the same block of agp space which isn't used for anything else at
638 void mgaDDInitPixelFuncs( GLcontext
*ctx
)
640 /* Pixel path fallbacks.
642 ctx
->Driver
.Accum
= _swrast_Accum
;
643 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
644 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
645 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
646 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
648 if (getenv("MGA_BLIT_PIXELS")) {
649 ctx
->Driver
.ReadPixels
= mgaDDReadPixels
; /* requires agp dest */
650 ctx
->Driver
.DrawPixels
= mgaDDDrawPixels
; /* works with agp/normal mem */