Merge branch 'const-buffer-changes'
[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 #include "vblank.h"
34 #include "xmlpool.h"
35
36 #include "dri_screen.h"
37 #include "dri_context.h"
38 #include "dri_drawable.h"
39
40 #include "pipe/p_context.h"
41 #include "pipe/p_screen.h"
42 #include "pipe/p_inlines.h"
43 #include "pipe/p_format.h"
44 #include "state_tracker/drm_api.h"
45 #include "state_tracker/dri1_api.h"
46 #include "state_tracker/st_public.h"
47 #include "state_tracker/st_cb_fbo.h"
48
49 PUBLIC const char __driConfigOptions[] =
50 DRI_CONF_BEGIN DRI_CONF_SECTION_PERFORMANCE
51 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
52 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
53 DRI_CONF_SECTION_END DRI_CONF_SECTION_QUALITY
54 /*DRI_CONF_FORCE_S3TC_ENABLE(false) */
55 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
56 DRI_CONF_SECTION_END DRI_CONF_END;
57
58 const uint __driNConfigOptions = 3;
59
60 static const __DRIextension *dri_screen_extensions[] = {
61 &driReadDrawableExtension,
62 &driCopySubBufferExtension.base,
63 &driSwapControlExtension.base,
64 &driFrameTrackingExtension.base,
65 &driMediaStreamCounterExtension.base,
66 NULL
67 };
68
69 struct dri1_api *__dri1_api_hooks = NULL;
70
71 static const __DRIconfig **
72 dri_fill_in_modes(__DRIscreenPrivate * psp,
73 unsigned pixel_bits, unsigned depth_bits,
74 unsigned stencil_bits, GLboolean have_back_buffer)
75 {
76 __DRIconfig **configs;
77 __GLcontextModes *m;
78 unsigned num_modes;
79 uint8_t depth_bits_array[3];
80 uint8_t stencil_bits_array[3];
81 uint8_t msaa_samples_array[1];
82 unsigned depth_buffer_factor;
83 unsigned back_buffer_factor;
84 unsigned msaa_samples_factor;
85 GLenum fb_format;
86 GLenum fb_type;
87 int i;
88
89 static const GLenum back_buffer_modes[] = {
90 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
91 };
92
93 /* TODO probe the hardware of what is supports */
94 depth_bits_array[0] = 0;
95 depth_bits_array[1] = 24;
96 depth_bits_array[2] = 24;
97
98 stencil_bits_array[0] = 0; /* no depth or stencil */
99 stencil_bits_array[1] = 0; /* z24x8 */
100 stencil_bits_array[2] = 8; /* z24s8 */
101
102 msaa_samples_array[0] = 0;
103
104 depth_buffer_factor = 3;
105 back_buffer_factor = 3;
106 msaa_samples_factor = 1;
107
108 num_modes =
109 depth_buffer_factor * back_buffer_factor * msaa_samples_factor * 4;
110
111 if (pixel_bits == 16) {
112 fb_format = GL_RGB;
113 fb_type = GL_UNSIGNED_SHORT_5_6_5;
114 } else {
115 fb_format = GL_BGRA;
116 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
117 }
118
119 configs = driCreateConfigs(fb_format, fb_type,
120 depth_bits_array,
121 stencil_bits_array, depth_buffer_factor,
122 back_buffer_modes, back_buffer_factor,
123 msaa_samples_array, msaa_samples_factor);
124 if (configs == NULL) {
125 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
126 return NULL;
127 }
128
129 for (i = 0; configs[i]; i++) {
130 m = &configs[i]->modes;
131 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
132 m->visualRating = GLX_SLOW_CONFIG;
133 }
134 }
135
136 return (const const __DRIconfig **)configs;
137 }
138
139 /**
140 * Get information about previous buffer swaps.
141 */
142 static int
143 dri_get_swap_info(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
144 {
145 if (dPriv == NULL || dPriv->driverPrivate == NULL || sInfo == NULL)
146 return -1;
147 else
148 return 0;
149 }
150
151 static INLINE void
152 dri_copy_version(struct dri1_api_version *dst,
153 const struct __DRIversionRec *src)
154 {
155 dst->major = src->major;
156 dst->minor = src->minor;
157 dst->patch_level = src->patch;
158 }
159
160 static const __DRIconfig **
161 dri_init_screen(__DRIscreenPrivate * sPriv)
162 {
163 struct dri_screen *screen;
164 const __DRIconfig **configs;
165 struct dri1_create_screen_arg arg;
166
167 dri_init_extensions(NULL);
168
169 screen = CALLOC_STRUCT(dri_screen);
170 if (!screen)
171 return NULL;
172
173 screen->sPriv = sPriv;
174 screen->fd = sPriv->fd;
175 screen->drmLock = (drmLock *) & sPriv->pSAREA->lock;
176
177 sPriv->private = (void *)screen;
178 sPriv->extensions = dri_screen_extensions;
179
180 arg.base.mode = DRM_CREATE_DRI1;
181 arg.lf = &dri1_lf;
182 arg.ddx_info = sPriv->pDevPriv;
183 arg.ddx_info_size = sPriv->devPrivSize;
184 arg.sarea = sPriv->pSAREA;
185 dri_copy_version(&arg.ddx_version, &sPriv->ddx_version);
186 dri_copy_version(&arg.dri_version, &sPriv->dri_version);
187 dri_copy_version(&arg.drm_version, &sPriv->drm_version);
188 arg.api = NULL;
189
190 screen->pipe_screen = drm_api_hooks.create_screen(screen->fd, &arg.base);
191
192 if (!screen->pipe_screen || !arg.api) {
193 debug_printf("%s: failed to create dri1 screen\n", __FUNCTION__);
194 goto out_no_screen;
195 }
196
197 __dri1_api_hooks = arg.api;
198
199 screen->pipe_screen->flush_frontbuffer = dri1_flush_frontbuffer;
200 driParseOptionInfo(&screen->optionCache,
201 __driConfigOptions, __driNConfigOptions);
202
203 configs = dri_fill_in_modes(sPriv, sPriv->fbBPP, 24, 8, 1);
204 if (!configs)
205 goto out_no_configs;
206
207 return configs;
208 out_no_configs:
209 screen->pipe_screen->destroy(screen->pipe_screen);
210 out_no_screen:
211 FREE(screen);
212 return NULL;
213 }
214
215 /**
216 * This is the driver specific part of the createNewScreen entry point.
217 *
218 * Returns the __GLcontextModes supported by this driver.
219 */
220 static const __DRIconfig **
221 dri_init_screen2(__DRIscreenPrivate * sPriv)
222 {
223 struct dri_screen *screen;
224 struct drm_create_screen_arg arg;
225
226 /* Set up dispatch table to cope with all known extensions */
227 dri_init_extensions(NULL);
228
229 screen = CALLOC_STRUCT(dri_screen);
230 if (!screen)
231 goto fail;
232
233 screen->sPriv = sPriv;
234 screen->fd = sPriv->fd;
235 sPriv->private = (void *)screen;
236 sPriv->extensions = dri_screen_extensions;
237 arg.mode = DRM_CREATE_NORMAL;
238
239 screen->pipe_screen = drm_api_hooks.create_screen(screen->fd, &arg);
240 if (!screen->pipe_screen) {
241 debug_printf("%s: failed to create pipe_screen\n", __FUNCTION__);
242 goto fail;
243 }
244
245 /* We need to hook in here */
246 screen->pipe_screen->flush_frontbuffer = dri_flush_frontbuffer;
247
248 driParseOptionInfo(&screen->optionCache,
249 __driConfigOptions, __driNConfigOptions);
250
251 return dri_fill_in_modes(sPriv, 4 * 8, 24, 8, 1);
252 fail:
253 return NULL;
254 }
255
256 static void
257 dri_destroy_screen(__DRIscreenPrivate * sPriv)
258 {
259 struct dri_screen *screen = dri_screen(sPriv);
260
261 screen->pipe_screen->destroy(screen->pipe_screen);
262 FREE(screen);
263 sPriv->private = NULL;
264 }
265
266 PUBLIC const struct __DriverAPIRec driDriverAPI = {
267 .InitScreen = dri_init_screen,
268 .DestroyScreen = dri_destroy_screen,
269 .CreateContext = dri_create_context,
270 .DestroyContext = dri_destroy_context,
271 .CreateBuffer = dri_create_buffer,
272 .DestroyBuffer = dri_destroy_buffer,
273 .SwapBuffers = dri_swap_buffers,
274 .MakeCurrent = dri_make_current,
275 .UnbindContext = dri_unbind_context,
276 .GetSwapInfo = dri_get_swap_info,
277 .GetDrawableMSC = driDrawableGetMSC32,
278 .WaitForMSC = driWaitForMSC32,
279 .CopySubBuffer = dri_copy_sub_buffer,
280 .InitScreen = dri_init_screen,
281 .InitScreen2 = dri_init_screen2,
282 };
283
284 /* vim: set sw=3 ts=8 sts=3 expandtab: */