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