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