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