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