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