3fbab4dc51df956de38d1625c07db0d53525f90c
[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 {
101 pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
102 pDraw->height,
103 pDraw->depth,
104 0);
105 tex = xorg_exa_get_texture(pPixmap);
106 }
107
108 if (!tex)
109 FatalError("NO TEXTURE IN DRI2\n");
110
111 ms->api->buffer_from_texture(ms->api, tex, &buf, &stride);
112 ms->api->global_handle_from_buffer(ms->api, ms->screen, buf, &handle);
113
114 buffers[i].name = handle;
115 buffers[i].attachment = attachments[i];
116 buffers[i].pitch = stride;
117 buffers[i].cpp = 4;
118 buffers[i].driverPrivate = &privates[i];
119 buffers[i].flags = 0; /* not tiled */
120 privates[i].pPixmap = pPixmap;
121 privates[i].buf = buf;
122 privates[i].tex = tex;
123 }
124
125 return buffers;
126
127 fail_privates:
128 xfree(buffers);
129 fail_buffers:
130 return NULL;
131 }
132
133 static void
134 driDestroyBuffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
135 {
136 ScreenPtr pScreen = pDraw->pScreen;
137 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
138 modesettingPtr ms = modesettingPTR(pScrn);
139 BufferPrivatePtr private;
140 int i;
141 (void)ms;
142
143 for (i = 0; i < count; i++) {
144 private = buffers[i].driverPrivate;
145
146 pipe_texture_reference(&private->tex, NULL);
147 pipe_buffer_reference(&private->buf, NULL);
148 ms->screen->fence_reference(ms->screen, &private->fence, NULL);
149
150 if (private->pPixmap)
151 (*pScreen->DestroyPixmap)(private->pPixmap);
152 }
153
154 if (buffers) {
155 xfree(buffers[0].driverPrivate);
156 xfree(buffers);
157 }
158 }
159
160 static void
161 driCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
162 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
163 {
164 ScreenPtr pScreen = pDraw->pScreen;
165 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
166 modesettingPtr ms = modesettingPTR(pScrn);
167 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
168 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
169 PixmapPtr src_pixmap;
170 PixmapPtr dst_pixmap;
171 GCPtr gc;
172 RegionPtr copy_clip;
173
174 src_pixmap = src_priv->pPixmap;
175 dst_pixmap = dst_priv->pPixmap;
176 if (pSrcBuffer->attachment == DRI2BufferFrontLeft)
177 src_pixmap = (PixmapPtr)pDraw;
178 if (pDestBuffer->attachment == DRI2BufferFrontLeft)
179 dst_pixmap = (PixmapPtr)pDraw;
180 gc = GetScratchGC(pDraw->depth, pScreen);
181 copy_clip = REGION_CREATE(pScreen, NULL, 0);
182 REGION_COPY(pScreen, copy_clip, pRegion);
183 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
184 ValidateGC(&dst_pixmap->drawable, gc);
185
186 /* If this is a full buffer swap, throttle on the previous one */
187 if (dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
188 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
189
190 if (extents->x1 == 0 && extents->y1 == 0 &&
191 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
192 ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
193 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
194 }
195 }
196
197 (*gc->ops->CopyArea)(&src_pixmap->drawable, &dst_pixmap->drawable, gc,
198 0, 0, pDraw->width, pDraw->height, 0, 0);
199
200 FreeScratchGC(gc);
201
202 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
203 pDestBuffer->attachment == DRI2BufferFrontLeft ?
204 &dst_priv->fence : NULL);
205 }
206
207 Bool
208 driScreenInit(ScreenPtr pScreen)
209 {
210 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
211 modesettingPtr ms = modesettingPTR(pScrn);
212 DRI2InfoRec dri2info;
213
214 dri2info.version = 1;
215 dri2info.fd = ms->fd;
216 #if 0
217 dri2info.driverName = pScrn->name;
218 #else
219 dri2info.driverName = "i915"; /* FIXME */
220 #endif
221 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
222
223 dri2info.CreateBuffers = driCreateBuffers;
224 dri2info.DestroyBuffers = driDestroyBuffers;
225 dri2info.CopyRegion = driCopyRegion;
226
227 return DRI2ScreenInit(pScreen, &dri2info);
228 }
229
230 void
231 driCloseScreen(ScreenPtr pScreen)
232 {
233 DRI2CloseScreen(pScreen);
234 }
235
236 /* vim: set sw=4 ts=8 sts=4: */