st/dri: mv driDriverAPI to backends
[mesa.git] / src / gallium / state_trackers / dri / common / dri_screen.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 "utils.h"
33 #include "xmlpool.h"
34
35 #include "dri_screen.h"
36 #include "dri_context.h"
37 #include "dri_drawable.h"
38 #include "dri1_helper.h"
39
40 #include "util/u_inlines.h"
41 #include "pipe/p_screen.h"
42 #include "pipe/p_format.h"
43 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
44
45 #include "util/u_debug.h"
46
47 PUBLIC const char __driConfigOptions[] =
48 DRI_CONF_BEGIN DRI_CONF_SECTION_PERFORMANCE
49 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
50 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
51 DRI_CONF_SECTION_END DRI_CONF_SECTION_QUALITY
52 /* DRI_CONF_FORCE_S3TC_ENABLE(false) */
53 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
54 DRI_CONF_SECTION_END DRI_CONF_END;
55
56 static const uint __driNConfigOptions = 3;
57
58 static const __DRIconfig **
59 dri_fill_in_modes(struct dri_screen *screen,
60 unsigned pixel_bits)
61 {
62 __DRIconfig **configs = NULL;
63 __DRIconfig **configs_r5g6b5 = NULL;
64 __DRIconfig **configs_a8r8g8b8 = NULL;
65 __DRIconfig **configs_x8r8g8b8 = NULL;
66 uint8_t depth_bits_array[5];
67 uint8_t stencil_bits_array[5];
68 uint8_t msaa_samples_array[5];
69 unsigned depth_buffer_factor;
70 unsigned back_buffer_factor;
71 unsigned msaa_samples_factor;
72 unsigned i;
73 struct pipe_screen *p_screen = screen->base.screen;
74 boolean pf_r5g6b5, pf_a8r8g8b8, pf_x8r8g8b8;
75 boolean pf_z16, pf_x8z24, pf_z24x8, pf_s8z24, pf_z24s8, pf_z32;
76
77 static const GLenum back_buffer_modes[] = {
78 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
79 };
80
81 depth_bits_array[0] = 0;
82 stencil_bits_array[0] = 0;
83 depth_buffer_factor = 1;
84
85 pf_x8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
86 PIPE_TEXTURE_2D, 0,
87 PIPE_BIND_DEPTH_STENCIL, 0);
88 pf_z24x8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
89 PIPE_TEXTURE_2D, 0,
90 PIPE_BIND_DEPTH_STENCIL, 0);
91 pf_s8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24_UNORM_S8_USCALED,
92 PIPE_TEXTURE_2D, 0,
93 PIPE_BIND_DEPTH_STENCIL, 0);
94 pf_z24s8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_S8_USCALED_Z24_UNORM,
95 PIPE_TEXTURE_2D, 0,
96 PIPE_BIND_DEPTH_STENCIL, 0);
97 pf_a8r8g8b8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_B8G8R8A8_UNORM,
98 PIPE_TEXTURE_2D, 0,
99 PIPE_BIND_RENDER_TARGET, 0);
100 pf_x8r8g8b8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_B8G8R8X8_UNORM,
101 PIPE_TEXTURE_2D, 0,
102 PIPE_BIND_RENDER_TARGET, 0);
103 pf_r5g6b5 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_B5G6R5_UNORM,
104 PIPE_TEXTURE_2D, 0,
105 PIPE_BIND_RENDER_TARGET, 0);
106
107 /* We can only get a 16 or 32 bit depth buffer with getBuffersWithFormat */
108 if (dri_with_format(screen->sPriv)) {
109 pf_z16 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
110 PIPE_TEXTURE_2D, 0,
111 PIPE_BIND_DEPTH_STENCIL, 0);
112 pf_z32 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z32_UNORM,
113 PIPE_TEXTURE_2D, 0,
114 PIPE_BIND_DEPTH_STENCIL, 0);
115 } else {
116 pf_z16 = FALSE;
117 pf_z32 = FALSE;
118 }
119
120 if (pf_z16) {
121 depth_bits_array[depth_buffer_factor] = 16;
122 stencil_bits_array[depth_buffer_factor++] = 0;
123 }
124 if (pf_x8z24 || pf_z24x8) {
125 depth_bits_array[depth_buffer_factor] = 24;
126 stencil_bits_array[depth_buffer_factor++] = 0;
127 screen->d_depth_bits_last = pf_x8z24;
128 }
129 if (pf_s8z24 || pf_z24s8) {
130 depth_bits_array[depth_buffer_factor] = 24;
131 stencil_bits_array[depth_buffer_factor++] = 8;
132 screen->sd_depth_bits_last = pf_s8z24;
133 }
134 if (pf_z32) {
135 depth_bits_array[depth_buffer_factor] = 32;
136 stencil_bits_array[depth_buffer_factor++] = 0;
137 }
138
139 msaa_samples_array[0] = 0;
140 back_buffer_factor = 3;
141
142 /* also test color for msaa 2/4/6/8 - just assume it'll work for all depth buffers */
143 if (pf_r5g6b5) {
144 msaa_samples_factor = 1;
145 for (i = 1; i < 5; i++) {
146 if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_B5G6R5_UNORM,
147 PIPE_TEXTURE_2D, i*2,
148 PIPE_BIND_RENDER_TARGET, 0)) {
149 msaa_samples_array[msaa_samples_factor] = i * 2;
150 msaa_samples_factor++;
151 }
152 }
153
154 configs_r5g6b5 = driCreateConfigs(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
155 depth_bits_array, stencil_bits_array,
156 depth_buffer_factor, back_buffer_modes,
157 back_buffer_factor,
158 msaa_samples_array, msaa_samples_factor,
159 GL_TRUE);
160 }
161
162 if (pf_a8r8g8b8) {
163 msaa_samples_factor = 1;
164 for (i = 1; i < 5; i++) {
165 if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_B8G8R8A8_UNORM,
166 PIPE_TEXTURE_2D, i*2,
167 PIPE_BIND_RENDER_TARGET, 0)) {
168 msaa_samples_array[msaa_samples_factor] = i * 2;
169 msaa_samples_factor++;
170 }
171 }
172
173 configs_a8r8g8b8 = driCreateConfigs(GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
174 depth_bits_array,
175 stencil_bits_array,
176 depth_buffer_factor,
177 back_buffer_modes,
178 back_buffer_factor,
179 msaa_samples_array,
180 msaa_samples_factor,
181 GL_TRUE);
182 }
183
184 if (pf_x8r8g8b8) {
185 msaa_samples_factor = 1;
186 for (i = 1; i < 5; i++) {
187 if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_B8G8R8X8_UNORM,
188 PIPE_TEXTURE_2D, i*2,
189 PIPE_BIND_RENDER_TARGET, 0)) {
190 msaa_samples_array[msaa_samples_factor] = i * 2;
191 msaa_samples_factor++;
192 }
193 }
194
195 configs_x8r8g8b8 = driCreateConfigs(GL_BGR, GL_UNSIGNED_INT_8_8_8_8_REV,
196 depth_bits_array,
197 stencil_bits_array,
198 depth_buffer_factor,
199 back_buffer_modes,
200 back_buffer_factor,
201 msaa_samples_array,
202 msaa_samples_factor,
203 GL_TRUE);
204 }
205
206 if (pixel_bits == 16) {
207 configs = configs_r5g6b5;
208 if (configs_a8r8g8b8)
209 configs = configs ? driConcatConfigs(configs, configs_a8r8g8b8) : configs_a8r8g8b8;
210 if (configs_x8r8g8b8)
211 configs = configs ? driConcatConfigs(configs, configs_x8r8g8b8) : configs_x8r8g8b8;
212 } else {
213 configs = configs_a8r8g8b8;
214 if (configs_x8r8g8b8)
215 configs = configs ? driConcatConfigs(configs, configs_x8r8g8b8) : configs_x8r8g8b8;
216 if (configs_r5g6b5)
217 configs = configs ? driConcatConfigs(configs, configs_r5g6b5) : configs_r5g6b5;
218 }
219
220 if (configs == NULL) {
221 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
222 return NULL;
223 }
224
225 return (const __DRIconfig **)configs;
226 }
227
228 /**
229 * Roughly the converse of dri_fill_in_modes.
230 */
231 void
232 dri_fill_st_visual(struct st_visual *stvis, struct dri_screen *screen,
233 const __GLcontextModes *mode)
234 {
235 memset(stvis, 0, sizeof(*stvis));
236
237 stvis->samples = mode->samples;
238 stvis->render_buffer = ST_ATTACHMENT_INVALID;
239
240 if (mode->redBits == 8) {
241 if (mode->alphaBits == 8)
242 stvis->color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
243 else
244 stvis->color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
245 } else {
246 stvis->color_format = PIPE_FORMAT_B5G6R5_UNORM;
247 }
248
249 switch (mode->depthBits) {
250 default:
251 case 0:
252 stvis->depth_stencil_format = PIPE_FORMAT_NONE;
253 break;
254 case 16:
255 stvis->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
256 break;
257 case 24:
258 if (mode->stencilBits == 0) {
259 stvis->depth_stencil_format = (screen->d_depth_bits_last) ?
260 PIPE_FORMAT_Z24X8_UNORM:
261 PIPE_FORMAT_X8Z24_UNORM;
262 } else {
263 stvis->depth_stencil_format = (screen->sd_depth_bits_last) ?
264 PIPE_FORMAT_Z24_UNORM_S8_USCALED:
265 PIPE_FORMAT_S8_USCALED_Z24_UNORM;
266 }
267 break;
268 case 32:
269 stvis->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
270 break;
271 }
272
273 stvis->accum_format = (mode->haveAccumBuffer) ?
274 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
275
276 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
277 if (mode->doubleBufferMode)
278 stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
279 if (mode->stereoMode) {
280 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
281 if (mode->doubleBufferMode)
282 stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
283 }
284
285 if (mode->haveDepthBuffer || mode->haveStencilBuffer)
286 stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
287 /* let the state tracker allocate the accum buffer */
288 }
289
290 static boolean
291 dri_get_egl_image(struct st_manager *smapi,
292 struct st_context_iface *stctxi,
293 void *egl_image,
294 struct st_egl_image *stimg)
295 {
296 struct dri_context *ctx =
297 (struct dri_context *)stctxi->st_manager_private;
298 struct dri_screen *screen = dri_screen(ctx->sPriv);
299 __DRIimage *img = NULL;
300
301 if (screen->lookup_egl_image) {
302 img = screen->lookup_egl_image(ctx, egl_image);
303 }
304
305 if (!img)
306 return FALSE;
307
308 stimg->texture = NULL;
309 pipe_resource_reference(&stimg->texture, img->texture);
310 stimg->face = img->face;
311 stimg->level = img->level;
312 stimg->zslice = img->zslice;
313
314 return TRUE;
315 }
316
317 static int
318 dri_get_param(struct st_manager *smapi,
319 enum st_manager_param param)
320 {
321 struct dri_screen *screen = (struct dri_screen *)smapi;
322
323 switch(param) {
324 case ST_MANAGER_BROKEN_INVALIDATE:
325 return screen->broken_invalidate;
326 default:
327 return 0;
328 }
329 }
330
331 static void
332 dri_destroy_option_cache(struct dri_screen * screen)
333 {
334 int i;
335
336 if (screen->optionCache.info) {
337 for (i = 0; i < (1 << screen->optionCache.tableSize); ++i) {
338 FREE(screen->optionCache.info[i].name);
339 FREE(screen->optionCache.info[i].ranges);
340 }
341 FREE(screen->optionCache.info);
342 }
343
344 FREE(screen->optionCache.values);
345 }
346
347 void
348 dri_destroy_screen_helper(struct dri_screen * screen)
349 {
350 dri1_destroy_pipe_context(screen);
351
352 if (screen->st_api && screen->st_api->destroy)
353 screen->st_api->destroy(screen->st_api);
354
355 if (screen->base.screen)
356 screen->base.screen->destroy(screen->base.screen);
357
358 dri_destroy_option_cache(screen);
359 }
360
361 void
362 dri_destroy_screen(__DRIscreen * sPriv)
363 {
364 struct dri_screen *screen = dri_screen(sPriv);
365
366 dri_destroy_screen_helper(screen);
367
368 FREE(screen);
369 sPriv->private = NULL;
370 sPriv->extensions = NULL;
371 }
372
373 const __DRIconfig **
374 dri_init_screen_helper(struct dri_screen *screen,
375 struct pipe_screen *pscreen,
376 unsigned pixel_bits)
377 {
378 screen->base.screen = pscreen;
379 if (!screen->base.screen) {
380 debug_printf("%s: failed to create pipe_screen\n", __FUNCTION__);
381 return NULL;
382 }
383
384 screen->base.get_egl_image = dri_get_egl_image;
385 screen->base.get_param = dri_get_param;
386 screen->st_api = st_gl_api_create();
387
388 if (!screen->st_api)
389 return NULL;
390
391 driParseOptionInfo(&screen->optionCache,
392 __driConfigOptions, __driNConfigOptions);
393
394 return dri_fill_in_modes(screen, pixel_bits);
395 }
396
397 /* vim: set sw=3 ts=8 sts=3 expandtab: */