Merge branch '7.8'
[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 #include "dri1_helper.h"
36
37 #include "pipe/p_screen.h"
38 #include "util/u_format.h"
39 #include "util/u_memory.h"
40 #include "util/u_inlines.h"
41
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) {
71 if (new_stamp && screen->update_drawable_info)
72 screen->update_drawable_info(drawable);
73
74 screen->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 struct dri_screen *screen = dri_screen(drawable->sPriv);
104
105 /* XXX remove this and just set the correct one on the framebuffer */
106 screen->flush_frontbuffer(drawable, statt);
107
108 return TRUE;
109 }
110
111 /**
112 * This is called when we need to set up GL rendering to a new X window.
113 */
114 boolean
115 dri_create_buffer(__DRIscreen * sPriv,
116 __DRIdrawable * dPriv,
117 const __GLcontextModes * visual, boolean isPixmap)
118 {
119 struct dri_screen *screen = sPriv->private;
120 struct dri_drawable *drawable = NULL;
121
122 if (isPixmap)
123 goto fail; /* not implemented */
124
125 drawable = CALLOC_STRUCT(dri_drawable);
126 if (drawable == NULL)
127 goto fail;
128
129 dri_fill_st_visual(&drawable->stvis, screen, visual);
130
131 /* setup the st_framebuffer_iface */
132 drawable->base.visual = &drawable->stvis;
133 drawable->base.flush_front = dri_st_framebuffer_flush_front;
134 drawable->base.validate = dri_st_framebuffer_validate;
135 drawable->base.st_manager_private = (void *) drawable;
136
137 drawable->sPriv = sPriv;
138 drawable->dPriv = dPriv;
139 dPriv->driverPrivate = (void *)drawable;
140
141 drawable->desired_fences = 2;
142
143 return GL_TRUE;
144 fail:
145 FREE(drawable);
146 return GL_FALSE;
147 }
148
149 void
150 dri_destroy_buffer(__DRIdrawable * dPriv)
151 {
152 struct dri_drawable *drawable = dri_drawable(dPriv);
153 int i;
154
155 dri1_swap_fences_clear(drawable);
156
157 dri1_destroy_pipe_surface(drawable);
158
159 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
160 pipe_resource_reference(&drawable->textures[i], NULL);
161
162 drawable->desired_fences = 0;
163
164 FREE(drawable);
165 }
166
167 /**
168 * Validate the texture at an attachment. Allocate the texture if it does not
169 * exist.
170 */
171 void
172 dri_drawable_validate_att(struct dri_drawable *drawable,
173 enum st_attachment_type statt)
174 {
175 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
176 unsigned i, count = 0;
177
178 /* check if buffer already exists */
179 if (drawable->texture_mask & (1 << statt))
180 return;
181
182 /* make sure DRI2 does not destroy existing buffers */
183 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
184 if (drawable->texture_mask & (1 << i)) {
185 statts[count++] = i;
186 }
187 }
188 statts[count++] = statt;
189
190 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
191
192 /* this calles into the manager */
193 drawable->base.validate(&drawable->base, statts, count, NULL);
194 }
195
196 /**
197 * Get the format and binding of an attachment.
198 */
199 void
200 dri_drawable_get_format(struct dri_drawable *drawable,
201 enum st_attachment_type statt,
202 enum pipe_format *format,
203 unsigned *bind)
204 {
205 switch (statt) {
206 case ST_ATTACHMENT_FRONT_LEFT:
207 case ST_ATTACHMENT_BACK_LEFT:
208 case ST_ATTACHMENT_FRONT_RIGHT:
209 case ST_ATTACHMENT_BACK_RIGHT:
210 *format = drawable->stvis.color_format;
211 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
212 break;
213 case ST_ATTACHMENT_DEPTH_STENCIL:
214 *format = drawable->stvis.depth_stencil_format;
215 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
216 break;
217 default:
218 *format = PIPE_FORMAT_NONE;
219 *bind = 0;
220 break;
221 }
222 }
223
224 /* vim: set sw=3 ts=8 sts=3 expandtab: */