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_format.h"
44 #include "util/u_rect.h"
46 /* Make all the #if cases in the code esier to read */
47 #ifndef DRI2INFOREC_VERSION
48 #define DRI2INFOREC_VERSION 1
51 #if DRI2INFOREC_VERSION == 2
52 static Bool set_format_in_do_create_buffer
;
57 struct pipe_texture
*tex
;
58 struct pipe_fence_handle
*fence
;
62 dri2_do_create_buffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
, unsigned int format
)
64 struct pipe_texture
*tex
= NULL
;
65 ScreenPtr pScreen
= pDraw
->pScreen
;
66 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
67 modesettingPtr ms
= modesettingPTR(pScrn
);
68 struct exa_pixmap_priv
*exa_priv
;
69 BufferPrivatePtr
private = buffer
->driverPrivate
;
71 unsigned stride
, handle
;
73 if (pDraw
->type
== DRAWABLE_PIXMAP
)
74 pPixmap
= (PixmapPtr
) pDraw
;
76 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
) pDraw
);
77 exa_priv
= exaGetPixmapDriverPrivate(pPixmap
);
79 switch (buffer
->attachment
) {
81 if (buffer
->attachment
!= DRI2BufferFakeFrontLeft
||
82 pDraw
->type
!= DRAWABLE_PIXMAP
) {
83 private->pPixmap
= (*pScreen
->CreatePixmap
)(pScreen
, pDraw
->width
,
89 case DRI2BufferFrontLeft
:
91 case DRI2BufferStencil
:
92 #if DRI2INFOREC_VERSION >= 3
93 case DRI2BufferDepthStencil
:
95 /* Works on old X servers because sanity checking is for the weak */
98 if (exa_priv
->depth_stencil_tex
&&
99 !util_format_is_depth_or_stencil(exa_priv
->depth_stencil_tex
->format
))
100 exa_priv
->depth_stencil_tex
= NULL
;
102 case DRI2BufferDepth
:
103 if (exa_priv
->depth_stencil_tex
)
104 pipe_texture_reference(&tex
, exa_priv
->depth_stencil_tex
);
106 struct pipe_texture
template;
107 memset(&template, 0, sizeof(template));
108 template.target
= PIPE_TEXTURE_2D
;
109 if (buffer
->attachment
== DRI2BufferDepth
)
110 template.format
= ms
->ds_depth_bits_last
?
111 PIPE_FORMAT_X8Z24_UNORM
: PIPE_FORMAT_Z24X8_UNORM
;
113 template.format
= ms
->ds_depth_bits_last
?
114 PIPE_FORMAT_S8Z24_UNORM
: PIPE_FORMAT_Z24S8_UNORM
;
115 template.width0
= pDraw
->width
;
116 template.height0
= pDraw
->height
;
118 template.last_level
= 0;
119 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
120 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
121 tex
= ms
->screen
->texture_create(ms
->screen
, &template);
122 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, tex
);
127 if (!private->pPixmap
) {
128 private->pPixmap
= pPixmap
;
133 /* First call to make sure we have a pixmap private */
134 exaMoveInPixmap(private->pPixmap
);
135 xorg_exa_set_shared_usage(private->pPixmap
);
136 pScreen
->ModifyPixmapHeader(private->pPixmap
, 0, 0, 0, 0, 0, NULL
);
137 /* Second call to make sure texture has valid contents */
138 exaMoveInPixmap(private->pPixmap
);
139 tex
= xorg_exa_get_texture(private->pPixmap
);
143 FatalError("NO TEXTURE IN DRI2\n");
145 ms
->api
->shared_handle_from_texture(ms
->api
, ms
->screen
, tex
, &stride
, &handle
);
147 buffer
->name
= handle
;
148 buffer
->pitch
= stride
;
150 buffer
->driverPrivate
= private;
151 buffer
->flags
= 0; /* not tiled */
152 #if DRI2INFOREC_VERSION == 2
153 /* ABI forwards/backwards compatibility */
154 if (set_format_in_do_create_buffer
)
155 ((DRI2Buffer2Ptr
)buffer
)->format
= 0;
156 #elif DRI2INFOREC_VERSION >= 3
165 dri2_do_destroy_buffer(DrawablePtr pDraw
, DRI2BufferPtr buffer
)
167 ScreenPtr pScreen
= pDraw
->pScreen
;
168 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
169 modesettingPtr ms
= modesettingPTR(pScrn
);
170 BufferPrivatePtr
private = buffer
->driverPrivate
;
171 struct exa_pixmap_priv
*exa_priv
= exaGetPixmapDriverPrivate(private->pPixmap
);
173 pipe_texture_reference(&private->tex
, NULL
);
174 ms
->screen
->fence_reference(ms
->screen
, &private->fence
, NULL
);
175 pipe_texture_reference(&exa_priv
->depth_stencil_tex
, NULL
);
176 (*pScreen
->DestroyPixmap
)(private->pPixmap
);
179 #if DRI2INFOREC_VERSION >= 2
181 static DRI2Buffer2Ptr
182 dri2_create_buffer(DrawablePtr pDraw
, unsigned int attachment
, unsigned int format
)
184 DRI2Buffer2Ptr buffer
;
185 BufferPrivatePtr
private;
187 buffer
= xcalloc(1, sizeof *buffer
);
191 private = xcalloc(1, sizeof *private);
196 buffer
->attachment
= attachment
;
197 buffer
->driverPrivate
= private;
199 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
200 if (dri2_do_create_buffer(pDraw
, (DRI2BufferPtr
)buffer
, format
))
210 dri2_destroy_buffer(DrawablePtr pDraw
, DRI2Buffer2Ptr buffer
)
212 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
213 dri2_do_destroy_buffer(pDraw
, (DRI2BufferPtr
)buffer
);
215 xfree(buffer
->driverPrivate
);
219 #endif /* DRI2INFOREC_VERSION >= 2 */
221 #if DRI2INFOREC_VERSION <= 2
224 dri2_create_buffers(DrawablePtr pDraw
, unsigned int *attachments
, int count
)
226 BufferPrivatePtr privates
;
227 DRI2BufferPtr buffers
;
230 buffers
= xcalloc(count
, sizeof *buffers
);
234 privates
= xcalloc(count
, sizeof *privates
);
238 for (i
= 0; i
< count
; i
++) {
239 buffers
[i
].attachment
= attachments
[i
];
240 buffers
[i
].driverPrivate
= &privates
[i
];
242 if (!dri2_do_create_buffer(pDraw
, &buffers
[i
], 0))
257 dri2_destroy_buffers(DrawablePtr pDraw
, DRI2BufferPtr buffers
, int count
)
261 for (i
= 0; i
< count
; i
++) {
262 dri2_do_destroy_buffer(pDraw
, &buffers
[i
]);
266 xfree(buffers
[0].driverPrivate
);
271 #endif /* DRI2INFOREC_VERSION <= 2 */
274 dri2_copy_region(DrawablePtr pDraw
, RegionPtr pRegion
,
275 DRI2BufferPtr pDestBuffer
, DRI2BufferPtr pSrcBuffer
)
277 ScreenPtr pScreen
= pDraw
->pScreen
;
278 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
279 modesettingPtr ms
= modesettingPTR(pScrn
);
280 BufferPrivatePtr dst_priv
= pDestBuffer
->driverPrivate
;
281 BufferPrivatePtr src_priv
= pSrcBuffer
->driverPrivate
;
282 DrawablePtr src_draw
;
283 DrawablePtr dst_draw
;
289 * In driCreateBuffers we dewrap windows into the
290 * backing pixmaps in order to get to the texture.
291 * We need to use the real drawable in CopyArea
292 * so that cliprects and offsets are correct.
294 src_draw
= (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
) ? pDraw
:
295 &src_priv
->pPixmap
->drawable
;
296 dst_draw
= (pDestBuffer
->attachment
== DRI2BufferFrontLeft
) ? pDraw
:
297 &dst_priv
->pPixmap
->drawable
;
300 * The clients implements glXWaitX with a copy front to fake and then
301 * waiting on the server to signal its completion of it. While
302 * glXWaitGL is a client side flush and a copy from fake to front.
303 * This is how it is done in the DRI2 protocol, how ever depending
304 * which type of drawables the server does things a bit differently
305 * then what the protocol says as the fake and front are the same.
307 * for pixmaps glXWaitX is a server flush.
308 * for pixmaps glXWaitGL is a client flush.
309 * for windows glXWaitX is a copy from front to fake then a server flush.
310 * for windows glXWaitGL is a client flush then a copy from fake to front.
312 * XXX in the windows case this code always flushes but that isn't a
313 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
314 * or a glFlush/glFinish call.
316 if (dst_priv
->pPixmap
== src_priv
->pPixmap
) {
317 /* pixmap glXWaitX */
318 if (pSrcBuffer
->attachment
== DRI2BufferFrontLeft
&&
319 pDestBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
320 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
, NULL
);
323 /* pixmap glXWaitGL */
324 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
&&
325 pSrcBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
328 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
329 "copying between the same pixmap\n");
333 gc
= GetScratchGC(pDraw
->depth
, pScreen
);
334 copy_clip
= REGION_CREATE(pScreen
, NULL
, 0);
335 REGION_COPY(pScreen
, copy_clip
, pRegion
);
336 (*gc
->funcs
->ChangeClip
) (gc
, CT_REGION
, copy_clip
, 0);
337 ValidateGC(dst_draw
, gc
);
339 /* If this is a full buffer swap, throttle on the previous one */
340 if (dst_priv
->fence
&& REGION_NUM_RECTS(pRegion
) == 1) {
341 BoxPtr extents
= REGION_EXTENTS(pScreen
, pRegion
);
343 if (extents
->x1
== 0 && extents
->y1
== 0 &&
344 extents
->x2
== pDraw
->width
&& extents
->y2
== pDraw
->height
) {
345 ms
->screen
->fence_finish(ms
->screen
, dst_priv
->fence
, 0);
346 ms
->screen
->fence_reference(ms
->screen
, &dst_priv
->fence
, NULL
);
350 /* Try to make sure the blit will be accelerated */
351 save_accel
= ms
->exa
->accel
;
352 ms
->exa
->accel
= TRUE
;
354 /* In case it won't be though, make sure the GPU copy contents of the
355 * source pixmap will be used for the software fallback - presumably the
356 * client modified them before calling in here.
358 exaMoveInPixmap(src_priv
->pPixmap
);
359 DamageRegionAppend(src_draw
, pRegion
);
360 DamageRegionProcessPending(src_draw
);
362 (*gc
->ops
->CopyArea
)(src_draw
, dst_draw
, gc
,
363 0, 0, pDraw
->width
, pDraw
->height
, 0, 0);
364 ms
->exa
->accel
= save_accel
;
368 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
,
369 pDestBuffer
->attachment
== DRI2BufferFrontLeft
?
370 &dst_priv
->fence
: NULL
);
374 xorg_dri2_init(ScreenPtr pScreen
)
376 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
377 modesettingPtr ms
= modesettingPTR(pScrn
);
378 DRI2InfoRec dri2info
;
381 if (xf86LoaderCheckSymbol("DRI2Version")) {
382 DRI2Version(&major
, &minor
);
384 /* Assume version 1.0 */
389 dri2info
.version
= DRI2INFOREC_VERSION
;
390 dri2info
.fd
= ms
->fd
;
392 dri2info
.driverName
= pScrn
->driverName
;
393 dri2info
.deviceName
= "/dev/dri/card0"; /* FIXME */
395 #if DRI2INFOREC_VERSION >= 2
396 dri2info
.CreateBuffer
= dri2_create_buffer
;
397 dri2info
.DestroyBuffer
= dri2_destroy_buffer
;
400 /* For X servers in the 1.6.x series there where two DRI2 version.
401 * This allows us to build one binary that works on both servers.
403 #if DRI2INFOREC_VERSION == 2
405 set_format_in_do_create_buffer
= FALSE
;
406 dri2info
.CreateBuffers
= dri2_create_buffers
;
407 dri2info
.DestroyBuffers
= dri2_destroy_buffers
;
409 set_format_in_do_create_buffer
= FALSE
;
412 /* For version 1 set these unconditionaly. */
413 #if DRI2INFOREC_VERSION == 1
414 dri2info
.CreateBuffers
= dri2_create_buffers
;
415 dri2info
.DestroyBuffers
= dri2_destroy_buffers
;
417 dri2info
.CopyRegion
= dri2_copy_region
;
418 dri2info
.Wait
= NULL
;
420 ms
->d_depth_bits_last
=
421 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_X8Z24_UNORM
,
423 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
424 ms
->ds_depth_bits_last
=
425 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_S8Z24_UNORM
,
427 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
429 return DRI2ScreenInit(pScreen
, &dri2info
);
433 xorg_dri2_close(ScreenPtr pScreen
)
435 DRI2CloseScreen(pScreen
);
438 /* vim: set sw=4 ts=8 sts=4: */