2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
31 #include "xorg-server.h"
33 #include "xf86_OSproc.h"
35 #include "xorg_tracker.h"
39 #include "pipe/p_state.h"
40 #include "pipe/p_inlines.h"
42 #include "util/u_rect.h"
46 struct pipe_texture
*tex
;
47 struct pipe_fence_handle
*fence
;
51 driCreateBuffers(DrawablePtr pDraw
, unsigned int *attachments
, int count
)
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
;
61 unsigned stride
, handle
;
62 boolean have_depth
= FALSE
, have_stencil
= FALSE
;
65 buffers
= xcalloc(count
, sizeof *buffers
);
69 privates
= xcalloc(count
, sizeof *privates
);
73 for (i
= 0; i
< count
; i
++) {
74 if (attachments
[i
] == DRI2BufferDepth
)
76 else if (attachments
[i
] == DRI2BufferStencil
)
80 if (have_stencil
&& !have_depth
)
81 FatalError("Doesn't support only stencil yet\n");
84 for (i
= 0; i
< count
; i
++) {
88 if (attachments
[i
] == DRI2BufferFrontLeft
) {
89 if (pDraw
->type
== DRAWABLE_PIXMAP
)
90 pPixmap
= (PixmapPtr
) pDraw
;
92 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
) pDraw
);
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
;
101 template.format
= ms
->ds_depth_bits_last
?
102 PIPE_FORMAT_S8Z24_UNORM
: PIPE_FORMAT_Z24S8_UNORM
;
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);
115 } else if (attachments
[i
] == DRI2BufferFakeFrontLeft
&&
116 pDraw
->type
== DRAWABLE_PIXMAP
) {
117 pPixmap
= (PixmapPtr
) pDraw
;
120 pPixmap
= (*pScreen
->CreatePixmap
)(pScreen
, pDraw
->width
,
127 xorg_exa_set_shared_usage(pPixmap
);
128 pScreen
->ModifyPixmapHeader(pPixmap
, 0, 0, 0, 0, 0, NULL
);
129 tex
= xorg_exa_get_texture(pPixmap
);
133 FatalError("NO TEXTURE IN DRI2\n");
135 ms
->api
->shared_handle_from_texture(ms
->api
, ms
->screen
, tex
, &stride
, &handle
);
137 buffers
[i
].name
= handle
;
138 buffers
[i
].attachment
= attachments
[i
];
139 buffers
[i
].pitch
= stride
;
141 buffers
[i
].driverPrivate
= &privates
[i
];
142 buffers
[i
].flags
= 0; /* not tiled */
143 privates
[i
].pPixmap
= pPixmap
;
144 privates
[i
].tex
= tex
;
156 driDestroyBuffers(DrawablePtr pDraw
, DRI2BufferPtr buffers
, int count
)
158 ScreenPtr pScreen
= pDraw
->pScreen
;
159 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
160 modesettingPtr ms
= modesettingPTR(pScrn
);
161 BufferPrivatePtr
private;
165 for (i
= 0; i
< count
; i
++) {
166 private = buffers
[i
].driverPrivate
;
168 pipe_texture_reference(&private->tex
, NULL
);
169 ms
->screen
->fence_reference(ms
->screen
, &private->fence
, NULL
);
171 if (private->pPixmap
)
172 (*pScreen
->DestroyPixmap
)(private->pPixmap
);
176 xfree(buffers
[0].driverPrivate
);
182 driCopyRegion(DrawablePtr pDraw
, RegionPtr pRegion
,
183 DRI2BufferPtr pDestBuffer
, DRI2BufferPtr pSrcBuffer
)
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
;
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.
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
;
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.
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.
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.
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
);
232 /* pixmap glXWaitGL */
233 if (pDestBuffer
->attachment
== DRI2BufferFrontLeft
&&
234 pSrcBuffer
->attachment
== DRI2BufferFakeFrontLeft
) {
237 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
238 "copying between the same pixmap\n");
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
);
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
);
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
);
259 (*gc
->ops
->CopyArea
)(&src_pixmap
->drawable
, &dst_pixmap
->drawable
, gc
,
260 0, 0, pDraw
->width
, pDraw
->height
, 0, 0);
264 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
,
265 pDestBuffer
->attachment
== DRI2BufferFrontLeft
?
266 &dst_priv
->fence
: NULL
);
270 driScreenInit(ScreenPtr pScreen
)
272 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
273 modesettingPtr ms
= modesettingPTR(pScrn
);
274 DRI2InfoRec dri2info
;
276 dri2info
.version
= 1;
277 dri2info
.fd
= ms
->fd
;
279 dri2info
.driverName
= pScrn
->driverName
;
280 dri2info
.deviceName
= "/dev/dri/card0"; /* FIXME */
282 dri2info
.CreateBuffers
= driCreateBuffers
;
283 dri2info
.DestroyBuffers
= driDestroyBuffers
;
284 dri2info
.CopyRegion
= driCopyRegion
;
286 ms
->d_depth_bits_last
=
287 ms
->screen
->is_format_supported(ms
->screen
, PIPE_FORMAT_X8Z24_UNORM
,
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
,
293 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0);
295 return DRI2ScreenInit(pScreen
, &dri2info
);
299 driCloseScreen(ScreenPtr pScreen
)
301 DRI2CloseScreen(pScreen
);
304 /* vim: set sw=4 ts=8 sts=4: */