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>
31 #include "xorg-server.h"
33 #include "xf86_OSproc.h"
35 #include "xorg_tracker.h"
40 #include "pipe/p_state.h"
41 #include "pipe/p_inlines.h"
43 #include "util/u_rect.h"
45 /* Make all the #if cases in the code esier to read */
46 #ifndef DRI2INFOREC_VERSION
47 #define DRI2INFOREC_VERSION 1
52 struct pipe_texture
*tex
;
53 struct pipe_fence_handle
*fence
;
57 dri2_do_create_buffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
, unsigned int format
)
59 struct pipe_texture
*tex
= NULL
;
60 ScreenPtr pScreen
= pDraw
->pScreen
;
61 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
62 modesettingPtr ms
= modesettingPTR(pScrn
);
63 struct exa_pixmap_priv
*exa_priv
;
64 BufferPrivatePtr
private = buffer
->driverPrivate
;
66 unsigned stride
, handle
;
68 if (pDraw
->type
== DRAWABLE_PIXMAP
)
69 pPixmap
= (PixmapPtr
) pDraw
;
71 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
) pDraw
);
72 exa_priv
= exaGetPixmapDriverPrivate(pPixmap
);
74 switch (buffer
->attachment
) {
76 if (buffer
->attachment
!= DRI2BufferFakeFrontLeft
||
77 pDraw
->type
!= DRAWABLE_PIXMAP
) {
78 private->pPixmap
= (*pScreen
->CreatePixmap
)(pScreen
, pDraw
->width
,
84 case DRI2BufferFrontLeft
:
86 case DRI2BufferStencil
:
87 #if DRI2INFOREC_VERSION >= 3
88 case DRI2BufferDepthStencil
:
90 /* Works on old X servers because sanity checking is for the weak */
93 if (exa_priv
->depth_stencil_tex
&&
94 !pf_is_depth_stencil(exa_priv
->depth_stencil_tex
->format
))
95 exa_priv
->depth_stencil_tex
= NULL
;
98 if (exa_priv
->depth_stencil_tex
)
99 pipe_texture_reference(&tex
, exa_priv
->depth_stencil_tex
);
101 struct pipe_texture
template;
102 memset(&template, 0, sizeof(template));
103 template.target
= PIPE_TEXTURE_2D
;
104 if (buffer
->attachment
== DRI2BufferDepth
)
105 template.format
= ms
->ds_depth_bits_last
?
106 PIPE_FORMAT_X8Z24_UNORM
: PIPE_FORMAT_Z24X8_UNORM
;
108 template.format
= ms
->ds_depth_bits_last
?
109 PIPE_FORMAT_S8Z24_UNORM
: PIPE_FORMAT_Z24S8_UNORM
;
110 pf_get_block(template.format
, &template.block
);
111 template.width
[0] = pDraw
->width
;
112 template.height
[0] = pDraw
->height
;
113 template.depth
[0] = 1;
114 template.last_level
= 0;
115 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
116 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
117 tex
= ms
->screen
->texture_create(ms
->screen
, &template);
118 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, tex
);
123 if (!private->pPixmap
) {
124 private->pPixmap
= pPixmap
;
129 /* First call to make sure we have a pixmap private */
130 exaMoveInPixmap(private->pPixmap
);
131 xorg_exa_set_shared_usage(private->pPixmap
);
132 pScreen
->ModifyPixmapHeader(private->pPixmap
, 0, 0, 0, 0, 0, NULL
);
133 /* Second call to make sure texture has valid contents */
134 exaMoveInPixmap(private->pPixmap
);
135 tex
= xorg_exa_get_texture(private->pPixmap
);
139 FatalError("NO TEXTURE IN DRI2\n");
141 ms
->api
->shared_handle_from_texture(ms
->api
, ms
->screen
, tex
, &stride
, &handle
);
143 buffer
->name
= handle
;
144 buffer
->pitch
= stride
;
146 buffer
->driverPrivate
= private;
147 buffer
->flags
= 0; /* not tiled */
148 #if DRI2INFOREC_VERSION == 2
149 ((DRI2Buffer2Ptr
)buffer
)->format
= 0;
150 #elif DRI2INFOREC_VERSION >= 3
159 dri2_do_destroy_buffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
)
161 ScreenPtr pScreen
= pDraw
->pScreen
;
162 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
163 modesettingPtr ms
= modesettingPTR(pScrn
);
164 BufferPrivatePtr
private = buffer
->driverPrivate
;
165 struct exa_pixmap_priv
*exa_priv
= exaGetPixmapDriverPrivate(private->pPixmap
);
167 pipe_texture_reference(&private->tex
, NULL
);
168 ms
->screen
->fence_reference(ms
->screen
, &private->fence
, NULL
);
169 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, NULL
);
170 (*pScreen
->DestroyPixmap
)(private->pPixmap
);
173 #if DRI2INFOREC_VERSION >= 2
175 static DRI2Buffer2Ptr
176 dri2_create_buffer(DrawablePtr pDraw
, unsigned int attachment
, unsigned int format
)
178 DRI2Buffer2Ptr buffer
;
179 BufferPrivatePtr
private;
181 buffer
= xcalloc(1, sizeof *buffer
);
185 private = xcalloc(1, sizeof *private);
190 buffer
->attachment
= attachment
;
191 buffer
->driverPrivate
= private;
193 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
194 if (dri2_do_create_buffer(pDraw
, (DRI2BufferPtr
)buffer
, format
))
204 dri2_destroy_buffer(DrawablePtr pDraw
, DRI2Buffer2Ptr buffer
)
206 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
207 dri2_do_destroy_buffer(pDraw
, (DRI2BufferPtr
)buffer
);
209 xfree(buffer
->driverPrivate
);
213 #else /* DRI2INFOREC_VERSION < 2 */
216 dri2_create_buffers(DrawablePtr pDraw
, unsigned int *attachments
, int count
)
218 BufferPrivatePtr privates
;
219 DRI2BufferPtr buffers
;
222 buffers
= xcalloc(count
, sizeof *buffers
);
226 privates
= xcalloc(count
, sizeof *privates
);
230 for (i
= 0; i
< count
; i
++) {
231 buffers
[i
].attachment
= attachments
[i
];
232 buffers
[i
].driverPrivate
= &privates
[i
];
234 if (!dri2_do_create_buffer(pDraw
, &buffers
[i
], 0))
249 dri2_destroy_buffers(DrawablePtr pDraw
, DRI2BufferPtr buffers
, int count
)
253 for (i
= 0; i
< count
; i
++) {
254 dri2_do_destroy_buffer(pDraw
, &buffers
[i
]);
258 xfree(buffers
[0].driverPrivate
);
263 #endif /* DRI2INFOREC_VERSION >= 2 */
266 dri2_copy_region(DrawablePtr pDraw
, RegionPtr pRegion
,
267 DRI2BufferPtr pDestBuffer
, DRI2BufferPtr pSrcBuffer
)
269 ScreenPtr pScreen
= pDraw
->pScreen
;
270 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
271 modesettingPtr ms
= modesettingPTR(pScrn
);
272 BufferPrivatePtr dst_priv
= pDestBuffer
->driverPrivate
;
273 BufferPrivatePtr src_priv
= pSrcBuffer
->driverPrivate
;
274 DrawablePtr src_draw
;
275 DrawablePtr dst_draw
;
281 * In driCreateBuffers we dewrap windows into the
282 * backing pixmaps in order to get to the texture.
283 * We need to use the real drawable in CopyArea
284 * so that cliprects and offsets are correct.
286 src_draw
= (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
) ? pDraw
:
287 &src_priv
->pPixmap
->drawable
;
288 dst_draw
= (pDestBuffer
->attachment
== DRI2BufferFrontLeft
) ? pDraw
:
289 &dst_priv
->pPixmap
->drawable
;
292 * The clients implements glXWaitX with a copy front to fake and then
293 * waiting on the server to signal its completion of it. While
294 * glXWaitGL is a client side flush and a copy from fake to front.
295 * This is how it is done in the DRI2 protocol, how ever depending
296 * which type of drawables the server does things a bit differently
297 * then what the protocol says as the fake and front are the same.
299 * for pixmaps glXWaitX is a server flush.
300 * for pixmaps glXWaitGL is a client flush.
301 * for windows glXWaitX is a copy from front to fake then a server flush.
302 * for windows glXWaitGL is a client flush then a copy from fake to front.
304 * XXX in the windows case this code always flushes but that isn't a
305 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
306 * or a glFlush/glFinish call.
308 if (dst_priv
->pPixmap
== src_priv
->pPixmap
) {
309 /* pixmap glXWaitX */
310 if (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
&&
311 pDestBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
312 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
, NULL
);
315 /* pixmap glXWaitGL */
316 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
&&
317 pSrcBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
320 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
321 "copying between the same pixmap\n");
325 gc
= GetScratchGC(pDraw
->depth
, pScreen
);
326 copy_clip
= REGION_CREATE(pScreen
, NULL
, 0);
327 REGION_COPY(pScreen
, copy_clip
, pRegion
);
328 (*gc
->funcs
->ChangeClip
) (gc
, CT_REGION
, copy_clip
, 0);
329 ValidateGC(dst_draw
, gc
);
331 /* If this is a full buffer swap, throttle on the previous one */
332 if (dst_priv
->fence
&& REGION_NUM_RECTS(pRegion
) == 1) {
333 BoxPtr extents
= REGION_EXTENTS(pScreen
, pRegion
);
335 if (extents
->x1
== 0 && extents
->y1
== 0 &&
336 extents
->x2
== pDraw
->width
&& extents
->y2
== pDraw
->height
) {
337 ms
->screen
->fence_finish(ms
->screen
, dst_priv
->fence
, 0);
338 ms
->screen
->fence_reference(ms
->screen
, &dst_priv
->fence
, NULL
);
342 /* Try to make sure the blit will be accelerated */
343 save_accel
= ms
->exa
->accel
;
344 ms
->exa
->accel
= TRUE
;
346 /* In case it won't be though, make sure the GPU copy contents of the
347 * source pixmap will be used for the software fallback - presumably the
348 * client modified them before calling in here.
350 exaMoveInPixmap(src_priv
->pPixmap
);
351 DamageRegionAppend(src_draw
, pRegion
);
352 DamageRegionProcessPending(src_draw
);
354 (*gc
->ops
->CopyArea
)(src_draw
, dst_draw
, gc
,
355 0, 0, pDraw
->width
, pDraw
->height
, 0, 0);
356 ms
->exa
->accel
= save_accel
;
360 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
,
361 pDestBuffer
->attachment
== DRI2BufferFrontLeft
?
362 &dst_priv
->fence
: NULL
);
366 xorg_dri2_init(ScreenPtr pScreen
)
368 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
369 modesettingPtr ms
= modesettingPTR(pScrn
);
370 DRI2InfoRec dri2info
;
372 dri2info
.version
= DRI2INFOREC_VERSION
;
373 dri2info
.fd
= ms
->fd
;
375 dri2info
.driverName
= pScrn
->driverName
;
376 dri2info
.deviceName
= "/dev/dri/card0"; /* FIXME */
378 #if DRI2INFOREC_VERSION >= 2
379 dri2info
.CreateBuffer
= dri2_create_buffer
;
380 dri2info
.DestroyBuffer
= dri2_destroy_buffer
;
382 dri2info
.CreateBuffers
= dri2_create_buffers
;
383 dri2info
.DestroyBuffers
= dri2_destroy_buffers
;
385 dri2info
.CopyRegion
= dri2_copy_region
;
386 dri2info
.Wait
= NULL
;
388 ms
->d_depth_bits_last
=
389 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_X8Z24_UNORM
,
391 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
392 ms
->ds_depth_bits_last
=
393 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_S8Z24_UNORM
,
395 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
397 return DRI2ScreenInit(pScreen
, &dri2info
);
401 xorg_dri2_close(ScreenPtr pScreen
)
403 DRI2CloseScreen(pScreen
);
406 /* vim: set sw=4 ts=8 sts=4: */