Merge branch 'mesa_7_5_branch'
[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 typedef struct {
43 PixmapPtr pPixmap;
44 struct pipe_texture *tex;
45 struct pipe_buffer *buf;
46 } *BufferPrivatePtr;
47
48 static DRI2BufferPtr
49 driCreateBuffers(DrawablePtr pDraw, unsigned int *attachments, int count)
50 {
51 struct pipe_texture *depth, *tex;
52 struct pipe_buffer *buf;
53 ScreenPtr pScreen = pDraw->pScreen;
54 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
55 modesettingPtr ms = modesettingPTR(pScrn);
56 BufferPrivatePtr privates;
57 DRI2BufferPtr buffers;
58 PixmapPtr pPixmap;
59 unsigned stride, handle;
60 int i;
61
62 buffers = xcalloc(count, sizeof *buffers);
63 if (!buffers)
64 goto fail_buffers;
65
66 privates = xcalloc(count, sizeof *privates);
67 if (!privates)
68 goto fail_privates;
69
70 depth = NULL;
71 for (i = 0; i < count; i++) {
72 pPixmap = NULL;
73 tex = NULL;
74 buf = NULL;
75 if (attachments[i] == DRI2BufferFrontLeft) {
76 if (pDraw->type == DRAWABLE_PIXMAP)
77 pPixmap = (PixmapPtr) pDraw;
78 else
79 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
80 pPixmap->refcnt++;
81 tex = xorg_exa_get_texture(pPixmap);
82 } else if (attachments[i] == DRI2BufferStencil) {
83 pipe_texture_reference(&tex, depth);
84 } else if (attachments[i] == DRI2BufferDepth) {
85 struct pipe_texture template;
86
87 memset(&template, 0, sizeof(template));
88 template.target = PIPE_TEXTURE_2D;
89 template.format = PIPE_FORMAT_S8Z24_UNORM;
90 pf_get_block(template.format, &template.block);
91 template.width[0] = pDraw->width;
92 template.height[0] = pDraw->height;
93 template.depth[0] = 1;
94 template.last_level = 0;
95 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
96 tex = ms->screen->texture_create(ms->screen, &template);
97 } else {
98 struct pipe_texture template;
99 memset(&template, 0, sizeof(template));
100 template.target = PIPE_TEXTURE_2D;
101 template.format = PIPE_FORMAT_A8R8G8B8_UNORM;
102 pf_get_block(template.format, &template.block);
103 template.width[0] = pDraw->width;
104 template.height[0] = pDraw->height;
105 template.depth[0] = 1;
106 template.last_level = 0;
107 template.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
108 tex = ms->screen->texture_create(ms->screen, &template);
109 }
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
142 for (i = 0; i < count; i++) {
143 private = buffers[i].driverPrivate;
144
145 if (private->pPixmap)
146 (*pScreen->DestroyPixmap)(private->pPixmap);
147
148 pipe_texture_reference(&private->tex, NULL);
149 pipe_buffer_reference(&private->buf, NULL);
150 }
151
152 if (buffers) {
153 xfree(buffers[0].driverPrivate);
154 xfree(buffers);
155 }
156 }
157
158 static void
159 driCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
160 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
161 {
162 ScreenPtr pScreen = pDraw->pScreen;
163 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
164 modesettingPtr ms = modesettingPTR(pScrn);
165 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
166 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
167
168 struct pipe_surface *dst_surf =
169 ms->screen->get_tex_surface(ms->screen, dst_priv->tex, 0, 0, 0,
170 PIPE_BUFFER_USAGE_GPU_WRITE);
171 struct pipe_surface *src_surf =
172 ms->screen->get_tex_surface(ms->screen, src_priv->tex, 0, 0, 0,
173 PIPE_BUFFER_USAGE_GPU_READ);
174
175 ms->ctx->surface_copy(ms->ctx, dst_surf, 0, 0, src_surf,
176 0, 0, pDraw->width, pDraw->height);
177
178 pipe_surface_reference(&dst_surf, NULL);
179 pipe_surface_reference(&src_surf, NULL);
180 }
181
182 Bool
183 driScreenInit(ScreenPtr pScreen)
184 {
185 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
186 modesettingPtr ms = modesettingPTR(pScrn);
187 DRI2InfoRec dri2info;
188
189 dri2info.version = 1;
190 dri2info.fd = ms->fd;
191 #if 0
192 dri2info.driverName = pScrn->name;
193 #else
194 dri2info.driverName = "i915"; /* FIXME */
195 #endif
196 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
197
198 dri2info.CreateBuffers = driCreateBuffers;
199 dri2info.DestroyBuffers = driDestroyBuffers;
200 dri2info.CopyRegion = driCopyRegion;
201
202 return DRI2ScreenInit(pScreen, &dri2info);
203 }
204
205 void
206 driCloseScreen(ScreenPtr pScreen)
207 {
208 DRI2CloseScreen(pScreen);
209 }
210
211 /* vim: set sw=4 ts=8 sts=4: */