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