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