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