gallium: fix more statetrackers/drivers for not using texture width/height/depth...
[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 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->width0, priv->tex->height0);
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 picture_check_formats(struct exa_pixmap_priv *pSrc, PicturePtr pSrcPicture)
472 {
473 if (pSrc->picture_format == pSrcPicture->format)
474 return TRUE;
475
476 if (pSrc->picture_format != PICT_a8r8g8b8)
477 return FALSE;
478
479 /* pSrc->picture_format == PICT_a8r8g8b8 */
480 switch (pSrcPicture->format) {
481 case PICT_a8r8g8b8:
482 case PICT_x8r8g8b8:
483 case PICT_a8b8g8r8:
484 case PICT_x8b8g8r8:
485 /* just treat these two as x8... */
486 case PICT_r8g8b8:
487 case PICT_b8g8r8:
488 return TRUE;
489 #ifdef PICT_TYPE_BGRA
490 case PICT_b8g8r8a8:
491 case PICT_b8g8r8x8:
492 return FALSE; /* does not support swizzleing the alpha channel yet */
493 case PICT_a2r10g10b10:
494 case PICT_x2r10g10b10:
495 case PICT_a2b10g10r10:
496 case PICT_x2b10g10r10:
497 return FALSE;
498 #endif
499 default:
500 return FALSE;
501 }
502 return FALSE;
503 }
504
505 static Bool
506 ExaPrepareComposite(int op, PicturePtr pSrcPicture,
507 PicturePtr pMaskPicture, PicturePtr pDstPicture,
508 PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
509 {
510 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
511 modesettingPtr ms = modesettingPTR(pScrn);
512 struct exa_context *exa = ms->exa;
513 struct exa_pixmap_priv *priv;
514
515 #if DEBUG_PRINT
516 debug_printf("ExaPrepareComposite(%d, src=0x%p, mask=0x%p, dst=0x%p)\n",
517 op, pSrcPicture, pMaskPicture, pDstPicture);
518 debug_printf("\tFormats: src(%s), mask(%s), dst(%s)\n",
519 pSrcPicture ? render_format_name(pSrcPicture->format) : "none",
520 pMaskPicture ? render_format_name(pMaskPicture->format) : "none",
521 pDstPicture ? render_format_name(pDstPicture->format) : "none");
522 #endif
523 if (!exa->pipe)
524 XORG_FALLBACK("accle not enabled");
525
526 priv = exaGetPixmapDriverPrivate(pDst);
527 if (!priv || !priv->tex)
528 XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
529
530 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
531 priv->tex->target,
532 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
533 XORG_FALLBACK("pDst format: %s", pf_name(priv->tex->format));
534
535 if (priv->picture_format != pDstPicture->format)
536 XORG_FALLBACK("pDst pic_format: %s != %s",
537 render_format_name(priv->picture_format),
538 render_format_name(pDstPicture->format));
539
540 if (pSrc) {
541 priv = exaGetPixmapDriverPrivate(pSrc);
542 if (!priv || !priv->tex)
543 XORG_FALLBACK("pSrc %s", !priv ? "!priv" : "!priv->tex");
544
545 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
546 priv->tex->target,
547 PIPE_TEXTURE_USAGE_SAMPLER, 0))
548 XORG_FALLBACK("pSrc format: %s", pf_name(priv->tex->format));
549
550 if (!picture_check_formats(priv, pSrcPicture))
551 XORG_FALLBACK("pSrc pic_format: %s != %s",
552 render_format_name(priv->picture_format),
553 render_format_name(pSrcPicture->format));
554
555 if (priv->picture_format == PICT_a8)
556 XORG_FALLBACK("pSrc pic_format == PICT_a8");
557 }
558
559 if (pMask) {
560 priv = exaGetPixmapDriverPrivate(pMask);
561 if (!priv || !priv->tex)
562 XORG_FALLBACK("pMask %s", !priv ? "!priv" : "!priv->tex");
563
564 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
565 priv->tex->target,
566 PIPE_TEXTURE_USAGE_SAMPLER, 0))
567 XORG_FALLBACK("pMask format: %s", pf_name(priv->tex->format));
568
569 if (!picture_check_formats(priv, pMaskPicture))
570 XORG_FALLBACK("pMask pic_format: %s != %s",
571 render_format_name(priv->picture_format),
572 render_format_name(pMaskPicture->format));
573 }
574
575 return ACCEL_ENABLED &&
576 xorg_composite_bind_state(exa, op, pSrcPicture, pMaskPicture,
577 pDstPicture,
578 pSrc ? exaGetPixmapDriverPrivate(pSrc) : NULL,
579 pMask ? exaGetPixmapDriverPrivate(pMask) : NULL,
580 exaGetPixmapDriverPrivate(pDst));
581 }
582
583 static void
584 ExaComposite(PixmapPtr pDst, int srcX, int srcY, int maskX, int maskY,
585 int dstX, int dstY, int width, int height)
586 {
587 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
588 modesettingPtr ms = modesettingPTR(pScrn);
589 struct exa_context *exa = ms->exa;
590 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDst);
591
592 #if DEBUG_PRINT
593 debug_printf("\tExaComposite(src[%d,%d], mask=[%d, %d], dst=[%d, %d], dim=[%d, %d])\n",
594 srcX, srcY, maskX, maskY, dstX, dstY, width, height);
595 debug_printf("\t Num bound samplers = %d\n",
596 exa->num_bound_samplers);
597 #endif
598
599 xorg_composite(exa, priv, srcX, srcY, maskX, maskY,
600 dstX, dstY, width, height);
601 }
602
603 static Bool
604 ExaCheckComposite(int op,
605 PicturePtr pSrcPicture, PicturePtr pMaskPicture,
606 PicturePtr pDstPicture)
607 {
608 boolean accelerated = xorg_composite_accelerated(op,
609 pSrcPicture,
610 pMaskPicture,
611 pDstPicture);
612 #if DEBUG_PRINT
613 debug_printf("ExaCheckComposite(%d, %p, %p, %p) = %d\n",
614 op, pSrcPicture, pMaskPicture, pDstPicture, accelerated);
615 #endif
616 return ACCEL_ENABLED && accelerated;
617 }
618
619 static void *
620 ExaCreatePixmap(ScreenPtr pScreen, int size, int align)
621 {
622 struct exa_pixmap_priv *priv;
623
624 priv = xcalloc(1, sizeof(struct exa_pixmap_priv));
625 if (!priv)
626 return NULL;
627
628 return priv;
629 }
630
631 static void
632 ExaDestroyPixmap(ScreenPtr pScreen, void *dPriv)
633 {
634 struct exa_pixmap_priv *priv = (struct exa_pixmap_priv *)dPriv;
635
636 if (!priv)
637 return;
638
639 pipe_texture_reference(&priv->tex, NULL);
640
641 xfree(priv);
642 }
643
644 static Bool
645 ExaPixmapIsOffscreen(PixmapPtr pPixmap)
646 {
647 struct exa_pixmap_priv *priv;
648
649 priv = exaGetPixmapDriverPrivate(pPixmap);
650
651 if (!priv)
652 return FALSE;
653
654 if (priv->tex)
655 return TRUE;
656
657 return FALSE;
658 }
659
660 int
661 xorg_exa_set_displayed_usage(PixmapPtr pPixmap)
662 {
663 struct exa_pixmap_priv *priv;
664 priv = exaGetPixmapDriverPrivate(pPixmap);
665
666 if (!priv) {
667 FatalError("NO PIXMAP PRIVATE\n");
668 return 0;
669 }
670
671 priv->flags |= PIPE_TEXTURE_USAGE_PRIMARY;
672
673 return 0;
674 }
675
676 int
677 xorg_exa_set_shared_usage(PixmapPtr pPixmap)
678 {
679 struct exa_pixmap_priv *priv;
680 priv = exaGetPixmapDriverPrivate(pPixmap);
681
682 if (!priv) {
683 FatalError("NO PIXMAP PRIVATE\n");
684 return 0;
685 }
686
687 priv->flags |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
688
689 return 0;
690 }
691
692 unsigned
693 xorg_exa_get_pixmap_handle(PixmapPtr pPixmap, unsigned *stride_out)
694 {
695 ScreenPtr pScreen = pPixmap->drawable.pScreen;
696 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
697 modesettingPtr ms = modesettingPTR(pScrn);
698 struct exa_pixmap_priv *priv;
699 unsigned handle;
700 unsigned stride;
701
702 if (!ms->exa) {
703 FatalError("NO MS->EXA\n");
704 return 0;
705 }
706
707 priv = exaGetPixmapDriverPrivate(pPixmap);
708
709 if (!priv) {
710 FatalError("NO PIXMAP PRIVATE\n");
711 return 0;
712 }
713
714 ms->api->local_handle_from_texture(ms->api, ms->screen, priv->tex, &stride, &handle);
715 if (stride_out)
716 *stride_out = stride;
717
718 return handle;
719 }
720
721 static Bool
722 ExaModifyPixmapHeader(PixmapPtr pPixmap, int width, int height,
723 int depth, int bitsPerPixel, int devKind,
724 pointer pPixData)
725 {
726 ScreenPtr pScreen = pPixmap->drawable.pScreen;
727 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
728 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
729 modesettingPtr ms = modesettingPTR(pScrn);
730 struct exa_context *exa = ms->exa;
731
732 if (!priv || pPixData)
733 return FALSE;
734
735 if (depth <= 0)
736 depth = pPixmap->drawable.depth;
737
738 if (bitsPerPixel <= 0)
739 bitsPerPixel = pPixmap->drawable.bitsPerPixel;
740
741 if (width <= 0)
742 width = pPixmap->drawable.width;
743
744 if (height <= 0)
745 height = pPixmap->drawable.height;
746
747 if (width <= 0 || height <= 0 || depth <= 0)
748 return FALSE;
749
750 miModifyPixmapHeader(pPixmap, width, height, depth,
751 bitsPerPixel, devKind, NULL);
752
753 /* Deal with screen resize */
754 if (!priv->tex ||
755 (priv->tex->width0 != width ||
756 priv->tex->height0 != height ||
757 priv->tex_flags != priv->flags)) {
758 struct pipe_texture *texture = NULL;
759 struct pipe_texture template;
760
761 memset(&template, 0, sizeof(template));
762 template.target = PIPE_TEXTURE_2D;
763 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &priv->picture_format);
764 pf_get_block(template.format, &template.block);
765 template.width0 = width;
766 template.height0 = height;
767 template.depth0 = 1;
768 template.last_level = 0;
769 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET | priv->flags;
770 priv->tex_flags = priv->flags;
771 texture = exa->scrn->texture_create(exa->scrn, &template);
772
773 if (priv->tex) {
774 struct pipe_surface *dst_surf;
775 struct pipe_surface *src_surf;
776
777 dst_surf = exa->scrn->get_tex_surface(
778 exa->scrn, texture, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
779 src_surf = xorg_gpu_surface(exa->pipe->screen, priv);
780 if (exa->pipe->surface_copy) {
781 exa->pipe->surface_copy(exa->pipe, dst_surf, 0, 0, src_surf,
782 0, 0, min(width, texture->width0),
783 min(height, texture->height0));
784 } else {
785 util_surface_copy(exa->pipe, FALSE, dst_surf, 0, 0, src_surf,
786 0, 0, min(width, texture->width0),
787 min(height, texture->height0));
788 }
789 exa->scrn->tex_surface_destroy(dst_surf);
790 exa->scrn->tex_surface_destroy(src_surf);
791 }
792
793 pipe_texture_reference(&priv->tex, texture);
794 /* the texture we create has one reference */
795 pipe_texture_reference(&texture, NULL);
796 }
797
798 return TRUE;
799 }
800
801 struct pipe_texture *
802 xorg_exa_get_texture(PixmapPtr pPixmap)
803 {
804 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
805 struct pipe_texture *tex = NULL;
806 pipe_texture_reference(&tex, priv->tex);
807 return tex;
808 }
809
810 Bool
811 xorg_exa_set_texture(PixmapPtr pPixmap, struct pipe_texture *tex)
812 {
813 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
814
815 int mask = PIPE_TEXTURE_USAGE_PRIMARY | PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
816
817 if (!priv)
818 return FALSE;
819
820 if (pPixmap->drawable.width != tex->width0 ||
821 pPixmap->drawable.height != tex->height0)
822 return FALSE;
823
824 pipe_texture_reference(&priv->tex, tex);
825 priv->tex_flags = tex->tex_usage & mask;
826
827 return TRUE;
828 }
829
830 struct pipe_texture *
831 xorg_exa_create_root_texture(ScrnInfoPtr pScrn,
832 int width, int height,
833 int depth, int bitsPerPixel)
834 {
835 modesettingPtr ms = modesettingPTR(pScrn);
836 struct exa_context *exa = ms->exa;
837 struct pipe_texture template;
838 int dummy;
839
840 memset(&template, 0, sizeof(template));
841 template.target = PIPE_TEXTURE_2D;
842 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &dummy);
843 pf_get_block(template.format, &template.block);
844 template.width0 = width;
845 template.height0 = height;
846 template.depth0 = 1;
847 template.last_level = 0;
848 template.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
849 template.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
850 template.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
851
852 return exa->scrn->texture_create(exa->scrn, &template);
853 }
854
855 void
856 xorg_exa_close(ScrnInfoPtr pScrn)
857 {
858 modesettingPtr ms = modesettingPTR(pScrn);
859 struct exa_context *exa = ms->exa;
860
861 renderer_destroy(exa->renderer);
862
863 if (exa->pipe)
864 exa->pipe->destroy(exa->pipe);
865
866 exaDriverFini(pScrn->pScreen);
867 xfree(exa);
868 ms->exa = NULL;
869 }
870
871 void *
872 xorg_exa_init(ScrnInfoPtr pScrn)
873 {
874 modesettingPtr ms = modesettingPTR(pScrn);
875 struct exa_context *exa;
876 ExaDriverPtr pExa;
877
878 exa = xcalloc(1, sizeof(struct exa_context));
879 if (!exa)
880 return NULL;
881
882 pExa = exaDriverAlloc();
883 if (!pExa) {
884 goto out_err;
885 }
886
887 memset(pExa, 0, sizeof(*pExa));
888
889 pExa->exa_major = 2;
890 pExa->exa_minor = 2;
891 pExa->memoryBase = 0;
892 pExa->memorySize = 0;
893 pExa->offScreenBase = 0;
894 pExa->pixmapOffsetAlign = 0;
895 pExa->pixmapPitchAlign = 1;
896 pExa->flags = EXA_OFFSCREEN_PIXMAPS | EXA_HANDLES_PIXMAPS;
897 #ifdef EXA_SUPPORTS_PREPARE_AUX
898 pExa->flags |= EXA_SUPPORTS_PREPARE_AUX;
899 #endif
900 #ifdef EXA_MIXED_PIXMAPS
901 pExa->flags |= EXA_MIXED_PIXMAPS;
902 #endif
903 pExa->maxX = 8191; /* FIXME */
904 pExa->maxY = 8191; /* FIXME */
905
906 pExa->WaitMarker = ExaWaitMarker;
907 pExa->MarkSync = ExaMarkSync;
908 pExa->PrepareSolid = ExaPrepareSolid;
909 pExa->Solid = ExaSolid;
910 pExa->DoneSolid = ExaDone;
911 pExa->PrepareCopy = ExaPrepareCopy;
912 pExa->Copy = ExaCopy;
913 pExa->DoneCopy = ExaDone;
914 pExa->CheckComposite = ExaCheckComposite;
915 pExa->PrepareComposite = ExaPrepareComposite;
916 pExa->Composite = ExaComposite;
917 pExa->DoneComposite = ExaDoneComposite;
918 pExa->PixmapIsOffscreen = ExaPixmapIsOffscreen;
919 pExa->DownloadFromScreen = ExaDownloadFromScreen;
920 pExa->UploadToScreen = ExaUploadToScreen;
921 pExa->PrepareAccess = ExaPrepareAccess;
922 pExa->FinishAccess = ExaFinishAccess;
923 pExa->CreatePixmap = ExaCreatePixmap;
924 pExa->DestroyPixmap = ExaDestroyPixmap;
925 pExa->ModifyPixmapHeader = ExaModifyPixmapHeader;
926
927 if (!exaDriverInit(pScrn->pScreen, pExa)) {
928 goto out_err;
929 }
930
931 exa->scrn = ms->screen;
932 exa->pipe = ms->api->create_context(ms->api, exa->scrn);
933 /* Share context with DRI */
934 ms->ctx = exa->pipe;
935
936 exa->renderer = renderer_create(exa->pipe);
937
938 return (void *)exa;
939
940 out_err:
941 xorg_exa_close(pScrn);
942
943 return NULL;
944 }
945
946 struct pipe_surface *
947 xorg_gpu_surface(struct pipe_screen *scrn, struct exa_pixmap_priv *priv)
948 {
949 return scrn->get_tex_surface(scrn, priv->tex, 0, 0, 0,
950 PIPE_BUFFER_USAGE_GPU_READ |
951 PIPE_BUFFER_USAGE_GPU_WRITE);
952
953 }
954
955 void xorg_exa_flush(struct exa_context *exa, uint pipeFlushFlags,
956 struct pipe_fence_handle **fence)
957 {
958 exa->pipe->flush(exa->pipe, pipeFlushFlags, fence);
959 }
960
961 void xorg_exa_finish(struct exa_context *exa)
962 {
963 struct pipe_fence_handle *fence = NULL;
964
965 xorg_exa_flush(exa, PIPE_FLUSH_RENDER_CACHE, &fence);
966
967 exa->pipe->screen->fence_finish(exa->pipe->screen, fence, 0);
968 exa->pipe->screen->fence_reference(exa->pipe->screen, &fence, NULL);
969 }
970