xorg_exa.c revision 4c7001462607e6e99e474d6271dd481d3f8f201c
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("accle 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, 0)) {
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("accle not enabled");
418
419    if (!priv || !priv->tex)
420	XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
421
422    if (!src_priv || !src_priv->tex)
423	XORG_FALLBACK("pSrc %s", !src_priv ? "!priv" : "!priv->tex");
424
425    if (!EXA_PM_IS_SOLID(&pSrcPixmap->drawable, planeMask))
426	XORG_FALLBACK("planeMask is not solid");
427
428    if (alu != GXcopy)
429	XORG_FALLBACK("alu not GXcopy");
430
431    if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
432                                        priv->tex->target, 0,
433                                        PIPE_BIND_RENDER_TARGET, 0))
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, 0))
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
588#if DEBUG_PRINT
589   debug_printf("ExaCheckComposite(%d, %p, %p, %p) = %d\n",
590                op, pSrcPicture, pMaskPicture, pDstPicture, accelerated);
591#endif
592
593   if (!exa->accel)
594       return FALSE;
595
596   return xorg_composite_accelerated(op,
597				     pSrcPicture,
598				     pMaskPicture,
599				     pDstPicture);
600}
601
602
603static Bool
604ExaPrepareComposite(int op, PicturePtr pSrcPicture,
605		    PicturePtr pMaskPicture, PicturePtr pDstPicture,
606		    PixmapPtr pSrc, PixmapPtr pMask, PixmapPtr pDst)
607{
608   ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
609   modesettingPtr ms = modesettingPTR(pScrn);
610   struct exa_context *exa = ms->exa;
611   struct exa_pixmap_priv *priv;
612
613   if (!exa->accel)
614       return FALSE;
615
616#if DEBUG_PRINT
617   debug_printf("ExaPrepareComposite(%d, src=0x%p, mask=0x%p, dst=0x%p)\n",
618                op, pSrcPicture, pMaskPicture, pDstPicture);
619   debug_printf("\tFormats: src(%s), mask(%s), dst(%s)\n",
620                pSrcPicture ? render_format_name(pSrcPicture->format) : "none",
621                pMaskPicture ? render_format_name(pMaskPicture->format) : "none",
622                pDstPicture ? render_format_name(pDstPicture->format) : "none");
623#endif
624   if (!exa->pipe)
625      XORG_FALLBACK("accle not enabled");
626
627   priv = exaGetPixmapDriverPrivate(pDst);
628   if (!priv || !priv->tex)
629      XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
630
631   if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
632                                       priv->tex->target, 0,
633                                       PIPE_BIND_RENDER_TARGET, 0))
634      XORG_FALLBACK("pDst format: %s", util_format_name(priv->tex->format));
635
636   if (priv->picture_format != pDstPicture->format)
637      XORG_FALLBACK("pDst pic_format: %s != %s",
638                    render_format_name(priv->picture_format),
639                    render_format_name(pDstPicture->format));
640
641   if (pSrc) {
642      priv = exaGetPixmapDriverPrivate(pSrc);
643      if (!priv || !priv->tex)
644         XORG_FALLBACK("pSrc %s", !priv ? "!priv" : "!priv->tex");
645
646      if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
647                                          priv->tex->target, 0,
648                                          PIPE_BIND_SAMPLER_VIEW, 0))
649         XORG_FALLBACK("pSrc format: %s", util_format_name(priv->tex->format));
650
651      if (!picture_check_formats(priv, pSrcPicture))
652         XORG_FALLBACK("pSrc pic_format: %s != %s",
653                       render_format_name(priv->picture_format),
654                       render_format_name(pSrcPicture->format));
655
656   }
657
658   if (pMask) {
659      priv = exaGetPixmapDriverPrivate(pMask);
660      if (!priv || !priv->tex)
661         XORG_FALLBACK("pMask %s", !priv ? "!priv" : "!priv->tex");
662
663      if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
664                                          priv->tex->target, 0,
665                                          PIPE_BIND_SAMPLER_VIEW, 0))
666         XORG_FALLBACK("pMask format: %s", util_format_name(priv->tex->format));
667
668      if (!picture_check_formats(priv, pMaskPicture))
669         XORG_FALLBACK("pMask pic_format: %s != %s",
670                       render_format_name(priv->picture_format),
671                       render_format_name(pMaskPicture->format));
672   }
673
674   return xorg_composite_bind_state(exa, op, pSrcPicture, pMaskPicture,
675                                    pDstPicture,
676                                    pSrc ? exaGetPixmapDriverPrivate(pSrc) : NULL,
677                                    pMask ? exaGetPixmapDriverPrivate(pMask) : NULL,
678                                    exaGetPixmapDriverPrivate(pDst));
679}
680
681static void
682ExaComposite(PixmapPtr pDst, int srcX, int srcY, int maskX, int maskY,
683	     int dstX, int dstY, int width, int height)
684{
685   ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
686   modesettingPtr ms = modesettingPTR(pScrn);
687   struct exa_context *exa = ms->exa;
688   struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDst);
689
690#if DEBUG_PRINT
691   debug_printf("\tExaComposite(src[%d,%d], mask=[%d, %d], dst=[%d, %d], dim=[%d, %d])\n",
692                srcX, srcY, maskX, maskY, dstX, dstY, width, height);
693   debug_printf("\t   Num bound samplers = %d\n",
694                exa->num_bound_samplers);
695#endif
696
697   xorg_composite(exa, priv, srcX, srcY, maskX, maskY,
698                  dstX, dstY, width, height);
699}
700
701
702
703static void
704ExaDoneComposite(PixmapPtr pPixmap)
705{
706   ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
707   modesettingPtr ms = modesettingPTR(pScrn);
708   struct exa_context *exa = ms->exa;
709
710   xorg_composite_done(exa);
711}
712
713
714/***********************************************************************
715 * Pixmaps
716 */
717
718static void *
719ExaCreatePixmap(ScreenPtr pScreen, int size, int align)
720{
721    struct exa_pixmap_priv *priv;
722
723    priv = calloc(1, sizeof(struct exa_pixmap_priv));
724    if (!priv)
725	return NULL;
726
727    return priv;
728}
729
730static void
731ExaDestroyPixmap(ScreenPtr pScreen, void *dPriv)
732{
733    struct exa_pixmap_priv *priv = (struct exa_pixmap_priv *)dPriv;
734
735    if (!priv)
736	return;
737
738    pipe_resource_reference(&priv->tex, NULL);
739
740    free(priv);
741}
742
743static Bool
744ExaPixmapIsOffscreen(PixmapPtr pPixmap)
745{
746    struct exa_pixmap_priv *priv;
747
748    priv = exaGetPixmapDriverPrivate(pPixmap);
749
750    if (!priv)
751	return FALSE;
752
753    if (priv->tex)
754	return TRUE;
755
756    return FALSE;
757}
758
759int
760xorg_exa_set_displayed_usage(PixmapPtr pPixmap)
761{
762    struct exa_pixmap_priv *priv;
763    priv = exaGetPixmapDriverPrivate(pPixmap);
764
765    if (!priv) {
766	FatalError("NO PIXMAP PRIVATE\n");
767	return 0;
768    }
769
770    priv->flags |= PIPE_BIND_SCANOUT;
771
772    return 0;
773}
774
775int
776xorg_exa_set_shared_usage(PixmapPtr pPixmap)
777{
778    struct exa_pixmap_priv *priv;
779    priv = exaGetPixmapDriverPrivate(pPixmap);
780
781    if (!priv) {
782	FatalError("NO PIXMAP PRIVATE\n");
783	return 0;
784    }
785
786    priv->flags |= PIPE_BIND_SHARED;
787
788    return 0;
789}
790
791
792
793static Bool
794size_match( int width, int tex_width )
795{
796#if ROUND_UP_TEXTURES
797   if (width > tex_width)
798      return FALSE;
799
800   if (width * 2 < tex_width)
801      return FALSE;
802
803   return TRUE;
804#else
805   return width == tex_width;
806#endif
807}
808
809static Bool
810ExaModifyPixmapHeader(PixmapPtr pPixmap, int width, int height,
811		      int depth, int bitsPerPixel, int devKind,
812		      pointer pPixData)
813{
814    ScreenPtr pScreen = pPixmap->drawable.pScreen;
815    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
816    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
817    modesettingPtr ms = modesettingPTR(pScrn);
818    struct exa_context *exa = ms->exa;
819
820    if (!priv || pPixData)
821	return FALSE;
822
823    if (0) {
824       debug_printf("%s pixmap %p sz %dx%dx%d devKind %d\n",
825                    __FUNCTION__, pPixmap, width, height, bitsPerPixel, devKind);
826
827       if (priv->tex)
828          debug_printf("  ==> old texture %dx%d\n",
829                       priv->tex->width0,
830                       priv->tex->height0);
831    }
832
833
834    if (depth <= 0)
835	depth = pPixmap->drawable.depth;
836
837    if (bitsPerPixel <= 0)
838	bitsPerPixel = pPixmap->drawable.bitsPerPixel;
839
840    if (width <= 0)
841	width = pPixmap->drawable.width;
842
843    if (height <= 0)
844	height = pPixmap->drawable.height;
845
846    if (width <= 0 || height <= 0 || depth <= 0)
847	return FALSE;
848
849    miModifyPixmapHeader(pPixmap, width, height, depth,
850			     bitsPerPixel, devKind, NULL);
851
852    priv->width = width;
853    priv->height = height;
854
855    /* Deal with screen resize */
856    if ((exa->accel || priv->flags) &&
857        (!priv->tex ||
858         !size_match(width, priv->tex->width0) ||
859         !size_match(height, priv->tex->height0) ||
860         priv->tex_flags != priv->flags)) {
861	struct pipe_resource *texture = NULL;
862	struct pipe_resource template;
863
864	memset(&template, 0, sizeof(template));
865	template.target = PIPE_TEXTURE_2D;
866	exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &priv->picture_format);
867        if (ROUND_UP_TEXTURES && priv->flags == 0) {
868           template.width0 = util_next_power_of_two(width);
869           template.height0 = util_next_power_of_two(height);
870        }
871        else {
872           template.width0 = width;
873           template.height0 = height;
874        }
875
876	template.depth0 = 1;
877	template.array_size = 1;
878	template.last_level = 0;
879	template.bind = PIPE_BIND_RENDER_TARGET | priv->flags;
880	priv->tex_flags = priv->flags;
881	texture = exa->scrn->resource_create(exa->scrn, &template);
882
883	if (priv->tex) {
884            struct pipe_box src_box;
885            u_box_origin_2d(min(width, texture->width0),
886                            min(height, texture->height0),
887                            &src_box);
888            exa->pipe->resource_copy_region(exa->pipe, texture,
889                                            0, 0, 0, 0,
890                                            priv->tex,
891                                            0, &src_box);
892	}
893
894	pipe_resource_reference(&priv->tex, texture);
895	/* the texture we create has one reference */
896	pipe_resource_reference(&texture, NULL);
897    }
898
899    return TRUE;
900}
901
902struct pipe_resource *
903xorg_exa_get_texture(PixmapPtr pPixmap)
904{
905   struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
906   struct pipe_resource *tex = NULL;
907   pipe_resource_reference(&tex, priv->tex);
908   return tex;
909}
910
911Bool
912xorg_exa_set_texture(PixmapPtr pPixmap, struct  pipe_resource *tex)
913{
914    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
915
916    int mask = PIPE_BIND_SHARED | PIPE_BIND_SCANOUT;
917
918    if (!priv)
919	return FALSE;
920
921    if (pPixmap->drawable.width != tex->width0 ||
922	pPixmap->drawable.height != tex->height0)
923	return FALSE;
924
925    pipe_resource_reference(&priv->tex, tex);
926    priv->tex_flags = tex->bind & mask;
927
928    return TRUE;
929}
930
931struct pipe_resource *
932xorg_exa_create_root_texture(ScrnInfoPtr pScrn,
933			     int width, int height,
934			     int depth, int bitsPerPixel)
935{
936    modesettingPtr ms = modesettingPTR(pScrn);
937    struct exa_context *exa = ms->exa;
938    struct pipe_resource template;
939    int dummy;
940
941    memset(&template, 0, sizeof(template));
942    template.target = PIPE_TEXTURE_2D;
943    exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &dummy);
944    template.width0 = width;
945    template.height0 = height;
946    template.depth0 = 1;
947    template.array_size = 1;
948    template.last_level = 0;
949    template.bind |= PIPE_BIND_RENDER_TARGET;
950    template.bind |= PIPE_BIND_SCANOUT;
951    template.bind |= PIPE_BIND_SHARED;
952
953    return exa->scrn->resource_create(exa->scrn, &template);
954}
955
956void
957xorg_exa_close(ScrnInfoPtr pScrn)
958{
959   modesettingPtr ms = modesettingPTR(pScrn);
960   struct exa_context *exa = ms->exa;
961
962   pipe_sampler_view_reference(&exa->bound_sampler_views[0], NULL);
963   pipe_sampler_view_reference(&exa->bound_sampler_views[1], NULL);
964
965   renderer_destroy(exa->renderer);
966
967   xorg_exa_finish(exa);
968
969   if (exa->pipe)
970      exa->pipe->destroy(exa->pipe);
971   exa->pipe = NULL;
972   /* Since this was shared be proper with the pointer */
973   ms->ctx = NULL;
974
975   exaDriverFini(pScrn->pScreen);
976   free(exa);
977   ms->exa = NULL;
978}
979
980void *
981xorg_exa_init(ScrnInfoPtr pScrn, Bool accel)
982{
983   modesettingPtr ms = modesettingPTR(pScrn);
984   struct exa_context *exa;
985   ExaDriverPtr pExa;
986   CustomizerPtr cust = ms->cust;
987
988   exa = calloc(1, sizeof(struct exa_context));
989   if (!exa)
990      return NULL;
991
992   pExa = exaDriverAlloc();
993   if (!pExa) {
994      goto out_err;
995   }
996
997   memset(pExa, 0, sizeof(*pExa));
998
999   pExa->exa_major         = 2;
1000   pExa->exa_minor         = 2;
1001   pExa->memoryBase        = 0;
1002   pExa->memorySize        = 0;
1003   pExa->offScreenBase     = 0;
1004   pExa->pixmapOffsetAlign = 0;
1005   pExa->pixmapPitchAlign  = 1;
1006   pExa->flags             = EXA_OFFSCREEN_PIXMAPS | EXA_HANDLES_PIXMAPS;
1007#ifdef EXA_SUPPORTS_PREPARE_AUX
1008   pExa->flags            |= EXA_SUPPORTS_PREPARE_AUX;
1009#endif
1010#ifdef EXA_MIXED_PIXMAPS
1011   pExa->flags            |= EXA_MIXED_PIXMAPS;
1012#endif
1013   pExa->maxX              = 8191; /* FIXME */
1014   pExa->maxY              = 8191; /* FIXME */
1015
1016   pExa->WaitMarker         = ExaWaitMarker;
1017   pExa->MarkSync           = ExaMarkSync;
1018   pExa->PrepareSolid       = ExaPrepareSolid;
1019   pExa->Solid              = ExaSolid;
1020   pExa->DoneSolid          = ExaDoneSolid;
1021   pExa->PrepareCopy        = ExaPrepareCopy;
1022   pExa->Copy               = ExaCopy;
1023   pExa->DoneCopy           = ExaDoneCopy;
1024   pExa->CheckComposite     = ExaCheckComposite;
1025   pExa->PrepareComposite   = ExaPrepareComposite;
1026   pExa->Composite          = ExaComposite;
1027   pExa->DoneComposite      = ExaDoneComposite;
1028   pExa->PixmapIsOffscreen  = ExaPixmapIsOffscreen;
1029   pExa->DownloadFromScreen = ExaDownloadFromScreen;
1030   pExa->UploadToScreen     = ExaUploadToScreen;
1031   pExa->PrepareAccess      = ExaPrepareAccess;
1032   pExa->FinishAccess       = ExaFinishAccess;
1033   pExa->CreatePixmap       = ExaCreatePixmap;
1034   pExa->DestroyPixmap      = ExaDestroyPixmap;
1035   pExa->ModifyPixmapHeader = ExaModifyPixmapHeader;
1036
1037   if (!exaDriverInit(pScrn->pScreen, pExa)) {
1038      goto out_err;
1039   }
1040
1041   exa->scrn = ms->screen;
1042   exa->pipe = exa->scrn->context_create(exa->scrn, NULL);
1043   if (exa->pipe == NULL)
1044      goto out_err;
1045
1046   /* Share context with DRI */
1047   ms->ctx = exa->pipe;
1048   if (cust && cust->winsys_context_throttle)
1049       cust->winsys_context_throttle(cust, ms->ctx, THROTTLE_RENDER);
1050
1051   exa->renderer = renderer_create(exa->pipe);
1052   exa->accel = accel;
1053
1054   return (void *)exa;
1055
1056out_err:
1057   xorg_exa_close(pScrn);
1058   free(exa);
1059
1060   return NULL;
1061}
1062
1063struct pipe_surface *
1064xorg_gpu_surface(struct pipe_context *pipe, struct exa_pixmap_priv *priv)
1065{
1066   struct pipe_surface surf_tmpl;
1067   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1068   u_surface_default_template(&surf_tmpl, priv->tex,
1069                              PIPE_BIND_RENDER_TARGET);
1070
1071   return pipe->create_surface(pipe, priv->tex, &surf_tmpl);
1072
1073}
1074
1075void xorg_exa_flush(struct exa_context *exa, uint pipeFlushFlags,
1076                    struct pipe_fence_handle **fence)
1077{
1078   exa->pipe->flush(exa->pipe, pipeFlushFlags, fence);
1079}
1080
1081void xorg_exa_finish(struct exa_context *exa)
1082{
1083   struct pipe_fence_handle *fence = NULL;
1084
1085   xorg_exa_flush(exa, PIPE_FLUSH_RENDER_CACHE, &fence);
1086
1087   exa->pipe->screen->fence_finish(exa->pipe->screen, fence, 0);
1088   exa->pipe->screen->fence_reference(exa->pipe->screen, &fence, NULL);
1089}
1090
1091