st/xorg: Guard against realy old versions of Xorg
[mesa.git] / src / gallium / state_trackers / xorg / xorg_dri2.c
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 typedef struct {
46 PixmapPtr pPixmap;
47 struct pipe_texture *tex;
48 struct pipe_fence_handle *fence;
49 } *BufferPrivatePtr;
50
51 static Bool
52 driDoCreateBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer, unsigned int format)
53 {
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;
60 PixmapPtr pPixmap;
61 unsigned stride, handle;
62
63 if (pDraw->type == DRAWABLE_PIXMAP)
64 pPixmap = (PixmapPtr) pDraw;
65 else
66 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
67 exa_priv = exaGetPixmapDriverPrivate(pPixmap);
68
69 switch (buffer->attachment) {
70 default:
71 if (buffer->attachment != DRI2BufferFakeFrontLeft ||
72 pDraw->type != DRAWABLE_PIXMAP) {
73 private->pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
74 pDraw->height,
75 pDraw->depth,
76 0);
77 }
78 break;
79 case DRI2BufferFrontLeft:
80 break;
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;
87 /* Fall through */
88 #endif
89 case DRI2BufferDepth:
90 if (exa_priv->depth_stencil_tex)
91 pipe_texture_reference(&tex, exa_priv->depth_stencil_tex);
92 else {
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;
99 else
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);
111 }
112 break;
113 }
114
115 if (!private->pPixmap) {
116 private->pPixmap = pPixmap;
117 pPixmap->refcnt++;
118 }
119
120 if (!tex) {
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);
124 }
125
126 if (!tex)
127 FatalError("NO TEXTURE IN DRI2\n");
128
129 ms->api->shared_handle_from_texture(ms->api, ms->screen, tex, &stride, &handle);
130
131 buffer->name = handle;
132 buffer->pitch = stride;
133 buffer->cpp = 4;
134 buffer->driverPrivate = private;
135 buffer->flags = 0; /* not tiled */
136 private->tex = tex;
137
138 return TRUE;
139 }
140
141 static void
142 driDoDestroyBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
143 {
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);
149
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);
154 }
155
156 #if defined(DRI2INFOREC_VERSION) && DRI2INFOREC_VERSION > 2
157
158 static DRI2BufferPtr
159 driCreateBuffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
160 {
161 DRI2BufferPtr buffer;
162 BufferPrivatePtr private;
163
164 buffer = xcalloc(1, sizeof *buffer);
165 if (!buffer)
166 return NULL;
167
168 private = xcalloc(1, sizeof *private);
169 if (!private) {
170 goto fail;
171 }
172
173 buffer->attachment = attachment;
174 buffer->driverPrivate = private;
175
176 if (driDoCreateBuffer(pDraw, buffer, format))
177 return buffer;
178
179 xfree(private);
180 fail:
181 xfree(buffer);
182 return NULL;
183 }
184
185 static void
186 driDestroyBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
187 {
188 driDoDestroyBuffer(pDraw, buffer);
189
190 xfree(buffer->driverPrivate);
191 xfree(buffer);
192 }
193
194 #else /* DRI2INFOREC_VERSION <= 2 */
195
196 static DRI2BufferPtr
197 driCreateBuffers(DrawablePtr pDraw, unsigned int *attachments, int count)
198 {
199 BufferPrivatePtr privates;
200 DRI2BufferPtr buffers;
201 int i;
202
203 buffers = xcalloc(count, sizeof *buffers);
204 if (!buffers)
205 goto fail_buffers;
206
207 privates = xcalloc(count, sizeof *privates);
208 if (!privates)
209 goto fail_privates;
210
211 for (i = 0; i < count; i++) {
212 buffers[i].attachment = attachments[i];
213 buffers[i].driverPrivate = &privates[i];
214
215 if (!driDoCreateBuffer(pDraw, &buffers[i], 0))
216 goto fail;
217 }
218
219 return buffers;
220
221 fail:
222 xfree(privates);
223 fail_privates:
224 xfree(buffers);
225 fail_buffers:
226 return NULL;
227 }
228
229 static void
230 driDestroyBuffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
231 {
232 int i;
233
234 for (i = 0; i < count; i++) {
235 driDoDestroyBuffer(pDraw, &buffers[i]);
236 }
237
238 if (buffers) {
239 xfree(buffers[0].driverPrivate);
240 xfree(buffers);
241 }
242 }
243
244 #endif /* DRI2INFOREC_VERSION */
245
246 static void
247 driCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
248 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
249 {
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;
257 GCPtr gc;
258 RegionPtr copy_clip;
259
260 /*
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.
265 */
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;
272
273 /*
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.
280 *
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.
285 *
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.
289 */
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);
295 return;
296 }
297 /* pixmap glXWaitGL */
298 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
299 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
300 return;
301 } else {
302 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
303 "copying between the same pixmap\n");
304 }
305 }
306
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);
312
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);
316
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);
321 }
322 }
323
324 (*gc->ops->CopyArea)(&src_pixmap->drawable, &dst_pixmap->drawable, gc,
325 0, 0, pDraw->width, pDraw->height, 0, 0);
326
327 FreeScratchGC(gc);
328
329 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
330 pDestBuffer->attachment == DRI2BufferFrontLeft ?
331 &dst_priv->fence : NULL);
332 }
333
334 Bool
335 driScreenInit(ScreenPtr pScreen)
336 {
337 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
338 modesettingPtr ms = modesettingPTR(pScrn);
339 DRI2InfoRec dri2info;
340
341 #if defined(DRI2INFOREC_VERSION)
342 dri2info.version = DRI2INFOREC_VERSION;
343 #else
344 dri2info.version = 1;
345 #endif
346 dri2info.fd = ms->fd;
347
348 dri2info.driverName = pScrn->driverName;
349 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
350
351 #if defined(DRI2INFOREC_VERSION) && DRI2INFOREC_VERSION > 2
352 dri2info.CreateBuffer = driCreateBuffer;
353 dri2info.DestroyBuffer = driDestroyBuffer;
354 #else
355 dri2info.CreateBuffers = driCreateBuffers;
356 dri2info.DestroyBuffers = driDestroyBuffers;
357 #endif
358 dri2info.CopyRegion = driCopyRegion;
359 dri2info.Wait = NULL;
360
361 ms->d_depth_bits_last =
362 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_X8Z24_UNORM,
363 PIPE_TEXTURE_2D,
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,
367 PIPE_TEXTURE_2D,
368 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
369
370 return DRI2ScreenInit(pScreen, &dri2info);
371 }
372
373 void
374 driCloseScreen(ScreenPtr pScreen)
375 {
376 DRI2CloseScreen(pScreen);
377 }
378
379 /* vim: set sw=4 ts=8 sts=4: */