Merge branch 'gallium-edgeflags'
[mesa.git] / src / gallium / state_trackers / xorg / xorg_exa.c
1 /*
2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 *
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
28 *
29 */
30
31 #include "xorg_exa.h"
32 #include "xorg_tracker.h"
33 #include "xorg_composite.h"
34 #include "xorg_exa_tgsi.h"
35
36 #include <xorg-server.h>
37 #include <xf86.h>
38 #include <picturestr.h>
39 #include <picture.h>
40
41 #include "pipe/p_format.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_state.h"
44 #include "pipe/p_inlines.h"
45
46 #include "util/u_format.h"
47 #include "util/u_rect.h"
48 #include "util/u_math.h"
49 #include "util/u_debug.h"
50
51 #define DEBUG_PRINT 0
52 #define ROUND_UP_TEXTURES 1
53
54 /*
55 * Helper functions
56 */
57 struct render_format_str {
58 int format;
59 const char *name;
60 };
61 static const struct render_format_str formats_info[] =
62 {
63 {PICT_a8r8g8b8, "PICT_a8r8g8b8"},
64 {PICT_x8r8g8b8, "PICT_x8r8g8b8"},
65 {PICT_a8b8g8r8, "PICT_a8b8g8r8"},
66 {PICT_x8b8g8r8, "PICT_x8b8g8r8"},
67 #ifdef PICT_TYPE_BGRA
68 {PICT_b8g8r8a8, "PICT_b8g8r8a8"},
69 {PICT_b8g8r8x8, "PICT_b8g8r8x8"},
70 {PICT_a2r10g10b10, "PICT_a2r10g10b10"},
71 {PICT_x2r10g10b10, "PICT_x2r10g10b10"},
72 {PICT_a2b10g10r10, "PICT_a2b10g10r10"},
73 {PICT_x2b10g10r10, "PICT_x2b10g10r10"},
74 #endif
75 {PICT_r8g8b8, "PICT_r8g8b8"},
76 {PICT_b8g8r8, "PICT_b8g8r8"},
77 {PICT_r5g6b5, "PICT_r5g6b5"},
78 {PICT_b5g6r5, "PICT_b5g6r5"},
79 {PICT_a1r5g5b5, "PICT_a1r5g5b5"},
80 {PICT_x1r5g5b5, "PICT_x1r5g5b5"},
81 {PICT_a1b5g5r5, "PICT_a1b5g5r5"},
82 {PICT_x1b5g5r5, "PICT_x1b5g5r5"},
83 {PICT_a4r4g4b4, "PICT_a4r4g4b4"},
84 {PICT_x4r4g4b4, "PICT_x4r4g4b4"},
85 {PICT_a4b4g4r4, "PICT_a4b4g4r4"},
86 {PICT_x4b4g4r4, "PICT_x4b4g4r4"},
87 {PICT_a8, "PICT_a8"},
88 {PICT_r3g3b2, "PICT_r3g3b2"},
89 {PICT_b2g3r3, "PICT_b2g3r3"},
90 {PICT_a2r2g2b2, "PICT_a2r2g2b2"},
91 {PICT_a2b2g2r2, "PICT_a2b2g2r2"},
92 {PICT_c8, "PICT_c8"},
93 {PICT_g8, "PICT_g8"},
94 {PICT_x4a4, "PICT_x4a4"},
95 {PICT_x4c4, "PICT_x4c4"},
96 {PICT_x4g4, "PICT_x4g4"},
97 {PICT_a4, "PICT_a4"},
98 {PICT_r1g2b1, "PICT_r1g2b1"},
99 {PICT_b1g2r1, "PICT_b1g2r1"},
100 {PICT_a1r1g1b1, "PICT_a1r1g1b1"},
101 {PICT_a1b1g1r1, "PICT_a1b1g1r1"},
102 {PICT_c4, "PICT_c4"},
103 {PICT_g4, "PICT_g4"},
104 {PICT_a1, "PICT_a1"},
105 {PICT_g1, "PICT_g1"}
106 };
107 static const char *render_format_name(int format)
108 {
109 int i = 0;
110 for (i = 0; i < sizeof(formats_info)/sizeof(formats_info[0]); ++i) {
111 if (formats_info[i].format == format)
112 return formats_info[i].name;
113 }
114 return NULL;
115 }
116
117 static void
118 exa_get_pipe_format(int depth, enum pipe_format *format, int *bbp, int *picture_format)
119 {
120 switch (depth) {
121 case 32:
122 *format = PIPE_FORMAT_A8R8G8B8_UNORM;
123 *picture_format = PICT_a8r8g8b8;
124 assert(*bbp == 32);
125 break;
126 case 24:
127 *format = PIPE_FORMAT_X8R8G8B8_UNORM;
128 *picture_format = PICT_x8r8g8b8;
129 assert(*bbp == 32);
130 break;
131 case 16:
132 *format = PIPE_FORMAT_R5G6B5_UNORM;
133 *picture_format = PICT_r5g6b5;
134 assert(*bbp == 16);
135 break;
136 case 15:
137 *format = PIPE_FORMAT_A1R5G5B5_UNORM;
138 *picture_format = PICT_x1r5g5b5;
139 assert(*bbp == 16);
140 break;
141 case 8:
142 *format = PIPE_FORMAT_L8_UNORM;
143 *picture_format = PICT_a8;
144 assert(*bbp == 8);
145 break;
146 case 4:
147 case 1:
148 *format = PIPE_FORMAT_A8R8G8B8_UNORM; /* bad bad bad */
149 break;
150 default:
151 assert(0);
152 break;
153 }
154 }
155
156
157 /*
158 * Static exported EXA functions
159 */
160
161 static void
162 ExaWaitMarker(ScreenPtr pScreen, int marker)
163 {
164 /* Nothing to do, handled in the PrepareAccess hook */
165 }
166
167 static int
168 ExaMarkSync(ScreenPtr pScreen)
169 {
170 return 1;
171 }
172
173
174 /***********************************************************************
175 * Screen upload/download
176 */
177
178 static Bool
179 ExaDownloadFromScreen(PixmapPtr pPix, int x, int y, int w, int h, char *dst,
180 int dst_pitch)
181 {
182 ScreenPtr pScreen = pPix->drawable.pScreen;
183 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
184 modesettingPtr ms = modesettingPTR(pScrn);
185 struct exa_context *exa = ms->exa;
186 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPix);
187 struct pipe_transfer *transfer;
188
189 if (!priv || !priv->tex)
190 return FALSE;
191
192 if (exa->pipe->is_texture_referenced(exa->pipe, priv->tex, 0, 0) &
193 PIPE_REFERENCED_FOR_WRITE)
194 exa->pipe->flush(exa->pipe, 0, NULL);
195
196 transfer = exa->scrn->get_tex_transfer(exa->scrn, priv->tex, 0, 0, 0,
197 PIPE_TRANSFER_READ, x, y, w, h);
198 if (!transfer)
199 return FALSE;
200
201 #if DEBUG_PRINT
202 debug_printf("------ ExaDownloadFromScreen(%d, %d, %d, %d, %d)\n",
203 x, y, w, h, dst_pitch);
204 #endif
205
206 util_copy_rect((unsigned char*)dst, priv->tex->format, dst_pitch, 0, 0,
207 w, h, exa->scrn->transfer_map(exa->scrn, transfer),
208 transfer->stride, 0, 0);
209
210 exa->scrn->transfer_unmap(exa->scrn, transfer);
211 exa->scrn->tex_transfer_destroy(transfer);
212
213 return TRUE;
214 }
215
216 static Bool
217 ExaUploadToScreen(PixmapPtr pPix, int x, int y, int w, int h, char *src,
218 int src_pitch)
219 {
220 ScreenPtr pScreen = pPix->drawable.pScreen;
221 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
222 modesettingPtr ms = modesettingPTR(pScrn);
223 struct exa_context *exa = ms->exa;
224 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPix);
225 struct pipe_transfer *transfer;
226
227 if (!priv || !priv->tex)
228 return FALSE;
229
230 /* make sure that any pending operations are flushed to hardware */
231 if (exa->pipe->is_texture_referenced(exa->pipe, priv->tex, 0, 0) &
232 (PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE))
233 xorg_exa_flush(exa, 0, NULL);
234
235 transfer = exa->scrn->get_tex_transfer(exa->scrn, priv->tex, 0, 0, 0,
236 PIPE_TRANSFER_WRITE, x, y, w, h);
237 if (!transfer)
238 return FALSE;
239
240 #if DEBUG_PRINT
241 debug_printf("++++++ ExaUploadToScreen(%d, %d, %d, %d, %d)\n",
242 x, y, w, h, src_pitch);
243 #endif
244
245 util_copy_rect(exa->scrn->transfer_map(exa->scrn, transfer),
246 priv->tex->format, transfer->stride, 0, 0, w, h,
247 (unsigned char*)src, src_pitch, 0, 0);
248
249 exa->scrn->transfer_unmap(exa->scrn, transfer);
250 exa->scrn->tex_transfer_destroy(transfer);
251
252 return TRUE;
253 }
254
255 static Bool
256 ExaPrepareAccess(PixmapPtr pPix, int index)
257 {
258 ScreenPtr pScreen = pPix->drawable.pScreen;
259 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
260 modesettingPtr ms = modesettingPTR(pScrn);
261 struct exa_context *exa = ms->exa;
262 struct exa_pixmap_priv *priv;
263
264 priv = exaGetPixmapDriverPrivate(pPix);
265
266 if (!priv)
267 return FALSE;
268
269 if (!priv->tex)
270 return FALSE;
271
272 if (priv->map_count == 0)
273 {
274 if (exa->pipe->is_texture_referenced(exa->pipe, priv->tex, 0, 0) &
275 PIPE_REFERENCED_FOR_WRITE)
276 exa->pipe->flush(exa->pipe, 0, NULL);
277
278 assert(pPix->drawable.width <= priv->tex->width0);
279 assert(pPix->drawable.height <= priv->tex->height0);
280
281 priv->map_transfer =
282 exa->scrn->get_tex_transfer(exa->scrn, priv->tex, 0, 0, 0,
283 #ifdef EXA_MIXED_PIXMAPS
284 PIPE_TRANSFER_MAP_DIRECTLY |
285 #endif
286 PIPE_TRANSFER_READ_WRITE,
287 0, 0,
288 pPix->drawable.width,
289 pPix->drawable.height );
290 if (!priv->map_transfer)
291 #ifdef EXA_MIXED_PIXMAPS
292 return FALSE;
293 #else
294 FatalError("failed to create transfer\n");
295 #endif
296
297 pPix->devPrivate.ptr =
298 exa->scrn->transfer_map(exa->scrn, priv->map_transfer);
299 pPix->devKind = priv->map_transfer->stride;
300 }
301
302 priv->map_count++;
303
304 return TRUE;
305 }
306
307 static void
308 ExaFinishAccess(PixmapPtr pPix, int index)
309 {
310 ScreenPtr pScreen = pPix->drawable.pScreen;
311 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
312 modesettingPtr ms = modesettingPTR(pScrn);
313 struct exa_context *exa = ms->exa;
314 struct exa_pixmap_priv *priv;
315 priv = exaGetPixmapDriverPrivate(pPix);
316
317 if (!priv)
318 return;
319
320 if (!priv->map_transfer)
321 return;
322
323 if (--priv->map_count == 0) {
324 assert(priv->map_transfer);
325 exa->scrn->transfer_unmap(exa->scrn, priv->map_transfer);
326 exa->scrn->tex_transfer_destroy(priv->map_transfer);
327 priv->map_transfer = NULL;
328 pPix->devPrivate.ptr = NULL;
329 }
330 }
331
332 /***********************************************************************
333 * Solid Fills
334 */
335
336 static Bool
337 ExaPrepareSolid(PixmapPtr pPixmap, int alu, Pixel planeMask, Pixel fg)
338 {
339 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
340 modesettingPtr ms = modesettingPTR(pScrn);
341 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
342 struct exa_context *exa = ms->exa;
343
344 #if DEBUG_PRINT
345 debug_printf("ExaPrepareSolid(0x%x)\n", fg);
346 #endif
347 if (!exa->accel)
348 return FALSE;
349
350 if (!exa->pipe)
351 XORG_FALLBACK("accle not enabled");
352
353 if (!priv || !priv->tex)
354 XORG_FALLBACK("%s", !priv ? "!priv" : "!priv->tex");
355
356 if (!EXA_PM_IS_SOLID(&pPixmap->drawable, planeMask))
357 XORG_FALLBACK("planeMask is not solid");
358
359 if (alu != GXcopy)
360 XORG_FALLBACK("not GXcopy");
361
362 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
363 priv->tex->target,
364 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
365 XORG_FALLBACK("format %s", pf_name(priv->tex->format));
366 }
367
368 return xorg_solid_bind_state(exa, priv, fg);
369 }
370
371 static void
372 ExaSolid(PixmapPtr pPixmap, int x0, int y0, int x1, int y1)
373 {
374 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
375 modesettingPtr ms = modesettingPTR(pScrn);
376 struct exa_context *exa = ms->exa;
377 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
378
379 #if DEBUG_PRINT
380 debug_printf("\tExaSolid(%d, %d, %d, %d)\n", x0, y0, x1, y1);
381 #endif
382
383 if (x0 == 0 && y0 == 0 &&
384 x1 == pPixmap->drawable.width && y1 == pPixmap->drawable.height) {
385 exa->pipe->clear(exa->pipe, PIPE_CLEAR_COLOR, exa->solid_color, 0.0, 0);
386 return;
387 }
388
389 xorg_solid(exa, priv, x0, y0, x1, y1) ;
390 }
391
392
393 static void
394 ExaDoneSolid(PixmapPtr pPixmap)
395 {
396 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
397 modesettingPtr ms = modesettingPTR(pScrn);
398 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
399 struct exa_context *exa = ms->exa;
400
401 if (!priv)
402 return;
403
404 xorg_composite_done(exa);
405 }
406
407 /***********************************************************************
408 * Copy Blits
409 */
410
411 static Bool
412 ExaPrepareCopy(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap, int xdir,
413 int ydir, int alu, Pixel planeMask)
414 {
415 ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
416 modesettingPtr ms = modesettingPTR(pScrn);
417 struct exa_context *exa = ms->exa;
418 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
419 struct exa_pixmap_priv *src_priv = exaGetPixmapDriverPrivate(pSrcPixmap);
420
421 #if DEBUG_PRINT
422 debug_printf("ExaPrepareCopy\n");
423 #endif
424
425 if (!exa->accel)
426 return FALSE;
427
428 if (!exa->pipe)
429 XORG_FALLBACK("accle not enabled");
430
431 if (!priv || !priv->tex)
432 XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
433
434 if (!src_priv || !src_priv->tex)
435 XORG_FALLBACK("pSrc %s", !src_priv ? "!priv" : "!priv->tex");
436
437 if (!EXA_PM_IS_SOLID(&pSrcPixmap->drawable, planeMask))
438 XORG_FALLBACK("planeMask is not solid");
439
440 if (alu != GXcopy)
441 XORG_FALLBACK("alu not GXcopy");
442
443 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
444 priv->tex->target,
445 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
446 XORG_FALLBACK("pDst format %s", pf_name(priv->tex->format));
447
448 if (!exa->scrn->is_format_supported(exa->scrn, src_priv->tex->format,
449 src_priv->tex->target,
450 PIPE_TEXTURE_USAGE_SAMPLER, 0))
451 XORG_FALLBACK("pSrc format %s", pf_name(src_priv->tex->format));
452
453 exa->copy.src = src_priv;
454 exa->copy.dst = priv;
455
456 /* For same-surface copies, the pipe->surface_copy path is clearly
457 * superior, providing it is implemented. In other cases it's not
458 * clear what the better path would be, and eventually we'd
459 * probably want to gather timings and choose dynamically.
460 */
461 if (exa->pipe->surface_copy &&
462 exa->copy.src == exa->copy.dst) {
463
464 exa->copy.use_surface_copy = TRUE;
465
466 exa->copy.src_surface =
467 exa->scrn->get_tex_surface( exa->scrn,
468 exa->copy.src->tex,
469 0, 0, 0,
470 PIPE_BUFFER_USAGE_GPU_READ);
471
472 exa->copy.dst_surface =
473 exa->scrn->get_tex_surface( exa->scrn,
474 exa->copy.dst->tex,
475 0, 0, 0,
476 PIPE_BUFFER_USAGE_GPU_WRITE );
477 }
478 else {
479 exa->copy.use_surface_copy = FALSE;
480
481 if (exa->copy.dst == exa->copy.src)
482 exa->copy.src_texture = renderer_clone_texture( exa->renderer,
483 exa->copy.src->tex );
484 else
485 pipe_texture_reference(&exa->copy.src_texture,
486 exa->copy.src->tex);
487
488 exa->copy.dst_surface =
489 exa->scrn->get_tex_surface(exa->scrn,
490 exa->copy.dst->tex,
491 0, 0, 0,
492 PIPE_BUFFER_USAGE_GPU_WRITE);
493
494
495 renderer_copy_prepare(exa->renderer,
496 exa->copy.dst_surface,
497 exa->copy.src_texture );
498 }
499
500
501 return TRUE;
502 }
503
504 static void
505 ExaCopy(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX, int dstY,
506 int width, int height)
507 {
508 ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
509 modesettingPtr ms = modesettingPTR(pScrn);
510 struct exa_context *exa = ms->exa;
511 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
512
513 #if DEBUG_PRINT
514 debug_printf("\tExaCopy(srcx=%d, srcy=%d, dstX=%d, dstY=%d, w=%d, h=%d)\n",
515 srcX, srcY, dstX, dstY, width, height);
516 #endif
517
518 debug_assert(priv == exa->copy.dst);
519
520 if (exa->copy.use_surface_copy) {
521 /* XXX: consider exposing >1 box in surface_copy interface.
522 */
523 exa->pipe->surface_copy( exa->pipe,
524 exa->copy.dst_surface,
525 dstX, dstY,
526 exa->copy.src_surface,
527 srcX, srcY,
528 width, height );
529 }
530 else {
531 renderer_copy_pixmap(exa->renderer,
532 dstX, dstY,
533 srcX, srcY,
534 width, height,
535 exa->copy.src_texture->width0,
536 exa->copy.src_texture->height0);
537 }
538 }
539
540 static void
541 ExaDoneCopy(PixmapPtr pPixmap)
542 {
543 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
544 modesettingPtr ms = modesettingPTR(pScrn);
545 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
546 struct exa_context *exa = ms->exa;
547
548 if (!priv)
549 return;
550
551 renderer_draw_flush(exa->renderer);
552
553 exa->copy.src = NULL;
554 exa->copy.dst = NULL;
555 pipe_surface_reference(&exa->copy.src_surface, NULL);
556 pipe_surface_reference(&exa->copy.dst_surface, NULL);
557 pipe_texture_reference(&exa->copy.src_texture, NULL);
558 }
559
560
561
562 static Bool
563 picture_check_formats(struct exa_pixmap_priv *pSrc, PicturePtr pSrcPicture)
564 {
565 if (pSrc->picture_format == pSrcPicture->format)
566 return TRUE;
567
568 if (pSrc->picture_format != PICT_a8r8g8b8)
569 return FALSE;
570
571 /* pSrc->picture_format == PICT_a8r8g8b8 */
572 switch (pSrcPicture->format) {
573 case PICT_a8r8g8b8:
574 case PICT_x8r8g8b8:
575 case PICT_a8b8g8r8:
576 case PICT_x8b8g8r8:
577 /* just treat these two as x8... */
578 case PICT_r8g8b8:
579 case PICT_b8g8r8:
580 return TRUE;
581 #ifdef PICT_TYPE_BGRA
582 case PICT_b8g8r8a8:
583 case PICT_b8g8r8x8:
584 return FALSE; /* does not support swizzleing the alpha channel yet */
585 case PICT_a2r10g10b10:
586 case PICT_x2r10g10b10:
587 case PICT_a2b10g10r10:
588 case PICT_x2b10g10r10:
589 return FALSE;
590 #endif
591 default:
592 return FALSE;
593 }
594 return FALSE;
595 }
596
597 /***********************************************************************
598 * Composite entrypoints
599 */
600
601 static Bool
602 ExaCheckComposite(int op,
603 PicturePtr pSrcPicture, PicturePtr pMaskPicture,
604 PicturePtr pDstPicture)
605 {
606 ScrnInfoPtr pScrn = xf86Screens[pDstPicture->pDrawable->pScreen->myNum];
607 modesettingPtr ms = modesettingPTR(pScrn);
608 struct exa_context *exa = ms->exa;
609
610 #if DEBUG_PRINT
611 debug_printf("ExaCheckComposite(%d, %p, %p, %p) = %d\n",
612 op, pSrcPicture, pMaskPicture, pDstPicture, accelerated);
613 #endif
614
615 if (!exa->accel)
616 return FALSE;
617
618 return xorg_composite_accelerated(op,
619 pSrcPicture,
620 pMaskPicture,
621 pDstPicture);
622 }
623
624
625 static Bool
626 ExaPrepareComposite(int op, PicturePtr pSrcPicture,
627 PicturePtr pMaskPicture, PicturePtr pDstPicture,
628 PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
629 {
630 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
631 modesettingPtr ms = modesettingPTR(pScrn);
632 struct exa_context *exa = ms->exa;
633 struct exa_pixmap_priv *priv;
634
635 if (!exa->accel)
636 return FALSE;
637
638 #if DEBUG_PRINT
639 debug_printf("ExaPrepareComposite(%d, src=0x%p, mask=0x%p, dst=0x%p)\n",
640 op, pSrcPicture, pMaskPicture, pDstPicture);
641 debug_printf("\tFormats: src(%s), mask(%s), dst(%s)\n",
642 pSrcPicture ? render_format_name(pSrcPicture->format) : "none",
643 pMaskPicture ? render_format_name(pMaskPicture->format) : "none",
644 pDstPicture ? render_format_name(pDstPicture->format) : "none");
645 #endif
646 if (!exa->pipe)
647 XORG_FALLBACK("accle not enabled");
648
649 priv = exaGetPixmapDriverPrivate(pDst);
650 if (!priv || !priv->tex)
651 XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
652
653 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
654 priv->tex->target,
655 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
656 XORG_FALLBACK("pDst format: %s", pf_name(priv->tex->format));
657
658 if (priv->picture_format != pDstPicture->format)
659 XORG_FALLBACK("pDst pic_format: %s != %s",
660 render_format_name(priv->picture_format),
661 render_format_name(pDstPicture->format));
662
663 if (pSrc) {
664 priv = exaGetPixmapDriverPrivate(pSrc);
665 if (!priv || !priv->tex)
666 XORG_FALLBACK("pSrc %s", !priv ? "!priv" : "!priv->tex");
667
668 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
669 priv->tex->target,
670 PIPE_TEXTURE_USAGE_SAMPLER, 0))
671 XORG_FALLBACK("pSrc format: %s", pf_name(priv->tex->format));
672
673 if (!picture_check_formats(priv, pSrcPicture))
674 XORG_FALLBACK("pSrc pic_format: %s != %s",
675 render_format_name(priv->picture_format),
676 render_format_name(pSrcPicture->format));
677
678 }
679
680 if (pMask) {
681 priv = exaGetPixmapDriverPrivate(pMask);
682 if (!priv || !priv->tex)
683 XORG_FALLBACK("pMask %s", !priv ? "!priv" : "!priv->tex");
684
685 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
686 priv->tex->target,
687 PIPE_TEXTURE_USAGE_SAMPLER, 0))
688 XORG_FALLBACK("pMask format: %s", pf_name(priv->tex->format));
689
690 if (!picture_check_formats(priv, pMaskPicture))
691 XORG_FALLBACK("pMask pic_format: %s != %s",
692 render_format_name(priv->picture_format),
693 render_format_name(pMaskPicture->format));
694 }
695
696 return xorg_composite_bind_state(exa, op, pSrcPicture, pMaskPicture,
697 pDstPicture,
698 pSrc ? exaGetPixmapDriverPrivate(pSrc) : NULL,
699 pMask ? exaGetPixmapDriverPrivate(pMask) : NULL,
700 exaGetPixmapDriverPrivate(pDst));
701 }
702
703 static void
704 ExaComposite(PixmapPtr pDst, int srcX, int srcY, int maskX, int maskY,
705 int dstX, int dstY, int width, int height)
706 {
707 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
708 modesettingPtr ms = modesettingPTR(pScrn);
709 struct exa_context *exa = ms->exa;
710 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDst);
711
712 #if DEBUG_PRINT
713 debug_printf("\tExaComposite(src[%d,%d], mask=[%d, %d], dst=[%d, %d], dim=[%d, %d])\n",
714 srcX, srcY, maskX, maskY, dstX, dstY, width, height);
715 debug_printf("\t Num bound samplers = %d\n",
716 exa->num_bound_samplers);
717 #endif
718
719 xorg_composite(exa, priv, srcX, srcY, maskX, maskY,
720 dstX, dstY, width, height);
721 }
722
723
724
725 static void
726 ExaDoneComposite(PixmapPtr pPixmap)
727 {
728 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
729 modesettingPtr ms = modesettingPTR(pScrn);
730 struct exa_context *exa = ms->exa;
731
732 xorg_composite_done(exa);
733 }
734
735
736 /***********************************************************************
737 * Pixmaps
738 */
739
740 static void *
741 ExaCreatePixmap(ScreenPtr pScreen, int size, int align)
742 {
743 struct exa_pixmap_priv *priv;
744
745 priv = xcalloc(1, sizeof(struct exa_pixmap_priv));
746 if (!priv)
747 return NULL;
748
749 return priv;
750 }
751
752 static void
753 ExaDestroyPixmap(ScreenPtr pScreen, void *dPriv)
754 {
755 struct exa_pixmap_priv *priv = (struct exa_pixmap_priv *)dPriv;
756
757 if (!priv)
758 return;
759
760 pipe_texture_reference(&priv->tex, NULL);
761
762 xfree(priv);
763 }
764
765 static Bool
766 ExaPixmapIsOffscreen(PixmapPtr pPixmap)
767 {
768 struct exa_pixmap_priv *priv;
769
770 priv = exaGetPixmapDriverPrivate(pPixmap);
771
772 if (!priv)
773 return FALSE;
774
775 if (priv->tex)
776 return TRUE;
777
778 return FALSE;
779 }
780
781 int
782 xorg_exa_set_displayed_usage(PixmapPtr pPixmap)
783 {
784 struct exa_pixmap_priv *priv;
785 priv = exaGetPixmapDriverPrivate(pPixmap);
786
787 if (!priv) {
788 FatalError("NO PIXMAP PRIVATE\n");
789 return 0;
790 }
791
792 priv->flags |= PIPE_TEXTURE_USAGE_PRIMARY;
793
794 return 0;
795 }
796
797 int
798 xorg_exa_set_shared_usage(PixmapPtr pPixmap)
799 {
800 struct exa_pixmap_priv *priv;
801 priv = exaGetPixmapDriverPrivate(pPixmap);
802
803 if (!priv) {
804 FatalError("NO PIXMAP PRIVATE\n");
805 return 0;
806 }
807
808 priv->flags |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
809
810 return 0;
811 }
812
813 unsigned
814 xorg_exa_get_pixmap_handle(PixmapPtr pPixmap, unsigned *stride_out)
815 {
816 ScreenPtr pScreen = pPixmap->drawable.pScreen;
817 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
818 modesettingPtr ms = modesettingPTR(pScrn);
819 struct exa_pixmap_priv *priv;
820 unsigned handle;
821 unsigned stride;
822
823 if (!ms->exa) {
824 FatalError("NO MS->EXA\n");
825 return 0;
826 }
827
828 priv = exaGetPixmapDriverPrivate(pPixmap);
829
830 if (!priv) {
831 FatalError("NO PIXMAP PRIVATE\n");
832 return 0;
833 }
834
835 ms->api->local_handle_from_texture(ms->api, ms->screen, priv->tex, &stride, &handle);
836 if (stride_out)
837 *stride_out = stride;
838
839 return handle;
840 }
841
842 static Bool
843 size_match( int width, int tex_width )
844 {
845 #if ROUND_UP_TEXTURES
846 if (width > tex_width)
847 return FALSE;
848
849 if (width * 2 < tex_width)
850 return FALSE;
851
852 return TRUE;
853 #else
854 return width == tex_width;
855 #endif
856 }
857
858 static Bool
859 ExaModifyPixmapHeader(PixmapPtr pPixmap, int width, int height,
860 int depth, int bitsPerPixel, int devKind,
861 pointer pPixData)
862 {
863 ScreenPtr pScreen = pPixmap->drawable.pScreen;
864 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
865 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
866 modesettingPtr ms = modesettingPTR(pScrn);
867 struct exa_context *exa = ms->exa;
868
869 if (!priv || pPixData)
870 return FALSE;
871
872 if (0) {
873 debug_printf("%s pixmap %p sz %dx%dx%d devKind %d\n",
874 __FUNCTION__, pPixmap, width, height, bitsPerPixel, devKind);
875
876 if (priv->tex)
877 debug_printf(" ==> old texture %dx%d\n",
878 priv->tex->width0,
879 priv->tex->height0);
880 }
881
882
883 if (depth <= 0)
884 depth = pPixmap->drawable.depth;
885
886 if (bitsPerPixel <= 0)
887 bitsPerPixel = pPixmap->drawable.bitsPerPixel;
888
889 if (width <= 0)
890 width = pPixmap->drawable.width;
891
892 if (height <= 0)
893 height = pPixmap->drawable.height;
894
895 if (width <= 0 || height <= 0 || depth <= 0)
896 return FALSE;
897
898 miModifyPixmapHeader(pPixmap, width, height, depth,
899 bitsPerPixel, devKind, NULL);
900
901 priv->width = width;
902 priv->height = height;
903
904 /* Deal with screen resize */
905 if ((exa->accel || priv->flags) &&
906 (!priv->tex ||
907 !size_match(width, priv->tex->width0) ||
908 !size_match(height, priv->tex->height0) ||
909 priv->tex_flags != priv->flags)) {
910 struct pipe_texture *texture = NULL;
911 struct pipe_texture template;
912
913 memset(&template, 0, sizeof(template));
914 template.target = PIPE_TEXTURE_2D;
915 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &priv->picture_format);
916 if (ROUND_UP_TEXTURES && priv->flags == 0) {
917 template.width0 = util_next_power_of_two(width);
918 template.height0 = util_next_power_of_two(height);
919 }
920 else {
921 template.width0 = width;
922 template.height0 = height;
923 }
924
925 template.depth0 = 1;
926 template.last_level = 0;
927 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET | priv->flags;
928 priv->tex_flags = priv->flags;
929 texture = exa->scrn->texture_create(exa->scrn, &template);
930
931 if (priv->tex) {
932 struct pipe_surface *dst_surf;
933 struct pipe_surface *src_surf;
934
935 dst_surf = exa->scrn->get_tex_surface(
936 exa->scrn, texture, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
937 src_surf = xorg_gpu_surface(exa->pipe->screen, priv);
938 if (exa->pipe->surface_copy) {
939 exa->pipe->surface_copy(exa->pipe, dst_surf, 0, 0, src_surf,
940 0, 0, min(width, texture->width0),
941 min(height, texture->height0));
942 } else {
943 util_surface_copy(exa->pipe, FALSE, dst_surf, 0, 0, src_surf,
944 0, 0, min(width, texture->width0),
945 min(height, texture->height0));
946 }
947 exa->scrn->tex_surface_destroy(dst_surf);
948 exa->scrn->tex_surface_destroy(src_surf);
949 }
950
951 pipe_texture_reference(&priv->tex, texture);
952 /* the texture we create has one reference */
953 pipe_texture_reference(&texture, NULL);
954 }
955
956 return TRUE;
957 }
958
959 struct pipe_texture *
960 xorg_exa_get_texture(PixmapPtr pPixmap)
961 {
962 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
963 struct pipe_texture *tex = NULL;
964 pipe_texture_reference(&tex, priv->tex);
965 return tex;
966 }
967
968 Bool
969 xorg_exa_set_texture(PixmapPtr pPixmap, struct pipe_texture *tex)
970 {
971 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
972
973 int mask = PIPE_TEXTURE_USAGE_PRIMARY | PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
974
975 if (!priv)
976 return FALSE;
977
978 if (pPixmap->drawable.width != tex->width0 ||
979 pPixmap->drawable.height != tex->height0)
980 return FALSE;
981
982 pipe_texture_reference(&priv->tex, tex);
983 priv->tex_flags = tex->tex_usage & mask;
984
985 return TRUE;
986 }
987
988 struct pipe_texture *
989 xorg_exa_create_root_texture(ScrnInfoPtr pScrn,
990 int width, int height,
991 int depth, int bitsPerPixel)
992 {
993 modesettingPtr ms = modesettingPTR(pScrn);
994 struct exa_context *exa = ms->exa;
995 struct pipe_texture template;
996 int dummy;
997
998 memset(&template, 0, sizeof(template));
999 template.target = PIPE_TEXTURE_2D;
1000 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &dummy);
1001 template.width0 = width;
1002 template.height0 = height;
1003 template.depth0 = 1;
1004 template.last_level = 0;
1005 template.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
1006 template.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
1007 template.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
1008
1009 return exa->scrn->texture_create(exa->scrn, &template);
1010 }
1011
1012 void
1013 xorg_exa_close(ScrnInfoPtr pScrn)
1014 {
1015 modesettingPtr ms = modesettingPTR(pScrn);
1016 struct exa_context *exa = ms->exa;
1017
1018 renderer_destroy(exa->renderer);
1019
1020 if (exa->pipe)
1021 exa->pipe->destroy(exa->pipe);
1022
1023 exaDriverFini(pScrn->pScreen);
1024 xfree(exa);
1025 ms->exa = NULL;
1026 }
1027
1028 void *
1029 xorg_exa_init(ScrnInfoPtr pScrn, Bool accel)
1030 {
1031 modesettingPtr ms = modesettingPTR(pScrn);
1032 struct exa_context *exa;
1033 ExaDriverPtr pExa;
1034
1035 exa = xcalloc(1, sizeof(struct exa_context));
1036 if (!exa)
1037 return NULL;
1038
1039 pExa = exaDriverAlloc();
1040 if (!pExa) {
1041 goto out_err;
1042 }
1043
1044 memset(pExa, 0, sizeof(*pExa));
1045
1046 pExa->exa_major = 2;
1047 pExa->exa_minor = 2;
1048 pExa->memoryBase = 0;
1049 pExa->memorySize = 0;
1050 pExa->offScreenBase = 0;
1051 pExa->pixmapOffsetAlign = 0;
1052 pExa->pixmapPitchAlign = 1;
1053 pExa->flags = EXA_OFFSCREEN_PIXMAPS | EXA_HANDLES_PIXMAPS;
1054 #ifdef EXA_SUPPORTS_PREPARE_AUX
1055 pExa->flags |= EXA_SUPPORTS_PREPARE_AUX;
1056 #endif
1057 #ifdef EXA_MIXED_PIXMAPS
1058 pExa->flags |= EXA_MIXED_PIXMAPS;
1059 #endif
1060 pExa->maxX = 8191; /* FIXME */
1061 pExa->maxY = 8191; /* FIXME */
1062
1063 pExa->WaitMarker = ExaWaitMarker;
1064 pExa->MarkSync = ExaMarkSync;
1065 pExa->PrepareSolid = ExaPrepareSolid;
1066 pExa->Solid = ExaSolid;
1067 pExa->DoneSolid = ExaDoneSolid;
1068 pExa->PrepareCopy = ExaPrepareCopy;
1069 pExa->Copy = ExaCopy;
1070 pExa->DoneCopy = ExaDoneCopy;
1071 pExa->CheckComposite = ExaCheckComposite;
1072 pExa->PrepareComposite = ExaPrepareComposite;
1073 pExa->Composite = ExaComposite;
1074 pExa->DoneComposite = ExaDoneComposite;
1075 pExa->PixmapIsOffscreen = ExaPixmapIsOffscreen;
1076 pExa->DownloadFromScreen = ExaDownloadFromScreen;
1077 pExa->UploadToScreen = ExaUploadToScreen;
1078 pExa->PrepareAccess = ExaPrepareAccess;
1079 pExa->FinishAccess = ExaFinishAccess;
1080 pExa->CreatePixmap = ExaCreatePixmap;
1081 pExa->DestroyPixmap = ExaDestroyPixmap;
1082 pExa->ModifyPixmapHeader = ExaModifyPixmapHeader;
1083
1084 if (!exaDriverInit(pScrn->pScreen, pExa)) {
1085 goto out_err;
1086 }
1087
1088 exa->scrn = ms->screen;
1089 exa->pipe = ms->api->create_context(ms->api, exa->scrn);
1090 /* Share context with DRI */
1091 ms->ctx = exa->pipe;
1092
1093 exa->renderer = renderer_create(exa->pipe);
1094 exa->accel = accel;
1095
1096 return (void *)exa;
1097
1098 out_err:
1099 xorg_exa_close(pScrn);
1100
1101 return NULL;
1102 }
1103
1104 struct pipe_surface *
1105 xorg_gpu_surface(struct pipe_screen *scrn, struct exa_pixmap_priv *priv)
1106 {
1107 return scrn->get_tex_surface(scrn, priv->tex, 0, 0, 0,
1108 PIPE_BUFFER_USAGE_GPU_READ |
1109 PIPE_BUFFER_USAGE_GPU_WRITE);
1110
1111 }
1112
1113 void xorg_exa_flush(struct exa_context *exa, uint pipeFlushFlags,
1114 struct pipe_fence_handle **fence)
1115 {
1116 exa->pipe->flush(exa->pipe, pipeFlushFlags, fence);
1117 }
1118
1119 void xorg_exa_finish(struct exa_context *exa)
1120 {
1121 struct pipe_fence_handle *fence = NULL;
1122
1123 xorg_exa_flush(exa, PIPE_FLUSH_RENDER_CACHE, &fence);
1124
1125 exa->pipe->screen->fence_finish(exa->pipe->screen, fence, 0);
1126 exa->pipe->screen->fence_reference(exa->pipe->screen, &fence, NULL);
1127 }
1128