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