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