Merge branch 'mesa_7_7_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_format.h"
44 #include "util/u_rect.h"
45
46 /* Make all the #if cases in the code esier to read */
47 #ifndef DRI2INFOREC_VERSION
48 #define DRI2INFOREC_VERSION 1
49 #endif
50
51 #if DRI2INFOREC_VERSION == 2
52 static Bool set_format_in_do_create_buffer;
53 #endif
54
55 typedef struct {
56 PixmapPtr pPixmap;
57 struct pipe_texture *tex;
58 struct pipe_fence_handle *fence;
59 } *BufferPrivatePtr;
60
61 static Bool
62 dri2_do_create_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer, unsigned int format)
63 {
64 struct pipe_texture *tex = NULL;
65 ScreenPtr pScreen = pDraw->pScreen;
66 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
67 modesettingPtr ms = modesettingPTR(pScrn);
68 struct exa_pixmap_priv *exa_priv;
69 BufferPrivatePtr private = buffer->driverPrivate;
70 PixmapPtr pPixmap;
71 unsigned stride, handle;
72
73 if (pDraw->type == DRAWABLE_PIXMAP)
74 pPixmap = (PixmapPtr) pDraw;
75 else
76 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
77 exa_priv = exaGetPixmapDriverPrivate(pPixmap);
78
79 switch (buffer->attachment) {
80 default:
81 if (buffer->attachment != DRI2BufferFakeFrontLeft ||
82 pDraw->type != DRAWABLE_PIXMAP) {
83 private->pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
84 pDraw->height,
85 pDraw->depth,
86 0);
87 }
88 break;
89 case DRI2BufferFrontLeft:
90 break;
91 case DRI2BufferStencil:
92 #if DRI2INFOREC_VERSION >= 3
93 case DRI2BufferDepthStencil:
94 #else
95 /* Works on old X servers because sanity checking is for the weak */
96 case 9:
97 #endif
98 if (exa_priv->depth_stencil_tex &&
99 !util_format_is_depth_or_stencil(exa_priv->depth_stencil_tex->format))
100 exa_priv->depth_stencil_tex = NULL;
101 /* Fall through */
102 case DRI2BufferDepth:
103 if (exa_priv->depth_stencil_tex)
104 pipe_texture_reference(&tex, exa_priv->depth_stencil_tex);
105 else {
106 struct pipe_texture template;
107 memset(&template, 0, sizeof(template));
108 template.target = PIPE_TEXTURE_2D;
109 if (buffer->attachment == DRI2BufferDepth)
110 template.format = ms->ds_depth_bits_last ?
111 PIPE_FORMAT_X8Z24_UNORM : PIPE_FORMAT_Z24X8_UNORM;
112 else
113 template.format = ms->ds_depth_bits_last ?
114 PIPE_FORMAT_S8Z24_UNORM : PIPE_FORMAT_Z24S8_UNORM;
115 template.width0 = pDraw->width;
116 template.height0 = pDraw->height;
117 template.depth0 = 1;
118 template.last_level = 0;
119 template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
120 PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
121 tex = ms->screen->texture_create(ms->screen, &template);
122 pipe_texture_reference(&exa_priv->depth_stencil_tex, tex);
123 }
124 break;
125 }
126
127 if (!private->pPixmap) {
128 private->pPixmap = pPixmap;
129 pPixmap->refcnt++;
130 }
131
132 if (!tex) {
133 /* First call to make sure we have a pixmap private */
134 exaMoveInPixmap(private->pPixmap);
135 xorg_exa_set_shared_usage(private->pPixmap);
136 pScreen->ModifyPixmapHeader(private->pPixmap, 0, 0, 0, 0, 0, NULL);
137 /* Second call to make sure texture has valid contents */
138 exaMoveInPixmap(private->pPixmap);
139 tex = xorg_exa_get_texture(private->pPixmap);
140 }
141
142 if (!tex)
143 FatalError("NO TEXTURE IN DRI2\n");
144
145 ms->api->shared_handle_from_texture(ms->api, ms->screen, tex, &stride, &handle);
146
147 buffer->name = handle;
148 buffer->pitch = stride;
149 buffer->cpp = 4;
150 buffer->driverPrivate = private;
151 buffer->flags = 0; /* not tiled */
152 #if DRI2INFOREC_VERSION == 2
153 /* ABI forwards/backwards compatibility */
154 if (set_format_in_do_create_buffer)
155 ((DRI2Buffer2Ptr)buffer)->format = 0;
156 #elif DRI2INFOREC_VERSION >= 3
157 buffer->format = 0;
158 #endif
159 private->tex = tex;
160
161 return TRUE;
162 }
163
164 static void
165 dri2_do_destroy_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
166 {
167 ScreenPtr pScreen = pDraw->pScreen;
168 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
169 modesettingPtr ms = modesettingPTR(pScrn);
170 BufferPrivatePtr private = buffer->driverPrivate;
171 struct exa_pixmap_priv *exa_priv = exaGetPixmapDriverPrivate(private->pPixmap);
172
173 pipe_texture_reference(&private->tex, NULL);
174 ms->screen->fence_reference(ms->screen, &private->fence, NULL);
175 pipe_texture_reference(&exa_priv->depth_stencil_tex, NULL);
176 (*pScreen->DestroyPixmap)(private->pPixmap);
177 }
178
179 #if DRI2INFOREC_VERSION >= 2
180
181 static DRI2Buffer2Ptr
182 dri2_create_buffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
183 {
184 DRI2Buffer2Ptr buffer;
185 BufferPrivatePtr private;
186
187 buffer = xcalloc(1, sizeof *buffer);
188 if (!buffer)
189 return NULL;
190
191 private = xcalloc(1, sizeof *private);
192 if (!private) {
193 goto fail;
194 }
195
196 buffer->attachment = attachment;
197 buffer->driverPrivate = private;
198
199 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
200 if (dri2_do_create_buffer(pDraw, (DRI2BufferPtr)buffer, format))
201 return buffer;
202
203 xfree(private);
204 fail:
205 xfree(buffer);
206 return NULL;
207 }
208
209 static void
210 dri2_destroy_buffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
211 {
212 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
213 dri2_do_destroy_buffer(pDraw, (DRI2BufferPtr)buffer);
214
215 xfree(buffer->driverPrivate);
216 xfree(buffer);
217 }
218
219 #endif /* DRI2INFOREC_VERSION >= 2 */
220
221 #if DRI2INFOREC_VERSION <= 2
222
223 static DRI2BufferPtr
224 dri2_create_buffers(DrawablePtr pDraw, unsigned int *attachments, int count)
225 {
226 BufferPrivatePtr privates;
227 DRI2BufferPtr buffers;
228 int i;
229
230 buffers = xcalloc(count, sizeof *buffers);
231 if (!buffers)
232 goto fail_buffers;
233
234 privates = xcalloc(count, sizeof *privates);
235 if (!privates)
236 goto fail_privates;
237
238 for (i = 0; i < count; i++) {
239 buffers[i].attachment = attachments[i];
240 buffers[i].driverPrivate = &privates[i];
241
242 if (!dri2_do_create_buffer(pDraw, &buffers[i], 0))
243 goto fail;
244 }
245
246 return buffers;
247
248 fail:
249 xfree(privates);
250 fail_privates:
251 xfree(buffers);
252 fail_buffers:
253 return NULL;
254 }
255
256 static void
257 dri2_destroy_buffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
258 {
259 int i;
260
261 for (i = 0; i < count; i++) {
262 dri2_do_destroy_buffer(pDraw, &buffers[i]);
263 }
264
265 if (buffers) {
266 xfree(buffers[0].driverPrivate);
267 xfree(buffers);
268 }
269 }
270
271 #endif /* DRI2INFOREC_VERSION <= 2 */
272
273 static void
274 dri2_copy_region(DrawablePtr pDraw, RegionPtr pRegion,
275 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
276 {
277 ScreenPtr pScreen = pDraw->pScreen;
278 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
279 modesettingPtr ms = modesettingPTR(pScrn);
280 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
281 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
282 DrawablePtr src_draw;
283 DrawablePtr dst_draw;
284 GCPtr gc;
285 RegionPtr copy_clip;
286 Bool save_accel;
287
288 /*
289 * In driCreateBuffers we dewrap windows into the
290 * backing pixmaps in order to get to the texture.
291 * We need to use the real drawable in CopyArea
292 * so that cliprects and offsets are correct.
293 */
294 src_draw = (pSrcBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
295 &src_priv->pPixmap->drawable;
296 dst_draw = (pDestBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
297 &dst_priv->pPixmap->drawable;
298
299 /*
300 * The clients implements glXWaitX with a copy front to fake and then
301 * waiting on the server to signal its completion of it. While
302 * glXWaitGL is a client side flush and a copy from fake to front.
303 * This is how it is done in the DRI2 protocol, how ever depending
304 * which type of drawables the server does things a bit differently
305 * then what the protocol says as the fake and front are the same.
306 *
307 * for pixmaps glXWaitX is a server flush.
308 * for pixmaps glXWaitGL is a client flush.
309 * for windows glXWaitX is a copy from front to fake then a server flush.
310 * for windows glXWaitGL is a client flush then a copy from fake to front.
311 *
312 * XXX in the windows case this code always flushes but that isn't a
313 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
314 * or a glFlush/glFinish call.
315 */
316 if (dst_priv->pPixmap == src_priv->pPixmap) {
317 /* pixmap glXWaitX */
318 if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
319 pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
320 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
321 return;
322 }
323 /* pixmap glXWaitGL */
324 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
325 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
326 return;
327 } else {
328 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
329 "copying between the same pixmap\n");
330 }
331 }
332
333 gc = GetScratchGC(pDraw->depth, pScreen);
334 copy_clip = REGION_CREATE(pScreen, NULL, 0);
335 REGION_COPY(pScreen, copy_clip, pRegion);
336 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
337 ValidateGC(dst_draw, gc);
338
339 /* If this is a full buffer swap, throttle on the previous one */
340 if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
341 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
342
343 if (extents->x1 == 0 && extents->y1 == 0 &&
344 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
345 ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
346 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
347 }
348 }
349
350 /* Try to make sure the blit will be accelerated */
351 save_accel = ms->exa->accel;
352 ms->exa->accel = TRUE;
353
354 /* In case it won't be though, make sure the GPU copy contents of the
355 * source pixmap will be used for the software fallback - presumably the
356 * client modified them before calling in here.
357 */
358 exaMoveInPixmap(src_priv->pPixmap);
359 DamageRegionAppend(src_draw, pRegion);
360 DamageRegionProcessPending(src_draw);
361
362 (*gc->ops->CopyArea)(src_draw, dst_draw, gc,
363 0, 0, pDraw->width, pDraw->height, 0, 0);
364 ms->exa->accel = save_accel;
365
366 FreeScratchGC(gc);
367
368 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
369 pDestBuffer->attachment == DRI2BufferFrontLeft ?
370 &dst_priv->fence : NULL);
371 }
372
373 Bool
374 xorg_dri2_init(ScreenPtr pScreen)
375 {
376 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
377 modesettingPtr ms = modesettingPTR(pScrn);
378 DRI2InfoRec dri2info;
379 #if DRI2INFOREC_VERSION >= 2
380 int major, minor;
381
382 if (xf86LoaderCheckSymbol("DRI2Version")) {
383 DRI2Version(&major, &minor);
384 } else {
385 /* Assume version 1.0 */
386 major = 1;
387 minor = 0;
388 }
389 #endif
390
391 dri2info.version = DRI2INFOREC_VERSION;
392 dri2info.fd = ms->fd;
393
394 dri2info.driverName = pScrn->driverName;
395 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
396
397 #if DRI2INFOREC_VERSION >= 2
398 dri2info.CreateBuffer = dri2_create_buffer;
399 dri2info.DestroyBuffer = dri2_destroy_buffer;
400 #endif
401
402 /* For X servers in the 1.6.x series there where two DRI2 version.
403 * This allows us to build one binary that works on both servers.
404 */
405 #if DRI2INFOREC_VERSION == 2
406 if (minor == 0) {
407 set_format_in_do_create_buffer = FALSE;
408 dri2info.CreateBuffers = dri2_create_buffers;
409 dri2info.DestroyBuffers = dri2_destroy_buffers;
410 } else
411 set_format_in_do_create_buffer = FALSE;
412 #endif
413
414 /* For version 1 set these unconditionaly. */
415 #if DRI2INFOREC_VERSION == 1
416 dri2info.CreateBuffers = dri2_create_buffers;
417 dri2info.DestroyBuffers = dri2_destroy_buffers;
418 #endif
419 dri2info.CopyRegion = dri2_copy_region;
420 dri2info.Wait = NULL;
421
422 ms->d_depth_bits_last =
423 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_X8Z24_UNORM,
424 PIPE_TEXTURE_2D,
425 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
426 ms->ds_depth_bits_last =
427 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_S8Z24_UNORM,
428 PIPE_TEXTURE_2D,
429 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
430
431 return DRI2ScreenInit(pScreen, &dri2info);
432 }
433
434 void
435 xorg_dri2_close(ScreenPtr pScreen)
436 {
437 DRI2CloseScreen(pScreen);
438 }
439
440 /* vim: set sw=4 ts=8 sts=4: */