state_trackers/dri/sw: Implement texture_from_pixmap.
[mesa.git] / src / gallium / state_trackers / dri / sw / dri_drawable.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30 */
31
32 #include "dri_screen.h"
33 #include "dri_context.h"
34 #include "dri_drawable.h"
35
36 #include "pipe/p_screen.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
40
41 #include "state_tracker/st_context.h"
42
43 static boolean
44 dri_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
45 const enum st_attachment_type *statts,
46 unsigned count,
47 struct pipe_resource **out)
48 {
49 struct dri_drawable *drawable =
50 (struct dri_drawable *) stfbi->st_manager_private;
51 struct dri_screen *screen = dri_screen(drawable->sPriv);
52 unsigned statt_mask, new_mask;
53 boolean new_stamp;
54 int i;
55
56 statt_mask = 0x0;
57 for (i = 0; i < count; i++)
58 statt_mask |= (1 << statts[i]);
59
60 /* record newly allocated textures */
61 new_mask = (statt_mask & ~drawable->texture_mask);
62
63 /*
64 * dPriv->pStamp is the server stamp. It should be accessed with a lock, at
65 * least for DRI1. dPriv->lastStamp is the client stamp. It has the value
66 * of the server stamp when last checked.
67 */
68 new_stamp = (drawable->texture_stamp != drawable->dPriv->lastStamp);
69
70 if (new_stamp || new_mask || screen->broken_invalidate) {
71 if (new_stamp && drawable->update_drawable_info)
72 drawable->update_drawable_info(drawable);
73
74 drawable->allocate_textures(drawable, statts, count);
75
76 /* add existing textures */
77 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
78 if (drawable->textures[i])
79 statt_mask |= (1 << i);
80 }
81
82 drawable->texture_stamp = drawable->dPriv->lastStamp;
83 drawable->texture_mask = statt_mask;
84 }
85
86 if (!out)
87 return TRUE;
88
89 for (i = 0; i < count; i++) {
90 out[i] = NULL;
91 pipe_resource_reference(&out[i], drawable->textures[statts[i]]);
92 }
93
94 return TRUE;
95 }
96
97 static boolean
98 dri_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi,
99 enum st_attachment_type statt)
100 {
101 struct dri_drawable *drawable =
102 (struct dri_drawable *) stfbi->st_manager_private;
103
104 /* XXX remove this and just set the correct one on the framebuffer */
105 drawable->flush_frontbuffer(drawable, statt);
106
107 return TRUE;
108 }
109
110 /**
111 * This is called when we need to set up GL rendering to a new X window.
112 */
113 boolean
114 dri_create_buffer(__DRIscreen * sPriv,
115 __DRIdrawable * dPriv,
116 const struct gl_config * visual, boolean isPixmap)
117 {
118 struct dri_screen *screen = sPriv->private;
119 struct dri_drawable *drawable = NULL;
120
121 if (isPixmap)
122 goto fail; /* not implemented */
123
124 drawable = CALLOC_STRUCT(dri_drawable);
125 if (drawable == NULL)
126 goto fail;
127
128 dri_fill_st_visual(&drawable->stvis, screen, visual);
129
130 /* setup the st_framebuffer_iface */
131 drawable->base.visual = &drawable->stvis;
132 drawable->base.flush_front = dri_st_framebuffer_flush_front;
133 drawable->base.validate = dri_st_framebuffer_validate;
134 drawable->base.st_manager_private = (void *) drawable;
135
136 drawable->screen = screen;
137 drawable->sPriv = sPriv;
138 drawable->dPriv = dPriv;
139 dPriv->driverPrivate = (void *)drawable;
140 p_atomic_set(&drawable->base.stamp, 1);
141
142 return GL_TRUE;
143 fail:
144 FREE(drawable);
145 return GL_FALSE;
146 }
147
148 void
149 dri_destroy_buffer(__DRIdrawable * dPriv)
150 {
151 struct dri_drawable *drawable = dri_drawable(dPriv);
152 int i;
153
154 pipe_surface_reference(&drawable->drisw_surface, NULL);
155
156 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
157 pipe_resource_reference(&drawable->textures[i], NULL);
158
159 FREE(drawable);
160 }
161
162 /**
163 * Validate the texture at an attachment. Allocate the texture if it does not
164 * exist. Used by the TFP extension.
165 */
166 static void
167 dri_drawable_validate_att(struct dri_drawable *drawable,
168 enum st_attachment_type statt)
169 {
170 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
171 unsigned i, count = 0;
172
173 /* check if buffer already exists */
174 if (drawable->texture_mask & (1 << statt))
175 return;
176
177 /* make sure DRI2 does not destroy existing buffers */
178 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
179 if (drawable->texture_mask & (1 << i)) {
180 statts[count++] = i;
181 }
182 }
183 statts[count++] = statt;
184
185 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
186
187 drawable->base.validate(&drawable->base, statts, count, NULL);
188 }
189
190 /**
191 * These are used for GLX_EXT_texture_from_pixmap
192 */
193 static void
194 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
195 GLint format, __DRIdrawable *dPriv)
196 {
197 struct dri_context *ctx = dri_context(pDRICtx);
198 struct dri_drawable *drawable = dri_drawable(dPriv);
199 struct pipe_resource *res;
200 struct st_context *stctx = (struct st_context *)ctx->st;
201 struct pipe_context *pipe = stctx->pipe;
202 struct pipe_transfer *tex_xfer;
203 char *map;
204 __DRIscreen *sPriv = dPriv->driScreenPriv;
205 int x, y, w, h, line, ximage_stride;
206
207 sPriv->swrast_loader->getDrawableInfo(dPriv, &x, &y, &w, &h, dPriv->loaderPrivate);
208
209 dri_drawable_validate_att(drawable, ST_ATTACHMENT_FRONT_LEFT);
210
211 /* Use the pipe resource associated with the X drawable */
212 res = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
213
214 if (res) {
215 enum pipe_format internal_format = res->format;
216
217 if (format == __DRI_TEXTURE_FORMAT_RGB) {
218 /* only need to cover the formats recognized by dri_fill_st_visual */
219 switch (internal_format) {
220 case PIPE_FORMAT_B8G8R8A8_UNORM:
221 internal_format = PIPE_FORMAT_B8G8R8X8_UNORM;
222 break;
223 case PIPE_FORMAT_A8R8G8B8_UNORM:
224 internal_format = PIPE_FORMAT_X8R8G8B8_UNORM;
225 break;
226 default:
227 break;
228 }
229 }
230
231
232 tex_xfer = pipe_get_transfer(pipe, res,
233 0, 0, // level, layer
234 PIPE_TRANSFER_WRITE,
235 x, y,
236 w, h);
237
238
239 map = pipe_transfer_map(pipe, tex_xfer);
240
241 /* Copy the Drawable content to the mapped texture buffer */
242 sPriv->swrast_loader->getImage(dPriv, x, y, w, h, map,
243 dPriv->loaderPrivate);
244
245 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
246 We assume 32 bit pixels. */
247 ximage_stride = w * 4;
248 for (line = h-1; line; --line) {
249 memmove(&map[line * tex_xfer->stride], &map[line * ximage_stride], ximage_stride);
250 }
251
252 pipe_transfer_unmap(pipe, tex_xfer);
253
254 pipe_transfer_destroy(pipe, tex_xfer);
255
256 ctx->st->teximage(ctx->st,
257 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT,
258 0, internal_format, res, FALSE);
259
260 }
261 }
262
263 static void
264 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
265 __DRIdrawable *dPriv)
266 {
267 dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
268 }
269
270 const __DRItexBufferExtension driTexBufferExtension = {
271 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
272 dri_set_tex_buffer,
273 dri_set_tex_buffer2,
274 NULL,
275 };
276
277 /**
278 * Get the format and binding of an attachment.
279 */
280 void
281 dri_drawable_get_format(struct dri_drawable *drawable,
282 enum st_attachment_type statt,
283 enum pipe_format *format,
284 unsigned *bind)
285 {
286 switch (statt) {
287 case ST_ATTACHMENT_FRONT_LEFT:
288 case ST_ATTACHMENT_BACK_LEFT:
289 case ST_ATTACHMENT_FRONT_RIGHT:
290 case ST_ATTACHMENT_BACK_RIGHT:
291 *format = drawable->stvis.color_format;
292 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
293 break;
294 case ST_ATTACHMENT_DEPTH_STENCIL:
295 *format = drawable->stvis.depth_stencil_format;
296 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
297 break;
298 default:
299 *format = PIPE_FORMAT_NONE;
300 *bind = 0;
301 break;
302 }
303 }
304
305 /* vim: set sw=3 ts=8 sts=3 expandtab: */