Merge git://proxy01.pd.intel.com:9419/git/mesa/mesa into crestline
[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( const __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, &intel->vbl_seq, intel->vblank_flags, & 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_SRC_COPY_BLT_WRITE_ALPHA |
114 XY_SRC_COPY_BLT_WRITE_RGB);
115 }
116
117 if (src->tiled) {
118 CMD |= XY_SRC_TILED;
119 src_pitch /= 4;
120 }
121
122 if (dst->tiled) {
123 CMD |= XY_DST_TILED;
124 dst_pitch /= 4;
125 }
126
127 for (i = 0 ; i < nbox; i++, pbox++)
128 {
129 drm_clip_rect_t tmp = *pbox;
130
131 if (rect) {
132 if (!intel_intersect_cliprects(&tmp, &tmp, rect))
133 continue;
134 }
135
136
137 if (tmp.x1 > tmp.x2 ||
138 tmp.y1 > tmp.y2 ||
139 tmp.x2 > intelScreen->width ||
140 tmp.y2 > intelScreen->height)
141 continue;
142
143 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
144 OUT_BATCH( CMD );
145 OUT_BATCH( dst_pitch | BR13 );
146 OUT_BATCH( (tmp.y1 << 16) | tmp.x1 );
147 OUT_BATCH( (tmp.y2 << 16) | tmp.x2 );
148 OUT_BATCH( bmBufferOffset(intel, dst->buffer) );
149 OUT_BATCH( (tmp.y1 << 16) | tmp.x1 );
150 OUT_BATCH( src_pitch );
151 OUT_BATCH( bmBufferOffset(intel, src->buffer) );
152 ADVANCE_BATCH();
153 }
154 }
155
156 intel_batchbuffer_flush( intel->batch );
157 intel->second_last_swap_fence = intel->last_swap_fence;
158 intel->last_swap_fence = bmSetFenceLock( intel );
159 UNLOCK_HARDWARE( intel );
160
161 if (!rect)
162 {
163 intel->swap_count++;
164 (*dri_interface->getUST)(&ust);
165 if (missed_target) {
166 intel->swap_missed_count++;
167 intel->swap_missed_ust = ust - intel->swap_ust;
168 }
169
170 intel->swap_ust = ust;
171 }
172
173 }
174
175
176
177
178 void intelEmitFillBlit( struct intel_context *intel,
179 GLuint cpp,
180 GLshort dst_pitch,
181 struct buffer *dst_buffer,
182 GLuint dst_offset,
183 GLboolean dst_tiled,
184 GLshort x, GLshort y,
185 GLshort w, GLshort h,
186 GLuint color )
187 {
188 GLuint BR13, CMD;
189 BATCH_LOCALS;
190
191 dst_pitch *= cpp;
192
193 switch(cpp) {
194 case 1:
195 case 2:
196 case 3:
197 BR13 = (0xF0 << 16) | (1<<24);
198 CMD = XY_COLOR_BLT_CMD;
199 break;
200 case 4:
201 BR13 = (0xF0 << 16) | (1<<24) | (1<<25);
202 CMD = (XY_COLOR_BLT_CMD | XY_COLOR_BLT_WRITE_ALPHA |
203 XY_COLOR_BLT_WRITE_RGB);
204 break;
205 default:
206 return;
207 }
208
209 if (dst_tiled) {
210 CMD |= XY_DST_TILED;
211 dst_pitch /= 4;
212 }
213
214 BEGIN_BATCH(6, INTEL_BATCH_NO_CLIPRECTS);
215 OUT_BATCH( CMD );
216 OUT_BATCH( dst_pitch | BR13 );
217 OUT_BATCH( (y << 16) | x );
218 OUT_BATCH( ((y+h) << 16) | (x+w) );
219 OUT_BATCH( bmBufferOffset(intel, dst_buffer) + dst_offset );
220 OUT_BATCH( color );
221 ADVANCE_BATCH();
222 }
223
224 static GLuint translate_raster_op(GLenum logicop)
225 {
226 switch(logicop) {
227 case GL_CLEAR: return 0x00;
228 case GL_AND: return 0x88;
229 case GL_AND_REVERSE: return 0x44;
230 case GL_COPY: return 0xCC;
231 case GL_AND_INVERTED: return 0x22;
232 case GL_NOOP: return 0xAA;
233 case GL_XOR: return 0x66;
234 case GL_OR: return 0xEE;
235 case GL_NOR: return 0x11;
236 case GL_EQUIV: return 0x99;
237 case GL_INVERT: return 0x55;
238 case GL_OR_REVERSE: return 0xDD;
239 case GL_COPY_INVERTED: return 0x33;
240 case GL_OR_INVERTED: return 0xBB;
241 case GL_NAND: return 0x77;
242 case GL_SET: return 0xFF;
243 default: return 0;
244 }
245 }
246
247
248 /* Copy BitBlt
249 */
250 void intelEmitCopyBlit( struct intel_context *intel,
251 GLuint cpp,
252 GLshort src_pitch,
253 struct buffer *src_buffer,
254 GLuint src_offset,
255 GLboolean src_tiled,
256 GLshort dst_pitch,
257 struct buffer *dst_buffer,
258 GLuint dst_offset,
259 GLboolean dst_tiled,
260 GLshort src_x, GLshort src_y,
261 GLshort dst_x, GLshort dst_y,
262 GLshort w, GLshort h,
263 GLenum logic_op )
264 {
265 GLuint CMD, BR13;
266 int dst_y2 = dst_y + h;
267 int dst_x2 = dst_x + w;
268 BATCH_LOCALS;
269
270
271 DBG("%s src:buf(%d)/%d %d,%d dst:buf(%d)/%d %d,%d sz:%dx%d op:%d\n",
272 __FUNCTION__,
273 src_buffer, src_pitch, src_x, src_y,
274 dst_buffer, dst_pitch, dst_x, dst_y,
275 w,h,logic_op);
276
277 assert( logic_op - GL_CLEAR >= 0 );
278 assert( logic_op - GL_CLEAR < 0x10 );
279
280 src_pitch *= cpp;
281 dst_pitch *= cpp;
282
283 switch(cpp) {
284 case 1:
285 case 2:
286 case 3:
287 BR13 = (translate_raster_op(logic_op) << 16) | (1<<24);
288 CMD = XY_SRC_COPY_BLT_CMD;
289 break;
290 case 4:
291 BR13 = (translate_raster_op(logic_op) << 16) | (1<<24) |
292 (1<<25);
293 CMD = (XY_SRC_COPY_BLT_CMD | XY_SRC_COPY_BLT_WRITE_ALPHA |
294 XY_SRC_COPY_BLT_WRITE_RGB);
295 break;
296 default:
297 return;
298 }
299
300 if (src_tiled) {
301 CMD |= XY_SRC_TILED;
302 src_pitch /= 4;
303 }
304
305 if (dst_tiled) {
306 CMD |= XY_DST_TILED;
307 dst_pitch /= 4;
308 }
309
310 if (dst_y2 < dst_y ||
311 dst_x2 < dst_x) {
312 return;
313 }
314
315 dst_pitch &= 0xffff;
316 src_pitch &= 0xffff;
317
318 /* Initial y values don't seem to work with negative pitches. If
319 * we adjust the offsets manually (below), it seems to work fine.
320 *
321 * On the other hand, if we always adjust, the hardware doesn't
322 * know which blit directions to use, so overlapping copypixels get
323 * the wrong result.
324 */
325 if (dst_pitch > 0 && src_pitch > 0) {
326 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
327 OUT_BATCH( CMD );
328 OUT_BATCH( dst_pitch | BR13 );
329 OUT_BATCH( (dst_y << 16) | dst_x );
330 OUT_BATCH( (dst_y2 << 16) | dst_x2 );
331 OUT_BATCH( bmBufferOffset(intel, dst_buffer) + dst_offset );
332 OUT_BATCH( (src_y << 16) | src_x );
333 OUT_BATCH( src_pitch );
334 OUT_BATCH( bmBufferOffset(intel, src_buffer) + src_offset );
335 ADVANCE_BATCH();
336 }
337 else {
338 BEGIN_BATCH(8, INTEL_BATCH_NO_CLIPRECTS);
339 OUT_BATCH( CMD );
340 OUT_BATCH( (dst_pitch & 0xffff) | BR13 );
341 OUT_BATCH( (0 << 16) | dst_x );
342 OUT_BATCH( (h << 16) | dst_x2 );
343 OUT_BATCH( bmBufferOffset(intel, dst_buffer) + dst_offset + dst_y * dst_pitch );
344 OUT_BATCH( (0 << 16) | src_x );
345 OUT_BATCH( (src_pitch & 0xffff) );
346 OUT_BATCH( bmBufferOffset(intel, src_buffer) + 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_COLOR_BLT_WRITE_ALPHA |
393 XY_COLOR_BLT_WRITE_RGB);
394 DEPTH_CMD = XY_COLOR_BLT_CMD;
395 if (flags & BUFFER_BIT_DEPTH) DEPTH_CMD |= XY_COLOR_BLT_WRITE_RGB;
396 if (flags & BUFFER_BIT_STENCIL) DEPTH_CMD |= XY_COLOR_BLT_WRITE_ALPHA;
397 break;
398 default:
399 return;
400 }
401
402
403
404 intelFlush( &intel->ctx );
405 LOCK_HARDWARE( intel );
406 {
407 /* get clear bounds after locking */
408 cx = ctx->DrawBuffer->_Xmin;
409 cy = ctx->DrawBuffer->_Ymin;
410 ch = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
411 cw = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
412 all = (cw == ctx->DrawBuffer->Width && ch == ctx->DrawBuffer->Height);
413
414 /* flip top to bottom */
415 cy = intel->driDrawable->h - cy - ch;
416 cx = cx + intel->drawX;
417 cy += intel->drawY;
418
419 /* adjust for page flipping */
420 if ( intel->sarea->pf_current_page == 0 ) {
421 front = intel->front_region;
422 back = intel->back_region;
423 }
424 else {
425 back = intel->front_region;
426 front = intel->back_region;
427 }
428
429 front_pitch = front->pitch * front->cpp;
430 back_pitch = back->pitch * back->cpp;
431 depth_pitch = depth->pitch * depth->cpp;
432
433 if (front->tiled) {
434 FRONT_CMD |= XY_DST_TILED;
435 front_pitch /= 4;
436 }
437
438 if (back->tiled) {
439 BACK_CMD |= XY_DST_TILED;
440 back_pitch /= 4;
441 }
442
443 if (depth->tiled) {
444 DEPTH_CMD |= XY_DST_TILED;
445 depth_pitch /= 4;
446 }
447
448 for (i = 0 ; i < intel->numClipRects ; i++)
449 {
450 drm_clip_rect_t *box = &intel->pClipRects[i];
451 drm_clip_rect_t b;
452
453 if (!all) {
454 GLint x = box->x1;
455 GLint y = box->y1;
456 GLint w = box->x2 - x;
457 GLint h = box->y2 - y;
458
459 if (x < cx) w -= cx - x, x = cx;
460 if (y < cy) h -= cy - y, y = cy;
461 if (x + w > cx + cw) w = cx + cw - x;
462 if (y + h > cy + ch) h = cy + ch - y;
463 if (w <= 0) continue;
464 if (h <= 0) continue;
465
466 b.x1 = x;
467 b.y1 = y;
468 b.x2 = x + w;
469 b.y2 = y + h;
470 } else {
471 b = *box;
472 }
473
474
475 if (b.x1 > b.x2 ||
476 b.y1 > b.y2 ||
477 b.x2 > intelScreen->width ||
478 b.y2 > intelScreen->height)
479 continue;
480
481 if ( flags & BUFFER_BIT_FRONT_LEFT ) {
482 BEGIN_BATCH(6, INTEL_BATCH_NO_CLIPRECTS);
483 OUT_BATCH( FRONT_CMD );
484 OUT_BATCH( front_pitch | BR13 );
485 OUT_BATCH( (b.y1 << 16) | b.x1 );
486 OUT_BATCH( (b.y2 << 16) | b.x2 );
487 OUT_BATCH( bmBufferOffset(intel, front->buffer) );
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_BATCH( bmBufferOffset(intel, back->buffer) );
499 OUT_BATCH( clear_color );
500 ADVANCE_BATCH();
501 }
502
503 if ( flags & (BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH) ) {
504 BEGIN_BATCH(6, INTEL_BATCH_NO_CLIPRECTS);
505 OUT_BATCH( DEPTH_CMD );
506 OUT_BATCH( depth_pitch | BR13 );
507 OUT_BATCH( (b.y1 << 16) | b.x1 );
508 OUT_BATCH( (b.y2 << 16) | b.x2 );
509 OUT_BATCH( bmBufferOffset(intel, depth->buffer) );
510 OUT_BATCH( clear_depth );
511 ADVANCE_BATCH();
512 }
513 }
514 }
515 intel_batchbuffer_flush( intel->batch );
516 UNLOCK_HARDWARE( intel );
517 }
518
519
520
521 #define BR13_565 0x1
522 #define BR13_8888 0x3
523
524
525 void
526 intelEmitImmediateColorExpandBlit(struct intel_context *intel,
527 GLuint cpp,
528 GLubyte *src_bits, GLuint src_size,
529 GLuint fg_color,
530 GLshort dst_pitch,
531 struct buffer *dst_buffer,
532 GLuint dst_offset,
533 GLboolean dst_tiled,
534 GLshort x, GLshort y,
535 GLshort w, GLshort h)
536 {
537 struct xy_setup_blit setup;
538 struct xy_text_immediate_blit text;
539 int dwords = ((src_size + 7) & ~7) / 4;
540
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(&setup, 0, sizeof(setup));
555
556 setup.br0.client = CLIENT_2D;
557 setup.br0.opcode = OPCODE_XY_SETUP_BLT;
558 setup.br0.write_alpha = (cpp == 4);
559 setup.br0.write_rgb = (cpp == 4);
560 setup.br0.dst_tiled = dst_tiled;
561 setup.br0.length = (sizeof(setup) / sizeof(int)) - 2;
562
563 setup.br13.dest_pitch = dst_pitch;
564 setup.br13.rop = 0xcc;
565 setup.br13.color_depth = (cpp == 4) ? BR13_8888 : BR13_565;
566 setup.br13.clipping_enable = 0;
567 setup.br13.mono_source_transparency = 1;
568
569 setup.dw2.clip_y1 = 0;
570 setup.dw2.clip_x1 = 0;
571 setup.dw3.clip_y2 = 100;
572 setup.dw3.clip_x2 = 100;
573
574 setup.dest_base_addr = bmBufferOffset(intel, dst_buffer) + dst_offset;
575 setup.background_color = 0;
576 setup.foreground_color = fg_color;
577 setup.pattern_base_addr = 0;
578
579 memset(&text, 0, sizeof(text));
580 text.dw0.client = CLIENT_2D;
581 text.dw0.opcode = OPCODE_XY_TEXT_IMMEDIATE_BLT;
582 text.dw0.pad0 = 0;
583 text.dw0.byte_packed = 1; /* ?maybe? */
584 text.dw0.pad1 = 0;
585 text.dw0.dst_tiled = dst_tiled;
586 text.dw0.pad2 = 0;
587 text.dw0.length = (sizeof(text)/sizeof(int)) - 2 + dwords;
588 text.dw1.dest_y1 = y; /* duplicates info in setup blit */
589 text.dw1.dest_x1 = x;
590 text.dw2.dest_y2 = y + h;
591 text.dw2.dest_x2 = x + w;
592
593 intel_batchbuffer_require_space( intel->batch,
594 sizeof(setup) +
595 sizeof(text) +
596 dwords,
597 INTEL_BATCH_NO_CLIPRECTS );
598
599 intel_batchbuffer_data( intel->batch,
600 &setup,
601 sizeof(setup),
602 INTEL_BATCH_NO_CLIPRECTS );
603
604 intel_batchbuffer_data( intel->batch,
605 &text,
606 sizeof(text),
607 INTEL_BATCH_NO_CLIPRECTS );
608
609 intel_batchbuffer_data( intel->batch,
610 src_bits,
611 dwords * 4,
612 INTEL_BATCH_NO_CLIPRECTS );
613 }
614