st/dri: Add dri2 prefix for all dri2.c functions
[mesa.git] / src / gallium / state_trackers / dri / 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 #ifndef __NOT_HAVE_DRM_H
34 #include "vblank.h"
35 #endif
36 #include "xmlpool.h"
37
38 #include "dri_screen.h"
39 #include "dri_context.h"
40 #include "dri_drawable.h"
41 #include "dri_st_api.h"
42 #include "dri1_helper.h"
43 #include "dri1.h"
44 #include "dri2.h"
45 #include "drisw.h"
46
47 #include "util/u_inlines.h"
48 #include "pipe/p_screen.h"
49 #include "pipe/p_format.h"
50 #include "state_tracker/drm_api.h"
51
52 #include "util/u_debug.h"
53
54 PUBLIC const char __driConfigOptions[] =
55 DRI_CONF_BEGIN DRI_CONF_SECTION_PERFORMANCE
56 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
57 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
58 DRI_CONF_SECTION_END DRI_CONF_SECTION_QUALITY
59 /* DRI_CONF_FORCE_S3TC_ENABLE(false) */
60 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
61 DRI_CONF_SECTION_END DRI_CONF_END;
62
63 const uint __driNConfigOptions = 3;
64
65 const __DRIconfig **
66 dri_fill_in_modes(struct dri_screen *screen,
67 unsigned pixel_bits)
68 {
69 __DRIconfig **configs = NULL;
70 __DRIconfig **configs_r5g6b5 = NULL;
71 __DRIconfig **configs_a8r8g8b8 = NULL;
72 __DRIconfig **configs_x8r8g8b8 = NULL;
73 unsigned num_modes;
74 uint8_t depth_bits_array[5];
75 uint8_t stencil_bits_array[5];
76 uint8_t msaa_samples_array[2];
77 unsigned depth_buffer_factor;
78 unsigned back_buffer_factor;
79 unsigned msaa_samples_factor;
80 struct pipe_screen *p_screen = screen->pipe_screen;
81 boolean pf_r5g6b5, pf_a8r8g8b8, pf_x8r8g8b8;
82 boolean pf_z16, pf_x8z24, pf_z24x8, pf_s8z24, pf_z24s8, pf_z32;
83
84 static const GLenum back_buffer_modes[] = {
85 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
86 };
87
88 depth_bits_array[0] = 0;
89 stencil_bits_array[0] = 0;
90 depth_buffer_factor = 1;
91
92 pf_x8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
93 PIPE_TEXTURE_2D,
94 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
95 pf_z24x8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
96 PIPE_TEXTURE_2D,
97 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
98 pf_s8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24S8_UNORM,
99 PIPE_TEXTURE_2D,
100 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
101 pf_z24s8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_S8Z24_UNORM,
102 PIPE_TEXTURE_2D,
103 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
104 pf_a8r8g8b8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_B8G8R8A8_UNORM,
105 PIPE_TEXTURE_2D,
106 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0);
107 pf_x8r8g8b8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_B8G8R8X8_UNORM,
108 PIPE_TEXTURE_2D,
109 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0);
110 pf_r5g6b5 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_B5G6R5_UNORM,
111 PIPE_TEXTURE_2D,
112 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0);
113
114 /* We can only get a 16 or 32 bit depth buffer with getBuffersWithFormat */
115 if (dri_with_format(screen->sPriv)) {
116 pf_z16 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
117 PIPE_TEXTURE_2D,
118 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
119 pf_z32 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z32_UNORM,
120 PIPE_TEXTURE_2D,
121 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
122 } else {
123 pf_z16 = FALSE;
124 pf_z32 = FALSE;
125 }
126
127 if (pf_z16) {
128 depth_bits_array[depth_buffer_factor] = 16;
129 stencil_bits_array[depth_buffer_factor++] = 0;
130 }
131 if (pf_x8z24 || pf_z24x8) {
132 depth_bits_array[depth_buffer_factor] = 24;
133 stencil_bits_array[depth_buffer_factor++] = 0;
134 screen->d_depth_bits_last = pf_x8z24;
135 }
136 if (pf_s8z24 || pf_z24s8) {
137 depth_bits_array[depth_buffer_factor] = 24;
138 stencil_bits_array[depth_buffer_factor++] = 8;
139 screen->sd_depth_bits_last = pf_s8z24;
140 }
141 if (pf_z32) {
142 depth_bits_array[depth_buffer_factor] = 32;
143 stencil_bits_array[depth_buffer_factor++] = 0;
144 }
145
146 msaa_samples_array[0] = 0;
147 msaa_samples_array[1] = 4;
148 back_buffer_factor = 3;
149 msaa_samples_factor = 2;
150
151 num_modes =
152 depth_buffer_factor * back_buffer_factor * msaa_samples_factor * 4;
153
154 if (pf_r5g6b5)
155 configs_r5g6b5 = driCreateConfigs(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
156 depth_bits_array, stencil_bits_array,
157 depth_buffer_factor, back_buffer_modes,
158 back_buffer_factor,
159 msaa_samples_array, msaa_samples_factor,
160 GL_TRUE);
161
162 if (pf_a8r8g8b8)
163 configs_a8r8g8b8 = driCreateConfigs(GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
164 depth_bits_array,
165 stencil_bits_array,
166 depth_buffer_factor,
167 back_buffer_modes,
168 back_buffer_factor,
169 msaa_samples_array,
170 msaa_samples_factor,
171 GL_TRUE);
172
173 if (pf_x8r8g8b8)
174 configs_x8r8g8b8 = driCreateConfigs(GL_BGR, GL_UNSIGNED_INT_8_8_8_8_REV,
175 depth_bits_array,
176 stencil_bits_array,
177 depth_buffer_factor,
178 back_buffer_modes,
179 back_buffer_factor,
180 msaa_samples_array,
181 msaa_samples_factor,
182 GL_TRUE);
183
184 if (pixel_bits == 16) {
185 configs = configs_r5g6b5;
186 if (configs_a8r8g8b8)
187 configs = configs ? driConcatConfigs(configs, configs_a8r8g8b8) : configs_a8r8g8b8;
188 if (configs_x8r8g8b8)
189 configs = configs ? driConcatConfigs(configs, configs_x8r8g8b8) : configs_x8r8g8b8;
190 } else {
191 configs = configs_a8r8g8b8;
192 if (configs_x8r8g8b8)
193 configs = configs ? driConcatConfigs(configs, configs_x8r8g8b8) : configs_x8r8g8b8;
194 if (configs_r5g6b5)
195 configs = configs ? driConcatConfigs(configs, configs_r5g6b5) : configs_r5g6b5;
196 }
197
198 if (configs == NULL) {
199 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
200 return NULL;
201 }
202
203 return (const __DRIconfig **)configs;
204 }
205
206 /**
207 * Roughly the converse of dri_fill_in_modes.
208 */
209 void
210 dri_fill_st_visual(struct st_visual *stvis, struct dri_screen *screen,
211 const __GLcontextModes *mode)
212 {
213 memset(stvis, 0, sizeof(*stvis));
214
215 stvis->samples = mode->samples;
216 stvis->render_buffer = ST_ATTACHMENT_INVALID;
217
218 if (mode->redBits == 8) {
219 if (mode->alphaBits == 8)
220 stvis->color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
221 else
222 stvis->color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
223 } else {
224 stvis->color_format = PIPE_FORMAT_B5G6R5_UNORM;
225 }
226
227 switch (mode->depthBits) {
228 default:
229 case 0:
230 stvis->depth_stencil_format = PIPE_FORMAT_NONE;
231 break;
232 case 16:
233 stvis->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
234 break;
235 case 24:
236 if (mode->stencilBits == 0) {
237 stvis->depth_stencil_format = (screen->d_depth_bits_last) ?
238 PIPE_FORMAT_Z24X8_UNORM:
239 PIPE_FORMAT_X8Z24_UNORM;
240 } else {
241 stvis->depth_stencil_format = (screen->sd_depth_bits_last) ?
242 PIPE_FORMAT_Z24S8_UNORM:
243 PIPE_FORMAT_S8Z24_UNORM;
244 }
245 break;
246 case 32:
247 stvis->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
248 break;
249 }
250
251 stvis->accum_format = (mode->haveAccumBuffer) ?
252 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
253
254 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
255 if (mode->doubleBufferMode)
256 stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
257 if (mode->stereoMode) {
258 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
259 if (mode->doubleBufferMode)
260 stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
261 }
262
263 if (mode->haveDepthBuffer || mode->haveStencilBuffer)
264 stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
265 /* let the state tracker allocate the accum buffer */
266 }
267
268 #ifndef __NOT_HAVE_DRM_H
269
270 /**
271 * Get information about previous buffer swaps.
272 */
273 static int
274 dri_get_swap_info(__DRIdrawable * dPriv, __DRIswapInfo * sInfo)
275 {
276 if (dPriv == NULL || dPriv->driverPrivate == NULL || sInfo == NULL)
277 return -1;
278 else
279 return 0;
280 }
281
282 #endif
283
284 static void
285 dri_destroy_option_cache(struct dri_screen * screen)
286 {
287 int i;
288
289 for (i = 0; i < (1 << screen->optionCache.tableSize); ++i) {
290 FREE(screen->optionCache.info[i].name);
291 FREE(screen->optionCache.info[i].ranges);
292 }
293
294 FREE(screen->optionCache.info);
295 FREE(screen->optionCache.values);
296 }
297
298 void
299 dri_destroy_screen(__DRIscreen * sPriv)
300 {
301 struct dri_screen *screen = dri_screen(sPriv);
302
303 dri1_destroy_pipe_context(screen);
304
305 if (screen->smapi)
306 dri_destroy_st_manager(screen->smapi);
307
308 if (screen->pipe_screen)
309 screen->pipe_screen->destroy(screen->pipe_screen);
310
311 dri_destroy_option_cache(screen);
312
313 FREE(screen);
314 sPriv->private = NULL;
315 sPriv->extensions = NULL;
316 }
317
318 #ifndef __NOT_HAVE_DRM_H
319
320 const struct __DriverAPIRec driDriverAPI = {
321 .DestroyScreen = dri_destroy_screen,
322 .CreateContext = dri_create_context,
323 .DestroyContext = dri_destroy_context,
324 .CreateBuffer = dri_create_buffer,
325 .DestroyBuffer = dri_destroy_buffer,
326 .MakeCurrent = dri_make_current,
327 .UnbindContext = dri_unbind_context,
328 .GetSwapInfo = dri_get_swap_info,
329 .GetDrawableMSC = driDrawableGetMSC32,
330 .WaitForMSC = driWaitForMSC32,
331 .InitScreen2 = dri2_init_screen,
332
333 .InitScreen = dri1_init_screen,
334 .SwapBuffers = dri1_swap_buffers,
335 .CopySubBuffer = dri1_copy_sub_buffer,
336 };
337
338 /* This is the table of extensions that the loader will dlsym() for. */
339 PUBLIC const __DRIextension *__driDriverExtensions[] = {
340 &driCoreExtension.base,
341 &driLegacyExtension.base,
342 &driDRI2Extension.base,
343 NULL
344 };
345
346 #else
347
348 const struct __DriverAPIRec driDriverAPI = {
349 .DestroyScreen = dri_destroy_screen,
350 .CreateContext = dri_create_context,
351 .DestroyContext = dri_destroy_context,
352 .CreateBuffer = dri_create_buffer,
353 .DestroyBuffer = dri_destroy_buffer,
354 .MakeCurrent = dri_make_current,
355 .UnbindContext = dri_unbind_context,
356
357 .InitScreen = drisw_init_screen,
358 .SwapBuffers = drisw_swap_buffers,
359 };
360
361 /* This is the table of extensions that the loader will dlsym() for. */
362 PUBLIC const __DRIextension *__driDriverExtensions[] = {
363 &driCoreExtension.base,
364 &driSWRastExtension.base,
365 NULL
366 };
367
368 #endif
369
370 /* vim: set sw=3 ts=8 sts=3 expandtab: */