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