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"
47 struct pipe_texture
*tex
;
48 struct pipe_fence_handle
*fence
;
52 driDoCreateBuffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
, unsigned int format
)
54 struct pipe_texture
*tex
= NULL
;
55 ScreenPtr pScreen
= pDraw
->pScreen
;
56 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
57 modesettingPtr ms
= modesettingPTR(pScrn
);
58 struct exa_pixmap_priv
*exa_priv
;
59 BufferPrivatePtr
private = buffer
->driverPrivate
;
61 unsigned stride
, handle
;
63 if (pDraw
->type
== DRAWABLE_PIXMAP
)
64 pPixmap
= (PixmapPtr
) pDraw
;
66 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
) pDraw
);
67 exa_priv
= exaGetPixmapDriverPrivate(pPixmap
);
69 switch (buffer
->attachment
) {
71 if (buffer
->attachment
!= DRI2BufferFakeFrontLeft
||
72 pDraw
->type
!= DRAWABLE_PIXMAP
) {
73 private->pPixmap
= (*pScreen
->CreatePixmap
)(pScreen
, pDraw
->width
,
79 case DRI2BufferFrontLeft
:
81 case DRI2BufferStencil
:
82 #if defined(DRI2INFOREC_VERSION) && DRI2INFOREC_VERSION > 2
83 case DRI2BufferDepthStencil
:
84 if (exa_priv
->depth_stencil_tex
&&
85 !pf_is_depth_stencil(exa_priv
->depth_stencil_tex
->format
))
86 exa_priv
->depth_stencil_tex
= NULL
;
90 if (exa_priv
->depth_stencil_tex
)
91 pipe_texture_reference(&tex
, exa_priv
->depth_stencil_tex
);
93 struct pipe_texture
template;
94 memset(&template, 0, sizeof(template));
95 template.target
= PIPE_TEXTURE_2D
;
96 if (buffer
->attachment
== DRI2BufferDepth
)
97 template.format
= ms
->ds_depth_bits_last
?
98 PIPE_FORMAT_X8Z24_UNORM
: PIPE_FORMAT_Z24X8_UNORM
;
100 template.format
= ms
->ds_depth_bits_last
?
101 PIPE_FORMAT_S8Z24_UNORM
: PIPE_FORMAT_Z24S8_UNORM
;
102 pf_get_block(template.format
, &template.block
);
103 template.width
[0] = pDraw
->width
;
104 template.height
[0] = pDraw
->height
;
105 template.depth
[0] = 1;
106 template.last_level
= 0;
107 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
108 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
109 tex
= ms
->screen
->texture_create(ms
->screen
, &template);
110 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, tex
);
115 if (!private->pPixmap
) {
116 private->pPixmap
= pPixmap
;
121 xorg_exa_set_shared_usage(private->pPixmap
);
122 pScreen
->ModifyPixmapHeader(private->pPixmap
, 0, 0, 0, 0, 0, NULL
);
123 tex
= xorg_exa_get_texture(private->pPixmap
);
127 FatalError("NO TEXTURE IN DRI2\n");
129 ms
->api
->shared_handle_from_texture(ms
->api
, ms
->screen
, tex
, &stride
, &handle
);
131 buffer
->name
= handle
;
132 buffer
->pitch
= stride
;
134 buffer
->driverPrivate
= private;
135 buffer
->flags
= 0; /* not tiled */
142 driDoDestroyBuffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
)
144 ScreenPtr pScreen
= pDraw
->pScreen
;
145 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
146 modesettingPtr ms
= modesettingPTR(pScrn
);
147 BufferPrivatePtr
private = buffer
->driverPrivate
;
148 struct exa_pixmap_priv
*exa_priv
= exaGetPixmapDriverPrivate(private->pPixmap
);
150 pipe_texture_reference(&private->tex
, NULL
);
151 ms
->screen
->fence_reference(ms
->screen
, &private->fence
, NULL
);
152 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, NULL
);
153 (*pScreen
->DestroyPixmap
)(private->pPixmap
);
156 #if defined(DRI2INFOREC_VERSION) && DRI2INFOREC_VERSION > 2
159 driCreateBuffer(DrawablePtr pDraw
, unsigned int attachment
, unsigned int format
)
161 DRI2BufferPtr buffer
;
162 BufferPrivatePtr
private;
164 buffer
= xcalloc(1, sizeof *buffer
);
168 private = xcalloc(1, sizeof *private);
173 buffer
->attachment
= attachment
;
174 buffer
->driverPrivate
= private;
176 if (driDoCreateBuffer(pDraw
, buffer
, format
))
186 driDestroyBuffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
)
188 driDoDestroyBuffer(pDraw
, buffer
);
190 xfree(buffer
->driverPrivate
);
194 #else /* DRI2INFOREC_VERSION <= 2 */
197 driCreateBuffers(DrawablePtr pDraw
, unsigned int *attachments
, int count
)
199 BufferPrivatePtr privates
;
200 DRI2BufferPtr buffers
;
203 buffers
= xcalloc(count
, sizeof *buffers
);
207 privates
= xcalloc(count
, sizeof *privates
);
211 for (i
= 0; i
< count
; i
++) {
212 buffers
[i
].attachment
= attachments
[i
];
213 buffers
[i
].driverPrivate
= &privates
[i
];
215 if (!driDoCreateBuffer(pDraw
, &buffers
[i
], 0))
230 driDestroyBuffers(DrawablePtr pDraw
, DRI2BufferPtr buffers
, int count
)
234 for (i
= 0; i
< count
; i
++) {
235 driDoDestroyBuffer(pDraw
, &buffers
[i
]);
239 xfree(buffers
[0].driverPrivate
);
244 #endif /* DRI2INFOREC_VERSION */
247 driCopyRegion(DrawablePtr pDraw
, RegionPtr pRegion
,
248 DRI2BufferPtr pDestBuffer
, DRI2BufferPtr pSrcBuffer
)
250 ScreenPtr pScreen
= pDraw
->pScreen
;
251 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
252 modesettingPtr ms
= modesettingPTR(pScrn
);
253 BufferPrivatePtr dst_priv
= pDestBuffer
->driverPrivate
;
254 BufferPrivatePtr src_priv
= pSrcBuffer
->driverPrivate
;
255 PixmapPtr src_pixmap
;
256 PixmapPtr dst_pixmap
;
261 * In driCreateBuffers we dewrap windows into the
262 * backing pixmaps in order to get to the texture.
263 * We need to use the real drawable in CopyArea
264 * so that cliprects and offsets are correct.
266 src_pixmap
= src_priv
->pPixmap
;
267 dst_pixmap
= dst_priv
->pPixmap
;
268 if (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
)
269 src_pixmap
= (PixmapPtr
)pDraw
;
270 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
)
271 dst_pixmap
= (PixmapPtr
)pDraw
;
274 * The clients implements glXWaitX with a copy front to fake and then
275 * waiting on the server to signal its completion of it. While
276 * glXWaitGL is a client side flush and a copy from fake to front.
277 * This is how it is done in the DRI2 protocol, how ever depending
278 * which type of drawables the server does things a bit differently
279 * then what the protocol says as the fake and front are the same.
281 * for pixmaps glXWaitX is a server flush.
282 * for pixmaps glXWaitGL is a client flush.
283 * for windows glXWaitX is a copy from front to fake then a server flush.
284 * for windows glXWaitGL is a client flush then a copy from fake to front.
286 * XXX in the windows case this code always flushes but that isn't a
287 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
288 * or a glFlush/glFinish call.
290 if (dst_pixmap
== src_pixmap
) {
291 /* pixmap glXWaitX */
292 if (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
&&
293 pDestBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
294 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
, NULL
);
297 /* pixmap glXWaitGL */
298 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
&&
299 pSrcBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
302 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
303 "copying between the same pixmap\n");
307 gc
= GetScratchGC(pDraw
->depth
, pScreen
);
308 copy_clip
= REGION_CREATE(pScreen
, NULL
, 0);
309 REGION_COPY(pScreen
, copy_clip
, pRegion
);
310 (*gc
->funcs
->ChangeClip
) (gc
, CT_REGION
, copy_clip
, 0);
311 ValidateGC(&dst_pixmap
->drawable
, gc
);
313 /* If this is a full buffer swap, throttle on the previous one */
314 if (dst_priv
->fence
&& REGION_NUM_RECTS(pRegion
) == 1) {
315 BoxPtr extents
= REGION_EXTENTS(pScreen
, pRegion
);
317 if (extents
->x1
== 0 && extents
->y1
== 0 &&
318 extents
->x2
== pDraw
->width
&& extents
->y2
== pDraw
->height
) {
319 ms
->screen
->fence_finish(ms
->screen
, dst_priv
->fence
, 0);
320 ms
->screen
->fence_reference(ms
->screen
, &dst_priv
->fence
, NULL
);
324 (*gc
->ops
->CopyArea
)(&src_pixmap
->drawable
, &dst_pixmap
->drawable
, gc
,
325 0, 0, pDraw
->width
, pDraw
->height
, 0, 0);
329 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
,
330 pDestBuffer
->attachment
== DRI2BufferFrontLeft
?
331 &dst_priv
->fence
: NULL
);
335 driScreenInit(ScreenPtr pScreen
)
337 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
338 modesettingPtr ms
= modesettingPTR(pScrn
);
339 DRI2InfoRec dri2info
;
341 #if defined(DRI2INFOREC_VERSION)
342 dri2info
.version
= DRI2INFOREC_VERSION
;
344 dri2info
.version
= 1;
346 dri2info
.fd
= ms
->fd
;
348 dri2info
.driverName
= pScrn
->driverName
;
349 dri2info
.deviceName
= "/dev/dri/card0"; /* FIXME */
351 #if defined(DRI2INFOREC_VERSION) && DRI2INFOREC_VERSION > 2
352 dri2info
.CreateBuffer
= driCreateBuffer
;
353 dri2info
.DestroyBuffer
= driDestroyBuffer
;
355 dri2info
.CreateBuffers
= driCreateBuffers
;
356 dri2info
.DestroyBuffers
= driDestroyBuffers
;
358 dri2info
.CopyRegion
= driCopyRegion
;
359 dri2info
.Wait
= NULL
;
361 ms
->d_depth_bits_last
=
362 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_X8Z24_UNORM
,
364 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
365 ms
->ds_depth_bits_last
=
366 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_S8Z24_UNORM
,
368 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
370 return DRI2ScreenInit(pScreen
, &dri2info
);
374 driCloseScreen(ScreenPtr pScreen
)
376 DRI2CloseScreen(pScreen
);
379 /* vim: set sw=4 ts=8 sts=4: */