st/xorg: Be proper with pipe pointers on close in exa
[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->width[0]);
278 assert(pPix->drawable.height <= priv->tex->height[0]);
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 0, 0,
287 pPix->drawable.width,
288 pPix->drawable.height );
289 if (!priv->map_transfer)
290 #ifdef EXA_MIXED_PIXMAPS
291 return FALSE;
292 #else
293 FatalError("failed to create transfer\n");
294 #endif
295
296 pPix->devPrivate.ptr =
297 exa->scrn->transfer_map(exa->scrn, priv->map_transfer);
298 pPix->devKind = priv->map_transfer->stride;
299 }
300
301 priv->map_count++;
302
303 return TRUE;
304 }
305
306 static void
307 ExaFinishAccess(PixmapPtr pPix, int index)
308 {
309 ScreenPtr pScreen = pPix->drawable.pScreen;
310 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
311 modesettingPtr ms = modesettingPTR(pScrn);
312 struct exa_context *exa = ms->exa;
313 struct exa_pixmap_priv *priv;
314 priv = exaGetPixmapDriverPrivate(pPix);
315
316 if (!priv)
317 return;
318
319 if (!priv->map_transfer)
320 return;
321
322 if (--priv->map_count == 0) {
323 assert(priv->map_transfer);
324 exa->scrn->transfer_unmap(exa->scrn, priv->map_transfer);
325 exa->scrn->tex_transfer_destroy(priv->map_transfer);
326 priv->map_transfer = NULL;
327 pPix->devPrivate.ptr = NULL;
328 }
329 }
330
331 /***********************************************************************
332 * Solid Fills
333 */
334
335 static Bool
336 ExaPrepareSolid(PixmapPtr pPixmap, int alu, Pixel planeMask, Pixel fg)
337 {
338 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
339 modesettingPtr ms = modesettingPTR(pScrn);
340 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
341 struct exa_context *exa = ms->exa;
342
343 #if DEBUG_PRINT
344 debug_printf("ExaPrepareSolid(0x%x)\n", fg);
345 #endif
346 if (!exa->accel)
347 return FALSE;
348
349 if (!exa->pipe)
350 XORG_FALLBACK("accle not enabled");
351
352 if (!priv || !priv->tex)
353 XORG_FALLBACK("%s", !priv ? "!priv" : "!priv->tex");
354
355 if (!EXA_PM_IS_SOLID(&pPixmap->drawable, planeMask))
356 XORG_FALLBACK("planeMask is not solid");
357
358 if (alu != GXcopy)
359 XORG_FALLBACK("not GXcopy");
360
361 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
362 priv->tex->target,
363 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
364 XORG_FALLBACK("format %s", pf_name(priv->tex->format));
365 }
366
367 return xorg_solid_bind_state(exa, priv, fg);
368 }
369
370 static void
371 ExaSolid(PixmapPtr pPixmap, int x0, int y0, int x1, int y1)
372 {
373 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
374 modesettingPtr ms = modesettingPTR(pScrn);
375 struct exa_context *exa = ms->exa;
376 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
377
378 #if DEBUG_PRINT
379 debug_printf("\tExaSolid(%d, %d, %d, %d)\n", x0, y0, x1, y1);
380 #endif
381
382 if (x0 == 0 && y0 == 0 &&
383 x1 == pPixmap->drawable.width && y1 == pPixmap->drawable.height) {
384 exa->pipe->clear(exa->pipe, PIPE_CLEAR_COLOR, exa->solid_color, 0.0, 0);
385 return;
386 }
387
388 xorg_solid(exa, priv, x0, y0, x1, y1) ;
389 }
390
391
392 static void
393 ExaDoneSolid(PixmapPtr pPixmap)
394 {
395 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
396 modesettingPtr ms = modesettingPTR(pScrn);
397 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
398 struct exa_context *exa = ms->exa;
399
400 if (!priv)
401 return;
402
403 xorg_composite_done(exa);
404 }
405
406 /***********************************************************************
407 * Copy Blits
408 */
409
410 static Bool
411 ExaPrepareCopy(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap, int xdir,
412 int ydir, int alu, Pixel planeMask)
413 {
414 ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
415 modesettingPtr ms = modesettingPTR(pScrn);
416 struct exa_context *exa = ms->exa;
417 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
418 struct exa_pixmap_priv *src_priv = exaGetPixmapDriverPrivate(pSrcPixmap);
419
420 #if DEBUG_PRINT
421 debug_printf("ExaPrepareCopy\n");
422 #endif
423
424 if (!exa->accel)
425 return FALSE;
426
427 if (!exa->pipe)
428 XORG_FALLBACK("accle not enabled");
429
430 if (!priv || !priv->tex)
431 XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
432
433 if (!src_priv || !src_priv->tex)
434 XORG_FALLBACK("pSrc %s", !src_priv ? "!priv" : "!priv->tex");
435
436 if (!EXA_PM_IS_SOLID(&pSrcPixmap->drawable, planeMask))
437 XORG_FALLBACK("planeMask is not solid");
438
439 if (alu != GXcopy)
440 XORG_FALLBACK("alu not GXcopy");
441
442 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
443 priv->tex->target,
444 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
445 XORG_FALLBACK("pDst format %s", pf_name(priv->tex->format));
446
447 if (!exa->scrn->is_format_supported(exa->scrn, src_priv->tex->format,
448 src_priv->tex->target,
449 PIPE_TEXTURE_USAGE_SAMPLER, 0))
450 XORG_FALLBACK("pSrc format %s", pf_name(src_priv->tex->format));
451
452 exa->copy.src = src_priv;
453 exa->copy.dst = priv;
454
455 /* For same-surface copies, the pipe->surface_copy path is clearly
456 * superior, providing it is implemented. In other cases it's not
457 * clear what the better path would be, and eventually we'd
458 * probably want to gather timings and choose dynamically.
459 */
460 if (exa->pipe->surface_copy &&
461 exa->copy.src == exa->copy.dst) {
462
463 exa->copy.use_surface_copy = TRUE;
464
465 exa->copy.src_surface =
466 exa->scrn->get_tex_surface( exa->scrn,
467 exa->copy.src->tex,
468 0, 0, 0,
469 PIPE_BUFFER_USAGE_GPU_READ);
470
471 exa->copy.dst_surface =
472 exa->scrn->get_tex_surface( exa->scrn,
473 exa->copy.dst->tex,
474 0, 0, 0,
475 PIPE_BUFFER_USAGE_GPU_WRITE );
476 }
477 else {
478 exa->copy.use_surface_copy = FALSE;
479
480 if (exa->copy.dst == exa->copy.src)
481 exa->copy.src_texture = renderer_clone_texture( exa->renderer,
482 exa->copy.src->tex );
483 else
484 pipe_texture_reference(&exa->copy.src_texture,
485 exa->copy.src->tex);
486
487 exa->copy.dst_surface =
488 exa->scrn->get_tex_surface(exa->scrn,
489 exa->copy.dst->tex,
490 0, 0, 0,
491 PIPE_BUFFER_USAGE_GPU_WRITE);
492
493
494 renderer_copy_prepare(exa->renderer,
495 exa->copy.dst_surface,
496 exa->copy.src_texture );
497 }
498
499
500 return TRUE;
501 }
502
503 static void
504 ExaCopy(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX, int dstY,
505 int width, int height)
506 {
507 ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
508 modesettingPtr ms = modesettingPTR(pScrn);
509 struct exa_context *exa = ms->exa;
510 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
511
512 #if DEBUG_PRINT
513 debug_printf("\tExaCopy(srcx=%d, srcy=%d, dstX=%d, dstY=%d, w=%d, h=%d)\n",
514 srcX, srcY, dstX, dstY, width, height);
515 #endif
516
517 debug_assert(priv == exa->copy.dst);
518 (void) priv;
519
520 if (exa->copy.use_surface_copy) {
521 /* XXX: consider exposing >1 box in surface_copy interface.
522 */
523 exa->pipe->surface_copy( exa->pipe,
524 exa->copy.dst_surface,
525 dstX, dstY,
526 exa->copy.src_surface,
527 srcX, srcY,
528 width, height );
529 }
530 else {
531 renderer_copy_pixmap(exa->renderer,
532 dstX, dstY,
533 srcX, srcY,
534 width, height,
535 exa->copy.src_texture->width[0],
536 exa->copy.src_texture->height[0]);
537 }
538 }
539
540 static void
541 ExaDoneCopy(PixmapPtr pPixmap)
542 {
543 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
544 modesettingPtr ms = modesettingPTR(pScrn);
545 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
546 struct exa_context *exa = ms->exa;
547
548 if (!priv)
549 return;
550
551 renderer_draw_flush(exa->renderer);
552
553 exa->copy.src = NULL;
554 exa->copy.dst = NULL;
555 pipe_surface_reference(&exa->copy.src_surface, NULL);
556 pipe_surface_reference(&exa->copy.dst_surface, NULL);
557 pipe_texture_reference(&exa->copy.src_texture, NULL);
558 }
559
560
561
562 static Bool
563 picture_check_formats(struct exa_pixmap_priv *pSrc, PicturePtr pSrcPicture)
564 {
565 if (pSrc->picture_format == pSrcPicture->format)
566 return TRUE;
567
568 if (pSrc->picture_format != PICT_a8r8g8b8)
569 return FALSE;
570
571 /* pSrc->picture_format == PICT_a8r8g8b8 */
572 switch (pSrcPicture->format) {
573 case PICT_a8r8g8b8:
574 case PICT_x8r8g8b8:
575 case PICT_a8b8g8r8:
576 case PICT_x8b8g8r8:
577 /* just treat these two as x8... */
578 case PICT_r8g8b8:
579 case PICT_b8g8r8:
580 return TRUE;
581 #ifdef PICT_TYPE_BGRA
582 case PICT_b8g8r8a8:
583 case PICT_b8g8r8x8:
584 return FALSE; /* does not support swizzleing the alpha channel yet */
585 case PICT_a2r10g10b10:
586 case PICT_x2r10g10b10:
587 case PICT_a2b10g10r10:
588 case PICT_x2b10g10r10:
589 return FALSE;
590 #endif
591 default:
592 return FALSE;
593 }
594 return FALSE;
595 }
596
597 /***********************************************************************
598 * Composite entrypoints
599 */
600
601 static Bool
602 ExaCheckComposite(int op,
603 PicturePtr pSrcPicture, PicturePtr pMaskPicture,
604 PicturePtr pDstPicture)
605 {
606 ScrnInfoPtr pScrn = xf86Screens[pDstPicture->pDrawable->pScreen->myNum];
607 modesettingPtr ms = modesettingPTR(pScrn);
608 struct exa_context *exa = ms->exa;
609
610 #if DEBUG_PRINT
611 debug_printf("ExaCheckComposite(%d, %p, %p, %p) = %d\n",
612 op, pSrcPicture, pMaskPicture, pDstPicture, accelerated);
613 #endif
614
615 if (!exa->accel)
616 return FALSE;
617
618 return xorg_composite_accelerated(op,
619 pSrcPicture,
620 pMaskPicture,
621 pDstPicture);
622 }
623
624
625 static Bool
626 ExaPrepareComposite(int op, PicturePtr pSrcPicture,
627 PicturePtr pMaskPicture, PicturePtr pDstPicture,
628 PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
629 {
630 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
631 modesettingPtr ms = modesettingPTR(pScrn);
632 struct exa_context *exa = ms->exa;
633 struct exa_pixmap_priv *priv;
634
635 if (!exa->accel)
636 return FALSE;
637
638 #if DEBUG_PRINT
639 debug_printf("ExaPrepareComposite(%d, src=0x%p, mask=0x%p, dst=0x%p)\n",
640 op, pSrcPicture, pMaskPicture, pDstPicture);
641 debug_printf("\tFormats: src(%s), mask(%s), dst(%s)\n",
642 pSrcPicture ? render_format_name(pSrcPicture->format) : "none",
643 pMaskPicture ? render_format_name(pMaskPicture->format) : "none",
644 pDstPicture ? render_format_name(pDstPicture->format) : "none");
645 #endif
646 if (!exa->pipe)
647 XORG_FALLBACK("accle not enabled");
648
649 priv = exaGetPixmapDriverPrivate(pDst);
650 if (!priv || !priv->tex)
651 XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
652
653 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
654 priv->tex->target,
655 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
656 XORG_FALLBACK("pDst format: %s", pf_name(priv->tex->format));
657
658 if (priv->picture_format != pDstPicture->format)
659 XORG_FALLBACK("pDst pic_format: %s != %s",
660 render_format_name(priv->picture_format),
661 render_format_name(pDstPicture->format));
662
663 if (pSrc) {
664 priv = exaGetPixmapDriverPrivate(pSrc);
665 if (!priv || !priv->tex)
666 XORG_FALLBACK("pSrc %s", !priv ? "!priv" : "!priv->tex");
667
668 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
669 priv->tex->target,
670 PIPE_TEXTURE_USAGE_SAMPLER, 0))
671 XORG_FALLBACK("pSrc format: %s", pf_name(priv->tex->format));
672
673 if (!picture_check_formats(priv, pSrcPicture))
674 XORG_FALLBACK("pSrc pic_format: %s != %s",
675 render_format_name(priv->picture_format),
676 render_format_name(pSrcPicture->format));
677
678 }
679
680 if (pMask) {
681 priv = exaGetPixmapDriverPrivate(pMask);
682 if (!priv || !priv->tex)
683 XORG_FALLBACK("pMask %s", !priv ? "!priv" : "!priv->tex");
684
685 if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
686 priv->tex->target,
687 PIPE_TEXTURE_USAGE_SAMPLER, 0))
688 XORG_FALLBACK("pMask format: %s", pf_name(priv->tex->format));
689
690 if (!picture_check_formats(priv, pMaskPicture))
691 XORG_FALLBACK("pMask pic_format: %s != %s",
692 render_format_name(priv->picture_format),
693 render_format_name(pMaskPicture->format));
694 }
695
696 return xorg_composite_bind_state(exa, op, pSrcPicture, pMaskPicture,
697 pDstPicture,
698 pSrc ? exaGetPixmapDriverPrivate(pSrc) : NULL,
699 pMask ? exaGetPixmapDriverPrivate(pMask) : NULL,
700 exaGetPixmapDriverPrivate(pDst));
701 }
702
703 static void
704 ExaComposite(PixmapPtr pDst, int srcX, int srcY, int maskX, int maskY,
705 int dstX, int dstY, int width, int height)
706 {
707 ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
708 modesettingPtr ms = modesettingPTR(pScrn);
709 struct exa_context *exa = ms->exa;
710 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDst);
711
712 #if DEBUG_PRINT
713 debug_printf("\tExaComposite(src[%d,%d], mask=[%d, %d], dst=[%d, %d], dim=[%d, %d])\n",
714 srcX, srcY, maskX, maskY, dstX, dstY, width, height);
715 debug_printf("\t Num bound samplers = %d\n",
716 exa->num_bound_samplers);
717 #endif
718
719 xorg_composite(exa, priv, srcX, srcY, maskX, maskY,
720 dstX, dstY, width, height);
721 }
722
723
724
725 static void
726 ExaDoneComposite(PixmapPtr pPixmap)
727 {
728 ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
729 modesettingPtr ms = modesettingPTR(pScrn);
730 struct exa_context *exa = ms->exa;
731
732 xorg_composite_done(exa);
733 }
734
735
736 /***********************************************************************
737 * Pixmaps
738 */
739
740 static void *
741 ExaCreatePixmap(ScreenPtr pScreen, int size, int align)
742 {
743 struct exa_pixmap_priv *priv;
744
745 priv = xcalloc(1, sizeof(struct exa_pixmap_priv));
746 if (!priv)
747 return NULL;
748
749 return priv;
750 }
751
752 static void
753 ExaDestroyPixmap(ScreenPtr pScreen, void *dPriv)
754 {
755 struct exa_pixmap_priv *priv = (struct exa_pixmap_priv *)dPriv;
756
757 if (!priv)
758 return;
759
760 pipe_texture_reference(&priv->tex, NULL);
761
762 xfree(priv);
763 }
764
765 static Bool
766 ExaPixmapIsOffscreen(PixmapPtr pPixmap)
767 {
768 struct exa_pixmap_priv *priv;
769
770 priv = exaGetPixmapDriverPrivate(pPixmap);
771
772 if (!priv)
773 return FALSE;
774
775 if (priv->tex)
776 return TRUE;
777
778 return FALSE;
779 }
780
781 int
782 xorg_exa_set_displayed_usage(PixmapPtr pPixmap)
783 {
784 struct exa_pixmap_priv *priv;
785 priv = exaGetPixmapDriverPrivate(pPixmap);
786
787 if (!priv) {
788 FatalError("NO PIXMAP PRIVATE\n");
789 return 0;
790 }
791
792 priv->flags |= PIPE_TEXTURE_USAGE_PRIMARY;
793
794 return 0;
795 }
796
797 int
798 xorg_exa_set_shared_usage(PixmapPtr pPixmap)
799 {
800 struct exa_pixmap_priv *priv;
801 priv = exaGetPixmapDriverPrivate(pPixmap);
802
803 if (!priv) {
804 FatalError("NO PIXMAP PRIVATE\n");
805 return 0;
806 }
807
808 priv->flags |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
809
810 return 0;
811 }
812
813 unsigned
814 xorg_exa_get_pixmap_handle(PixmapPtr pPixmap, unsigned *stride_out)
815 {
816 ScreenPtr pScreen = pPixmap->drawable.pScreen;
817 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
818 modesettingPtr ms = modesettingPTR(pScrn);
819 struct exa_pixmap_priv *priv;
820 unsigned handle;
821 unsigned stride;
822
823 if (!ms->exa) {
824 FatalError("NO MS->EXA\n");
825 return 0;
826 }
827
828 priv = exaGetPixmapDriverPrivate(pPixmap);
829
830 if (!priv) {
831 FatalError("NO PIXMAP PRIVATE\n");
832 return 0;
833 }
834
835 ms->api->local_handle_from_texture(ms->api, ms->screen, priv->tex, &stride, &handle);
836 if (stride_out)
837 *stride_out = stride;
838
839 return handle;
840 }
841
842 static Bool
843 size_match( int width, int tex_width )
844 {
845 #if ROUND_UP_TEXTURES
846 if (width > tex_width)
847 return FALSE;
848
849 if (width * 2 < tex_width)
850 return FALSE;
851
852 return TRUE;
853 #else
854 return width == tex_width;
855 #endif
856 }
857
858 static Bool
859 ExaModifyPixmapHeader(PixmapPtr pPixmap, int width, int height,
860 int depth, int bitsPerPixel, int devKind,
861 pointer pPixData)
862 {
863 ScreenPtr pScreen = pPixmap->drawable.pScreen;
864 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
865 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
866 modesettingPtr ms = modesettingPTR(pScrn);
867 struct exa_context *exa = ms->exa;
868
869 if (!priv || pPixData)
870 return FALSE;
871
872 if (0) {
873 debug_printf("%s pixmap %p sz %dx%dx%d devKind %d\n",
874 __FUNCTION__, pPixmap, width, height, bitsPerPixel, devKind);
875
876 if (priv->tex)
877 debug_printf(" ==> old texture %dx%d\n",
878 priv->tex->width[0],
879 priv->tex->height[0]);
880 }
881
882
883 if (depth <= 0)
884 depth = pPixmap->drawable.depth;
885
886 if (bitsPerPixel <= 0)
887 bitsPerPixel = pPixmap->drawable.bitsPerPixel;
888
889 if (width <= 0)
890 width = pPixmap->drawable.width;
891
892 if (height <= 0)
893 height = pPixmap->drawable.height;
894
895 if (width <= 0 || height <= 0 || depth <= 0)
896 return FALSE;
897
898 miModifyPixmapHeader(pPixmap, width, height, depth,
899 bitsPerPixel, devKind, NULL);
900
901 priv->width = width;
902 priv->height = height;
903
904 /* Deal with screen resize */
905 if ((exa->accel || priv->flags) &&
906 (!priv->tex ||
907 !size_match(width, priv->tex->width[0]) ||
908 !size_match(height, priv->tex->height[0]) ||
909 priv->tex_flags != priv->flags)) {
910 struct pipe_texture *texture = NULL;
911 struct pipe_texture template;
912
913 memset(&template, 0, sizeof(template));
914 template.target = PIPE_TEXTURE_2D;
915 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &priv->picture_format);
916 pf_get_block(template.format, &template.block);
917 if (ROUND_UP_TEXTURES && priv->flags == 0) {
918 template.width[0] = util_next_power_of_two(width);
919 template.height[0] = util_next_power_of_two(height);
920 }
921 else {
922 template.width[0] = width;
923 template.height[0] = height;
924 }
925
926 template.depth[0] = 1;
927 template.last_level = 0;
928 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET | priv->flags;
929 priv->tex_flags = priv->flags;
930 texture = exa->scrn->texture_create(exa->scrn, &template);
931
932 if (priv->tex) {
933 struct pipe_surface *dst_surf;
934 struct pipe_surface *src_surf;
935
936 dst_surf = exa->scrn->get_tex_surface(
937 exa->scrn, texture, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE);
938 src_surf = xorg_gpu_surface(exa->pipe->screen, priv);
939 if (exa->pipe->surface_copy) {
940 exa->pipe->surface_copy(exa->pipe, dst_surf, 0, 0, src_surf,
941 0, 0, min(width, texture->width[0]),
942 min(height, texture->height[0]));
943 } else {
944 util_surface_copy(exa->pipe, FALSE, dst_surf, 0, 0, src_surf,
945 0, 0, min(width, texture->width[0]),
946 min(height, texture->height[0]));
947 }
948 exa->scrn->tex_surface_destroy(dst_surf);
949 exa->scrn->tex_surface_destroy(src_surf);
950 }
951
952 pipe_texture_reference(&priv->tex, texture);
953 /* the texture we create has one reference */
954 pipe_texture_reference(&texture, NULL);
955 }
956
957 return TRUE;
958 }
959
960 struct pipe_texture *
961 xorg_exa_get_texture(PixmapPtr pPixmap)
962 {
963 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
964 struct pipe_texture *tex = NULL;
965 pipe_texture_reference(&tex, priv->tex);
966 return tex;
967 }
968
969 Bool
970 xorg_exa_set_texture(PixmapPtr pPixmap, struct pipe_texture *tex)
971 {
972 struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
973
974 int mask = PIPE_TEXTURE_USAGE_PRIMARY | PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
975
976 if (!priv)
977 return FALSE;
978
979 if (pPixmap->drawable.width != tex->width[0] ||
980 pPixmap->drawable.height != tex->height[0])
981 return FALSE;
982
983 pipe_texture_reference(&priv->tex, tex);
984 priv->tex_flags = tex->tex_usage & mask;
985
986 return TRUE;
987 }
988
989 struct pipe_texture *
990 xorg_exa_create_root_texture(ScrnInfoPtr pScrn,
991 int width, int height,
992 int depth, int bitsPerPixel)
993 {
994 modesettingPtr ms = modesettingPTR(pScrn);
995 struct exa_context *exa = ms->exa;
996 struct pipe_texture template;
997 int dummy;
998
999 memset(&template, 0, sizeof(template));
1000 template.target = PIPE_TEXTURE_2D;
1001 exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &dummy);
1002 pf_get_block(template.format, &template.block);
1003 template.width[0] = width;
1004 template.height[0] = height;
1005 template.depth[0] = 1;
1006 template.last_level = 0;
1007 template.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
1008 template.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
1009 template.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
1010
1011 return exa->scrn->texture_create(exa->scrn, &template);
1012 }
1013
1014 void
1015 xorg_exa_close(ScrnInfoPtr pScrn)
1016 {
1017 modesettingPtr ms = modesettingPTR(pScrn);
1018 struct exa_context *exa = ms->exa;
1019
1020 renderer_destroy(exa->renderer);
1021
1022 if (exa->pipe)
1023 exa->pipe->destroy(exa->pipe);
1024 exa->pipe = NULL;
1025 /* Since this was shared be proper with the pointer */
1026 ms->ctx = NULL;
1027
1028 exaDriverFini(pScrn->pScreen);
1029 xfree(exa);
1030 ms->exa = NULL;
1031 }
1032
1033 void *
1034 xorg_exa_init(ScrnInfoPtr pScrn, Bool accel)
1035 {
1036 modesettingPtr ms = modesettingPTR(pScrn);
1037 struct exa_context *exa;
1038 ExaDriverPtr pExa;
1039
1040 exa = xcalloc(1, sizeof(struct exa_context));
1041 if (!exa)
1042 return NULL;
1043
1044 pExa = exaDriverAlloc();
1045 if (!pExa) {
1046 goto out_err;
1047 }
1048
1049 memset(pExa, 0, sizeof(*pExa));
1050
1051 pExa->exa_major = 2;
1052 pExa->exa_minor = 2;
1053 pExa->memoryBase = 0;
1054 pExa->memorySize = 0;
1055 pExa->offScreenBase = 0;
1056 pExa->pixmapOffsetAlign = 0;
1057 pExa->pixmapPitchAlign = 1;
1058 pExa->flags = EXA_OFFSCREEN_PIXMAPS | EXA_HANDLES_PIXMAPS;
1059 #ifdef EXA_SUPPORTS_PREPARE_AUX
1060 pExa->flags |= EXA_SUPPORTS_PREPARE_AUX;
1061 #endif
1062 #ifdef EXA_MIXED_PIXMAPS
1063 pExa->flags |= EXA_MIXED_PIXMAPS;
1064 #endif
1065 pExa->maxX = 8191; /* FIXME */
1066 pExa->maxY = 8191; /* FIXME */
1067
1068 pExa->WaitMarker = ExaWaitMarker;
1069 pExa->MarkSync = ExaMarkSync;
1070 pExa->PrepareSolid = ExaPrepareSolid;
1071 pExa->Solid = ExaSolid;
1072 pExa->DoneSolid = ExaDoneSolid;
1073 pExa->PrepareCopy = ExaPrepareCopy;
1074 pExa->Copy = ExaCopy;
1075 pExa->DoneCopy = ExaDoneCopy;
1076 pExa->CheckComposite = ExaCheckComposite;
1077 pExa->PrepareComposite = ExaPrepareComposite;
1078 pExa->Composite = ExaComposite;
1079 pExa->DoneComposite = ExaDoneComposite;
1080 pExa->PixmapIsOffscreen = ExaPixmapIsOffscreen;
1081 pExa->DownloadFromScreen = ExaDownloadFromScreen;
1082 pExa->UploadToScreen = ExaUploadToScreen;
1083 pExa->PrepareAccess = ExaPrepareAccess;
1084 pExa->FinishAccess = ExaFinishAccess;
1085 pExa->CreatePixmap = ExaCreatePixmap;
1086 pExa->DestroyPixmap = ExaDestroyPixmap;
1087 pExa->ModifyPixmapHeader = ExaModifyPixmapHeader;
1088
1089 if (!exaDriverInit(pScrn->pScreen, pExa)) {
1090 goto out_err;
1091 }
1092
1093 exa->scrn = ms->screen;
1094 exa->pipe = ms->api->create_context(ms->api, exa->scrn);
1095 /* Share context with DRI */
1096 ms->ctx = exa->pipe;
1097
1098 exa->renderer = renderer_create(exa->pipe);
1099 exa->accel = accel;
1100
1101 return (void *)exa;
1102
1103 out_err:
1104 xorg_exa_close(pScrn);
1105
1106 return NULL;
1107 }
1108
1109 struct pipe_surface *
1110 xorg_gpu_surface(struct pipe_screen *scrn, struct exa_pixmap_priv *priv)
1111 {
1112 return scrn->get_tex_surface(scrn, priv->tex, 0, 0, 0,
1113 PIPE_BUFFER_USAGE_GPU_READ |
1114 PIPE_BUFFER_USAGE_GPU_WRITE);
1115
1116 }
1117
1118 void xorg_exa_flush(struct exa_context *exa, uint pipeFlushFlags,
1119 struct pipe_fence_handle **fence)
1120 {
1121 exa->pipe->flush(exa->pipe, pipeFlushFlags, fence);
1122 }
1123
1124 void xorg_exa_finish(struct exa_context *exa)
1125 {
1126 struct pipe_fence_handle *fence = NULL;
1127
1128 xorg_exa_flush(exa, PIPE_FLUSH_RENDER_CACHE, &fence);
1129
1130 exa->pipe->screen->fence_finish(exa->pipe->screen, fence, 0);
1131 exa->pipe->screen->fence_reference(exa->pipe->screen, &fence, NULL);
1132 }
1133