2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
32 #include "xorg_tracker.h"
33 #include "xorg_composite.h"
34 #include "xorg_exa_tgsi.h"
36 #include <xorg-server.h>
38 #include <picturestr.h>
41 #include "pipe/p_format.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_state.h"
44 #include "pipe/p_inlines.h"
46 #include "cso_cache/cso_context.h"
48 #include "util/u_rect.h"
55 exa_get_pipe_format(int depth
, enum pipe_format
*format
, int *bbp
)
59 *format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
63 *format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
67 *format
= PIPE_FORMAT_R5G6B5_UNORM
;
71 *format
= PIPE_FORMAT_A1R5G5B5_UNORM
;
77 *format
= PIPE_FORMAT_A8R8G8B8_UNORM
; /* bad bad bad */
86 * Static exported EXA functions
90 ExaWaitMarker(ScreenPtr pScreen
, int marker
)
95 ExaMarkSync(ScreenPtr pScreen
)
101 ExaDownloadFromScreen(PixmapPtr pPix
, int x
, int y
, int w
, int h
, char *dst
,
104 ScreenPtr pScreen
= pPix
->drawable
.pScreen
;
105 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
106 modesettingPtr ms
= modesettingPTR(pScrn
);
107 struct exa_context
*exa
= ms
->exa
;
108 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pPix
);
109 struct pipe_transfer
*transfer
;
111 if (!priv
|| !priv
->tex
)
114 if (exa
->ctx
->is_texture_referenced(exa
->ctx
, priv
->tex
, 0, 0) &
115 PIPE_REFERENCED_FOR_WRITE
)
116 exa
->ctx
->flush(exa
->ctx
, 0, NULL
);
118 transfer
= exa
->scrn
->get_tex_transfer(exa
->scrn
, priv
->tex
, 0, 0, 0,
119 PIPE_TRANSFER_READ
, x
, y
, w
, h
);
123 util_copy_rect((unsigned char*)dst
, &priv
->tex
->block
, dst_pitch
, 0, 0,
124 w
, h
, exa
->scrn
->transfer_map(exa
->scrn
, transfer
),
125 transfer
->stride
, 0, 0);
127 exa
->scrn
->transfer_unmap(exa
->scrn
, transfer
);
128 exa
->scrn
->tex_transfer_destroy(transfer
);
134 ExaUploadToScreen(PixmapPtr pPix
, int x
, int y
, int w
, int h
, char *src
,
137 ScreenPtr pScreen
= pPix
->drawable
.pScreen
;
138 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
139 modesettingPtr ms
= modesettingPTR(pScrn
);
140 struct exa_context
*exa
= ms
->exa
;
141 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pPix
);
142 struct pipe_transfer
*transfer
;
144 if (!priv
|| !priv
->tex
)
147 transfer
= exa
->scrn
->get_tex_transfer(exa
->scrn
, priv
->tex
, 0, 0, 0,
148 PIPE_TRANSFER_WRITE
, x
, y
, w
, h
);
152 util_copy_rect(exa
->scrn
->transfer_map(exa
->scrn
, transfer
),
153 &priv
->tex
->block
, transfer
->stride
, 0, 0, w
, h
,
154 (unsigned char*)src
, src_pitch
, 0, 0);
156 exa
->scrn
->transfer_unmap(exa
->scrn
, transfer
);
157 exa
->scrn
->tex_transfer_destroy(transfer
);
163 ExaPrepareAccess(PixmapPtr pPix
, int index
)
165 ScreenPtr pScreen
= pPix
->drawable
.pScreen
;
166 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
167 modesettingPtr ms
= modesettingPTR(pScrn
);
168 struct exa_context
*exa
= ms
->exa
;
169 struct exa_pixmap_priv
*priv
;
171 priv
= exaGetPixmapDriverPrivate(pPix
);
179 if (priv
->map_count
++ == 0)
181 if (exa
->ctx
->is_texture_referenced(exa
->ctx
, priv
->tex
, 0, 0) &
182 PIPE_REFERENCED_FOR_WRITE
)
183 exa
->ctx
->flush(exa
->ctx
, 0, NULL
);
186 exa
->scrn
->get_tex_transfer(exa
->scrn
, priv
->tex
, 0, 0, 0,
187 PIPE_TRANSFER_READ_WRITE
,
188 0, 0, priv
->tex
->width
[0], priv
->tex
->height
[0]);
190 pPix
->devPrivate
.ptr
=
191 exa
->scrn
->transfer_map(exa
->scrn
, priv
->map_transfer
);
192 pPix
->devKind
= priv
->map_transfer
->stride
;
199 ExaFinishAccess(PixmapPtr pPix
, int index
)
201 ScreenPtr pScreen
= pPix
->drawable
.pScreen
;
202 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
203 modesettingPtr ms
= modesettingPTR(pScrn
);
204 struct exa_context
*exa
= ms
->exa
;
205 struct exa_pixmap_priv
*priv
;
206 priv
= exaGetPixmapDriverPrivate(pPix
);
211 if (!priv
->map_transfer
)
214 if (--priv
->map_count
== 0) {
215 assert(priv
->map_transfer
);
216 exa
->scrn
->transfer_unmap(exa
->scrn
, priv
->map_transfer
);
217 exa
->scrn
->tex_transfer_destroy(priv
->map_transfer
);
218 priv
->map_transfer
= NULL
;
219 pPix
->devPrivate
.ptr
= NULL
;
224 ExaDone(PixmapPtr pPixmap
)
226 ScrnInfoPtr pScrn
= xf86Screens
[pPixmap
->drawable
.pScreen
->myNum
];
227 modesettingPtr ms
= modesettingPTR(pScrn
);
228 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pPixmap
);
229 struct exa_context
*exa
= ms
->exa
;
235 exa
->scrn
->tex_surface_destroy(priv
->src_surf
);
236 priv
->src_surf
= NULL
;
240 ExaDoneComposite(PixmapPtr pPixmap
)
246 ExaPrepareSolid(PixmapPtr pPixmap
, int alu
, Pixel planeMask
, Pixel fg
)
248 ScrnInfoPtr pScrn
= xf86Screens
[pPixmap
->drawable
.pScreen
->myNum
];
249 modesettingPtr ms
= modesettingPTR(pScrn
);
250 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pPixmap
);
251 struct exa_context
*exa
= ms
->exa
;
253 if (pPixmap
->drawable
.depth
< 15)
256 if (!EXA_PM_IS_SOLID(&pPixmap
->drawable
, planeMask
))
259 if (!priv
|| !priv
->tex
)
265 if (!exa
->ctx
|| !exa
->ctx
->surface_fill
)
274 ExaSolid(PixmapPtr pPixmap
, int x0
, int y0
, int x1
, int y1
)
276 ScrnInfoPtr pScrn
= xf86Screens
[pPixmap
->drawable
.pScreen
->myNum
];
277 modesettingPtr ms
= modesettingPTR(pScrn
);
278 struct exa_context
*exa
= ms
->exa
;
279 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pPixmap
);
280 struct pipe_surface
*surf
= exa_gpu_surface(exa
, priv
);
282 exa
->ctx
->surface_fill(exa
->ctx
, surf
, x0
, y0
, x1
- x0
, y1
- y0
,
285 exa
->scrn
->tex_surface_destroy(surf
);
289 ExaPrepareCopy(PixmapPtr pSrcPixmap
, PixmapPtr pDstPixmap
, int xdir
,
290 int ydir
, int alu
, Pixel planeMask
)
292 ScrnInfoPtr pScrn
= xf86Screens
[pDstPixmap
->drawable
.pScreen
->myNum
];
293 modesettingPtr ms
= modesettingPTR(pScrn
);
294 struct exa_context
*exa
= ms
->exa
;
295 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pDstPixmap
);
296 struct exa_pixmap_priv
*src_priv
= exaGetPixmapDriverPrivate(pSrcPixmap
);
301 if (pSrcPixmap
->drawable
.depth
< 15 || pDstPixmap
->drawable
.depth
< 15)
304 if (!EXA_PM_IS_SOLID(&pSrcPixmap
->drawable
, planeMask
))
307 if (!priv
|| !src_priv
)
310 if (!priv
->tex
|| !src_priv
->tex
)
313 if (!exa
->ctx
|| !exa
->ctx
->surface_copy
)
316 priv
->src_surf
= exa_gpu_surface(exa
, src_priv
);
322 ExaCopy(PixmapPtr pDstPixmap
, int srcX
, int srcY
, int dstX
, int dstY
,
323 int width
, int height
)
325 ScrnInfoPtr pScrn
= xf86Screens
[pDstPixmap
->drawable
.pScreen
->myNum
];
326 modesettingPtr ms
= modesettingPTR(pScrn
);
327 struct exa_context
*exa
= ms
->exa
;
328 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pDstPixmap
);
329 struct pipe_surface
*surf
= exa_gpu_surface(exa
, priv
);
331 exa
->ctx
->surface_copy(exa
->ctx
, surf
, dstX
, dstY
, priv
->src_surf
,
332 srcX
, srcY
, width
, height
);
333 exa
->scrn
->tex_surface_destroy(surf
);
337 ExaPrepareComposite(int op
, PicturePtr pSrcPicture
,
338 PicturePtr pMaskPicture
, PicturePtr pDstPicture
,
339 PixmapPtr pSrc
, PixmapPtr pMask
, PixmapPtr pDst
)
341 ScrnInfoPtr pScrn
= xf86Screens
[pDst
->drawable
.pScreen
->myNum
];
342 modesettingPtr ms
= modesettingPTR(pScrn
);
343 struct exa_context
*exa
= ms
->exa
;
345 return xorg_composite_bind_state(exa
, op
, pSrcPicture
, pMaskPicture
,
347 exaGetPixmapDriverPrivate(pSrc
),
348 exaGetPixmapDriverPrivate(pMask
),
349 exaGetPixmapDriverPrivate(pDst
));
353 ExaComposite(PixmapPtr pDst
, int srcX
, int srcY
, int maskX
, int maskY
,
354 int dstX
, int dstY
, int width
, int height
)
356 ScrnInfoPtr pScrn
= xf86Screens
[pDst
->drawable
.pScreen
->myNum
];
357 modesettingPtr ms
= modesettingPTR(pScrn
);
358 struct exa_context
*exa
= ms
->exa
;
359 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pDst
);
361 xorg_composite(exa
, priv
, srcX
, srcY
, maskX
, maskY
,
362 dstX
, dstY
, width
, height
);
366 ExaCheckComposite(int op
,
367 PicturePtr pSrcPicture
, PicturePtr pMaskPicture
,
368 PicturePtr pDstPicture
)
370 return xorg_composite_accelerated(op
,
377 ExaCreatePixmap(ScreenPtr pScreen
, int size
, int align
)
379 struct exa_pixmap_priv
*priv
;
381 priv
= xcalloc(1, sizeof(struct exa_pixmap_priv
));
389 ExaDestroyPixmap(ScreenPtr pScreen
, void *dPriv
)
391 struct exa_pixmap_priv
*priv
= (struct exa_pixmap_priv
*)dPriv
;
392 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
393 modesettingPtr ms
= modesettingPTR(pScrn
);
399 ms
->screen
->texture_destroy(priv
->tex
);
405 ExaPixmapIsOffscreen(PixmapPtr pPixmap
)
407 struct exa_pixmap_priv
*priv
;
409 priv
= exaGetPixmapDriverPrivate(pPixmap
);
421 xorg_exa_set_displayed_usage(PixmapPtr pPixmap
)
423 struct exa_pixmap_priv
*priv
;
424 priv
= exaGetPixmapDriverPrivate(pPixmap
);
427 FatalError("NO PIXMAP PRIVATE\n");
431 priv
->flags
|= PIPE_TEXTURE_USAGE_PRIMARY
;
437 xorg_exa_set_shared_usage(PixmapPtr pPixmap
)
439 struct exa_pixmap_priv
*priv
;
440 priv
= exaGetPixmapDriverPrivate(pPixmap
);
443 FatalError("NO PIXMAP PRIVATE\n");
447 priv
->flags
|= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
453 xorg_exa_get_pixmap_handle(PixmapPtr pPixmap
, unsigned *stride_out
)
455 ScreenPtr pScreen
= pPixmap
->drawable
.pScreen
;
456 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
457 modesettingPtr ms
= modesettingPTR(pScrn
);
458 struct exa_pixmap_priv
*priv
;
463 FatalError("NO MS->EXA\n");
467 priv
= exaGetPixmapDriverPrivate(pPixmap
);
470 FatalError("NO PIXMAP PRIVATE\n");
474 ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
, priv
->tex
, &stride
, &handle
);
476 *stride_out
= stride
;
482 ExaModifyPixmapHeader(PixmapPtr pPixmap
, int width
, int height
,
483 int depth
, int bitsPerPixel
, int devKind
,
486 ScreenPtr pScreen
= pPixmap
->drawable
.pScreen
;
487 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
488 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pPixmap
);
489 modesettingPtr ms
= modesettingPTR(pScrn
);
490 struct exa_context
*exa
= ms
->exa
;
492 if (!priv
|| pPixData
)
496 depth
= pPixmap
->drawable
.depth
;
498 if (bitsPerPixel
<= 0)
499 bitsPerPixel
= pPixmap
->drawable
.bitsPerPixel
;
502 width
= pPixmap
->drawable
.width
;
505 height
= pPixmap
->drawable
.height
;
507 if (width
<= 0 || height
<= 0 || depth
<= 0)
510 miModifyPixmapHeader(pPixmap
, width
, height
, depth
,
511 bitsPerPixel
, devKind
, NULL
);
513 /* Deal with screen resize */
515 (priv
->tex
->width
[0] != width
||
516 priv
->tex
->height
[0] != height
||
517 priv
->tex_flags
!= priv
->flags
)) {
518 struct pipe_texture
*texture
= NULL
;
520 #ifdef DRM_MODE_FEATURE_DIRTYFB
524 struct pipe_texture
template;
526 memset(&template, 0, sizeof(template));
527 template.target
= PIPE_TEXTURE_2D
;
528 exa_get_pipe_format(depth
, &template.format
, &bitsPerPixel
);
529 pf_get_block(template.format
, &template.block
);
530 template.width
[0] = width
;
531 template.height
[0] = height
;
532 template.depth
[0] = 1;
533 template.last_level
= 0;
534 template.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
| priv
->flags
;
535 priv
->tex_flags
= priv
->flags
;
536 texture
= exa
->scrn
->texture_create(exa
->scrn
, &template);
539 struct pipe_surface
*dst_surf
;
541 dst_surf
= exa
->scrn
->get_tex_surface(exa
->scrn
, texture
, 0, 0, 0,
542 PIPE_BUFFER_USAGE_GPU_WRITE
);
543 priv
->src_surf
= exa_gpu_surface(exa
, priv
);
544 exa
->ctx
->surface_copy(exa
->ctx
, dst_surf
, 0, 0, priv
->src_surf
,
545 0, 0, min(width
, texture
->width
[0]),
546 min(height
, texture
->height
[0]));
547 exa
->scrn
->tex_surface_destroy(dst_surf
);
548 exa
->scrn
->tex_surface_destroy(priv
->src_surf
);
549 priv
->src_surf
= NULL
;
550 } else if (pPixmap
->devPrivate
.ptr
) {
551 struct pipe_transfer
*transfer
;
553 if (priv
->map_count
!= 0)
554 FatalError("doing ExaModifyPixmapHeader on mapped buffer\n");
557 exa
->scrn
->get_tex_transfer(exa
->scrn
, texture
, 0, 0, 0,
559 0, 0, width
, height
);
560 util_copy_rect(exa
->scrn
->transfer_map(exa
->scrn
, transfer
),
561 &texture
->block
, transfer
->stride
, 0, 0,
562 width
, height
, pPixmap
->devPrivate
.ptr
,
563 pPixmap
->devKind
, 0, 0);
564 exa
->scrn
->transfer_unmap(exa
->scrn
, transfer
);
565 exa
->scrn
->tex_transfer_destroy(transfer
);
567 xfree(pPixmap
->devPrivate
.ptr
);
568 pPixmap
->devPrivate
.ptr
= NULL
;
571 #ifdef DRM_MODE_FEATURE_DIRTYFB
573 xfree(pPixmap
->devPrivate
.ptr
);
574 pPixmap
->devPrivate
.ptr
= xalloc(pPixmap
->drawable
.height
*
579 pipe_texture_reference(&priv
->tex
, texture
);
585 struct pipe_texture
*
586 xorg_exa_get_texture(PixmapPtr pPixmap
)
588 struct exa_pixmap_priv
*priv
= exaGetPixmapDriverPrivate(pPixmap
);
589 struct pipe_texture
*tex
= NULL
;
590 pipe_texture_reference(&tex
, priv
->tex
);
595 xorg_exa_close(ScrnInfoPtr pScrn
)
597 modesettingPtr ms
= modesettingPTR(pScrn
);
598 struct exa_context
*exa
= ms
->exa
;
599 struct pipe_constant_buffer
*vsbuf
= &exa
->vs_const_buffer
;
600 struct pipe_constant_buffer
*fsbuf
= &exa
->fs_const_buffer
;
603 xorg_shaders_destroy(exa
->shaders
);
606 if (vsbuf
&& vsbuf
->buffer
)
607 pipe_buffer_reference(&vsbuf
->buffer
, NULL
);
609 if (fsbuf
&& fsbuf
->buffer
)
610 pipe_buffer_reference(&fsbuf
->buffer
, NULL
);
613 cso_release_all(exa
->cso
);
614 cso_destroy_context(exa
->cso
);
618 exa
->ctx
->destroy(exa
->ctx
);
620 exaDriverFini(pScrn
->pScreen
);
626 xorg_exa_init(ScrnInfoPtr pScrn
)
628 modesettingPtr ms
= modesettingPTR(pScrn
);
629 struct exa_context
*exa
;
632 exa
= xcalloc(1, sizeof(struct exa_context
));
636 pExa
= exaDriverAlloc();
641 memset(pExa
, 0, sizeof(*pExa
));
645 pExa
->memoryBase
= 0;
646 pExa
->memorySize
= 0;
647 pExa
->offScreenBase
= 0;
648 pExa
->pixmapOffsetAlign
= 0;
649 pExa
->pixmapPitchAlign
= 1;
650 pExa
->flags
= EXA_OFFSCREEN_PIXMAPS
| EXA_HANDLES_PIXMAPS
;
651 #ifdef EXA_SUPPORTS_PREPARE_AUX
652 pExa
->flags
|= EXA_SUPPORTS_PREPARE_AUX
;
654 #ifdef EXA_MIXED_PIXMAPS
655 pExa
->flags
|= EXA_MIXED_PIXMAPS
;
657 pExa
->maxX
= 8191; /* FIXME */
658 pExa
->maxY
= 8191; /* FIXME */
660 pExa
->WaitMarker
= ExaWaitMarker
;
661 pExa
->MarkSync
= ExaMarkSync
;
662 pExa
->PrepareSolid
= ExaPrepareSolid
;
663 pExa
->Solid
= ExaSolid
;
664 pExa
->DoneSolid
= ExaDone
;
665 pExa
->PrepareCopy
= ExaPrepareCopy
;
666 pExa
->Copy
= ExaCopy
;
667 pExa
->DoneCopy
= ExaDone
;
668 pExa
->CheckComposite
= ExaCheckComposite
;
669 pExa
->PrepareComposite
= ExaPrepareComposite
;
670 pExa
->Composite
= ExaComposite
;
671 pExa
->DoneComposite
= ExaDoneComposite
;
672 pExa
->PixmapIsOffscreen
= ExaPixmapIsOffscreen
;
673 pExa
->DownloadFromScreen
= ExaDownloadFromScreen
;
674 pExa
->UploadToScreen
= ExaUploadToScreen
;
675 pExa
->PrepareAccess
= ExaPrepareAccess
;
676 pExa
->FinishAccess
= ExaFinishAccess
;
677 pExa
->CreatePixmap
= ExaCreatePixmap
;
678 pExa
->DestroyPixmap
= ExaDestroyPixmap
;
679 pExa
->ModifyPixmapHeader
= ExaModifyPixmapHeader
;
681 if (!exaDriverInit(pScrn
->pScreen
, pExa
)) {
685 exa
->scrn
= ms
->screen
;
686 exa
->ctx
= ms
->api
->create_context(ms
->api
, exa
->scrn
);
687 /* Share context with DRI */
690 exa
->cso
= cso_create_context(exa
->ctx
);
691 exa
->shaders
= xorg_shaders_create(exa
);
696 xorg_exa_close(pScrn
);
701 struct pipe_surface
*
702 exa_gpu_surface(struct exa_context
*exa
, struct exa_pixmap_priv
*priv
)
704 return exa
->scrn
->get_tex_surface(exa
->scrn
, priv
->tex
, 0, 0, 0,
705 PIPE_BUFFER_USAGE_GPU_READ
|
706 PIPE_BUFFER_USAGE_GPU_WRITE
);