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 "util/u_inlines.h"
43 #include "util/u_format.h"
45 /* Make all the #if cases in the code esier to read */
46 #ifndef DRI2INFOREC_VERSION
47 #define DRI2INFOREC_VERSION 1
50 #if DRI2INFOREC_VERSION == 2
51 static Bool set_format_in_do_create_buffer
;
56 struct pipe_texture
*tex
;
57 struct pipe_fence_handle
*fence
;
61 dri2_do_create_buffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
, unsigned int format
)
63 struct pipe_texture
*tex
= NULL
;
64 ScreenPtr pScreen
= pDraw
->pScreen
;
65 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
66 modesettingPtr ms
= modesettingPTR(pScrn
);
67 struct exa_pixmap_priv
*exa_priv
;
68 BufferPrivatePtr
private = buffer
->driverPrivate
;
70 unsigned stride
, handle
;
72 if (pDraw
->type
== DRAWABLE_PIXMAP
)
73 pPixmap
= (PixmapPtr
) pDraw
;
75 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
) pDraw
);
76 exa_priv
= exaGetPixmapDriverPrivate(pPixmap
);
78 switch (buffer
->attachment
) {
80 if (buffer
->attachment
!= DRI2BufferFakeFrontLeft
||
81 pDraw
->type
!= DRAWABLE_PIXMAP
) {
82 private->pPixmap
= (*pScreen
->CreatePixmap
)(pScreen
, pDraw
->width
,
88 case DRI2BufferFrontLeft
:
90 case DRI2BufferStencil
:
91 #if DRI2INFOREC_VERSION >= 3
92 case DRI2BufferDepthStencil
:
94 /* Works on old X servers because sanity checking is for the weak */
97 if (exa_priv
->depth_stencil_tex
&&
98 !util_format_is_depth_or_stencil(exa_priv
->depth_stencil_tex
->format
))
99 exa_priv
->depth_stencil_tex
= NULL
;
101 case DRI2BufferDepth
:
102 if (exa_priv
->depth_stencil_tex
)
103 pipe_texture_reference(&tex
, exa_priv
->depth_stencil_tex
);
105 struct pipe_texture
template;
106 unsigned depthBits
= (format
!= 0) ? format
: pDraw
->depth
;
107 memset(&template, 0, sizeof(template));
108 template.target
= PIPE_TEXTURE_2D
;
109 if (buffer
->attachment
== DRI2BufferDepth
) {
112 template.format
= PIPE_FORMAT_Z16_UNORM
;
115 template.format
= PIPE_FORMAT_Z32_UNORM
;
118 template.format
= ms
->ds_depth_bits_last
?
119 PIPE_FORMAT_X8Z24_UNORM
: PIPE_FORMAT_Z24X8_UNORM
;
123 template.format
= ms
->ds_depth_bits_last
?
124 PIPE_FORMAT_S8Z24_UNORM
: PIPE_FORMAT_Z24S8_UNORM
;
126 template.width0
= pDraw
->width
;
127 template.height0
= pDraw
->height
;
129 template.last_level
= 0;
130 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
131 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
132 tex
= ms
->screen
->texture_create(ms
->screen
, &template);
133 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, tex
);
138 if (!private->pPixmap
) {
139 private->pPixmap
= pPixmap
;
144 /* First call to make sure we have a pixmap private */
145 exaMoveInPixmap(private->pPixmap
);
146 xorg_exa_set_shared_usage(private->pPixmap
);
147 pScreen
->ModifyPixmapHeader(private->pPixmap
, 0, 0, 0, 0, 0, NULL
);
148 /* Second call to make sure texture has valid contents */
149 exaMoveInPixmap(private->pPixmap
);
150 tex
= xorg_exa_get_texture(private->pPixmap
);
154 FatalError("NO TEXTURE IN DRI2\n");
156 ms
->api
->shared_handle_from_texture(ms
->api
, ms
->screen
, tex
, &stride
, &handle
);
158 buffer
->name
= handle
;
159 buffer
->pitch
= stride
;
161 buffer
->driverPrivate
= private;
162 buffer
->flags
= 0; /* not tiled */
163 #if DRI2INFOREC_VERSION == 2
164 /* ABI forwards/backwards compatibility */
165 if (set_format_in_do_create_buffer
)
166 ((DRI2Buffer2Ptr
)buffer
)->format
= 0;
167 #elif DRI2INFOREC_VERSION >= 3
176 dri2_do_destroy_buffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
)
178 ScreenPtr pScreen
= pDraw
->pScreen
;
179 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
180 modesettingPtr ms
= modesettingPTR(pScrn
);
181 BufferPrivatePtr
private = buffer
->driverPrivate
;
182 struct exa_pixmap_priv
*exa_priv
= exaGetPixmapDriverPrivate(private->pPixmap
);
184 pipe_texture_reference(&private->tex
, NULL
);
185 ms
->screen
->fence_reference(ms
->screen
, &private->fence
, NULL
);
186 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, NULL
);
187 (*pScreen
->DestroyPixmap
)(private->pPixmap
);
190 #if DRI2INFOREC_VERSION >= 2
192 static DRI2Buffer2Ptr
193 dri2_create_buffer(DrawablePtr pDraw
, unsigned int attachment
, unsigned int format
)
195 DRI2Buffer2Ptr buffer
;
196 BufferPrivatePtr
private;
198 buffer
= xcalloc(1, sizeof *buffer
);
202 private = xcalloc(1, sizeof *private);
207 buffer
->attachment
= attachment
;
208 buffer
->driverPrivate
= private;
210 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
211 if (dri2_do_create_buffer(pDraw
, (DRI2BufferPtr
)buffer
, format
))
221 dri2_destroy_buffer(DrawablePtr pDraw
, DRI2Buffer2Ptr buffer
)
223 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
224 dri2_do_destroy_buffer(pDraw
, (DRI2BufferPtr
)buffer
);
226 xfree(buffer
->driverPrivate
);
230 #endif /* DRI2INFOREC_VERSION >= 2 */
232 #if DRI2INFOREC_VERSION <= 2
235 dri2_create_buffers(DrawablePtr pDraw
, unsigned int *attachments
, int count
)
237 BufferPrivatePtr privates
;
238 DRI2BufferPtr buffers
;
241 buffers
= xcalloc(count
, sizeof *buffers
);
245 privates
= xcalloc(count
, sizeof *privates
);
249 for (i
= 0; i
< count
; i
++) {
250 buffers
[i
].attachment
= attachments
[i
];
251 buffers
[i
].driverPrivate
= &privates
[i
];
253 if (!dri2_do_create_buffer(pDraw
, &buffers
[i
], 0))
268 dri2_destroy_buffers(DrawablePtr pDraw
, DRI2BufferPtr buffers
, int count
)
272 for (i
= 0; i
< count
; i
++) {
273 dri2_do_destroy_buffer(pDraw
, &buffers
[i
]);
277 xfree(buffers
[0].driverPrivate
);
282 #endif /* DRI2INFOREC_VERSION <= 2 */
285 dri2_copy_region(DrawablePtr pDraw
, RegionPtr pRegion
,
286 DRI2BufferPtr pDestBuffer
, DRI2BufferPtr pSrcBuffer
)
288 ScreenPtr pScreen
= pDraw
->pScreen
;
289 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
290 modesettingPtr ms
= modesettingPTR(pScrn
);
291 BufferPrivatePtr dst_priv
= pDestBuffer
->driverPrivate
;
292 BufferPrivatePtr src_priv
= pSrcBuffer
->driverPrivate
;
293 DrawablePtr src_draw
;
294 DrawablePtr dst_draw
;
300 * In driCreateBuffers we dewrap windows into the
301 * backing pixmaps in order to get to the texture.
302 * We need to use the real drawable in CopyArea
303 * so that cliprects and offsets are correct.
305 src_draw
= (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
) ? pDraw
:
306 &src_priv
->pPixmap
->drawable
;
307 dst_draw
= (pDestBuffer
->attachment
== DRI2BufferFrontLeft
) ? pDraw
:
308 &dst_priv
->pPixmap
->drawable
;
311 * The clients implements glXWaitX with a copy front to fake and then
312 * waiting on the server to signal its completion of it. While
313 * glXWaitGL is a client side flush and a copy from fake to front.
314 * This is how it is done in the DRI2 protocol, how ever depending
315 * which type of drawables the server does things a bit differently
316 * then what the protocol says as the fake and front are the same.
318 * for pixmaps glXWaitX is a server flush.
319 * for pixmaps glXWaitGL is a client flush.
320 * for windows glXWaitX is a copy from front to fake then a server flush.
321 * for windows glXWaitGL is a client flush then a copy from fake to front.
323 * XXX in the windows case this code always flushes but that isn't a
324 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
325 * or a glFlush/glFinish call.
327 if (dst_priv
->pPixmap
== src_priv
->pPixmap
) {
328 /* pixmap glXWaitX */
329 if (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
&&
330 pDestBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
331 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
, NULL
);
334 /* pixmap glXWaitGL */
335 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
&&
336 pSrcBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
339 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
340 "copying between the same pixmap\n");
344 gc
= GetScratchGC(pDraw
->depth
, pScreen
);
345 copy_clip
= REGION_CREATE(pScreen
, NULL
, 0);
346 REGION_COPY(pScreen
, copy_clip
, pRegion
);
347 (*gc
->funcs
->ChangeClip
) (gc
, CT_REGION
, copy_clip
, 0);
348 ValidateGC(dst_draw
, gc
);
350 /* If this is a full buffer swap, throttle on the previous one */
351 if (dst_priv
->fence
&& REGION_NUM_RECTS(pRegion
) == 1) {
352 BoxPtr extents
= REGION_EXTENTS(pScreen
, pRegion
);
354 if (extents
->x1
== 0 && extents
->y1
== 0 &&
355 extents
->x2
== pDraw
->width
&& extents
->y2
== pDraw
->height
) {
356 ms
->screen
->fence_finish(ms
->screen
, dst_priv
->fence
, 0);
357 ms
->screen
->fence_reference(ms
->screen
, &dst_priv
->fence
, NULL
);
361 /* Try to make sure the blit will be accelerated */
362 save_accel
= ms
->exa
->accel
;
363 ms
->exa
->accel
= TRUE
;
365 /* In case it won't be though, make sure the GPU copy contents of the
366 * source pixmap will be used for the software fallback - presumably the
367 * client modified them before calling in here.
369 exaMoveInPixmap(src_priv
->pPixmap
);
370 DamageRegionAppend(src_draw
, pRegion
);
371 DamageRegionProcessPending(src_draw
);
373 (*gc
->ops
->CopyArea
)(src_draw
, dst_draw
, gc
,
374 0, 0, pDraw
->width
, pDraw
->height
, 0, 0);
375 ms
->exa
->accel
= save_accel
;
379 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
,
380 pDestBuffer
->attachment
== DRI2BufferFrontLeft
?
381 &dst_priv
->fence
: NULL
);
385 xorg_dri2_init(ScreenPtr pScreen
)
387 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
388 modesettingPtr ms
= modesettingPTR(pScrn
);
389 DRI2InfoRec dri2info
;
390 #if DRI2INFOREC_VERSION >= 2
393 if (xf86LoaderCheckSymbol("DRI2Version")) {
394 DRI2Version(&major
, &minor
);
396 /* Assume version 1.0 */
402 dri2info
.version
= DRI2INFOREC_VERSION
;
403 dri2info
.fd
= ms
->fd
;
405 dri2info
.driverName
= pScrn
->driverName
;
406 dri2info
.deviceName
= "/dev/dri/card0"; /* FIXME */
408 #if DRI2INFOREC_VERSION >= 2
409 dri2info
.CreateBuffer
= dri2_create_buffer
;
410 dri2info
.DestroyBuffer
= dri2_destroy_buffer
;
413 /* For X servers in the 1.6.x series there where two DRI2 version.
414 * This allows us to build one binary that works on both servers.
416 #if DRI2INFOREC_VERSION == 2
418 set_format_in_do_create_buffer
= FALSE
;
419 dri2info
.CreateBuffers
= dri2_create_buffers
;
420 dri2info
.DestroyBuffers
= dri2_destroy_buffers
;
422 set_format_in_do_create_buffer
= FALSE
;
425 /* For version 1 set these unconditionaly. */
426 #if DRI2INFOREC_VERSION == 1
427 dri2info
.CreateBuffers
= dri2_create_buffers
;
428 dri2info
.DestroyBuffers
= dri2_destroy_buffers
;
430 dri2info
.CopyRegion
= dri2_copy_region
;
431 dri2info
.Wait
= NULL
;
433 ms
->d_depth_bits_last
=
434 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_X8Z24_UNORM
,
436 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
437 ms
->ds_depth_bits_last
=
438 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_S8Z24_UNORM
,
440 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
442 return DRI2ScreenInit(pScreen
, &dri2info
);
446 xorg_dri2_close(ScreenPtr pScreen
)
448 DRI2CloseScreen(pScreen
);
451 /* vim: set sw=4 ts=8 sts=4: */