xorg_exa.c revision 2966a25d3b6f7e767feb082bff7a616cbd60427c
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
50#define DEBUG_PRINT 0
51#define ROUND_UP_TEXTURES 1
52
53/*
54 * Helper functions
55 */
56struct render_format_str {
57   int format;
58   const char *name;
59};
60static 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};
106static 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
116static void
117exa_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_B8G8R8A8_UNORM;
122	*picture_format = PICT_a8r8g8b8;
123	assert(*bbp == 32);
124	break;
125    case 24:
126	*format = PIPE_FORMAT_B8G8R8X8_UNORM;
127	*picture_format = PICT_x8r8g8b8;
128	assert(*bbp == 32);
129	break;
130    case 16:
131	*format = PIPE_FORMAT_B5G6R5_UNORM;
132	*picture_format = PICT_r5g6b5;
133	assert(*bbp == 16);
134	break;
135    case 15:
136	*format = PIPE_FORMAT_B5G5R5A1_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_B8G8R8A8_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
160static void
161ExaWaitMarker(ScreenPtr pScreen, int marker)
162{
163   /* Nothing to do, handled in the PrepareAccess hook */
164}
165
166static int
167ExaMarkSync(ScreenPtr pScreen)
168{
169   return 1;
170}
171
172
173/***********************************************************************
174 * Screen upload/download
175 */
176
177static Bool
178ExaDownloadFromScreen(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    transfer = pipe_get_transfer(exa->pipe, priv->tex, 0, 0, 0,
192					   PIPE_TRANSFER_READ, x, y, w, h);
193    if (!transfer)
194	return FALSE;
195
196#if DEBUG_PRINT
197    debug_printf("------ ExaDownloadFromScreen(%d, %d, %d, %d, %d)\n",
198                 x, y, w, h, dst_pitch);
199#endif
200
201    util_copy_rect((unsigned char*)dst, priv->tex->format, dst_pitch, 0, 0,
202		   w, h, exa->pipe->transfer_map(exa->pipe, transfer),
203		   transfer->stride, 0, 0);
204
205    exa->pipe->transfer_unmap(exa->pipe, transfer);
206    exa->pipe->transfer_destroy(exa->pipe, transfer);
207
208    return TRUE;
209}
210
211static Bool
212ExaUploadToScreen(PixmapPtr pPix, int x, int y, int w, int h, char *src,
213		  int src_pitch)
214{
215    ScreenPtr pScreen = pPix->drawable.pScreen;
216    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
217    modesettingPtr ms = modesettingPTR(pScrn);
218    struct exa_context *exa = ms->exa;
219    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPix);
220    struct pipe_transfer *transfer;
221
222    if (!priv || !priv->tex)
223	return FALSE;
224
225    transfer = pipe_get_transfer(exa->pipe, priv->tex, 0, 0, 0,
226					   PIPE_TRANSFER_WRITE, x, y, w, h);
227    if (!transfer)
228	return FALSE;
229
230#if DEBUG_PRINT
231    debug_printf("++++++ ExaUploadToScreen(%d, %d, %d, %d, %d)\n",
232                 x, y, w, h, src_pitch);
233#endif
234
235    util_copy_rect(exa->pipe->transfer_map(exa->pipe, transfer),
236		   priv->tex->format, transfer->stride, 0, 0, w, h,
237		   (unsigned char*)src, src_pitch, 0, 0);
238
239    exa->pipe->transfer_unmap(exa->pipe, transfer);
240    exa->pipe->transfer_destroy(exa->pipe, transfer);
241
242    return TRUE;
243}
244
245static Bool
246ExaPrepareAccess(PixmapPtr pPix, int index)
247{
248    ScreenPtr pScreen = pPix->drawable.pScreen;
249    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
250    modesettingPtr ms = modesettingPTR(pScrn);
251    struct exa_context *exa = ms->exa;
252    struct exa_pixmap_priv *priv;
253
254    priv = exaGetPixmapDriverPrivate(pPix);
255
256    if (!priv)
257	return FALSE;
258
259    if (!priv->tex)
260	return FALSE;
261
262    if (priv->map_count == 0)
263    {
264        assert(pPix->drawable.width <= priv->tex->width0);
265        assert(pPix->drawable.height <= priv->tex->height0);
266
267	priv->map_transfer =
268	   pipe_get_transfer(exa->pipe, priv->tex, 0, 0, 0,
269#ifdef EXA_MIXED_PIXMAPS
270					PIPE_TRANSFER_MAP_DIRECTLY |
271#endif
272					PIPE_TRANSFER_READ_WRITE,
273					0, 0,
274                                        pPix->drawable.width,
275                                        pPix->drawable.height );
276	if (!priv->map_transfer)
277#ifdef EXA_MIXED_PIXMAPS
278	    return FALSE;
279#else
280	    FatalError("failed to create transfer\n");
281#endif
282
283	pPix->devPrivate.ptr =
284	    exa->pipe->transfer_map(exa->pipe, priv->map_transfer);
285	pPix->devKind = priv->map_transfer->stride;
286    }
287
288    priv->map_count++;
289
290    return TRUE;
291}
292
293static void
294ExaFinishAccess(PixmapPtr pPix, int index)
295{
296    ScreenPtr pScreen = pPix->drawable.pScreen;
297    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
298    modesettingPtr ms = modesettingPTR(pScrn);
299    struct exa_context *exa = ms->exa;
300    struct exa_pixmap_priv *priv;
301    priv = exaGetPixmapDriverPrivate(pPix);
302
303    if (!priv)
304	return;
305
306    if (!priv->map_transfer)
307	return;
308
309    if (--priv->map_count == 0) {
310	assert(priv->map_transfer);
311	exa->pipe->transfer_unmap(exa->pipe, priv->map_transfer);
312	exa->pipe->transfer_destroy(exa->pipe, priv->map_transfer);
313	priv->map_transfer = NULL;
314	pPix->devPrivate.ptr = NULL;
315    }
316}
317
318/***********************************************************************
319 * Solid Fills
320 */
321
322static Bool
323ExaPrepareSolid(PixmapPtr pPixmap, int alu, Pixel planeMask, Pixel fg)
324{
325    ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
326    modesettingPtr ms = modesettingPTR(pScrn);
327    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
328    struct exa_context *exa = ms->exa;
329
330#if DEBUG_PRINT
331    debug_printf("ExaPrepareSolid(0x%x)\n", fg);
332#endif
333    if (!exa->accel)
334	return FALSE;
335
336    if (!exa->pipe)
337	XORG_FALLBACK("accle not enabled");
338
339    if (!priv || !priv->tex)
340	XORG_FALLBACK("%s", !priv ? "!priv" : "!priv->tex");
341
342    if (!EXA_PM_IS_SOLID(&pPixmap->drawable, planeMask))
343	XORG_FALLBACK("planeMask is not solid");
344
345    if (alu != GXcopy)
346	XORG_FALLBACK("not GXcopy");
347
348    if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
349                                        priv->tex->target, 0,
350                                        PIPE_BIND_RENDER_TARGET, 0)) {
351	XORG_FALLBACK("format %s", util_format_name(priv->tex->format));
352    }
353
354    return xorg_solid_bind_state(exa, priv, fg);
355}
356
357static void
358ExaSolid(PixmapPtr pPixmap, int x0, int y0, int x1, int y1)
359{
360    ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
361    modesettingPtr ms = modesettingPTR(pScrn);
362    struct exa_context *exa = ms->exa;
363    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
364
365#if DEBUG_PRINT
366    debug_printf("\tExaSolid(%d, %d, %d, %d)\n", x0, y0, x1, y1);
367#endif
368
369    if (x0 == 0 && y0 == 0 &&
370        x1 == pPixmap->drawable.width && y1 == pPixmap->drawable.height) {
371       exa->pipe->clear(exa->pipe, PIPE_CLEAR_COLOR, exa->solid_color, 0.0, 0);
372       return;
373    }
374
375    xorg_solid(exa, priv, x0, y0, x1, y1) ;
376}
377
378
379static void
380ExaDoneSolid(PixmapPtr pPixmap)
381{
382    ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
383    modesettingPtr ms = modesettingPTR(pScrn);
384    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
385    struct exa_context *exa = ms->exa;
386
387    if (!priv)
388	return;
389
390    xorg_composite_done(exa);
391}
392
393/***********************************************************************
394 * Copy Blits
395 */
396
397static Bool
398ExaPrepareCopy(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap, int xdir,
399	       int ydir, int alu, Pixel planeMask)
400{
401    ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
402    modesettingPtr ms = modesettingPTR(pScrn);
403    struct exa_context *exa = ms->exa;
404    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
405    struct exa_pixmap_priv *src_priv = exaGetPixmapDriverPrivate(pSrcPixmap);
406
407#if DEBUG_PRINT
408    debug_printf("ExaPrepareCopy\n");
409#endif
410
411    if (!exa->accel)
412	return FALSE;
413
414    if (!exa->pipe)
415	XORG_FALLBACK("accle not enabled");
416
417    if (!priv || !priv->tex)
418	XORG_FALLBACK("pDst %s", !priv ? "!priv" : "!priv->tex");
419
420    if (!src_priv || !src_priv->tex)
421	XORG_FALLBACK("pSrc %s", !src_priv ? "!priv" : "!priv->tex");
422
423    if (!EXA_PM_IS_SOLID(&pSrcPixmap->drawable, planeMask))
424	XORG_FALLBACK("planeMask is not solid");
425
426    if (alu != GXcopy)
427	XORG_FALLBACK("alu not GXcopy");
428
429    if (!exa->scrn->is_format_supported(exa->scrn, priv->tex->format,
430                                        priv->tex->target, 0,
431                                        PIPE_BIND_RENDER_TARGET, 0))
432	XORG_FALLBACK("pDst format %s", util_format_name(priv->tex->format));
433
434    if (!exa->scrn->is_format_supported(exa->scrn, src_priv->tex->format,
435                                        src_priv->tex->target, 0,
436                                        PIPE_BIND_SAMPLER_VIEW, 0))
437	XORG_FALLBACK("pSrc format %s", util_format_name(src_priv->tex->format));
438
439    exa->copy.src = src_priv;
440    exa->copy.dst = priv;
441
442    /* XXX this used to use resource_copy_region for same-surface copies,
443     * but they were redefined to not allow overlaps (some of the util code
444     * always assumed this anyway).
445     * Drivers should implement accelerated resource_copy_region or it will
446     * be slow - disable for now.
447     */
448    if (0 && exa->copy.src != exa->copy.dst) {
449       exa->copy.use_surface_copy = TRUE;
450    }
451    else {
452       exa->copy.use_surface_copy = FALSE;
453
454       if (exa->copy.dst == exa->copy.src)
455          exa->copy.src_texture = renderer_clone_texture( exa->renderer,
456                                                          exa->copy.src->tex );
457       else
458          pipe_resource_reference(&exa->copy.src_texture,
459                                 exa->copy.src->tex);
460
461       exa->copy.dst_surface =
462          exa->scrn->get_tex_surface(exa->scrn,
463                                     exa->copy.dst->tex,
464                                     0, 0, 0,
465                                     PIPE_BIND_RENDER_TARGET);
466
467
468       renderer_copy_prepare(exa->renderer,
469                             exa->copy.dst_surface,
470                             exa->copy.src_texture );
471    }
472
473
474    return TRUE;
475}
476
477static void
478ExaCopy(PixmapPtr pDstPixmap, int srcX, int srcY, int dstX, int dstY,
479	int width, int height)
480{
481   ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
482   modesettingPtr ms = modesettingPTR(pScrn);
483   struct exa_context *exa = ms->exa;
484   struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pDstPixmap);
485
486#if DEBUG_PRINT
487   debug_printf("\tExaCopy(srcx=%d, srcy=%d, dstX=%d, dstY=%d, w=%d, h=%d)\n",
488                srcX, srcY, dstX, dstY, width, height);
489#endif
490
491   debug_assert(priv == exa->copy.dst);
492   (void) priv;
493
494   if (exa->copy.use_surface_copy) {
495      struct pipe_subresource subdst, subsrc;
496      subdst.face = 0;
497      subdst.level = 0;
498      subsrc.face = 0;
499      subsrc.level = 0;
500      exa->pipe->resource_copy_region( exa->pipe,
501                                       exa->copy.dst->tex,
502                                       subdst,
503                                       dstX, dstY, 0,
504                                       exa->copy.src->tex,
505                                       subsrc,
506                                       srcX, srcY, 0,
507                                       width, height );
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 = xcalloc(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    xfree(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.last_level = 0;
878	template.bind = PIPE_BIND_RENDER_TARGET | priv->flags;
879	priv->tex_flags = priv->flags;
880	texture = exa->scrn->resource_create(exa->scrn, &template);
881
882	if (priv->tex) {
883	    struct pipe_subresource subdst, subsrc;
884	    struct pipe_surface *src_surf;
885
886	    subdst.face = 0;
887	    subdst.level = 0;
888	    subsrc.face = 0;
889	    subsrc.level = 0;
890            exa->pipe->resource_copy_region(exa->pipe, texture,
891                                            subdst, 0, 0, 0,
892                                            priv->tex,
893                                            subsrc, 0, 0, 0,
894                                            min(width, texture->width0),
895                                            min(height, texture->height0));
896	}
897
898	pipe_resource_reference(&priv->tex, texture);
899	/* the texture we create has one reference */
900	pipe_resource_reference(&texture, NULL);
901    }
902
903    return TRUE;
904}
905
906struct pipe_resource *
907xorg_exa_get_texture(PixmapPtr pPixmap)
908{
909   struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
910   struct pipe_resource *tex = NULL;
911   pipe_resource_reference(&tex, priv->tex);
912   return tex;
913}
914
915Bool
916xorg_exa_set_texture(PixmapPtr pPixmap, struct  pipe_resource *tex)
917{
918    struct exa_pixmap_priv *priv = exaGetPixmapDriverPrivate(pPixmap);
919
920    int mask = PIPE_BIND_SHARED | PIPE_BIND_SCANOUT;
921
922    if (!priv)
923	return FALSE;
924
925    if (pPixmap->drawable.width != tex->width0 ||
926	pPixmap->drawable.height != tex->height0)
927	return FALSE;
928
929    pipe_resource_reference(&priv->tex, tex);
930    priv->tex_flags = tex->bind & mask;
931
932    return TRUE;
933}
934
935struct pipe_resource *
936xorg_exa_create_root_texture(ScrnInfoPtr pScrn,
937			     int width, int height,
938			     int depth, int bitsPerPixel)
939{
940    modesettingPtr ms = modesettingPTR(pScrn);
941    struct exa_context *exa = ms->exa;
942    struct pipe_resource template;
943    int dummy;
944
945    memset(&template, 0, sizeof(template));
946    template.target = PIPE_TEXTURE_2D;
947    exa_get_pipe_format(depth, &template.format, &bitsPerPixel, &dummy);
948    template.width0 = width;
949    template.height0 = height;
950    template.depth0 = 1;
951    template.last_level = 0;
952    template.bind |= PIPE_BIND_RENDER_TARGET;
953    template.bind |= PIPE_BIND_SCANOUT;
954    template.bind |= PIPE_BIND_SHARED;
955
956    return exa->scrn->resource_create(exa->scrn, &template);
957}
958
959void
960xorg_exa_close(ScrnInfoPtr pScrn)
961{
962   modesettingPtr ms = modesettingPTR(pScrn);
963   struct exa_context *exa = ms->exa;
964
965   pipe_sampler_view_reference(&exa->bound_sampler_views[0], NULL);
966   pipe_sampler_view_reference(&exa->bound_sampler_views[1], NULL);
967
968   renderer_destroy(exa->renderer);
969
970   xorg_exa_finish(exa);
971
972   if (exa->pipe)
973      exa->pipe->destroy(exa->pipe);
974   exa->pipe = NULL;
975   /* Since this was shared be proper with the pointer */
976   ms->ctx = NULL;
977
978   exaDriverFini(pScrn->pScreen);
979   xfree(exa);
980   ms->exa = NULL;
981}
982
983void *
984xorg_exa_init(ScrnInfoPtr pScrn, Bool accel)
985{
986   modesettingPtr ms = modesettingPTR(pScrn);
987   struct exa_context *exa;
988   ExaDriverPtr pExa;
989
990   exa = xcalloc(1, sizeof(struct exa_context));
991   if (!exa)
992      return NULL;
993
994   pExa = exaDriverAlloc();
995   if (!pExa) {
996      goto out_err;
997   }
998
999   memset(pExa, 0, sizeof(*pExa));
1000
1001   pExa->exa_major         = 2;
1002   pExa->exa_minor         = 2;
1003   pExa->memoryBase        = 0;
1004   pExa->memorySize        = 0;
1005   pExa->offScreenBase     = 0;
1006   pExa->pixmapOffsetAlign = 0;
1007   pExa->pixmapPitchAlign  = 1;
1008   pExa->flags             = EXA_OFFSCREEN_PIXMAPS | EXA_HANDLES_PIXMAPS;
1009#ifdef EXA_SUPPORTS_PREPARE_AUX
1010   pExa->flags            |= EXA_SUPPORTS_PREPARE_AUX;
1011#endif
1012#ifdef EXA_MIXED_PIXMAPS
1013   pExa->flags            |= EXA_MIXED_PIXMAPS;
1014#endif
1015   pExa->maxX              = 8191; /* FIXME */
1016   pExa->maxY              = 8191; /* FIXME */
1017
1018   pExa->WaitMarker         = ExaWaitMarker;
1019   pExa->MarkSync           = ExaMarkSync;
1020   pExa->PrepareSolid       = ExaPrepareSolid;
1021   pExa->Solid              = ExaSolid;
1022   pExa->DoneSolid          = ExaDoneSolid;
1023   pExa->PrepareCopy        = ExaPrepareCopy;
1024   pExa->Copy               = ExaCopy;
1025   pExa->DoneCopy           = ExaDoneCopy;
1026   pExa->CheckComposite     = ExaCheckComposite;
1027   pExa->PrepareComposite   = ExaPrepareComposite;
1028   pExa->Composite          = ExaComposite;
1029   pExa->DoneComposite      = ExaDoneComposite;
1030   pExa->PixmapIsOffscreen  = ExaPixmapIsOffscreen;
1031   pExa->DownloadFromScreen = ExaDownloadFromScreen;
1032   pExa->UploadToScreen     = ExaUploadToScreen;
1033   pExa->PrepareAccess      = ExaPrepareAccess;
1034   pExa->FinishAccess       = ExaFinishAccess;
1035   pExa->CreatePixmap       = ExaCreatePixmap;
1036   pExa->DestroyPixmap      = ExaDestroyPixmap;
1037   pExa->ModifyPixmapHeader = ExaModifyPixmapHeader;
1038
1039   if (!exaDriverInit(pScrn->pScreen, pExa)) {
1040      goto out_err;
1041   }
1042
1043   exa->scrn = ms->screen;
1044   exa->pipe = exa->scrn->context_create(exa->scrn, NULL);
1045   if (exa->pipe == NULL)
1046      goto out_err;
1047
1048   /* Share context with DRI */
1049   ms->ctx = exa->pipe;
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
1059   return NULL;
1060}
1061
1062struct pipe_surface *
1063xorg_gpu_surface(struct pipe_screen *scrn, struct exa_pixmap_priv *priv)
1064{
1065   return scrn->get_tex_surface(scrn, priv->tex, 0, 0, 0,
1066                                PIPE_BIND_RENDER_TARGET);
1067
1068}
1069
1070void xorg_exa_flush(struct exa_context *exa, uint pipeFlushFlags,
1071                    struct pipe_fence_handle **fence)
1072{
1073   exa->pipe->flush(exa->pipe, pipeFlushFlags, fence);
1074}
1075
1076void xorg_exa_finish(struct exa_context *exa)
1077{
1078   struct pipe_fence_handle *fence = NULL;
1079
1080   xorg_exa_flush(exa, PIPE_FLUSH_RENDER_CACHE, &fence);
1081
1082   exa->pipe->screen->fence_finish(exa->pipe->screen, fence, 0);
1083   exa->pipe->screen->fence_reference(exa->pipe->screen, &fence, NULL);
1084}
1085
1086