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