fixups for interface changes (mostly state trackers)
[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 /* 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
51 typedef struct {
52 PixmapPtr pPixmap;
53 struct pipe_texture *tex;
54 struct pipe_fence_handle *fence;
55 } *BufferPrivatePtr;
56
57 static Bool
58 driDoCreateBuffer(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 template.width0 = pDraw->width;
112 template.height0 = pDraw->height;
113 template.depth0 = 1;
114 template.last_level = 0;
115 template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
116 PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
117 tex = ms->screen->texture_create(ms->screen, &template);
118 pipe_texture_reference(&exa_priv->depth_stencil_tex, tex);
119 }
120 break;
121 }
122
123 if (!private->pPixmap) {
124 private->pPixmap = pPixmap;
125 pPixmap->refcnt++;
126 }
127
128 if (!tex) {
129 /* First call to make sure we have a pixmap private */
130 exaMoveInPixmap(private->pPixmap);
131 xorg_exa_set_shared_usage(private->pPixmap);
132 pScreen->ModifyPixmapHeader(private->pPixmap, 0, 0, 0, 0, 0, NULL);
133 /* Second call to make sure texture has valid contents */
134 exaMoveInPixmap(private->pPixmap);
135 tex = xorg_exa_get_texture(private->pPixmap);
136 }
137
138 if (!tex)
139 FatalError("NO TEXTURE IN DRI2\n");
140
141 ms->api->shared_handle_from_texture(ms->api, ms->screen, tex, &stride, &handle);
142
143 buffer->name = handle;
144 buffer->pitch = stride;
145 buffer->cpp = 4;
146 buffer->driverPrivate = private;
147 buffer->flags = 0; /* not tiled */
148 #if DRI2INFOREC_VERSION == 2
149 ((DRI2Buffer2Ptr)buffer)->format = 0;
150 #elif DRI2INFOREC_VERSION >= 3
151 buffer->format = 0;
152 #endif
153 private->tex = tex;
154
155 return TRUE;
156 }
157
158 static void
159 driDoDestroyBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
160 {
161 ScreenPtr pScreen = pDraw->pScreen;
162 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
163 modesettingPtr ms = modesettingPTR(pScrn);
164 BufferPrivatePtr private = buffer->driverPrivate;
165 struct exa_pixmap_priv *exa_priv = exaGetPixmapDriverPrivate(private->pPixmap);
166
167 pipe_texture_reference(&private->tex, NULL);
168 ms->screen->fence_reference(ms->screen, &private->fence, NULL);
169 pipe_texture_reference(&exa_priv->depth_stencil_tex, NULL);
170 (*pScreen->DestroyPixmap)(private->pPixmap);
171 }
172
173 #if DRI2INFOREC_VERSION >= 2
174
175 static DRI2Buffer2Ptr
176 driCreateBuffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
177 {
178 DRI2Buffer2Ptr buffer;
179 BufferPrivatePtr private;
180
181 buffer = xcalloc(1, sizeof *buffer);
182 if (!buffer)
183 return NULL;
184
185 private = xcalloc(1, sizeof *private);
186 if (!private) {
187 goto fail;
188 }
189
190 buffer->attachment = attachment;
191 buffer->driverPrivate = private;
192
193 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
194 if (driDoCreateBuffer(pDraw, (DRI2BufferPtr)buffer, format))
195 return buffer;
196
197 xfree(private);
198 fail:
199 xfree(buffer);
200 return NULL;
201 }
202
203 static void
204 driDestroyBuffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
205 {
206 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
207 driDoDestroyBuffer(pDraw, (DRI2BufferPtr)buffer);
208
209 xfree(buffer->driverPrivate);
210 xfree(buffer);
211 }
212
213 #else /* DRI2INFOREC_VERSION < 2 */
214
215 static DRI2BufferPtr
216 driCreateBuffers(DrawablePtr pDraw, unsigned int *attachments, int count)
217 {
218 BufferPrivatePtr privates;
219 DRI2BufferPtr buffers;
220 int i;
221
222 buffers = xcalloc(count, sizeof *buffers);
223 if (!buffers)
224 goto fail_buffers;
225
226 privates = xcalloc(count, sizeof *privates);
227 if (!privates)
228 goto fail_privates;
229
230 for (i = 0; i < count; i++) {
231 buffers[i].attachment = attachments[i];
232 buffers[i].driverPrivate = &privates[i];
233
234 if (!driDoCreateBuffer(pDraw, &buffers[i], 0))
235 goto fail;
236 }
237
238 return buffers;
239
240 fail:
241 xfree(privates);
242 fail_privates:
243 xfree(buffers);
244 fail_buffers:
245 return NULL;
246 }
247
248 static void
249 driDestroyBuffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
250 {
251 int i;
252
253 for (i = 0; i < count; i++) {
254 driDoDestroyBuffer(pDraw, &buffers[i]);
255 }
256
257 if (buffers) {
258 xfree(buffers[0].driverPrivate);
259 xfree(buffers);
260 }
261 }
262
263 #endif /* DRI2INFOREC_VERSION >= 2 */
264
265 static void
266 driCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
267 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
268 {
269 ScreenPtr pScreen = pDraw->pScreen;
270 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
271 modesettingPtr ms = modesettingPTR(pScrn);
272 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
273 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
274 PixmapPtr src_pixmap;
275 PixmapPtr dst_pixmap;
276 GCPtr gc;
277 RegionPtr copy_clip;
278 Bool save_accel;
279
280 /*
281 * In driCreateBuffers we dewrap windows into the
282 * backing pixmaps in order to get to the texture.
283 * We need to use the real drawable in CopyArea
284 * so that cliprects and offsets are correct.
285 */
286 src_pixmap = src_priv->pPixmap;
287 dst_pixmap = dst_priv->pPixmap;
288 if (pSrcBuffer->attachment == DRI2BufferFrontLeft)
289 src_pixmap = (PixmapPtr)pDraw;
290 if (pDestBuffer->attachment == DRI2BufferFrontLeft)
291 dst_pixmap = (PixmapPtr)pDraw;
292
293 /*
294 * The clients implements glXWaitX with a copy front to fake and then
295 * waiting on the server to signal its completion of it. While
296 * glXWaitGL is a client side flush and a copy from fake to front.
297 * This is how it is done in the DRI2 protocol, how ever depending
298 * which type of drawables the server does things a bit differently
299 * then what the protocol says as the fake and front are the same.
300 *
301 * for pixmaps glXWaitX is a server flush.
302 * for pixmaps glXWaitGL is a client flush.
303 * for windows glXWaitX is a copy from front to fake then a server flush.
304 * for windows glXWaitGL is a client flush then a copy from fake to front.
305 *
306 * XXX in the windows case this code always flushes but that isn't a
307 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
308 * or a glFlush/glFinish call.
309 */
310 if (dst_pixmap == src_pixmap) {
311 /* pixmap glXWaitX */
312 if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
313 pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
314 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
315 return;
316 }
317 /* pixmap glXWaitGL */
318 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
319 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
320 return;
321 } else {
322 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
323 "copying between the same pixmap\n");
324 }
325 }
326
327 gc = GetScratchGC(pDraw->depth, pScreen);
328 copy_clip = REGION_CREATE(pScreen, NULL, 0);
329 REGION_COPY(pScreen, copy_clip, pRegion);
330 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
331 ValidateGC(&dst_pixmap->drawable, gc);
332
333 /* If this is a full buffer swap, throttle on the previous one */
334 if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
335 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
336
337 if (extents->x1 == 0 && extents->y1 == 0 &&
338 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
339 ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
340 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
341 }
342 }
343
344 save_accel = ms->exa->accel;
345 ms->exa->accel = TRUE;
346 (*gc->ops->CopyArea)(&src_pixmap->drawable, &dst_pixmap->drawable, gc,
347 0, 0, pDraw->width, pDraw->height, 0, 0);
348 ms->exa->accel = save_accel;
349
350 FreeScratchGC(gc);
351
352 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
353 pDestBuffer->attachment == DRI2BufferFrontLeft ?
354 &dst_priv->fence : NULL);
355 }
356
357 Bool
358 driScreenInit(ScreenPtr pScreen)
359 {
360 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
361 modesettingPtr ms = modesettingPTR(pScrn);
362 DRI2InfoRec dri2info;
363
364 #if DRI2INFOREC_VERSION >= 2
365 dri2info.version = DRI2INFOREC_VERSION;
366 #else
367 dri2info.version = 1;
368 #endif
369 dri2info.fd = ms->fd;
370
371 dri2info.driverName = pScrn->driverName;
372 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
373
374 #if DRI2INFOREC_VERSION >= 2
375 dri2info.CreateBuffer = driCreateBuffer;
376 dri2info.DestroyBuffer = driDestroyBuffer;
377 #else
378 dri2info.CreateBuffers = driCreateBuffers;
379 dri2info.DestroyBuffers = driDestroyBuffers;
380 #endif
381 dri2info.CopyRegion = driCopyRegion;
382 dri2info.Wait = NULL;
383
384 ms->d_depth_bits_last =
385 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_X8Z24_UNORM,
386 PIPE_TEXTURE_2D,
387 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
388 ms->ds_depth_bits_last =
389 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_S8Z24_UNORM,
390 PIPE_TEXTURE_2D,
391 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
392
393 return DRI2ScreenInit(pScreen, &dri2info);
394 }
395
396 void
397 driCloseScreen(ScreenPtr pScreen)
398 {
399 DRI2CloseScreen(pScreen);
400 }
401
402 /* vim: set sw=4 ts=8 sts=4: */