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