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 /* XXX can it be set to 1? */
47 #ifndef DRI2INFOREC_VERSION
48 #define DRI2INFOREC_VERSION 0
53 struct pipe_texture
*tex
;
54 struct pipe_fence_handle
*fence
;
58 driDoCreateBuffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
, unsigned int format
)
60 struct pipe_texture
*tex
= NULL
;
61 ScreenPtr pScreen
= pDraw
->pScreen
;
62 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
63 modesettingPtr ms
= modesettingPTR(pScrn
);
64 struct exa_pixmap_priv
*exa_priv
;
65 BufferPrivatePtr
private = buffer
->driverPrivate
;
67 unsigned stride
, handle
;
69 if (pDraw
->type
== DRAWABLE_PIXMAP
)
70 pPixmap
= (PixmapPtr
) pDraw
;
72 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
) pDraw
);
73 exa_priv
= exaGetPixmapDriverPrivate(pPixmap
);
75 switch (buffer
->attachment
) {
77 if (buffer
->attachment
!= DRI2BufferFakeFrontLeft
||
78 pDraw
->type
!= DRAWABLE_PIXMAP
) {
79 private->pPixmap
= (*pScreen
->CreatePixmap
)(pScreen
, pDraw
->width
,
85 case DRI2BufferFrontLeft
:
87 case DRI2BufferStencil
:
88 #if DRI2INFOREC_VERSION >= 3
89 case DRI2BufferDepthStencil
:
91 /* Works on old X servers because sanity checking is for the weak */
94 if (exa_priv
->depth_stencil_tex
&&
95 !pf_is_depth_stencil(exa_priv
->depth_stencil_tex
->format
))
96 exa_priv
->depth_stencil_tex
= NULL
;
99 if (exa_priv
->depth_stencil_tex
)
100 pipe_texture_reference(&tex
, exa_priv
->depth_stencil_tex
);
102 struct pipe_texture
template;
103 memset(&template, 0, sizeof(template));
104 template.target
= PIPE_TEXTURE_2D
;
105 if (buffer
->attachment
== DRI2BufferDepth
)
106 template.format
= ms
->ds_depth_bits_last
?
107 PIPE_FORMAT_X8Z24_UNORM
: PIPE_FORMAT_Z24X8_UNORM
;
109 template.format
= ms
->ds_depth_bits_last
?
110 PIPE_FORMAT_S8Z24_UNORM
: PIPE_FORMAT_Z24S8_UNORM
;
111 pf_get_block(template.format
, &template.block
);
112 template.width
[0] = pDraw
->width
;
113 template.height
[0] = pDraw
->height
;
114 template.depth
[0] = 1;
115 template.last_level
= 0;
116 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
117 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
118 tex
= ms
->screen
->texture_create(ms
->screen
, &template);
119 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, tex
);
124 if (!private->pPixmap
) {
125 private->pPixmap
= pPixmap
;
130 exaMoveInPixmap(private->pPixmap
);
131 xorg_exa_set_shared_usage(private->pPixmap
);
132 pScreen
->ModifyPixmapHeader(private->pPixmap
, 0, 0, 0, 0, 0, NULL
);
133 tex
= xorg_exa_get_texture(private->pPixmap
);
137 FatalError("NO TEXTURE IN DRI2\n");
139 ms
->api
->shared_handle_from_texture(ms
->api
, ms
->screen
, tex
, &stride
, &handle
);
141 buffer
->name
= handle
;
142 buffer
->pitch
= stride
;
144 buffer
->driverPrivate
= private;
145 buffer
->flags
= 0; /* not tiled */
146 #if DRI2INFOREC_VERSION == 2
147 ((DRI2Buffer2Ptr
)buffer
)->format
= 0;
148 #elif DRI2INFOREC_VERSION >= 3
157 driDoDestroyBuffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
)
159 ScreenPtr pScreen
= pDraw
->pScreen
;
160 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
161 modesettingPtr ms
= modesettingPTR(pScrn
);
162 BufferPrivatePtr
private = buffer
->driverPrivate
;
163 struct exa_pixmap_priv
*exa_priv
= exaGetPixmapDriverPrivate(private->pPixmap
);
165 pipe_texture_reference(&private->tex
, NULL
);
166 ms
->screen
->fence_reference(ms
->screen
, &private->fence
, NULL
);
167 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, NULL
);
168 (*pScreen
->DestroyPixmap
)(private->pPixmap
);
171 #if DRI2INFOREC_VERSION >= 2
173 static DRI2Buffer2Ptr
174 driCreateBuffer(DrawablePtr pDraw
, unsigned int attachment
, unsigned int format
)
176 DRI2Buffer2Ptr buffer
;
177 BufferPrivatePtr
private;
179 buffer
= xcalloc(1, sizeof *buffer
);
183 private = xcalloc(1, sizeof *private);
188 buffer
->attachment
= attachment
;
189 buffer
->driverPrivate
= private;
191 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
192 if (driDoCreateBuffer(pDraw
, (DRI2BufferPtr
)buffer
, format
))
202 driDestroyBuffer(DrawablePtr pDraw
, DRI2Buffer2Ptr buffer
)
204 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
205 driDoDestroyBuffer(pDraw
, (DRI2BufferPtr
)buffer
);
207 xfree(buffer
->driverPrivate
);
211 #else /* DRI2INFOREC_VERSION < 2 */
214 driCreateBuffers(DrawablePtr pDraw
, unsigned int *attachments
, int count
)
216 BufferPrivatePtr privates
;
217 DRI2BufferPtr buffers
;
220 buffers
= xcalloc(count
, sizeof *buffers
);
224 privates
= xcalloc(count
, sizeof *privates
);
228 for (i
= 0; i
< count
; i
++) {
229 buffers
[i
].attachment
= attachments
[i
];
230 buffers
[i
].driverPrivate
= &privates
[i
];
232 if (!driDoCreateBuffer(pDraw
, &buffers
[i
], 0))
247 driDestroyBuffers(DrawablePtr pDraw
, DRI2BufferPtr buffers
, int count
)
251 for (i
= 0; i
< count
; i
++) {
252 driDoDestroyBuffer(pDraw
, &buffers
[i
]);
256 xfree(buffers
[0].driverPrivate
);
261 #endif /* DRI2INFOREC_VERSION >= 2 */
264 driCopyRegion(DrawablePtr pDraw
, RegionPtr pRegion
,
265 DRI2BufferPtr pDestBuffer
, DRI2BufferPtr pSrcBuffer
)
267 ScreenPtr pScreen
= pDraw
->pScreen
;
268 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
269 modesettingPtr ms
= modesettingPTR(pScrn
);
270 BufferPrivatePtr dst_priv
= pDestBuffer
->driverPrivate
;
271 BufferPrivatePtr src_priv
= pSrcBuffer
->driverPrivate
;
272 PixmapPtr src_pixmap
;
273 PixmapPtr dst_pixmap
;
278 * In driCreateBuffers we dewrap windows into the
279 * backing pixmaps in order to get to the texture.
280 * We need to use the real drawable in CopyArea
281 * so that cliprects and offsets are correct.
283 src_pixmap
= src_priv
->pPixmap
;
284 dst_pixmap
= dst_priv
->pPixmap
;
285 if (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
)
286 src_pixmap
= (PixmapPtr
)pDraw
;
287 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
)
288 dst_pixmap
= (PixmapPtr
)pDraw
;
291 * The clients implements glXWaitX with a copy front to fake and then
292 * waiting on the server to signal its completion of it. While
293 * glXWaitGL is a client side flush and a copy from fake to front.
294 * This is how it is done in the DRI2 protocol, how ever depending
295 * which type of drawables the server does things a bit differently
296 * then what the protocol says as the fake and front are the same.
298 * for pixmaps glXWaitX is a server flush.
299 * for pixmaps glXWaitGL is a client flush.
300 * for windows glXWaitX is a copy from front to fake then a server flush.
301 * for windows glXWaitGL is a client flush then a copy from fake to front.
303 * XXX in the windows case this code always flushes but that isn't a
304 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
305 * or a glFlush/glFinish call.
307 if (dst_pixmap
== src_pixmap
) {
308 /* pixmap glXWaitX */
309 if (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
&&
310 pDestBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
311 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
, NULL
);
314 /* pixmap glXWaitGL */
315 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
&&
316 pSrcBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
319 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
320 "copying between the same pixmap\n");
324 gc
= GetScratchGC(pDraw
->depth
, pScreen
);
325 copy_clip
= REGION_CREATE(pScreen
, NULL
, 0);
326 REGION_COPY(pScreen
, copy_clip
, pRegion
);
327 (*gc
->funcs
->ChangeClip
) (gc
, CT_REGION
, copy_clip
, 0);
328 ValidateGC(&dst_pixmap
->drawable
, gc
);
330 /* If this is a full buffer swap, throttle on the previous one */
331 if (dst_priv
->fence
&& REGION_NUM_RECTS(pRegion
) == 1) {
332 BoxPtr extents
= REGION_EXTENTS(pScreen
, pRegion
);
334 if (extents
->x1
== 0 && extents
->y1
== 0 &&
335 extents
->x2
== pDraw
->width
&& extents
->y2
== pDraw
->height
) {
336 ms
->screen
->fence_finish(ms
->screen
, dst_priv
->fence
, 0);
337 ms
->screen
->fence_reference(ms
->screen
, &dst_priv
->fence
, NULL
);
341 (*gc
->ops
->CopyArea
)(&src_pixmap
->drawable
, &dst_pixmap
->drawable
, gc
,
342 0, 0, pDraw
->width
, pDraw
->height
, 0, 0);
346 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
,
347 pDestBuffer
->attachment
== DRI2BufferFrontLeft
?
348 &dst_priv
->fence
: NULL
);
352 driScreenInit(ScreenPtr pScreen
)
354 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
355 modesettingPtr ms
= modesettingPTR(pScrn
);
356 DRI2InfoRec dri2info
;
358 #if DRI2INFOREC_VERSION >= 2
359 dri2info
.version
= DRI2INFOREC_VERSION
;
361 dri2info
.version
= 1;
363 dri2info
.fd
= ms
->fd
;
365 dri2info
.driverName
= pScrn
->driverName
;
366 dri2info
.deviceName
= "/dev/dri/card0"; /* FIXME */
368 #if DRI2INFOREC_VERSION >= 2
369 dri2info
.CreateBuffer
= driCreateBuffer
;
370 dri2info
.DestroyBuffer
= driDestroyBuffer
;
372 dri2info
.CreateBuffers
= driCreateBuffers
;
373 dri2info
.DestroyBuffers
= driDestroyBuffers
;
375 dri2info
.CopyRegion
= driCopyRegion
;
376 dri2info
.Wait
= NULL
;
378 ms
->d_depth_bits_last
=
379 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_X8Z24_UNORM
,
381 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
382 ms
->ds_depth_bits_last
=
383 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_S8Z24_UNORM
,
385 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
387 return DRI2ScreenInit(pScreen
, &dri2info
);
391 driCloseScreen(ScreenPtr pScreen
)
393 DRI2CloseScreen(pScreen
);
396 /* vim: set sw=4 ts=8 sts=4: */