Merge branch 'pipe-format-simplify'
[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->pipe)
348 XORG_FALLBACK("accle not enabled");
349
350 if (!priv || !priv->tex)
351 XORG_FALLBACK("%s", !priv ? "!priv" : "!priv->tex");
352
353 if (!EXA_PM_IS_SOLID(&pPixmap->drawable, planeMask))
354 XORG_FALLBACK("planeMask is not solid");
355
356 if (alu != GXcopy)
357 XORG_FALLBACK("not GXcopy");
358
359 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
360 priv->tex->target,
361 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
362 XORG_FALLBACK("format %s", pf_name(priv->tex->format));
363 }
364
365 return exa->accel && xorg_solid_bind_state(exa, priv, fg);
366 }
367
368 static void
369 ExaSolid(PixmapPtr pPixmap, int x0, int y0, int x1, int y1)
370 {
371 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
372 modesettingPtr ms = modesettingPTR(pScrn);
373 struct exa_context *exa = ms->exa;
374 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
375
376 #if DEBUG_PRINT
377 debug_printf("\tExaSolid(%d, %d, %d, %d)\n", x0, y0, x1, y1);
378 #endif
379
380 if (x0 == 0 && y0 == 0 &&
381 x1 == pPixmap->drawable.width && y1 == pPixmap->drawable.height) {
382 exa->pipe->clear(exa->pipe, PIPE_CLEAR_COLOR, exa->solid_color, 0.0, 0);
383 return;
384 }
385
386 xorg_solid(exa, priv, x0, y0, x1, y1) ;
387 }
388
389
390 static void
391 ExaDoneSolid(PixmapPtr pPixmap)
392 {
393 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
394 modesettingPtr ms = modesettingPTR(pScrn);
395 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
396 struct exa_context *exa = ms->exa;
397
398 if (!priv)
399 return;
400
401 xorg_composite_done(exa);
402 }
403
404 /***********************************************************************
405 * Copy Blits
406 */
407
408 static Bool
409 ExaPrepareCopy(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap, int xdir,
410 int ydir, int alu, Pixel planeMask)
411 {
412 ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
413 modesettingPtr ms = modesettingPTR(pScrn);
414 struct exa_context *exa = ms->exa;
415 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
416 struct exa_pixmap_priv *src_priv = exaGetPixmapDriverPrivate(pSrcPixmap);
417
418 #if DEBUG_PRINT
419 debug_printf("ExaPrepareCopy\n");
420 #endif
421 if (!exa->pipe)
422 XORG_FALLBACK("accle not enabled");
423
424 if (!priv || !priv->tex)
425 XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
426
427 if (!src_priv || !src_priv->tex)
428 XORG_FALLBACK("pSrc %s", !src_priv ? "!priv" : "!priv->tex");
429
430 if (!EXA_PM_IS_SOLID(&pSrcPixmap->drawable, planeMask))
431 XORG_FALLBACK("planeMask is not solid");
432
433 if (alu != GXcopy)
434 XORG_FALLBACK("alu not GXcopy");
435
436 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
437 priv->tex->target,
438 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
439 XORG_FALLBACK("pDst format %s", pf_name(priv->tex->format));
440
441 if (!exa->scrn->is_format_supported(exa->scrn, src_priv->tex->format,
442 src_priv->tex->target,
443 PIPE_TEXTURE_USAGE_SAMPLER, 0))
444 XORG_FALLBACK("pSrc format %s", pf_name(src_priv->tex->format));
445
446 exa->copy.src = src_priv;
447 exa->copy.dst = priv;
448
449 /* For same-surface copies, the pipe->surface_copy path is clearly
450 * superior, providing it is implemented. In other cases it's not
451 * clear what the better path would be, and eventually we'd
452 * probably want to gather timings and choose dynamically.
453 */
454 if (exa->pipe->surface_copy &&
455 exa->copy.src == exa->copy.dst) {
456
457 exa->copy.use_surface_copy = TRUE;
458
459 exa->copy.src_surface =
460 exa->scrn->get_tex_surface( exa->scrn,
461 exa->copy.src->tex,
462 0, 0, 0,
463 PIPE_BUFFER_USAGE_GPU_READ);
464
465 exa->copy.dst_surface =
466 exa->scrn->get_tex_surface( exa->scrn,
467 exa->copy.dst->tex,
468 0, 0, 0,
469 PIPE_BUFFER_USAGE_GPU_WRITE );
470 }
471 else {
472 exa->copy.use_surface_copy = FALSE;
473
474 if (exa->copy.dst == exa->copy.src)
475 exa->copy.src_texture = renderer_clone_texture( exa->renderer,
476 exa->copy.src->tex );
477 else
478 pipe_texture_reference(&exa->copy.src_texture,
479 exa->copy.src->tex);
480
481 exa->copy.dst_surface =
482 exa->scrn->get_tex_surface(exa->scrn,
483 exa->copy.dst->tex,
484 0, 0, 0,
485 PIPE_BUFFER_USAGE_GPU_WRITE);
486
487
488 renderer_copy_prepare(exa->renderer,
489 exa->copy.dst_surface,
490 exa->copy.src_texture );
491 }
492
493
494 return exa->accel;
495 }
496
497 static void
498 ExaCopy(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX, int dstY,
499 int width, int height)
500 {
501 ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
502 modesettingPtr ms = modesettingPTR(pScrn);
503 struct exa_context *exa = ms->exa;
504 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
505
506 #if DEBUG_PRINT
507 debug_printf("\tExaCopy(srcx=%d, srcy=%d, dstX=%d, dstY=%d, w=%d, h=%d)\n",
508 srcX, srcY, dstX, dstY, width, height);
509 #endif
510
511 debug_assert(priv == exa->copy.dst);
512
513 if (exa->copy.use_surface_copy) {
514 /* XXX: consider exposing >1 box in surface_copy interface.
515 */
516 exa->pipe->surface_copy( exa->pipe,
517 exa->copy.dst_surface,
518 dstX, dstY,
519 exa->copy.src_surface,
520 srcX, srcY,
521 width, height );
522 }
523 else {
524 renderer_copy_pixmap(exa->renderer,
525 dstX, dstY,
526 srcX, srcY,
527 width, height,
528 exa->copy.src_texture->width0,
529 exa->copy.src_texture->height0);
530 }
531 }
532
533 static void
534 ExaDoneCopy(PixmapPtr pPixmap)
535 {
536 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
537 modesettingPtr ms = modesettingPTR(pScrn);
538 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
539 struct exa_context *exa = ms->exa;
540
541 if (!priv)
542 return;
543
544 renderer_draw_flush(exa->renderer);
545
546 exa->copy.src = NULL;
547 exa->copy.dst = NULL;
548 pipe_surface_reference(&exa->copy.src_surface, NULL);
549 pipe_surface_reference(&exa->copy.dst_surface, NULL);
550 pipe_texture_reference(&exa->copy.src_texture, NULL);
551 }
552
553
554
555 static Bool
556 picture_check_formats(struct exa_pixmap_priv *pSrc, PicturePtr pSrcPicture)
557 {
558 if (pSrc->picture_format == pSrcPicture->format)
559 return TRUE;
560
561 if (pSrc->picture_format != PICT_a8r8g8b8)
562 return FALSE;
563
564 /* pSrc->picture_format == PICT_a8r8g8b8 */
565 switch (pSrcPicture->format) {
566 case PICT_a8r8g8b8:
567 case PICT_x8r8g8b8:
568 case PICT_a8b8g8r8:
569 case PICT_x8b8g8r8:
570 /* just treat these two as x8... */
571 case PICT_r8g8b8:
572 case PICT_b8g8r8:
573 return TRUE;
574 #ifdef PICT_TYPE_BGRA
575 case PICT_b8g8r8a8:
576 case PICT_b8g8r8x8:
577 return FALSE; /* does not support swizzleing the alpha channel yet */
578 case PICT_a2r10g10b10:
579 case PICT_x2r10g10b10:
580 case PICT_a2b10g10r10:
581 case PICT_x2b10g10r10:
582 return FALSE;
583 #endif
584 default:
585 return FALSE;
586 }
587 return FALSE;
588 }
589
590 /***********************************************************************
591 * Composite entrypoints
592 */
593
594 static Bool
595 ExaCheckComposite(int op,
596 PicturePtr pSrcPicture, PicturePtr pMaskPicture,
597 PicturePtr pDstPicture)
598 {
599 ScrnInfoPtr pScrn = xf86Screens[pDstPicture->pDrawable->pScreen->myNum];
600 modesettingPtr ms = modesettingPTR(pScrn);
601 struct exa_context *exa = ms->exa;
602 boolean accelerated = xorg_composite_accelerated(op,
603 pSrcPicture,
604 pMaskPicture,
605 pDstPicture);
606 #if DEBUG_PRINT
607 debug_printf("ExaCheckComposite(%d, %p, %p, %p) = %d\n",
608 op, pSrcPicture, pMaskPicture, pDstPicture, accelerated);
609 #endif
610 return exa->accel && accelerated;
611 }
612
613
614 static Bool
615 ExaPrepareComposite(int op, PicturePtr pSrcPicture,
616 PicturePtr pMaskPicture, PicturePtr pDstPicture,
617 PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
618 {
619 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
620 modesettingPtr ms = modesettingPTR(pScrn);
621 struct exa_context *exa = ms->exa;
622 struct exa_pixmap_priv *priv;
623
624 #if DEBUG_PRINT
625 debug_printf("ExaPrepareComposite(%d, src=0x%p, mask=0x%p, dst=0x%p)\n",
626 op, pSrcPicture, pMaskPicture, pDstPicture);
627 debug_printf("\tFormats: src(%s), mask(%s), dst(%s)\n",
628 pSrcPicture ? render_format_name(pSrcPicture->format) : "none",
629 pMaskPicture ? render_format_name(pMaskPicture->format) : "none",
630 pDstPicture ? render_format_name(pDstPicture->format) : "none");
631 #endif
632 if (!exa->pipe)
633 XORG_FALLBACK("accle not enabled");
634
635 priv = exaGetPixmapDriverPrivate(pDst);
636 if (!priv || !priv->tex)
637 XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
638
639 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
640 priv->tex->target,
641 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
642 XORG_FALLBACK("pDst format: %s", pf_name(priv->tex->format));
643
644 if (priv->picture_format != pDstPicture->format)
645 XORG_FALLBACK("pDst pic_format: %s != %s",
646 render_format_name(priv->picture_format),
647 render_format_name(pDstPicture->format));
648
649 if (pSrc) {
650 priv = exaGetPixmapDriverPrivate(pSrc);
651 if (!priv || !priv->tex)
652 XORG_FALLBACK("pSrc %s", !priv ? "!priv" : "!priv->tex");
653
654 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
655 priv->tex->target,
656 PIPE_TEXTURE_USAGE_SAMPLER, 0))
657 XORG_FALLBACK("pSrc format: %s", pf_name(priv->tex->format));
658
659 if (!picture_check_formats(priv, pSrcPicture))
660 XORG_FALLBACK("pSrc pic_format: %s != %s",
661 render_format_name(priv->picture_format),
662 render_format_name(pSrcPicture->format));
663
664 }
665
666 if (pMask) {
667 priv = exaGetPixmapDriverPrivate(pMask);
668 if (!priv || !priv->tex)
669 XORG_FALLBACK("pMask %s", !priv ? "!priv" : "!priv->tex");
670
671 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
672 priv->tex->target,
673 PIPE_TEXTURE_USAGE_SAMPLER, 0))
674 XORG_FALLBACK("pMask format: %s", pf_name(priv->tex->format));
675
676 if (!picture_check_formats(priv, pMaskPicture))
677 XORG_FALLBACK("pMask pic_format: %s != %s",
678 render_format_name(priv->picture_format),
679 render_format_name(pMaskPicture->format));
680 }
681
682 return exa->accel &&
683 xorg_composite_bind_state(exa, op, pSrcPicture, pMaskPicture,
684 pDstPicture,
685 pSrc ? exaGetPixmapDriverPrivate(pSrc) : NULL,
686 pMask ? exaGetPixmapDriverPrivate(pMask) : NULL,
687 exaGetPixmapDriverPrivate(pDst));
688 }
689
690 static void
691 ExaComposite(PixmapPtr pDst, int srcX, int srcY, int maskX, int maskY,
692 int dstX, int dstY, int width, int height)
693 {
694 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
695 modesettingPtr ms = modesettingPTR(pScrn);
696 struct exa_context *exa = ms->exa;
697 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDst);
698
699 #if DEBUG_PRINT
700 debug_printf("\tExaComposite(src[%d,%d], mask=[%d, %d], dst=[%d, %d], dim=[%d, %d])\n",
701 srcX, srcY, maskX, maskY, dstX, dstY, width, height);
702 debug_printf("\t Num bound samplers = %d\n",
703 exa->num_bound_samplers);
704 #endif
705
706 xorg_composite(exa, priv, srcX, srcY, maskX, maskY,
707 dstX, dstY, width, height);
708 }
709
710
711
712 static void
713 ExaDoneComposite(PixmapPtr pPixmap)
714 {
715 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
716 modesettingPtr ms = modesettingPTR(pScrn);
717 struct exa_context *exa = ms->exa;
718
719 xorg_composite_done(exa);
720 }
721
722
723 /***********************************************************************
724 * Pixmaps
725 */
726
727 static void *
728 ExaCreatePixmap(ScreenPtr pScreen, int size, int align)
729 {
730 struct exa_pixmap_priv *priv;
731
732 priv = xcalloc(1, sizeof(struct exa_pixmap_priv));
733 if (!priv)
734 return NULL;
735
736 return priv;
737 }
738
739 static void
740 ExaDestroyPixmap(ScreenPtr pScreen, void *dPriv)
741 {
742 struct exa_pixmap_priv *priv = (struct exa_pixmap_priv *)dPriv;
743
744 if (!priv)
745 return;
746
747 pipe_texture_reference(&priv->tex, NULL);
748
749 xfree(priv);
750 }
751
752 static Bool
753 ExaPixmapIsOffscreen(PixmapPtr pPixmap)
754 {
755 struct exa_pixmap_priv *priv;
756
757 priv = exaGetPixmapDriverPrivate(pPixmap);
758
759 if (!priv)
760 return FALSE;
761
762 if (priv->tex)
763 return TRUE;
764
765 return FALSE;
766 }
767
768 int
769 xorg_exa_set_displayed_usage(PixmapPtr pPixmap)
770 {
771 struct exa_pixmap_priv *priv;
772 priv = exaGetPixmapDriverPrivate(pPixmap);
773
774 if (!priv) {
775 FatalError("NO PIXMAP PRIVATE\n");
776 return 0;
777 }
778
779 priv->flags |= PIPE_TEXTURE_USAGE_PRIMARY;
780
781 return 0;
782 }
783
784 int
785 xorg_exa_set_shared_usage(PixmapPtr pPixmap)
786 {
787 struct exa_pixmap_priv *priv;
788 priv = exaGetPixmapDriverPrivate(pPixmap);
789
790 if (!priv) {
791 FatalError("NO PIXMAP PRIVATE\n");
792 return 0;
793 }
794
795 priv->flags |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
796
797 return 0;
798 }
799
800 unsigned
801 xorg_exa_get_pixmap_handle(PixmapPtr pPixmap, unsigned *stride_out)
802 {
803 ScreenPtr pScreen = pPixmap->drawable.pScreen;
804 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
805 modesettingPtr ms = modesettingPTR(pScrn);
806 struct exa_pixmap_priv *priv;
807 unsigned handle;
808 unsigned stride;
809
810 if (!ms->exa) {
811 FatalError("NO MS->EXA\n");
812 return 0;
813 }
814
815 priv = exaGetPixmapDriverPrivate(pPixmap);
816
817 if (!priv) {
818 FatalError("NO PIXMAP PRIVATE\n");
819 return 0;
820 }
821
822 ms->api->local_handle_from_texture(ms->api, ms->screen, priv->tex, &stride, &handle);
823 if (stride_out)
824 *stride_out = stride;
825
826 return handle;
827 }
828
829 static Bool
830 size_match( int width, int tex_width )
831 {
832 #if ROUND_UP_TEXTURES
833 if (width > tex_width)
834 return FALSE;
835
836 if (width * 2 < tex_width)
837 return FALSE;
838
839 return TRUE;
840 #else
841 return width == tex_width;
842 #endif
843 }
844
845 static Bool
846 ExaModifyPixmapHeader(PixmapPtr pPixmap, int width, int height,
847 int depth, int bitsPerPixel, int devKind,
848 pointer pPixData)
849 {
850 ScreenPtr pScreen = pPixmap->drawable.pScreen;
851 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
852 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
853 modesettingPtr ms = modesettingPTR(pScrn);
854 struct exa_context *exa = ms->exa;
855
856 if (!priv || pPixData)
857 return FALSE;
858
859 if (0) {
860 debug_printf("%s pixmap %p sz %dx%dx%d devKind %d\n",
861 __FUNCTION__, pPixmap, width, height, bitsPerPixel, devKind);
862
863 if (priv->tex)
864 debug_printf(" ==> old texture %dx%d\n",
865 priv->tex->width0,
866 priv->tex->height0);
867 }
868
869
870 if (depth <= 0)
871 depth = pPixmap->drawable.depth;
872
873 if (bitsPerPixel <= 0)
874 bitsPerPixel = pPixmap->drawable.bitsPerPixel;
875
876 if (width <= 0)
877 width = pPixmap->drawable.width;
878
879 if (height <= 0)
880 height = pPixmap->drawable.height;
881
882 if (width <= 0 || height <= 0 || depth <= 0)
883 return FALSE;
884
885 miModifyPixmapHeader(pPixmap, width, height, depth,
886 bitsPerPixel, devKind, NULL);
887
888 priv->width = width;
889 priv->height = height;
890
891 /* Deal with screen resize */
892 if ((exa->accel || priv->flags) &&
893 (!priv->tex ||
894 !size_match(width, priv->tex->width0) ||
895 !size_match(height, priv->tex->height0) ||
896 priv->tex_flags != priv->flags)) {
897 struct pipe_texture *texture = NULL;
898 struct pipe_texture template;
899
900 memset(&template, 0, sizeof(template));
901 template.target = PIPE_TEXTURE_2D;
902 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &priv->picture_format);
903 if (ROUND_UP_TEXTURES && priv->flags == 0) {
904 template.width0 = util_next_power_of_two(width);
905 template.height0 = util_next_power_of_two(height);
906 }
907 else {
908 template.width0 = width;
909 template.height0 = height;
910 }
911
912 template.depth0 = 1;
913 template.last_level = 0;
914 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET | priv->flags;
915 priv->tex_flags = priv->flags;
916 texture = exa->scrn->texture_create(exa->scrn, &template);
917
918 if (priv->tex) {
919 struct pipe_surface *dst_surf;
920 struct pipe_surface *src_surf;
921
922 dst_surf = exa->scrn->get_tex_surface(
923 exa->scrn, texture, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
924 src_surf = xorg_gpu_surface(exa->pipe->screen, priv);
925 if (exa->pipe->surface_copy) {
926 exa->pipe->surface_copy(exa->pipe, dst_surf, 0, 0, src_surf,
927 0, 0, min(width, texture->width0),
928 min(height, texture->height0));
929 } else {
930 util_surface_copy(exa->pipe, FALSE, dst_surf, 0, 0, src_surf,
931 0, 0, min(width, texture->width0),
932 min(height, texture->height0));
933 }
934 exa->scrn->tex_surface_destroy(dst_surf);
935 exa->scrn->tex_surface_destroy(src_surf);
936 }
937
938 pipe_texture_reference(&priv->tex, texture);
939 /* the texture we create has one reference */
940 pipe_texture_reference(&texture, NULL);
941 }
942
943 return TRUE;
944 }
945
946 struct pipe_texture *
947 xorg_exa_get_texture(PixmapPtr pPixmap)
948 {
949 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
950 struct pipe_texture *tex = NULL;
951 pipe_texture_reference(&tex, priv->tex);
952 return tex;
953 }
954
955 Bool
956 xorg_exa_set_texture(PixmapPtr pPixmap, struct pipe_texture *tex)
957 {
958 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
959
960 int mask = PIPE_TEXTURE_USAGE_PRIMARY | PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
961
962 if (!priv)
963 return FALSE;
964
965 if (pPixmap->drawable.width != tex->width0 ||
966 pPixmap->drawable.height != tex->height0)
967 return FALSE;
968
969 pipe_texture_reference(&priv->tex, tex);
970 priv->tex_flags = tex->tex_usage & mask;
971
972 return TRUE;
973 }
974
975 struct pipe_texture *
976 xorg_exa_create_root_texture(ScrnInfoPtr pScrn,
977 int width, int height,
978 int depth, int bitsPerPixel)
979 {
980 modesettingPtr ms = modesettingPTR(pScrn);
981 struct exa_context *exa = ms->exa;
982 struct pipe_texture template;
983 int dummy;
984
985 memset(&template, 0, sizeof(template));
986 template.target = PIPE_TEXTURE_2D;
987 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &dummy);
988 template.width0 = width;
989 template.height0 = height;
990 template.depth0 = 1;
991 template.last_level = 0;
992 template.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
993 template.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
994 template.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
995
996 return exa->scrn->texture_create(exa->scrn, &template);
997 }
998
999 void
1000 xorg_exa_close(ScrnInfoPtr pScrn)
1001 {
1002 modesettingPtr ms = modesettingPTR(pScrn);
1003 struct exa_context *exa = ms->exa;
1004
1005 renderer_destroy(exa->renderer);
1006
1007 if (exa->pipe)
1008 exa->pipe->destroy(exa->pipe);
1009
1010 exaDriverFini(pScrn->pScreen);
1011 xfree(exa);
1012 ms->exa = NULL;
1013 }
1014
1015 void *
1016 xorg_exa_init(ScrnInfoPtr pScrn, Bool accel)
1017 {
1018 modesettingPtr ms = modesettingPTR(pScrn);
1019 struct exa_context *exa;
1020 ExaDriverPtr pExa;
1021
1022 exa = xcalloc(1, sizeof(struct exa_context));
1023 if (!exa)
1024 return NULL;
1025
1026 pExa = exaDriverAlloc();
1027 if (!pExa) {
1028 goto out_err;
1029 }
1030
1031 memset(pExa, 0, sizeof(*pExa));
1032
1033 pExa->exa_major = 2;
1034 pExa->exa_minor = 2;
1035 pExa->memoryBase = 0;
1036 pExa->memorySize = 0;
1037 pExa->offScreenBase = 0;
1038 pExa->pixmapOffsetAlign = 0;
1039 pExa->pixmapPitchAlign = 1;
1040 pExa->flags = EXA_OFFSCREEN_PIXMAPS | EXA_HANDLES_PIXMAPS;
1041 #ifdef EXA_SUPPORTS_PREPARE_AUX
1042 pExa->flags |= EXA_SUPPORTS_PREPARE_AUX;
1043 #endif
1044 #ifdef EXA_MIXED_PIXMAPS
1045 pExa->flags |= EXA_MIXED_PIXMAPS;
1046 #endif
1047 pExa->maxX = 8191; /* FIXME */
1048 pExa->maxY = 8191; /* FIXME */
1049
1050 pExa->WaitMarker = ExaWaitMarker;
1051 pExa->MarkSync = ExaMarkSync;
1052 pExa->PrepareSolid = ExaPrepareSolid;
1053 pExa->Solid = ExaSolid;
1054 pExa->DoneSolid = ExaDoneSolid;
1055 pExa->PrepareCopy = ExaPrepareCopy;
1056 pExa->Copy = ExaCopy;
1057 pExa->DoneCopy = ExaDoneCopy;
1058 pExa->CheckComposite = ExaCheckComposite;
1059 pExa->PrepareComposite = ExaPrepareComposite;
1060 pExa->Composite = ExaComposite;
1061 pExa->DoneComposite = ExaDoneComposite;
1062 pExa->PixmapIsOffscreen = ExaPixmapIsOffscreen;
1063 pExa->DownloadFromScreen = ExaDownloadFromScreen;
1064 pExa->UploadToScreen = ExaUploadToScreen;
1065 pExa->PrepareAccess = ExaPrepareAccess;
1066 pExa->FinishAccess = ExaFinishAccess;
1067 pExa->CreatePixmap = ExaCreatePixmap;
1068 pExa->DestroyPixmap = ExaDestroyPixmap;
1069 pExa->ModifyPixmapHeader = ExaModifyPixmapHeader;
1070
1071 if (!exaDriverInit(pScrn->pScreen, pExa)) {
1072 goto out_err;
1073 }
1074
1075 exa->scrn = ms->screen;
1076 exa->pipe = ms->api->create_context(ms->api, exa->scrn);
1077 /* Share context with DRI */
1078 ms->ctx = exa->pipe;
1079
1080 exa->renderer = renderer_create(exa->pipe);
1081 exa->accel = accel;
1082
1083 return (void *)exa;
1084
1085 out_err:
1086 xorg_exa_close(pScrn);
1087
1088 return NULL;
1089 }
1090
1091 struct pipe_surface *
1092 xorg_gpu_surface(struct pipe_screen *scrn, struct exa_pixmap_priv *priv)
1093 {
1094 return scrn->get_tex_surface(scrn, priv->tex, 0, 0, 0,
1095 PIPE_BUFFER_USAGE_GPU_READ |
1096 PIPE_BUFFER_USAGE_GPU_WRITE);
1097
1098 }
1099
1100 void xorg_exa_flush(struct exa_context *exa, uint pipeFlushFlags,
1101 struct pipe_fence_handle **fence)
1102 {
1103 exa->pipe->flush(exa->pipe, pipeFlushFlags, fence);
1104 }
1105
1106 void xorg_exa_finish(struct exa_context *exa)
1107 {
1108 struct pipe_fence_handle *fence = NULL;
1109
1110 xorg_exa_flush(exa, PIPE_FLUSH_RENDER_CACHE, &fence);
1111
1112 exa->pipe->screen->fence_finish(exa->pipe->screen, fence, 0);
1113 exa->pipe->screen->fence_reference(exa->pipe->screen, &fence, NULL);
1114 }
1115