xorg: revert bad merge
[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
37 #include "dri2.h"
38
39 #include "pipe/p_state.h"
40 #include "pipe/p_inlines.h"
41
42 #include "util/u_rect.h"
43
44 typedef struct {
45 PixmapPtr pPixmap;
46 struct pipe_texture *tex;
47 struct pipe_buffer *buf;
48 struct pipe_fence_handle *fence;
49 } *BufferPrivatePtr;
50
51 static DRI2BufferPtr
52 driCreateBuffers(DrawablePtr pDraw, unsigned int *attachments, int count)
53 {
54 struct pipe_texture *depth, *tex;
55 struct pipe_buffer *buf;
56 ScreenPtr pScreen = pDraw->pScreen;
57 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
58 modesettingPtr ms = modesettingPTR(pScrn);
59 BufferPrivatePtr privates;
60 DRI2BufferPtr buffers;
61 PixmapPtr pPixmap;
62 unsigned stride, handle;
63 int i;
64
65 buffers = xcalloc(count, sizeof *buffers);
66 if (!buffers)
67 goto fail_buffers;
68
69 privates = xcalloc(count, sizeof *privates);
70 if (!privates)
71 goto fail_privates;
72
73 depth = NULL;
74 for (i = 0; i < count; i++) {
75 pPixmap = NULL;
76 tex = NULL;
77 buf = NULL;
78 if (attachments[i] == DRI2BufferFrontLeft) {
79 if (pDraw->type == DRAWABLE_PIXMAP)
80 pPixmap = (PixmapPtr) pDraw;
81 else
82 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
83 pPixmap->refcnt++;
84 tex = xorg_exa_get_texture(pPixmap);
85 } else if (attachments[i] == DRI2BufferStencil) {
86 pipe_texture_reference(&tex, depth);
87 } else if (attachments[i] == DRI2BufferDepth) {
88 struct pipe_texture template;
89 memset(&template, 0, sizeof(template));
90 template.target = PIPE_TEXTURE_2D;
91 template.format = PIPE_FORMAT_S8Z24_UNORM;
92 pf_get_block(template.format, &template.block);
93 template.width[0] = pDraw->width;
94 template.height[0] = pDraw->height;
95 template.depth[0] = 1;
96 template.last_level = 0;
97 template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
98 tex = ms->screen->texture_create(ms->screen, &template);
99 depth = tex;
100 } else if (attachments[i] == DRI2BufferFakeFrontLeft &&
101 pDraw->type == DRAWABLE_PIXMAP) {
102 pPixmap = (PixmapPtr) pDraw;
103 pPixmap->refcnt++;
104 tex = xorg_exa_get_texture(pPixmap);
105 } else {
106 pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
107 pDraw->height,
108 pDraw->depth,
109 0);
110 tex = xorg_exa_get_texture(pPixmap);
111 }
112
113 if (!tex)
114 FatalError("NO TEXTURE IN DRI2\n");
115
116 ms->api->buffer_from_texture(ms->api, tex, &buf, &stride);
117 ms->api->global_handle_from_buffer(ms->api, ms->screen, buf, &handle);
118
119 buffers[i].name = handle;
120 buffers[i].attachment = attachments[i];
121 buffers[i].pitch = stride;
122 buffers[i].cpp = 4;
123 buffers[i].driverPrivate = &privates[i];
124 buffers[i].flags = 0; /* not tiled */
125 privates[i].pPixmap = pPixmap;
126 privates[i].buf = buf;
127 privates[i].tex = tex;
128 }
129
130 return buffers;
131
132 fail_privates:
133 xfree(buffers);
134 fail_buffers:
135 return NULL;
136 }
137
138 static void
139 driDestroyBuffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
140 {
141 ScreenPtr pScreen = pDraw->pScreen;
142 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
143 modesettingPtr ms = modesettingPTR(pScrn);
144 BufferPrivatePtr private;
145 int i;
146 (void)ms;
147
148 for (i = 0; i < count; i++) {
149 private = buffers[i].driverPrivate;
150
151 pipe_texture_reference(&private->tex, NULL);
152 pipe_buffer_reference(&private->buf, NULL);
153 ms->screen->fence_reference(ms->screen, &private->fence, NULL);
154
155 if (private->pPixmap)
156 (*pScreen->DestroyPixmap)(private->pPixmap);
157 }
158
159 if (buffers) {
160 xfree(buffers[0].driverPrivate);
161 xfree(buffers);
162 }
163 }
164
165 static void
166 driCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
167 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
168 {
169 ScreenPtr pScreen = pDraw->pScreen;
170 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
171 modesettingPtr ms = modesettingPTR(pScrn);
172 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
173 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
174 PixmapPtr src_pixmap;
175 PixmapPtr dst_pixmap;
176 GCPtr gc;
177 RegionPtr copy_clip;
178
179 /*
180 * In driCreateBuffers we dewrap windows into the
181 * backing pixmaps in order to get to the texture.
182 * We need to use the real drawable in CopyArea
183 * so that cliprects and offsets are correct.
184 */
185 src_pixmap = src_priv->pPixmap;
186 dst_pixmap = dst_priv->pPixmap;
187 if (pSrcBuffer->attachment == DRI2BufferFrontLeft)
188 src_pixmap = (PixmapPtr)pDraw;
189 if (pDestBuffer->attachment == DRI2BufferFrontLeft)
190 dst_pixmap = (PixmapPtr)pDraw;
191
192 /*
193 * The clients implements glXWaitX with a copy front to fake and then
194 * waiting on the server to signal its completion of it. While
195 * glXWaitGL is a client side flush and a copy from fake to front.
196 * This is how it is done in the DRI2 protocol, how ever depending
197 * which type of drawables the server does things a bit differently
198 * then what the protocol says as the fake and front are the same.
199 *
200 * for pixmaps glXWaitX is a server flush.
201 * for pixmaps glXWaitGL is a client flush.
202 * for windows glXWaitX is a copy from front to fake then a server flush.
203 * for windows glXWaitGL is a client flush then a copy from fake to front.
204 *
205 * XXX in the windows case this code always flushes but that isn't a
206 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
207 * or a glFlush/glFinish call.
208 */
209 if (dst_pixmap == src_pixmap) {
210 /* pixmap glXWaitX */
211 if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
212 pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
213 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
214 return;
215 }
216 /* pixmap glXWaitGL */
217 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
218 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
219 return;
220 } else {
221 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
222 "copying between the same pixmap\n");
223 }
224 }
225
226 gc = GetScratchGC(pDraw->depth, pScreen);
227 copy_clip = REGION_CREATE(pScreen, NULL, 0);
228 REGION_COPY(pScreen, copy_clip, pRegion);
229 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
230 ValidateGC(&dst_pixmap->drawable, gc);
231
232 /* If this is a full buffer swap, throttle on the previous one */
233 if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
234 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
235
236 if (extents->x1 == 0 && extents->y1 == 0 &&
237 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
238 ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
239 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
240 }
241 }
242
243 (*gc->ops->CopyArea)(&src_pixmap->drawable, &dst_pixmap->drawable, gc,
244 0, 0, pDraw->width, pDraw->height, 0, 0);
245
246 FreeScratchGC(gc);
247
248 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
249 pDestBuffer->attachment == DRI2BufferFrontLeft ?
250 &dst_priv->fence : NULL);
251 }
252
253 Bool
254 driScreenInit(ScreenPtr pScreen)
255 {
256 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
257 modesettingPtr ms = modesettingPTR(pScrn);
258 DRI2InfoRec dri2info;
259
260 dri2info.version = 1;
261 dri2info.fd = ms->fd;
262 #if 0
263 dri2info.driverName = pScrn->name;
264 #else
265 dri2info.driverName = "i915"; /* FIXME */
266 #endif
267 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
268
269 dri2info.CreateBuffers = driCreateBuffers;
270 dri2info.DestroyBuffers = driDestroyBuffers;
271 dri2info.CopyRegion = driCopyRegion;
272
273 return DRI2ScreenInit(pScreen, &dri2info);
274 }
275
276 void
277 driCloseScreen(ScreenPtr pScreen)
278 {
279 DRI2CloseScreen(pScreen);
280 }
281
282 /* vim: set sw=4 ts=8 sts=4: */