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