Merge branch 'master' of git+ssh://joukj@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / drivers / dri / i965 / intel_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include "mtypes.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "vblank.h"
36
37 #include "intel_reg.h"
38 #include "intel_batchbuffer.h"
39 #include "intel_context.h"
40 #include "intel_blit.h"
41 #include "intel_regions.h"
42 #include "intel_structs.h"
43
44 #include "bufmgr.h"
45
46
47
48
49 /*
50 * Copy the back buffer to the front buffer.
51 */
52 void intelCopyBuffer( __DRIdrawablePrivate *dPriv,
53 const drm_clip_rect_t *rect )
54 {
55 struct intel_context *intel;
56 GLboolean missed_target;
57 int64_t ust;
58
59 DBG("%s\n", __FUNCTION__);
60
61 assert(dPriv);
62 assert(dPriv->driContextPriv);
63 assert(dPriv->driContextPriv->driverPrivate);
64
65 intel = (struct intel_context *) dPriv->driContextPriv->driverPrivate;
66 intelFlush( &intel->ctx );
67
68
69 bmFinishFenceLock(intel, intel->last_swap_fence);
70
71 /* The LOCK_HARDWARE is required for the cliprects. Buffer offsets
72 * should work regardless.
73 */
74 LOCK_HARDWARE( intel );
75
76 if (!rect)
77 {
78 UNLOCK_HARDWARE( intel );
79 driWaitForVBlank( dPriv, &missed_target );
80 LOCK_HARDWARE( intel );
81 }
82
83 {
84 intelScreenPrivate *intelScreen = intel->intelScreen;
85 __DRIdrawablePrivate *dPriv = intel->driDrawable;
86 int nbox = dPriv->numClipRects;
87 drm_clip_rect_t *pbox = dPriv->pClipRects;
88 int cpp = intelScreen->cpp;
89 struct intel_region *src, *dst;
90 int BR13, CMD;
91 int i;
92 int src_pitch, dst_pitch;
93
94 if (intel->sarea->pf_current_page == 0) {
95 dst = intel->front_region;
96 src = intel->back_region;
97 }
98 else {
99 assert(0);
100 src = intel->front_region;
101 dst = intel->back_region;
102 }
103
104 src_pitch = src->pitch * src->cpp;
105 dst_pitch = dst->pitch * dst->cpp;
106
107 if (cpp == 2) {
108 BR13 = (0xCC << 16) | (1<<24);
109 CMD = XY_SRC_COPY_BLT_CMD;
110 }
111 else {
112 BR13 = (0xCC << 16) | (1<<24) | (1<<25);
113 CMD = XY_SRC_COPY_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
114 }
115
116 if (src->tiled) {
117 CMD |= XY_SRC_TILED;
118 src_pitch /= 4;
119 }
120
121 if (dst->tiled) {
122 CMD |= XY_DST_TILED;
123 dst_pitch /= 4;
124 }
125
126 for (i = 0 ; i < nbox; i++, pbox++)
127 {
128 drm_clip_rect_t tmp = *pbox;
129
130 if (rect) {
131 if (!intel_intersect_cliprects(&tmp, &tmp, rect))
132 continue;
133 }
134
135
136 if (tmp.x1 > tmp.x2 ||
137 tmp.y1 > tmp.y2 ||
138 tmp.x2 > intelScreen->width ||
139 tmp.y2 > intelScreen->height)
140 continue;
141
142 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
143 OUT_BATCH( CMD );
144 OUT_BATCH( dst_pitch | BR13 );
145 OUT_BATCH( (tmp.y1 << 16) | tmp.x1 );
146 OUT_BATCH( (tmp.y2 << 16) | tmp.x2 );
147 OUT_RELOC( dst->buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE, 0 );
148 OUT_BATCH( (tmp.y1 << 16) | tmp.x1 );
149 OUT_BATCH( src_pitch );
150 OUT_RELOC( src->buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ, 0 );
151 ADVANCE_BATCH();
152 }
153 }
154
155 intel_batchbuffer_flush( intel->batch );
156 intel->second_last_swap_fence = intel->last_swap_fence;
157 intel->last_swap_fence = bmSetFenceLock( intel );
158 UNLOCK_HARDWARE( intel );
159
160 if (!rect)
161 {
162 intel->swap_count++;
163 (*dri_interface->getUST)(&ust);
164 if (missed_target) {
165 intel->swap_missed_count++;
166 intel->swap_missed_ust = ust - intel->swap_ust;
167 }
168
169 intel->swap_ust = ust;
170 }
171
172 }
173
174
175
176
177 void intelEmitFillBlit( struct intel_context *intel,
178 GLuint cpp,
179 GLshort dst_pitch,
180 struct buffer *dst_buffer,
181 GLuint dst_offset,
182 GLboolean dst_tiled,
183 GLshort x, GLshort y,
184 GLshort w, GLshort h,
185 GLuint color )
186 {
187 GLuint BR13, CMD;
188 BATCH_LOCALS;
189
190 dst_pitch *= cpp;
191
192 switch(cpp) {
193 case 1:
194 case 2:
195 case 3:
196 BR13 = (0xF0 << 16) | (1<<24);
197 CMD = XY_COLOR_BLT_CMD;
198 break;
199 case 4:
200 BR13 = (0xF0 << 16) | (1<<24) | (1<<25);
201 CMD = XY_COLOR_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
202 break;
203 default:
204 return;
205 }
206
207 if (dst_tiled) {
208 CMD |= XY_DST_TILED;
209 dst_pitch /= 4;
210 }
211
212 BEGIN_BATCH(6, INTEL_BATCH_NO_CLIPRECTS);
213 OUT_BATCH( CMD );
214 OUT_BATCH( dst_pitch | BR13 );
215 OUT_BATCH( (y << 16) | x );
216 OUT_BATCH( ((y+h) << 16) | (x+w) );
217 OUT_RELOC( dst_buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE, dst_offset );
218 OUT_BATCH( color );
219 ADVANCE_BATCH();
220 }
221
222 static GLuint translate_raster_op(GLenum logicop)
223 {
224 switch(logicop) {
225 case GL_CLEAR: return 0x00;
226 case GL_AND: return 0x88;
227 case GL_AND_REVERSE: return 0x44;
228 case GL_COPY: return 0xCC;
229 case GL_AND_INVERTED: return 0x22;
230 case GL_NOOP: return 0xAA;
231 case GL_XOR: return 0x66;
232 case GL_OR: return 0xEE;
233 case GL_NOR: return 0x11;
234 case GL_EQUIV: return 0x99;
235 case GL_INVERT: return 0x55;
236 case GL_OR_REVERSE: return 0xDD;
237 case GL_COPY_INVERTED: return 0x33;
238 case GL_OR_INVERTED: return 0xBB;
239 case GL_NAND: return 0x77;
240 case GL_SET: return 0xFF;
241 default: return 0;
242 }
243 }
244
245
246 /* Copy BitBlt
247 */
248 void intelEmitCopyBlit( struct intel_context *intel,
249 GLuint cpp,
250 GLshort src_pitch,
251 struct buffer *src_buffer,
252 GLuint src_offset,
253 GLboolean src_tiled,
254 GLshort dst_pitch,
255 struct buffer *dst_buffer,
256 GLuint dst_offset,
257 GLboolean dst_tiled,
258 GLshort src_x, GLshort src_y,
259 GLshort dst_x, GLshort dst_y,
260 GLshort w, GLshort h,
261 GLenum logic_op )
262 {
263 GLuint CMD, BR13;
264 int dst_y2 = dst_y + h;
265 int dst_x2 = dst_x + w;
266 BATCH_LOCALS;
267
268
269 DBG("%s src:buf(%d)/%d %d,%d dst:buf(%d)/%d %d,%d sz:%dx%d op:%d\n",
270 __FUNCTION__,
271 src_buffer, src_pitch, src_x, src_y,
272 dst_buffer, dst_pitch, dst_x, dst_y,
273 w,h,logic_op);
274
275 assert( logic_op - GL_CLEAR >= 0 );
276 assert( logic_op - GL_CLEAR < 0x10 );
277
278 src_pitch *= cpp;
279 dst_pitch *= cpp;
280
281 switch(cpp) {
282 case 1:
283 case 2:
284 case 3:
285 BR13 = (translate_raster_op(logic_op) << 16) | (1<<24);
286 CMD = XY_SRC_COPY_BLT_CMD;
287 break;
288 case 4:
289 BR13 = (translate_raster_op(logic_op) << 16) | (1<<24) |
290 (1<<25);
291 CMD = XY_SRC_COPY_BLT_CMD | XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
292 break;
293 default:
294 return;
295 }
296
297 if (src_tiled) {
298 CMD |= XY_SRC_TILED;
299 src_pitch /= 4;
300 }
301
302 if (dst_tiled) {
303 CMD |= XY_DST_TILED;
304 dst_pitch /= 4;
305 }
306
307 if (dst_y2 < dst_y ||
308 dst_x2 < dst_x) {
309 return;
310 }
311
312 dst_pitch &= 0xffff;
313 src_pitch &= 0xffff;
314
315 /* Initial y values don't seem to work with negative pitches. If
316 * we adjust the offsets manually (below), it seems to work fine.
317 *
318 * On the other hand, if we always adjust, the hardware doesn't
319 * know which blit directions to use, so overlapping copypixels get
320 * the wrong result.
321 */
322 if (dst_pitch > 0 && src_pitch > 0) {
323 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
324 OUT_BATCH( CMD );
325 OUT_BATCH( dst_pitch | BR13 );
326 OUT_BATCH( (dst_y << 16) | dst_x );
327 OUT_BATCH( (dst_y2 << 16) | dst_x2 );
328 OUT_RELOC( dst_buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE,
329 dst_offset );
330 OUT_BATCH( (src_y << 16) | src_x );
331 OUT_BATCH( src_pitch );
332 OUT_RELOC( src_buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
333 src_offset );
334 ADVANCE_BATCH();
335 }
336 else {
337 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
338 OUT_BATCH( CMD );
339 OUT_BATCH( (dst_pitch & 0xffff) | BR13 );
340 OUT_BATCH( (0 << 16) | dst_x );
341 OUT_BATCH( (h << 16) | dst_x2 );
342 OUT_RELOC( dst_buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE,
343 dst_offset + dst_y * dst_pitch );
344 OUT_BATCH( (src_pitch & 0xffff) );
345 OUT_RELOC( src_buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
346 src_offset + src_y * src_pitch );
347 ADVANCE_BATCH();
348 }
349 }
350
351
352
353 void intelClearWithBlit(GLcontext *ctx, GLbitfield flags)
354 {
355 struct intel_context *intel = intel_context( ctx );
356 intelScreenPrivate *intelScreen = intel->intelScreen;
357 GLuint clear_depth, clear_color;
358 GLint cx, cy, cw, ch;
359 GLint cpp = intelScreen->cpp;
360 GLboolean all;
361 GLint i;
362 struct intel_region *front = intel->front_region;
363 struct intel_region *back = intel->back_region;
364 struct intel_region *depth = intel->depth_region;
365 GLuint BR13, FRONT_CMD, BACK_CMD, DEPTH_CMD;
366 GLuint front_pitch;
367 GLuint back_pitch;
368 GLuint depth_pitch;
369 BATCH_LOCALS;
370
371
372 clear_color = intel->ClearColor;
373 clear_depth = 0;
374
375 if (flags & BUFFER_BIT_DEPTH) {
376 clear_depth = (GLuint)(ctx->Depth.Clear * intel->ClearDepth);
377 }
378
379 if (flags & BUFFER_BIT_STENCIL) {
380 clear_depth |= (ctx->Stencil.Clear & 0xff) << 24;
381 }
382
383 switch(cpp) {
384 case 2:
385 BR13 = (0xF0 << 16) | (1<<24);
386 BACK_CMD = FRONT_CMD = XY_COLOR_BLT_CMD;
387 DEPTH_CMD = XY_COLOR_BLT_CMD;
388 break;
389 case 4:
390 BR13 = (0xF0 << 16) | (1<<24) | (1<<25);
391 BACK_CMD = FRONT_CMD = XY_COLOR_BLT_CMD |
392 XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
393 DEPTH_CMD = XY_COLOR_BLT_CMD;
394 if (flags & BUFFER_BIT_DEPTH) DEPTH_CMD |= XY_BLT_WRITE_RGB;
395 if (flags & BUFFER_BIT_STENCIL) DEPTH_CMD |= XY_BLT_WRITE_ALPHA;
396 break;
397 default:
398 return;
399 }
400
401
402
403 intelFlush( &intel->ctx );
404 LOCK_HARDWARE( intel );
405 {
406 /* get clear bounds after locking */
407 cx = ctx->DrawBuffer->_Xmin;
408 cy = ctx->DrawBuffer->_Ymin;
409 ch = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
410 cw = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
411 all = (cw == ctx->DrawBuffer->Width && ch == ctx->DrawBuffer->Height);
412
413 /* flip top to bottom */
414 cy = intel->driDrawable->h - cy - ch;
415 cx = cx + intel->drawX;
416 cy += intel->drawY;
417
418 /* adjust for page flipping */
419 if ( intel->sarea->pf_current_page == 0 ) {
420 front = intel->front_region;
421 back = intel->back_region;
422 }
423 else {
424 back = intel->front_region;
425 front = intel->back_region;
426 }
427
428 front_pitch = front->pitch * front->cpp;
429 back_pitch = back->pitch * back->cpp;
430 depth_pitch = depth->pitch * depth->cpp;
431
432 if (front->tiled) {
433 FRONT_CMD |= XY_DST_TILED;
434 front_pitch /= 4;
435 }
436
437 if (back->tiled) {
438 BACK_CMD |= XY_DST_TILED;
439 back_pitch /= 4;
440 }
441
442 if (depth->tiled) {
443 DEPTH_CMD |= XY_DST_TILED;
444 depth_pitch /= 4;
445 }
446
447 for (i = 0 ; i < intel->numClipRects ; i++)
448 {
449 drm_clip_rect_t *box = &intel->pClipRects[i];
450 drm_clip_rect_t b;
451
452 if (!all) {
453 GLint x = box->x1;
454 GLint y = box->y1;
455 GLint w = box->x2 - x;
456 GLint h = box->y2 - y;
457
458 if (x < cx) w -= cx - x, x = cx;
459 if (y < cy) h -= cy - y, y = cy;
460 if (x + w > cx + cw) w = cx + cw - x;
461 if (y + h > cy + ch) h = cy + ch - y;
462 if (w <= 0) continue;
463 if (h <= 0) continue;
464
465 b.x1 = x;
466 b.y1 = y;
467 b.x2 = x + w;
468 b.y2 = y + h;
469 } else {
470 b = *box;
471 }
472
473
474 if (b.x1 > b.x2 ||
475 b.y1 > b.y2 ||
476 b.x2 > intelScreen->width ||
477 b.y2 > intelScreen->height)
478 continue;
479
480 if ( flags & BUFFER_BIT_FRONT_LEFT ) {
481 BEGIN_BATCH(6, INTEL_BATCH_NO_CLIPRECTS);
482 OUT_BATCH( FRONT_CMD );
483 OUT_BATCH( front_pitch | BR13 );
484 OUT_BATCH( (b.y1 << 16) | b.x1 );
485 OUT_BATCH( (b.y2 << 16) | b.x2 );
486 OUT_RELOC( front->buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE,
487 0 );
488 OUT_BATCH( clear_color );
489 ADVANCE_BATCH();
490 }
491
492 if ( flags & BUFFER_BIT_BACK_LEFT ) {
493 BEGIN_BATCH(6, INTEL_BATCH_NO_CLIPRECTS);
494 OUT_BATCH( BACK_CMD );
495 OUT_BATCH( back_pitch | BR13 );
496 OUT_BATCH( (b.y1 << 16) | b.x1 );
497 OUT_BATCH( (b.y2 << 16) | b.x2 );
498 OUT_RELOC( back->buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE,
499 0 );
500 OUT_BATCH( clear_color );
501 ADVANCE_BATCH();
502 }
503
504 if ( flags & (BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH) ) {
505 BEGIN_BATCH(6, INTEL_BATCH_NO_CLIPRECTS);
506 OUT_BATCH( DEPTH_CMD );
507 OUT_BATCH( depth_pitch | BR13 );
508 OUT_BATCH( (b.y1 << 16) | b.x1 );
509 OUT_BATCH( (b.y2 << 16) | b.x2 );
510 OUT_RELOC( depth->buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE,
511 0 );
512 OUT_BATCH( clear_depth );
513 ADVANCE_BATCH();
514 }
515 }
516 }
517 intel_batchbuffer_flush( intel->batch );
518 UNLOCK_HARDWARE( intel );
519 }
520
521
522 void
523 intelEmitImmediateColorExpandBlit(struct intel_context *intel,
524 GLuint cpp,
525 GLubyte *src_bits, GLuint src_size,
526 GLuint fg_color,
527 GLshort dst_pitch,
528 struct buffer *dst_buffer,
529 GLuint dst_offset,
530 GLboolean dst_tiled,
531 GLshort x, GLshort y,
532 GLshort w, GLshort h,
533 GLenum logic_op)
534 {
535 struct xy_text_immediate_blit text;
536 int dwords = ALIGN(src_size, 8) / 4;
537 uint32_t opcode, br13;
538
539 assert( logic_op - GL_CLEAR >= 0 );
540 assert( logic_op - GL_CLEAR < 0x10 );
541
542 if (w < 0 || h < 0)
543 return;
544
545 dst_pitch *= cpp;
546
547 if (dst_tiled)
548 dst_pitch /= 4;
549
550 DBG("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d, %d bytes %d dwords\n",
551 __FUNCTION__,
552 dst_buffer, dst_pitch, dst_offset, x, y, w, h, src_size, dwords);
553
554 memset(&text, 0, sizeof(text));
555 text.dw0.client = CLIENT_2D;
556 text.dw0.opcode = OPCODE_XY_TEXT_IMMEDIATE_BLT;
557 text.dw0.pad0 = 0;
558 text.dw0.byte_packed = 1; /* ?maybe? */
559 text.dw0.pad1 = 0;
560 text.dw0.dst_tiled = dst_tiled;
561 text.dw0.pad2 = 0;
562 text.dw0.length = (sizeof(text)/sizeof(int)) - 2 + dwords;
563 text.dw1.dest_y1 = y; /* duplicates info in setup blit */
564 text.dw1.dest_x1 = x;
565 text.dw2.dest_y2 = y + h;
566 text.dw2.dest_x2 = x + w;
567
568 intel_batchbuffer_require_space( intel->batch,
569 (8 * 4) +
570 sizeof(text) +
571 dwords,
572 INTEL_BATCH_NO_CLIPRECTS );
573
574 opcode = XY_SETUP_BLT_CMD;
575 if (cpp == 4)
576 opcode |= XY_BLT_WRITE_ALPHA | XY_BLT_WRITE_RGB;
577 if (dst_tiled)
578 opcode |= XY_DST_TILED;
579
580 br13 = dst_pitch | (translate_raster_op(logic_op) << 16) | (1 << 29);
581 if (cpp == 2)
582 br13 |= BR13_565;
583 else
584 br13 |= BR13_8888;
585
586 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
587 OUT_BATCH(opcode);
588 OUT_BATCH(br13);
589 OUT_BATCH((0 << 16) | 0); /* clip x1, y1 */
590 OUT_BATCH((100 << 16) | 100); /* clip x2, y2 */
591 OUT_RELOC(dst_buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_WRITE, dst_offset);
592 OUT_BATCH(0); /* bg */
593 OUT_BATCH(fg_color); /* fg */
594 OUT_BATCH(0); /* pattern base addr */
595 ADVANCE_BATCH();
596
597 intel_batchbuffer_data( intel->batch,
598 &text,
599 sizeof(text),
600 INTEL_BATCH_NO_CLIPRECTS );
601
602 intel_batchbuffer_data( intel->batch,
603 src_bits,
604 dwords * 4,
605 INTEL_BATCH_NO_CLIPRECTS );
606 }
607