st/dri: use PIPE_TEXTURE_RECT if appropriate
[mesa.git] / src / gallium / state_trackers / dri / common / 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
42 static boolean
43 dri_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
44 const enum st_attachment_type *statts,
45 unsigned count,
46 struct pipe_resource **out)
47 {
48 struct dri_drawable *drawable =
49 (struct dri_drawable *) stfbi->st_manager_private;
50 struct dri_screen *screen = dri_screen(drawable->sPriv);
51 unsigned statt_mask, new_mask;
52 boolean new_stamp;
53 int i;
54
55 statt_mask = 0x0;
56 for (i = 0; i < count; i++)
57 statt_mask |= (1 << statts[i]);
58
59 /* record newly allocated textures */
60 new_mask = (statt_mask & ~drawable->texture_mask);
61
62 /*
63 * dPriv->pStamp is the server stamp. It should be accessed with a lock, at
64 * least for DRI1. dPriv->lastStamp is the client stamp. It has the value
65 * of the server stamp when last checked.
66 */
67 new_stamp = (drawable->texture_stamp != drawable->dPriv->lastStamp);
68
69 if (new_stamp || new_mask || screen->broken_invalidate) {
70 if (new_stamp && drawable->update_drawable_info)
71 drawable->update_drawable_info(drawable);
72
73 drawable->allocate_textures(drawable, statts, count);
74
75 /* add existing textures */
76 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
77 if (drawable->textures[i])
78 statt_mask |= (1 << i);
79 }
80
81 drawable->texture_stamp = drawable->dPriv->lastStamp;
82 drawable->texture_mask = statt_mask;
83 }
84
85 if (!out)
86 return TRUE;
87
88 for (i = 0; i < count; i++) {
89 out[i] = NULL;
90 pipe_resource_reference(&out[i], drawable->textures[statts[i]]);
91 }
92
93 return TRUE;
94 }
95
96 static boolean
97 dri_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi,
98 enum st_attachment_type statt)
99 {
100 struct dri_drawable *drawable =
101 (struct dri_drawable *) stfbi->st_manager_private;
102
103 /* XXX remove this and just set the correct one on the framebuffer */
104 drawable->flush_frontbuffer(drawable, statt);
105
106 return TRUE;
107 }
108
109 /**
110 * This is called when we need to set up GL rendering to a new X window.
111 */
112 boolean
113 dri_create_buffer(__DRIscreen * sPriv,
114 __DRIdrawable * dPriv,
115 const __GLcontextModes * visual, boolean isPixmap)
116 {
117 struct dri_screen *screen = sPriv->private;
118 struct dri_drawable *drawable = NULL;
119
120 if (isPixmap)
121 goto fail; /* not implemented */
122
123 drawable = CALLOC_STRUCT(dri_drawable);
124 if (drawable == NULL)
125 goto fail;
126
127 dri_fill_st_visual(&drawable->stvis, screen, visual);
128
129 /* setup the st_framebuffer_iface */
130 drawable->base.visual = &drawable->stvis;
131 drawable->base.flush_front = dri_st_framebuffer_flush_front;
132 drawable->base.validate = dri_st_framebuffer_validate;
133 drawable->base.st_manager_private = (void *) drawable;
134
135 drawable->sPriv = sPriv;
136 drawable->dPriv = dPriv;
137 dPriv->driverPrivate = (void *)drawable;
138
139 return GL_TRUE;
140 fail:
141 FREE(drawable);
142 return GL_FALSE;
143 }
144
145 void
146 dri_destroy_buffer(__DRIdrawable * dPriv)
147 {
148 struct dri_drawable *drawable = dri_drawable(dPriv);
149 int i;
150
151 pipe_surface_reference(&drawable->drisw_surface, NULL);
152
153 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
154 pipe_resource_reference(&drawable->textures[i], NULL);
155
156 FREE(drawable);
157 }
158
159 /**
160 * Validate the texture at an attachment. Allocate the texture if it does not
161 * exist. Used by the TFP extension.
162 */
163 static void
164 dri_drawable_validate_att(struct dri_drawable *drawable,
165 enum st_attachment_type statt)
166 {
167 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
168 unsigned i, count = 0;
169
170 /* check if buffer already exists */
171 if (drawable->texture_mask & (1 << statt))
172 return;
173
174 /* make sure DRI2 does not destroy existing buffers */
175 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
176 if (drawable->texture_mask & (1 << i)) {
177 statts[count++] = i;
178 }
179 }
180 statts[count++] = statt;
181
182 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
183
184 drawable->base.validate(&drawable->base, statts, count, NULL);
185 }
186
187 /**
188 * These are used for GLX_EXT_texture_from_pixmap
189 */
190 static void
191 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
192 GLint format, __DRIdrawable *dPriv)
193 {
194 struct dri_context *ctx = dri_context(pDRICtx);
195 struct dri_drawable *drawable = dri_drawable(dPriv);
196 struct pipe_resource *pt;
197
198 dri_drawable_validate_att(drawable, ST_ATTACHMENT_FRONT_LEFT);
199
200 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
201
202 if (pt) {
203 enum pipe_format internal_format = pt->format;
204
205 if (format == __DRI_TEXTURE_FORMAT_RGB) {
206 /* only need to cover the formats recognized by dri_fill_st_visual */
207 switch (internal_format) {
208 case PIPE_FORMAT_B8G8R8A8_UNORM:
209 internal_format = PIPE_FORMAT_B8G8R8X8_UNORM;
210 break;
211 case PIPE_FORMAT_A8R8G8B8_UNORM:
212 internal_format = PIPE_FORMAT_X8R8G8B8_UNORM;
213 break;
214 default:
215 break;
216 }
217 }
218
219 ctx->st->teximage(ctx->st,
220 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT,
221 0, internal_format, pt, FALSE);
222 }
223 }
224
225 static void
226 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
227 __DRIdrawable *dPriv)
228 {
229 dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
230 }
231
232 const __DRItexBufferExtension driTexBufferExtension = {
233 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
234 dri_set_tex_buffer,
235 dri_set_tex_buffer2,
236 };
237
238 /**
239 * Get the format and binding of an attachment.
240 */
241 void
242 dri_drawable_get_format(struct dri_drawable *drawable,
243 enum st_attachment_type statt,
244 enum pipe_format *format,
245 unsigned *bind)
246 {
247 switch (statt) {
248 case ST_ATTACHMENT_FRONT_LEFT:
249 case ST_ATTACHMENT_BACK_LEFT:
250 case ST_ATTACHMENT_FRONT_RIGHT:
251 case ST_ATTACHMENT_BACK_RIGHT:
252 *format = drawable->stvis.color_format;
253 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
254 break;
255 case ST_ATTACHMENT_DEPTH_STENCIL:
256 *format = drawable->stvis.depth_stencil_format;
257 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
258 break;
259 default:
260 *format = PIPE_FORMAT_NONE;
261 *bind = 0;
262 break;
263 }
264 }
265
266 /* vim: set sw=3 ts=8 sts=3 expandtab: */