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 #include "state_tracker/drm_driver.h"
47 /* Make all the #if cases in the code esier to read */
48 #ifndef DRI2INFOREC_VERSION
49 #define DRI2INFOREC_VERSION 1
52 #if DRI2INFOREC_VERSION == 2
53 static Bool set_format_in_do_create_buffer;
58 struct pipe_resource *tex;
59 struct pipe_fence_handle *fence;
63 dri2_do_create_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer, unsigned int format)
65 struct pipe_resource *tex = NULL;
66 ScreenPtr pScreen = pDraw->pScreen;
67 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
68 modesettingPtr ms = modesettingPTR(pScrn);
69 struct exa_pixmap_priv *exa_priv;
70 BufferPrivatePtr private = buffer->driverPrivate;
72 struct winsys_handle whandle;
74 if (pDraw->type == DRAWABLE_PIXMAP)
75 pPixmap = (PixmapPtr) pDraw;
77 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
78 exa_priv = exaGetPixmapDriverPrivate(pPixmap);
81 switch (buffer->attachment) {
83 if (buffer->attachment != DRI2BufferFakeFrontLeft ||
84 pDraw->type != DRAWABLE_PIXMAP) {
85 private->pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
91 case DRI2BufferFrontLeft:
93 case DRI2BufferStencil:
94 #if DRI2INFOREC_VERSION >= 3
95 case DRI2BufferDepthStencil:
97 /* Works on old X servers because sanity checking is for the weak */
100 if (exa_priv->depth_stencil_tex &&
101 !util_format_is_depth_or_stencil(exa_priv->depth_stencil_tex->format))
102 exa_priv->depth_stencil_tex = NULL;
104 case DRI2BufferDepth:
105 if (exa_priv->depth_stencil_tex)
106 pipe_resource_reference(&tex, exa_priv->depth_stencil_tex);
108 struct pipe_resource template;
109 unsigned depthBits = (format != 0) ? format : pDraw->depth;
110 memset(&template, 0, sizeof(template));
111 template.target = PIPE_TEXTURE_2D;
112 if (buffer->attachment == DRI2BufferDepth) {
115 template.format = PIPE_FORMAT_Z16_UNORM;
118 template.format = PIPE_FORMAT_Z32_UNORM;
121 template.format = ms->ds_depth_bits_last ?
122 PIPE_FORMAT_Z24X8_UNORM : PIPE_FORMAT_X8Z24_UNORM;
126 template.format = ms->ds_depth_bits_last ?
127 PIPE_FORMAT_Z24_UNORM_S8_USCALED : PIPE_FORMAT_S8_USCALED_Z24_UNORM;
129 template.width0 = pDraw->width;
130 template.height0 = pDraw->height;
132 template.array_size = 1;
133 template.last_level = 0;
134 template.bind = PIPE_BIND_DEPTH_STENCIL |
136 tex = ms->screen->resource_create(ms->screen, &template);
137 pipe_resource_reference(&exa_priv->depth_stencil_tex, tex);
142 if (!private->pPixmap) {
143 private->pPixmap = pPixmap;
148 /* First call to make sure we have a pixmap private */
149 exaMoveInPixmap(private->pPixmap);
150 xorg_exa_set_shared_usage(private->pPixmap);
151 pScreen->ModifyPixmapHeader(private->pPixmap, 0, 0, 0, 0, 0, NULL);
152 /* Second call to make sure texture has valid contents */
153 exaMoveInPixmap(private->pPixmap);
154 tex = xorg_exa_get_texture(private->pPixmap);
158 FatalError("NO TEXTURE IN DRI2\n");
160 memset(&whandle, 0, sizeof(whandle));
161 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
163 ms->screen->resource_get_handle(ms->screen, tex, &whandle);
165 buffer->name = whandle.handle;
166 buffer->pitch = whandle.stride;
168 buffer->driverPrivate = private;
169 buffer->flags = 0; /* not tiled */
170 #if DRI2INFOREC_VERSION == 2
171 /* ABI forwards/backwards compatibility */
172 if (set_format_in_do_create_buffer)
173 ((DRI2Buffer2Ptr)buffer)->format = 0;
174 #elif DRI2INFOREC_VERSION >= 3
183 dri2_do_destroy_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
185 ScreenPtr pScreen = pDraw->pScreen;
186 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
187 modesettingPtr ms = modesettingPTR(pScrn);
188 BufferPrivatePtr private = buffer->driverPrivate;
189 struct exa_pixmap_priv *exa_priv = exaGetPixmapDriverPrivate(private->pPixmap);
191 pipe_resource_reference(&private->tex, NULL);
192 ms->screen->fence_reference(ms->screen, &private->fence, NULL);
193 pipe_resource_reference(&exa_priv->depth_stencil_tex, NULL);
194 (*pScreen->DestroyPixmap)(private->pPixmap);
197 #if DRI2INFOREC_VERSION >= 2
199 static DRI2Buffer2Ptr
200 dri2_create_buffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
202 DRI2Buffer2Ptr buffer;
203 BufferPrivatePtr private;
205 buffer = calloc(1, sizeof *buffer);
209 private = calloc(1, sizeof *private);
214 buffer->attachment = attachment;
215 buffer->driverPrivate = private;
217 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
218 if (dri2_do_create_buffer(pDraw, (DRI2BufferPtr)buffer, format))
228 dri2_destroy_buffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
230 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
231 dri2_do_destroy_buffer(pDraw, (DRI2BufferPtr)buffer);
233 free(buffer->driverPrivate);
237 #endif /* DRI2INFOREC_VERSION >= 2 */
239 #if DRI2INFOREC_VERSION <= 2
242 dri2_create_buffers(DrawablePtr pDraw, unsigned int *attachments, int count)
244 BufferPrivatePtr privates;
245 DRI2BufferPtr buffers;
248 buffers = calloc(count, sizeof *buffers);
252 privates = calloc(count, sizeof *privates);
256 for (i = 0; i < count; i++) {
257 buffers[i].attachment = attachments[i];
258 buffers[i].driverPrivate = &privates[i];
260 if (!dri2_do_create_buffer(pDraw, &buffers[i], 0))
275 dri2_destroy_buffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
279 for (i = 0; i < count; i++) {
280 dri2_do_destroy_buffer(pDraw, &buffers[i]);
284 free(buffers[0].driverPrivate);
289 #endif /* DRI2INFOREC_VERSION <= 2 */
292 dri2_copy_region(DrawablePtr pDraw, RegionPtr pRegion,
293 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
295 ScreenPtr pScreen = pDraw->pScreen;
296 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
297 modesettingPtr ms = modesettingPTR(pScrn);
298 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
299 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
300 DrawablePtr src_draw;
301 DrawablePtr dst_draw;
305 CustomizerPtr cust = ms->cust;
308 * In driCreateBuffers we dewrap windows into the
309 * backing pixmaps in order to get to the texture.
310 * We need to use the real drawable in CopyArea
311 * so that cliprects and offsets are correct.
313 src_draw = (pSrcBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
314 &src_priv->pPixmap->drawable;
315 dst_draw = (pDestBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
316 &dst_priv->pPixmap->drawable;
319 * The clients implements glXWaitX with a copy front to fake and then
320 * waiting on the server to signal its completion of it. While
321 * glXWaitGL is a client side flush and a copy from fake to front.
322 * This is how it is done in the DRI2 protocol, how ever depending
323 * which type of drawables the server does things a bit differently
324 * then what the protocol says as the fake and front are the same.
326 * for pixmaps glXWaitX is a server flush.
327 * for pixmaps glXWaitGL is a client flush.
328 * for windows glXWaitX is a copy from front to fake then a server flush.
329 * for windows glXWaitGL is a client flush then a copy from fake to front.
331 * XXX in the windows case this code always flushes but that isn't a
332 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
333 * or a glFlush/glFinish call.
335 if (dst_priv->pPixmap == src_priv->pPixmap) {
336 /* pixmap glXWaitX */
337 if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
338 pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
339 ms->ctx->flush(ms->ctx, NULL);
342 /* pixmap glXWaitGL */
343 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
344 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
347 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
348 "copying between the same pixmap\n");
352 gc = GetScratchGC(pDraw->depth, pScreen);
353 copy_clip = REGION_CREATE(pScreen, NULL, 0);
354 REGION_COPY(pScreen, copy_clip, pRegion);
355 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
356 ValidateGC(dst_draw, gc);
358 /* If this is a full buffer swap, throttle on the previous one */
359 if (ms->swapThrottling &&
360 dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
361 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
363 if (extents->x1 == 0 && extents->y1 == 0 &&
364 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
365 ms->screen->fence_finish(ms->screen, dst_priv->fence,
366 PIPE_TIMEOUT_INFINITE);
367 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
371 /* Try to make sure the blit will be accelerated */
372 save_accel = ms->exa->accel;
373 ms->exa->accel = TRUE;
375 /* In case it won't be though, make sure the GPU copy contents of the
376 * source pixmap will be used for the software fallback - presumably the
377 * client modified them before calling in here.
379 exaMoveInPixmap(src_priv->pPixmap);
380 DamageRegionAppend(src_draw, pRegion);
381 DamageRegionProcessPending(src_draw);
383 if (cust && cust->winsys_context_throttle)
384 cust->winsys_context_throttle(cust, ms->ctx, THROTTLE_SWAP);
386 (*gc->ops->CopyArea)(src_draw, dst_draw, gc,
387 0, 0, pDraw->width, pDraw->height, 0, 0);
388 ms->exa->accel = save_accel;
392 ms->ctx->flush(ms->ctx,
393 (pDestBuffer->attachment == DRI2BufferFrontLeft
394 && ms->swapThrottling) ?
395 &dst_priv->fence : NULL);
397 if (cust && cust->winsys_context_throttle)
398 cust->winsys_context_throttle(cust, ms->ctx, THROTTLE_RENDER);
403 xorg_dri2_init(ScreenPtr pScreen)
405 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
406 modesettingPtr ms = modesettingPTR(pScrn);
407 DRI2InfoRec dri2info;
408 #if DRI2INFOREC_VERSION >= 2
411 if (xf86LoaderCheckSymbol("DRI2Version")) {
412 DRI2Version(&major, &minor);
414 /* Assume version 1.0 */
420 dri2info.version = min(DRI2INFOREC_VERSION, 3);
421 dri2info.fd = ms->fd;
423 dri2info.driverName = pScrn->driverName;
424 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
426 #if DRI2INFOREC_VERSION >= 2
427 dri2info.CreateBuffer = dri2_create_buffer;
428 dri2info.DestroyBuffer = dri2_destroy_buffer;
431 /* For X servers in the 1.6.x series there where two DRI2 version.
432 * This allows us to build one binary that works on both servers.
434 #if DRI2INFOREC_VERSION == 2
436 set_format_in_do_create_buffer = FALSE;
437 dri2info.CreateBuffers = dri2_create_buffers;
438 dri2info.DestroyBuffers = dri2_destroy_buffers;
440 set_format_in_do_create_buffer = FALSE;
443 /* For version 1 set these unconditionaly. */
444 #if DRI2INFOREC_VERSION == 1
445 dri2info.CreateBuffers = dri2_create_buffers;
446 dri2info.DestroyBuffers = dri2_destroy_buffers;
448 dri2info.CopyRegion = dri2_copy_region;
449 dri2info.Wait = NULL;
451 ms->d_depth_bits_last =
452 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_Z24X8_UNORM,
455 PIPE_BIND_DEPTH_STENCIL);
456 ms->ds_depth_bits_last =
457 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_Z24_UNORM_S8_USCALED,
460 PIPE_BIND_DEPTH_STENCIL);
462 return DRI2ScreenInit(pScreen, &dri2info);
466 xorg_dri2_close(ScreenPtr pScreen)
468 DRI2CloseScreen(pScreen);
471 /* vim: set sw=4 ts=8 sts=4: */