Merge branch 'gallium-noblocks'
[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 dri2_do_create_buffer(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 dri2_do_destroy_buffer(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 dri2_create_buffer(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 (dri2_do_create_buffer(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 dri2_destroy_buffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
205 {
206 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
207 dri2_do_destroy_buffer(pDraw, (DRI2BufferPtr)buffer);
208
209 xfree(buffer->driverPrivate);
210 xfree(buffer);
211 }
212
213 #else /* DRI2INFOREC_VERSION < 2 */
214
215 static DRI2BufferPtr
216 dri2_create_buffers(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 (!dri2_do_create_buffer(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 dri2_destroy_buffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
250 {
251 int i;
252
253 for (i = 0; i < count; i++) {
254 dri2_do_destroy_buffer(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 dri2_copy_region(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 DrawablePtr src_draw;
275 DrawablePtr dst_draw;
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_draw = (pSrcBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
287 &src_priv->pPixmap->drawable;
288 dst_draw = (pDestBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
289 &dst_priv->pPixmap->drawable;
290
291 /*
292 * The clients implements glXWaitX with a copy front to fake and then
293 * waiting on the server to signal its completion of it. While
294 * glXWaitGL is a client side flush and a copy from fake to front.
295 * This is how it is done in the DRI2 protocol, how ever depending
296 * which type of drawables the server does things a bit differently
297 * then what the protocol says as the fake and front are the same.
298 *
299 * for pixmaps glXWaitX is a server flush.
300 * for pixmaps glXWaitGL is a client flush.
301 * for windows glXWaitX is a copy from front to fake then a server flush.
302 * for windows glXWaitGL is a client flush then a copy from fake to front.
303 *
304 * XXX in the windows case this code always flushes but that isn't a
305 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
306 * or a glFlush/glFinish call.
307 */
308 if (dst_priv->pPixmap == src_priv->pPixmap) {
309 /* pixmap glXWaitX */
310 if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
311 pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
312 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
313 return;
314 }
315 /* pixmap glXWaitGL */
316 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
317 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
318 return;
319 } else {
320 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
321 "copying between the same pixmap\n");
322 }
323 }
324
325 gc = GetScratchGC(pDraw->depth, pScreen);
326 copy_clip = REGION_CREATE(pScreen, NULL, 0);
327 REGION_COPY(pScreen, copy_clip, pRegion);
328 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
329 ValidateGC(dst_draw, gc);
330
331 /* If this is a full buffer swap, throttle on the previous one */
332 if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
333 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
334
335 if (extents->x1 == 0 && extents->y1 == 0 &&
336 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
337 ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
338 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
339 }
340 }
341
342 /* Try to make sure the blit will be accelerated */
343 save_accel = ms->exa->accel;
344 ms->exa->accel = TRUE;
345
346 /* In case it won't be though, make sure the GPU copy contents of the
347 * source pixmap will be used for the software fallback - presumably the
348 * client modified them before calling in here.
349 */
350 exaMoveInPixmap(src_priv->pPixmap);
351 DamageRegionAppend(src_draw, pRegion);
352 DamageRegionProcessPending(src_draw);
353
354 (*gc->ops->CopyArea)(src_draw, dst_draw, gc,
355 0, 0, pDraw->width, pDraw->height, 0, 0);
356 ms->exa->accel = save_accel;
357
358 FreeScratchGC(gc);
359
360 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
361 pDestBuffer->attachment == DRI2BufferFrontLeft ?
362 &dst_priv->fence : NULL);
363 }
364
365 Bool
366 xorg_dri2_init(ScreenPtr pScreen)
367 {
368 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
369 modesettingPtr ms = modesettingPTR(pScrn);
370 DRI2InfoRec dri2info;
371
372 #if DRI2INFOREC_VERSION >= 2
373 dri2info.version = DRI2INFOREC_VERSION;
374 #else
375 dri2info.version = 1;
376 #endif
377 dri2info.fd = ms->fd;
378
379 dri2info.driverName = pScrn->driverName;
380 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
381
382 #if DRI2INFOREC_VERSION >= 2
383 dri2info.CreateBuffer = dri2_create_buffer;
384 dri2info.DestroyBuffer = dri2_destroy_buffer;
385 #else
386 dri2info.CreateBuffers = dri2_create_buffers;
387 dri2info.DestroyBuffers = dri2_destroy_buffers;
388 #endif
389 dri2info.CopyRegion = dri2_copy_region;
390 dri2info.Wait = NULL;
391
392 ms->d_depth_bits_last =
393 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_X8Z24_UNORM,
394 PIPE_TEXTURE_2D,
395 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
396 ms->ds_depth_bits_last =
397 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_S8Z24_UNORM,
398 PIPE_TEXTURE_2D,
399 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
400
401 return DRI2ScreenInit(pScreen, &dri2info);
402 }
403
404 void
405 xorg_dri2_close(ScreenPtr pScreen)
406 {
407 DRI2CloseScreen(pScreen);
408 }
409
410 /* vim: set sw=4 ts=8 sts=4: */