st/xorg: Don't initialize non-existing fields.
[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 driDoCreateBuffer(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 pf_get_block(template.format, &template.block);
112 template.width[0] = pDraw->width;
113 template.height[0] = pDraw->height;
114 template.depth[0] = 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 exaMoveInPixmap(private->pPixmap);
131 xorg_exa_set_shared_usage(private->pPixmap);
132 pScreen->ModifyPixmapHeader(private->pPixmap, 0, 0, 0, 0, 0, NULL);
133 tex = xorg_exa_get_texture(private->pPixmap);
134 }
135
136 if (!tex)
137 FatalError("NO TEXTURE IN DRI2\n");
138
139 ms->api->shared_handle_from_texture(ms->api, ms->screen, tex, &stride, &handle);
140
141 buffer->name = handle;
142 buffer->pitch = stride;
143 buffer->cpp = 4;
144 buffer->driverPrivate = private;
145 buffer->flags = 0; /* not tiled */
146 #if DRI2INFOREC_VERSION == 2
147 ((DRI2Buffer2Ptr)buffer)->format = 0;
148 #elif DRI2INFOREC_VERSION >= 3
149 buffer->format = 0;
150 #endif
151 private->tex = tex;
152
153 return TRUE;
154 }
155
156 static void
157 driDoDestroyBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
158 {
159 ScreenPtr pScreen = pDraw->pScreen;
160 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
161 modesettingPtr ms = modesettingPTR(pScrn);
162 BufferPrivatePtr private = buffer->driverPrivate;
163 struct exa_pixmap_priv *exa_priv = exaGetPixmapDriverPrivate(private->pPixmap);
164
165 pipe_texture_reference(&private->tex, NULL);
166 ms->screen->fence_reference(ms->screen, &private->fence, NULL);
167 pipe_texture_reference(&exa_priv->depth_stencil_tex, NULL);
168 (*pScreen->DestroyPixmap)(private->pPixmap);
169 }
170
171 #if DRI2INFOREC_VERSION >= 2
172
173 static DRI2Buffer2Ptr
174 driCreateBuffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
175 {
176 DRI2Buffer2Ptr buffer;
177 BufferPrivatePtr private;
178
179 buffer = xcalloc(1, sizeof *buffer);
180 if (!buffer)
181 return NULL;
182
183 private = xcalloc(1, sizeof *private);
184 if (!private) {
185 goto fail;
186 }
187
188 buffer->attachment = attachment;
189 buffer->driverPrivate = private;
190
191 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
192 if (driDoCreateBuffer(pDraw, (DRI2BufferPtr)buffer, format))
193 return buffer;
194
195 xfree(private);
196 fail:
197 xfree(buffer);
198 return NULL;
199 }
200
201 static void
202 driDestroyBuffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
203 {
204 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
205 driDoDestroyBuffer(pDraw, (DRI2BufferPtr)buffer);
206
207 xfree(buffer->driverPrivate);
208 xfree(buffer);
209 }
210
211 #else /* DRI2INFOREC_VERSION < 2 */
212
213 static DRI2BufferPtr
214 driCreateBuffers(DrawablePtr pDraw, unsigned int *attachments, int count)
215 {
216 BufferPrivatePtr privates;
217 DRI2BufferPtr buffers;
218 int i;
219
220 buffers = xcalloc(count, sizeof *buffers);
221 if (!buffers)
222 goto fail_buffers;
223
224 privates = xcalloc(count, sizeof *privates);
225 if (!privates)
226 goto fail_privates;
227
228 for (i = 0; i < count; i++) {
229 buffers[i].attachment = attachments[i];
230 buffers[i].driverPrivate = &privates[i];
231
232 if (!driDoCreateBuffer(pDraw, &buffers[i], 0))
233 goto fail;
234 }
235
236 return buffers;
237
238 fail:
239 xfree(privates);
240 fail_privates:
241 xfree(buffers);
242 fail_buffers:
243 return NULL;
244 }
245
246 static void
247 driDestroyBuffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
248 {
249 int i;
250
251 for (i = 0; i < count; i++) {
252 driDoDestroyBuffer(pDraw, &buffers[i]);
253 }
254
255 if (buffers) {
256 xfree(buffers[0].driverPrivate);
257 xfree(buffers);
258 }
259 }
260
261 #endif /* DRI2INFOREC_VERSION >= 2 */
262
263 static void
264 driCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
265 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
266 {
267 ScreenPtr pScreen = pDraw->pScreen;
268 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
269 modesettingPtr ms = modesettingPTR(pScrn);
270 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
271 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
272 PixmapPtr src_pixmap;
273 PixmapPtr dst_pixmap;
274 GCPtr gc;
275 RegionPtr copy_clip;
276
277 /*
278 * In driCreateBuffers we dewrap windows into the
279 * backing pixmaps in order to get to the texture.
280 * We need to use the real drawable in CopyArea
281 * so that cliprects and offsets are correct.
282 */
283 src_pixmap = src_priv->pPixmap;
284 dst_pixmap = dst_priv->pPixmap;
285 if (pSrcBuffer->attachment == DRI2BufferFrontLeft)
286 src_pixmap = (PixmapPtr)pDraw;
287 if (pDestBuffer->attachment == DRI2BufferFrontLeft)
288 dst_pixmap = (PixmapPtr)pDraw;
289
290 /*
291 * The clients implements glXWaitX with a copy front to fake and then
292 * waiting on the server to signal its completion of it. While
293 * glXWaitGL is a client side flush and a copy from fake to front.
294 * This is how it is done in the DRI2 protocol, how ever depending
295 * which type of drawables the server does things a bit differently
296 * then what the protocol says as the fake and front are the same.
297 *
298 * for pixmaps glXWaitX is a server flush.
299 * for pixmaps glXWaitGL is a client flush.
300 * for windows glXWaitX is a copy from front to fake then a server flush.
301 * for windows glXWaitGL is a client flush then a copy from fake to front.
302 *
303 * XXX in the windows case this code always flushes but that isn't a
304 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
305 * or a glFlush/glFinish call.
306 */
307 if (dst_pixmap == src_pixmap) {
308 /* pixmap glXWaitX */
309 if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
310 pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
311 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
312 return;
313 }
314 /* pixmap glXWaitGL */
315 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
316 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
317 return;
318 } else {
319 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
320 "copying between the same pixmap\n");
321 }
322 }
323
324 gc = GetScratchGC(pDraw->depth, pScreen);
325 copy_clip = REGION_CREATE(pScreen, NULL, 0);
326 REGION_COPY(pScreen, copy_clip, pRegion);
327 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
328 ValidateGC(&dst_pixmap->drawable, gc);
329
330 /* If this is a full buffer swap, throttle on the previous one */
331 if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
332 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
333
334 if (extents->x1 == 0 && extents->y1 == 0 &&
335 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
336 ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
337 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
338 }
339 }
340
341 (*gc->ops->CopyArea)(&src_pixmap->drawable, &dst_pixmap->drawable, gc,
342 0, 0, pDraw->width, pDraw->height, 0, 0);
343
344 FreeScratchGC(gc);
345
346 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
347 pDestBuffer->attachment == DRI2BufferFrontLeft ?
348 &dst_priv->fence : NULL);
349 }
350
351 Bool
352 driScreenInit(ScreenPtr pScreen)
353 {
354 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
355 modesettingPtr ms = modesettingPTR(pScrn);
356 DRI2InfoRec dri2info;
357
358 #if DRI2INFOREC_VERSION >= 2
359 dri2info.version = DRI2INFOREC_VERSION;
360 #else
361 dri2info.version = 1;
362 #endif
363 dri2info.fd = ms->fd;
364
365 dri2info.driverName = pScrn->driverName;
366 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
367
368 #if DRI2INFOREC_VERSION >= 2
369 dri2info.CreateBuffer = driCreateBuffer;
370 dri2info.DestroyBuffer = driDestroyBuffer;
371 #else
372 dri2info.CreateBuffers = driCreateBuffers;
373 dri2info.DestroyBuffers = driDestroyBuffers;
374 #endif
375 dri2info.CopyRegion = driCopyRegion;
376 dri2info.Wait = NULL;
377
378 ms->d_depth_bits_last =
379 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_X8Z24_UNORM,
380 PIPE_TEXTURE_2D,
381 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
382 ms->ds_depth_bits_last =
383 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_S8Z24_UNORM,
384 PIPE_TEXTURE_2D,
385 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
386
387 return DRI2ScreenInit(pScreen, &dri2info);
388 }
389
390 void
391 driCloseScreen(ScreenPtr pScreen)
392 {
393 DRI2CloseScreen(pScreen);
394 }
395
396 /* vim: set sw=4 ts=8 sts=4: */