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_buffer
*buf
;
48 struct pipe_fence_handle
*fence
;
52 driCreateBuffers(DrawablePtr pDraw
, unsigned int *attachments
, int count
)
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
;
62 unsigned stride
, handle
;
65 buffers
= xcalloc(count
, sizeof *buffers
);
69 privates
= xcalloc(count
, sizeof *privates
);
74 for (i
= 0; i
< count
; i
++) {
78 if (attachments
[i
] == DRI2BufferFrontLeft
) {
79 if (pDraw
->type
== DRAWABLE_PIXMAP
)
80 pPixmap
= (PixmapPtr
) pDraw
;
82 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
) pDraw
);
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);
101 pPixmap
= (*pScreen
->CreatePixmap
)(pScreen
, pDraw
->width
,
105 tex
= xorg_exa_get_texture(pPixmap
);
109 FatalError("NO TEXTURE IN DRI2\n");
111 ms
->api
->buffer_from_texture(ms
->api
, tex
, &buf
, &stride
);
112 ms
->api
->global_handle_from_buffer(ms
->api
, ms
->screen
, buf
, &handle
);
114 buffers
[i
].name
= handle
;
115 buffers
[i
].attachment
= attachments
[i
];
116 buffers
[i
].pitch
= stride
;
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
;
134 driDestroyBuffers(DrawablePtr pDraw
, DRI2BufferPtr buffers
, int count
)
136 ScreenPtr pScreen
= pDraw
->pScreen
;
137 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
138 modesettingPtr ms
= modesettingPTR(pScrn
);
139 BufferPrivatePtr
private;
143 for (i
= 0; i
< count
; i
++) {
144 private = buffers
[i
].driverPrivate
;
146 pipe_texture_reference(&private->tex
, NULL
);
147 pipe_buffer_reference(&private->buf
, NULL
);
148 ms
->screen
->fence_reference(ms
->screen
, &private->fence
, NULL
);
150 if (private->pPixmap
)
151 (*pScreen
->DestroyPixmap
)(private->pPixmap
);
155 xfree(buffers
[0].driverPrivate
);
161 driCopyRegion(DrawablePtr pDraw
, RegionPtr pRegion
,
162 DRI2BufferPtr pDestBuffer
, DRI2BufferPtr pSrcBuffer
)
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
;
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
);
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
);
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
);
197 (*gc
->ops
->CopyArea
)(&src_pixmap
->drawable
, &dst_pixmap
->drawable
, gc
,
198 0, 0, pDraw
->width
, pDraw
->height
, 0, 0);
202 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_SWAPBUFFERS
,
203 pDestBuffer
->attachment
== DRI2BufferFrontLeft
?
204 &dst_priv
->fence
: NULL
);
208 driScreenInit(ScreenPtr pScreen
)
210 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
211 modesettingPtr ms
= modesettingPTR(pScrn
);
212 DRI2InfoRec dri2info
;
214 dri2info
.version
= 1;
215 dri2info
.fd
= ms
->fd
;
217 dri2info
.driverName
= pScrn
->name
;
219 dri2info
.driverName
= "i915"; /* FIXME */
221 dri2info
.deviceName
= "/dev/dri/card0"; /* FIXME */
223 dri2info
.CreateBuffers
= driCreateBuffers
;
224 dri2info
.DestroyBuffers
= driDestroyBuffers
;
225 dri2info
.CopyRegion
= driCopyRegion
;
227 return DRI2ScreenInit(pScreen
, &dri2info
);
231 driCloseScreen(ScreenPtr pScreen
)
233 DRI2CloseScreen(pScreen
);
236 /* vim: set sw=4 ts=8 sts=4: */