1 /**************************************************************************
3 * Copyright 2003 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 portions
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 **************************************************************************/
29 #include "main/mtypes.h"
30 #include "main/context.h"
31 #include "main/enums.h"
32 #include "main/colormac.h"
34 #include "intel_blit.h"
35 #include "intel_buffers.h"
36 #include "intel_context.h"
37 #include "intel_fbo.h"
38 #include "intel_reg.h"
39 #include "intel_regions.h"
40 #include "intel_batchbuffer.h"
42 #define FILE_DEBUG_FLAG DEBUG_BLIT
45 * Copy the back color buffer to the front color buffer.
46 * Used for SwapBuffers().
49 intelCopyBuffer(const __DRIdrawablePrivate
* dPriv
,
50 const drm_clip_rect_t
* rect
)
53 struct intel_context
*intel
;
54 const intelScreenPrivate
*intelScreen
;
56 DBG("%s\n", __FUNCTION__
);
60 intel
= intelScreenContext(dPriv
->driScreenPriv
->private);
64 intelScreen
= intel
->intelScreen
;
66 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets
67 * should work regardless.
71 if (dPriv
&& dPriv
->numClipRects
) {
72 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
73 struct intel_region
*src
, *dst
;
74 int nbox
= dPriv
->numClipRects
;
75 drm_clip_rect_t
*pbox
= dPriv
->pClipRects
;
77 int src_pitch
, dst_pitch
;
78 unsigned short src_x
, src_y
;
81 dri_bo
*aper_array
[3];
83 src
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_BACK_LEFT
);
84 dst
= intel_get_rb_region(&intel_fb
->Base
, BUFFER_FRONT_LEFT
);
86 src_pitch
= src
->pitch
* src
->cpp
;
87 dst_pitch
= dst
->pitch
* dst
->cpp
;
92 ASSERT(intel_fb
->Base
.Name
== 0); /* Not a user-created FBO */
95 ASSERT(src
->cpp
== dst
->cpp
);
98 BR13
= (0xCC << 16) | BR13_565
;
99 CMD
= XY_SRC_COPY_BLT_CMD
;
102 BR13
= (0xCC << 16) | BR13_8888
;
103 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
106 assert(src
->tiling
!= I915_TILING_Y
);
107 assert(dst
->tiling
!= I915_TILING_Y
);
109 if (src
->tiling
!= I915_TILING_NONE
) {
113 if (dst
->tiling
!= I915_TILING_NONE
) {
118 /* do space/cliprects check before going any further */
119 intel_batchbuffer_require_space(intel
->batch
, 8 * 4,
120 REFERENCES_CLIPRECTS
);
122 aper_array
[0] = intel
->batch
->buf
;
123 aper_array
[1] = dst
->buffer
;
124 aper_array
[2] = src
->buffer
;
126 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
127 intel_batchbuffer_flush(intel
->batch
);
131 for (i
= 0; i
< nbox
; i
++, pbox
++) {
132 drm_clip_rect_t box
= *pbox
;
135 if (!intel_intersect_cliprects(&box
, &box
, rect
))
139 if (box
.x1
>= box
.x2
||
143 assert(box
.x1
< box
.x2
);
144 assert(box
.y1
< box
.y2
);
145 src_x
= box
.x1
- dPriv
->x
+ dPriv
->backX
;
146 src_y
= box
.y1
- dPriv
->y
+ dPriv
->backY
;
148 BEGIN_BATCH(8, REFERENCES_CLIPRECTS
);
150 OUT_BATCH(BR13
| dst_pitch
);
151 OUT_BATCH((box
.y1
<< 16) | box
.x1
);
152 OUT_BATCH((box
.y2
<< 16) | box
.x2
);
154 OUT_RELOC(dst
->buffer
,
155 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
157 OUT_BATCH((src_y
<< 16) | src_x
);
158 OUT_BATCH(src_pitch
);
159 OUT_RELOC(src
->buffer
,
160 I915_GEM_DOMAIN_RENDER
, 0,
165 /* Flush the rendering and the batch so that the results all land on the
166 * screen in a timely fashion.
168 intel_batchbuffer_emit_mi_flush(intel
->batch
);
169 intel_batchbuffer_flush(intel
->batch
);
172 UNLOCK_HARDWARE(intel
);
175 static GLuint
translate_raster_op(GLenum logicop
)
178 case GL_CLEAR
: return 0x00;
179 case GL_AND
: return 0x88;
180 case GL_AND_REVERSE
: return 0x44;
181 case GL_COPY
: return 0xCC;
182 case GL_AND_INVERTED
: return 0x22;
183 case GL_NOOP
: return 0xAA;
184 case GL_XOR
: return 0x66;
185 case GL_OR
: return 0xEE;
186 case GL_NOR
: return 0x11;
187 case GL_EQUIV
: return 0x99;
188 case GL_INVERT
: return 0x55;
189 case GL_OR_REVERSE
: return 0xDD;
190 case GL_COPY_INVERTED
: return 0x33;
191 case GL_OR_INVERTED
: return 0xBB;
192 case GL_NAND
: return 0x77;
193 case GL_SET
: return 0xFF;
202 intelEmitCopyBlit(struct intel_context
*intel
,
212 GLshort src_x
, GLshort src_y
,
213 GLshort dst_x
, GLshort dst_y
,
214 GLshort w
, GLshort h
,
217 GLuint CMD
, BR13
, pass
= 0;
218 int dst_y2
= dst_y
+ h
;
219 int dst_x2
= dst_x
+ w
;
220 dri_bo
*aper_array
[3];
223 if (dst_tiling
!= I915_TILING_NONE
) {
224 if (dst_offset
& 4095)
226 if (dst_tiling
== I915_TILING_Y
)
229 if (src_tiling
!= I915_TILING_NONE
) {
230 if (src_offset
& 4095)
232 if (src_tiling
== I915_TILING_Y
)
236 /* do space/cliprects check before going any further */
238 aper_array
[0] = intel
->batch
->buf
;
239 aper_array
[1] = dst_buffer
;
240 aper_array
[2] = src_buffer
;
242 if (dri_bufmgr_check_aperture_space(aper_array
, 3) != 0) {
243 intel_batchbuffer_flush(intel
->batch
);
250 LOCK_HARDWARE(intel
);
251 dri_bo_map(dst_buffer
, GL_TRUE
);
252 dri_bo_map(src_buffer
, GL_FALSE
);
253 _mesa_copy_rect((GLubyte
*)dst_buffer
->virtual + dst_offset
,
258 (GLubyte
*)src_buffer
->virtual + src_offset
,
262 dri_bo_unmap(src_buffer
);
263 dri_bo_unmap(dst_buffer
);
264 UNLOCK_HARDWARE(intel
);
269 intel_batchbuffer_require_space(intel
->batch
, 8 * 4, NO_LOOP_CLIPRECTS
);
270 DBG("%s src:buf(%p)/%d+%d %d,%d dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
272 src_buffer
, src_pitch
, src_offset
, src_x
, src_y
,
273 dst_buffer
, dst_pitch
, dst_offset
, dst_x
, dst_y
, w
, h
);
278 BR13
= translate_raster_op(logic_op
) << 16;
282 CMD
= XY_SRC_COPY_BLT_CMD
;
286 CMD
= XY_SRC_COPY_BLT_CMD
;
290 CMD
= XY_SRC_COPY_BLT_CMD
| XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
297 if (dst_tiling
!= I915_TILING_NONE
) {
301 if (src_tiling
!= I915_TILING_NONE
) {
307 if (dst_y2
<= dst_y
|| dst_x2
<= dst_x
) {
311 assert(dst_x
< dst_x2
);
312 assert(dst_y
< dst_y2
);
314 BEGIN_BATCH(8, NO_LOOP_CLIPRECTS
);
316 OUT_BATCH(BR13
| (uint16_t)dst_pitch
);
317 OUT_BATCH((dst_y
<< 16) | dst_x
);
318 OUT_BATCH((dst_y2
<< 16) | dst_x2
);
319 OUT_RELOC(dst_buffer
,
320 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
322 OUT_BATCH((src_y
<< 16) | src_x
);
323 OUT_BATCH((uint16_t)src_pitch
);
324 OUT_RELOC(src_buffer
,
325 I915_GEM_DOMAIN_RENDER
, 0,
329 intel_batchbuffer_emit_mi_flush(intel
->batch
);
336 * Use blitting to clear the renderbuffers named by 'flags'.
337 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
338 * since that might include software renderbuffers or renderbuffers
339 * which we're clearing with triangles.
340 * \param mask bitmask of BUFFER_BIT_* values indicating buffers to clear
343 intelClearWithBlit(GLcontext
*ctx
, GLbitfield mask
)
345 struct intel_context
*intel
= intel_context(ctx
);
346 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
348 GLbitfield skipBuffers
= 0;
349 unsigned int num_cliprects
;
350 struct drm_clip_rect
*cliprects
;
355 * Compute values for clearing the buffers.
358 if (mask
& BUFFER_BIT_DEPTH
) {
359 clear_depth
= (GLuint
) (fb
->_DepthMax
* ctx
->Depth
.Clear
);
361 if (mask
& BUFFER_BIT_STENCIL
) {
362 clear_depth
|= (ctx
->Stencil
.Clear
& 0xff) << 24;
365 /* If clearing both depth and stencil, skip BUFFER_BIT_STENCIL in
368 if ((mask
& BUFFER_BIT_DEPTH
) && (mask
& BUFFER_BIT_STENCIL
)) {
369 skipBuffers
= BUFFER_BIT_STENCIL
;
372 LOCK_HARDWARE(intel
);
374 intel_get_cliprects(intel
, &cliprects
, &num_cliprects
, &x_off
, &y_off
);
376 GLint cx
, cy
, cw
, ch
;
377 drm_clip_rect_t clear
;
380 /* Get clear bounds after locking */
387 /* clearing a window */
389 /* flip top to bottom */
390 clear
.x1
= cx
+ x_off
;
391 clear
.y1
= intel
->driDrawable
->y
+ intel
->driDrawable
->h
- cy
- ch
;
392 clear
.x2
= clear
.x1
+ cw
;
393 clear
.y2
= clear
.y1
+ ch
;
397 assert(num_cliprects
== 1);
398 assert(cliprects
== &intel
->fboRect
);
401 clear
.x2
= clear
.x1
+ cw
;
402 clear
.y2
= clear
.y1
+ ch
;
403 /* no change to mask */
406 for (i
= 0; i
< num_cliprects
; i
++) {
407 const drm_clip_rect_t
*box
= &cliprects
[i
];
410 GLuint clearMask
= mask
; /* use copy, since we modify it below */
411 GLboolean all
= (cw
== fb
->Width
&& ch
== fb
->Height
);
414 intel_intersect_cliprects(&b
, &clear
, box
);
420 if (b
.x1
>= b
.x2
|| b
.y1
>= b
.y2
)
424 _mesa_printf("clear %d,%d..%d,%d, mask %x\n",
425 b
.x1
, b
.y1
, b
.x2
, b
.y2
, mask
);
427 /* Loop over all renderbuffers */
428 for (buf
= 0; buf
< BUFFER_COUNT
&& clearMask
; buf
++) {
429 const GLbitfield bufBit
= 1 << buf
;
430 if ((clearMask
& bufBit
) && !(bufBit
& skipBuffers
)) {
431 /* OK, clear this renderbuffer */
432 struct intel_renderbuffer
*irb
= intel_get_renderbuffer(fb
, buf
);
433 dri_bo
*write_buffer
=
434 intel_region_buffer(intel
, irb
->region
,
435 all
? INTEL_WRITE_FULL
:
437 int x1
= b
.x1
+ irb
->region
->draw_x
;
438 int y1
= b
.y1
+ irb
->region
->draw_y
;
439 int x2
= b
.x2
+ irb
->region
->draw_x
;
440 int y2
= b
.y2
+ irb
->region
->draw_y
;
446 pitch
= irb
->region
->pitch
;
447 cpp
= irb
->region
->cpp
;
449 DBG("%s dst:buf(%p)/%d %d,%d sz:%dx%d\n",
451 irb
->region
->buffer
, (pitch
* cpp
),
452 x1
, y1
, x2
- x1
, y2
- y1
);
455 CMD
= XY_COLOR_BLT_CMD
;
457 /* Setup the blit command */
460 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
461 if (clearMask
& BUFFER_BIT_DEPTH
)
462 CMD
|= XY_BLT_WRITE_RGB
;
463 if (clearMask
& BUFFER_BIT_STENCIL
)
464 CMD
|= XY_BLT_WRITE_ALPHA
;
468 CMD
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
476 assert(irb
->region
->tiling
!= I915_TILING_Y
);
479 if (irb
->region
->tiling
!= I915_TILING_NONE
) {
484 BR13
|= (pitch
* cpp
);
486 if (buf
== BUFFER_DEPTH
|| buf
== BUFFER_STENCIL
) {
487 clearVal
= clear_depth
;
491 GLclampf
*color
= ctx
->Color
.ClearColor
;
493 CLAMPED_FLOAT_TO_UBYTE(clear
[0], color
[0]);
494 CLAMPED_FLOAT_TO_UBYTE(clear
[1], color
[1]);
495 CLAMPED_FLOAT_TO_UBYTE(clear
[2], color
[2]);
496 CLAMPED_FLOAT_TO_UBYTE(clear
[3], color
[3]);
498 switch (irb
->Base
.Format
) {
499 case MESA_FORMAT_ARGB8888
:
500 case MESA_FORMAT_XRGB8888
:
501 clearVal
= intel
->ClearColor8888
;
503 case MESA_FORMAT_RGB565
:
504 clearVal
= intel
->ClearColor565
;
506 case MESA_FORMAT_ARGB4444
:
507 clearVal
= PACK_COLOR_4444(clear
[3], clear
[0],
510 case MESA_FORMAT_ARGB1555
:
511 clearVal
= PACK_COLOR_1555(clear
[3], clear
[0],
515 _mesa_problem(ctx
, "Unexpected renderbuffer format: %d\n",
522 _mesa_debug(ctx, "hardware blit clear buf %d rb id %d\n",
523 buf, irb->Base.Name);
529 BEGIN_BATCH(6, REFERENCES_CLIPRECTS
);
532 OUT_BATCH((y1
<< 16) | x1
);
533 OUT_BATCH((y2
<< 16) | x2
);
534 OUT_RELOC(write_buffer
,
535 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
539 clearMask
&= ~bufBit
; /* turn off bit, for faster loop exit */
545 UNLOCK_HARDWARE(intel
);
549 intelEmitImmediateColorExpandBlit(struct intel_context
*intel
,
551 GLubyte
*src_bits
, GLuint src_size
,
557 GLshort x
, GLshort y
,
558 GLshort w
, GLshort h
,
561 int dwords
= ALIGN(src_size
, 8) / 4;
562 uint32_t opcode
, br13
, blit_cmd
;
564 if (dst_tiling
!= I915_TILING_NONE
) {
565 if (dst_offset
& 4095)
567 if (dst_tiling
== I915_TILING_Y
)
571 assert( logic_op
- GL_CLEAR
>= 0 );
572 assert( logic_op
- GL_CLEAR
< 0x10 );
573 assert(dst_pitch
> 0);
580 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
582 dst_buffer
, dst_pitch
, dst_offset
, x
, y
, w
, h
, src_size
, dwords
);
584 intel_batchbuffer_require_space( intel
->batch
,
588 REFERENCES_CLIPRECTS
);
590 opcode
= XY_SETUP_BLT_CMD
;
592 opcode
|= XY_BLT_WRITE_ALPHA
| XY_BLT_WRITE_RGB
;
594 if (dst_tiling
!= I915_TILING_NONE
) {
595 opcode
|= XY_DST_TILED
;
600 br13
= dst_pitch
| (translate_raster_op(logic_op
) << 16) | (1 << 29);
606 blit_cmd
= XY_TEXT_IMMEDIATE_BLIT_CMD
| XY_TEXT_BYTE_PACKED
; /* packing? */
607 if (dst_tiling
!= I915_TILING_NONE
)
608 blit_cmd
|= XY_DST_TILED
;
610 BEGIN_BATCH(8 + 3, REFERENCES_CLIPRECTS
);
613 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
614 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
615 OUT_RELOC(dst_buffer
,
616 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
618 OUT_BATCH(0); /* bg */
619 OUT_BATCH(fg_color
); /* fg */
620 OUT_BATCH(0); /* pattern base addr */
622 OUT_BATCH(blit_cmd
| ((3 - 2) + dwords
));
623 OUT_BATCH((y
<< 16) | x
);
624 OUT_BATCH(((y
+ h
) << 16) | (x
+ w
));
627 intel_batchbuffer_data( intel
->batch
,
630 REFERENCES_CLIPRECTS
);
632 intel_batchbuffer_emit_mi_flush(intel
->batch
);
637 /* We don't have a memmove-type blit like some other hardware, so we'll do a
638 * rectangular blit covering a large space, then emit 1-scanline blit at the
639 * end to cover the last if we need.
642 intel_emit_linear_blit(struct intel_context
*intel
,
643 drm_intel_bo
*dst_bo
,
644 unsigned int dst_offset
,
645 drm_intel_bo
*src_bo
,
646 unsigned int src_offset
,
649 GLuint pitch
, height
;
651 /* The pitch is a signed value. */
652 pitch
= MIN2(size
, (1 << 15) - 1);
653 height
= size
/ pitch
;
654 intelEmitCopyBlit(intel
, 1,
655 pitch
, src_bo
, src_offset
, I915_TILING_NONE
,
656 pitch
, dst_bo
, dst_offset
, I915_TILING_NONE
,
659 pitch
, height
, /* w, h */
662 src_offset
+= pitch
* height
;
663 dst_offset
+= pitch
* height
;
664 size
-= pitch
* height
;
665 assert (size
< (1 << 15));
667 intelEmitCopyBlit(intel
, 1,
668 size
, src_bo
, src_offset
, I915_TILING_NONE
,
669 size
, dst_bo
, dst_offset
, I915_TILING_NONE
,