xorg_dri2.c revision 6713a83bb8f836f3cb7ba4419a62ec286d5b88fd
1/*
2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 * All Rights Reserved.
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 *
25 *
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
28 *
29 */
30
31#include "xorg-server.h"
32#include "xf86.h"
33#include "xf86_OSproc.h"
34
35#include "xorg_tracker.h"
36#include "xorg_exa.h"
37
38#include "dri2.h"
39
40#include "pipe/p_state.h"
41#include "pipe/p_inlines.h"
42
43#include "util/u_rect.h"
44
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
49#endif
50
51typedef struct {
52    PixmapPtr pPixmap;
53    struct pipe_texture *tex;
54    struct pipe_fence_handle *fence;
55} *BufferPrivatePtr;
56
57static Bool
58dri2_do_create_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer, unsigned int format)
59{
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;
66    PixmapPtr pPixmap;
67    unsigned stride, handle;
68
69    if (pDraw->type == DRAWABLE_PIXMAP)
70	pPixmap = (PixmapPtr) pDraw;
71    else
72	pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
73    exa_priv = exaGetPixmapDriverPrivate(pPixmap);
74
75    switch (buffer->attachment) {
76    default:
77	if (buffer->attachment != DRI2BufferFakeFrontLeft ||
78	    pDraw->type != DRAWABLE_PIXMAP) {
79	    private->pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
80							pDraw->height,
81							pDraw->depth,
82							0);
83	}
84	break;
85    case DRI2BufferFrontLeft:
86	break;
87    case DRI2BufferStencil:
88#if DRI2INFOREC_VERSION >= 3
89    case DRI2BufferDepthStencil:
90#else
91    /* Works on old X servers because sanity checking is for the weak */
92    case 9:
93#endif
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;
97        /* Fall through */
98    case DRI2BufferDepth:
99	if (exa_priv->depth_stencil_tex)
100	    pipe_texture_reference(&tex, exa_priv->depth_stencil_tex);
101        else {
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;
108	    else
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);
120	}
121	break;
122    }
123
124    if (!private->pPixmap) {
125	private->pPixmap = pPixmap;
126	pPixmap->refcnt++;
127    }
128
129    if (!tex) {
130	/* First call to make sure we have a pixmap private */
131	exaMoveInPixmap(private->pPixmap);
132	xorg_exa_set_shared_usage(private->pPixmap);
133	pScreen->ModifyPixmapHeader(private->pPixmap, 0, 0, 0, 0, 0, NULL);
134	/* Second call to make sure texture has valid contents */
135	exaMoveInPixmap(private->pPixmap);
136	tex = xorg_exa_get_texture(private->pPixmap);
137    }
138
139    if (!tex)
140	FatalError("NO TEXTURE IN DRI2\n");
141
142    ms->api->shared_handle_from_texture(ms->api, ms->screen, tex, &stride, &handle);
143
144    buffer->name = handle;
145    buffer->pitch = stride;
146    buffer->cpp = 4;
147    buffer->driverPrivate = private;
148    buffer->flags = 0; /* not tiled */
149#if DRI2INFOREC_VERSION == 2
150    ((DRI2Buffer2Ptr)buffer)->format = 0;
151#elif DRI2INFOREC_VERSION >= 3
152    buffer->format = 0;
153#endif
154    private->tex = tex;
155
156    return TRUE;
157}
158
159static void
160dri2_do_destroy_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
161{
162    ScreenPtr pScreen = pDraw->pScreen;
163    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
164    modesettingPtr ms = modesettingPTR(pScrn);
165    BufferPrivatePtr private = buffer->driverPrivate;
166    struct exa_pixmap_priv *exa_priv = exaGetPixmapDriverPrivate(private->pPixmap);
167
168    pipe_texture_reference(&private->tex, NULL);
169    ms->screen->fence_reference(ms->screen, &private->fence, NULL);
170    pipe_texture_reference(&exa_priv->depth_stencil_tex, NULL);
171    (*pScreen->DestroyPixmap)(private->pPixmap);
172}
173
174#if DRI2INFOREC_VERSION >= 2
175
176static DRI2Buffer2Ptr
177dri2_create_buffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
178{
179    DRI2Buffer2Ptr buffer;
180    BufferPrivatePtr private;
181
182    buffer = xcalloc(1, sizeof *buffer);
183    if (!buffer)
184	return NULL;
185
186    private = xcalloc(1, sizeof *private);
187    if (!private) {
188	goto fail;
189    }
190
191    buffer->attachment = attachment;
192    buffer->driverPrivate = private;
193
194    /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
195    if (dri2_do_create_buffer(pDraw, (DRI2BufferPtr)buffer, format))
196	return buffer;
197
198    xfree(private);
199fail:
200    xfree(buffer);
201    return NULL;
202}
203
204static void
205dri2_destroy_buffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
206{
207    /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
208    dri2_do_destroy_buffer(pDraw, (DRI2BufferPtr)buffer);
209
210    xfree(buffer->driverPrivate);
211    xfree(buffer);
212}
213
214#else /* DRI2INFOREC_VERSION < 2 */
215
216static DRI2BufferPtr
217dri2_create_buffers(DrawablePtr pDraw, unsigned int *attachments, int count)
218{
219    BufferPrivatePtr privates;
220    DRI2BufferPtr buffers;
221    int i;
222
223    buffers = xcalloc(count, sizeof *buffers);
224    if (!buffers)
225	goto fail_buffers;
226
227    privates = xcalloc(count, sizeof *privates);
228    if (!privates)
229	goto fail_privates;
230
231    for (i = 0; i < count; i++) {
232	buffers[i].attachment = attachments[i];
233	buffers[i].driverPrivate = &privates[i];
234
235	if (!dri2_do_create_buffer(pDraw, &buffers[i], 0))
236	    goto fail;
237    }
238
239    return buffers;
240
241fail:
242    xfree(privates);
243fail_privates:
244    xfree(buffers);
245fail_buffers:
246    return NULL;
247}
248
249static void
250dri2_destroy_buffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
251{
252    int i;
253
254    for (i = 0; i < count; i++) {
255	dri2_do_destroy_buffer(pDraw, &buffers[i]);
256    }
257
258    if (buffers) {
259	xfree(buffers[0].driverPrivate);
260	xfree(buffers);
261    }
262}
263
264#endif /* DRI2INFOREC_VERSION >= 2 */
265
266static void
267dri2_copy_region(DrawablePtr pDraw, RegionPtr pRegion,
268                 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
269{
270    ScreenPtr pScreen = pDraw->pScreen;
271    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
272    modesettingPtr ms = modesettingPTR(pScrn);
273    BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
274    BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
275    PixmapPtr src_pixmap;
276    PixmapPtr dst_pixmap;
277    GCPtr gc;
278    RegionPtr copy_clip;
279    Bool save_accel;
280
281    /*
282     * In driCreateBuffers we dewrap windows into the
283     * backing pixmaps in order to get to the texture.
284     * We need to use the real drawable in CopyArea
285     * so that cliprects and offsets are correct.
286     */
287    src_pixmap = src_priv->pPixmap;
288    dst_pixmap = dst_priv->pPixmap;
289    if (pSrcBuffer->attachment == DRI2BufferFrontLeft)
290	src_pixmap = (PixmapPtr)pDraw;
291    if (pDestBuffer->attachment == DRI2BufferFrontLeft)
292	dst_pixmap = (PixmapPtr)pDraw;
293
294    /*
295     * The clients implements glXWaitX with a copy front to fake and then
296     * waiting on the server to signal its completion of it. While
297     * glXWaitGL is a client side flush and a copy from fake to front.
298     * This is how it is done in the DRI2 protocol, how ever depending
299     * which type of drawables the server does things a bit differently
300     * then what the protocol says as the fake and front are the same.
301     *
302     * for pixmaps glXWaitX is a server flush.
303     * for pixmaps glXWaitGL is a client flush.
304     * for windows glXWaitX is a copy from front to fake then a server flush.
305     * for windows glXWaitGL is a client flush then a copy from fake to front.
306     *
307     * XXX in the windows case this code always flushes but that isn't a
308     * must in the glXWaitGL case but we don't know if this is a glXWaitGL
309     * or a glFlush/glFinish call.
310     */
311    if (dst_pixmap == src_pixmap) {
312	/* pixmap glXWaitX */
313	if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
314	    pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
315	    ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
316	    return;
317	}
318	/* pixmap glXWaitGL */
319	if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
320	    pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
321	    return;
322	} else {
323	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
324		"copying between the same pixmap\n");
325	}
326    }
327
328    gc = GetScratchGC(pDraw->depth, pScreen);
329    copy_clip = REGION_CREATE(pScreen, NULL, 0);
330    REGION_COPY(pScreen, copy_clip, pRegion);
331    (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
332    ValidateGC(&dst_pixmap->drawable, gc);
333
334    /* If this is a full buffer swap, throttle on the previous one */
335    if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
336	BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
337
338	if (extents->x1 == 0 && extents->y1 == 0 &&
339	    extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
340	    ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
341	    ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
342	}
343    }
344
345    save_accel = ms->exa->accel;
346    ms->exa->accel = TRUE;
347    (*gc->ops->CopyArea)(&src_pixmap->drawable, &dst_pixmap->drawable, gc,
348			 0, 0, pDraw->width, pDraw->height, 0, 0);
349    ms->exa->accel = save_accel;
350
351    FreeScratchGC(gc);
352
353    ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
354		   pDestBuffer->attachment == DRI2BufferFrontLeft ?
355		   &dst_priv->fence : NULL);
356}
357
358Bool
359xorg_dri2_init(ScreenPtr pScreen)
360{
361    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
362    modesettingPtr ms = modesettingPTR(pScrn);
363    DRI2InfoRec dri2info;
364
365#if DRI2INFOREC_VERSION >= 2
366    dri2info.version = DRI2INFOREC_VERSION;
367#else
368    dri2info.version = 1;
369#endif
370    dri2info.fd = ms->fd;
371
372    dri2info.driverName = pScrn->driverName;
373    dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
374
375#if DRI2INFOREC_VERSION >= 2
376    dri2info.CreateBuffer = dri2_create_buffer;
377    dri2info.DestroyBuffer = dri2_destroy_buffer;
378#else
379    dri2info.CreateBuffers = dri2_create_buffers;
380    dri2info.DestroyBuffers = dri2_destroy_buffers;
381#endif
382    dri2info.CopyRegion = dri2_copy_region;
383    dri2info.Wait = NULL;
384
385    ms->d_depth_bits_last =
386	 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_X8Z24_UNORM,
387					 PIPE_TEXTURE_2D,
388					 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
389    ms->ds_depth_bits_last =
390	 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_S8Z24_UNORM,
391					 PIPE_TEXTURE_2D,
392					 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
393
394    return DRI2ScreenInit(pScreen, &dri2info);
395}
396
397void
398xorg_dri2_close(ScreenPtr pScreen)
399{
400    DRI2CloseScreen(pScreen);
401}
402
403/* vim: set sw=4 ts=8 sts=4: */
404