c36bfe30ff81440fead92d0fb4fc509ff91e28ff
[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(struct dri_screen *screen,
73 unsigned pixel_bits)
74 {
75 __DRIconfig **configs;
76 unsigned num_modes;
77 uint8_t depth_bits_array[4];
78 uint8_t stencil_bits_array[4];
79 uint8_t msaa_samples_array[1];
80 unsigned depth_buffer_factor;
81 unsigned back_buffer_factor;
82 unsigned msaa_samples_factor;
83 GLenum fb_format;
84 GLenum fb_type;
85 struct pipe_screen *p_screen = screen->pipe_screen;
86
87 static const GLenum back_buffer_modes[] = {
88 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
89 };
90
91 depth_bits_array[0] = 0;
92 stencil_bits_array[0] = 0;
93 depth_buffer_factor = 1;
94
95 if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
96 PIPE_TEXTURE_2D,
97 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0)) {
98 depth_bits_array[depth_buffer_factor] = 16;
99 stencil_bits_array[depth_buffer_factor++] = 0;
100 }
101 if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
102 PIPE_TEXTURE_2D,
103 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0)) {
104 depth_bits_array[depth_buffer_factor] = 24;
105 stencil_bits_array[depth_buffer_factor++] = 0;
106 screen->d_depth_bits_last = TRUE;
107 } else if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
108 PIPE_TEXTURE_2D,
109 PIPE_TEXTURE_USAGE_DEPTH_STENCIL,
110 0)) {
111 depth_bits_array[depth_buffer_factor] = 24;
112 stencil_bits_array[depth_buffer_factor++] = 0;
113 screen->d_depth_bits_last = FALSE;
114 }
115 if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24S8_UNORM,
116 PIPE_TEXTURE_2D,
117 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0)) {
118 depth_bits_array[depth_buffer_factor] = 24;
119 stencil_bits_array[depth_buffer_factor++] = 8;
120 screen->sd_depth_bits_last = FALSE;
121 } else if (p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24S8_UNORM,
122 PIPE_TEXTURE_2D,
123 PIPE_TEXTURE_USAGE_DEPTH_STENCIL,
124 0)) {
125 depth_bits_array[depth_buffer_factor] = 24;
126 stencil_bits_array[depth_buffer_factor++] = 8;
127 screen->sd_depth_bits_last = TRUE;
128 }
129
130 msaa_samples_array[0] = 0;
131 back_buffer_factor = 3;
132 msaa_samples_factor = 1;
133
134 num_modes =
135 depth_buffer_factor * back_buffer_factor * msaa_samples_factor * 4;
136
137 if (pixel_bits == 16) {
138 if (!p_screen->is_format_supported(p_screen,
139 PIPE_FORMAT_R5G6B5_UNORM,
140 PIPE_TEXTURE_2D,
141 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
142 return NULL;
143 fb_format = GL_RGB;
144 fb_type = GL_UNSIGNED_SHORT_5_6_5;
145 } else {
146 if (!p_screen->is_format_supported(p_screen,
147 PIPE_FORMAT_A8R8G8B8_UNORM,
148 PIPE_TEXTURE_2D,
149 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
150 return NULL;
151 fb_format = GL_BGRA;
152 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
153 }
154
155 configs = driCreateConfigs(fb_format, fb_type,
156 depth_bits_array,
157 stencil_bits_array, depth_buffer_factor,
158 back_buffer_modes, back_buffer_factor,
159 msaa_samples_array, msaa_samples_factor);
160 if (configs == NULL) {
161 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
162 return NULL;
163 }
164
165 return (const const __DRIconfig **)configs;
166 }
167
168 /**
169 * Get information about previous buffer swaps.
170 */
171 static int
172 dri_get_swap_info(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
173 {
174 if (dPriv == NULL || dPriv->driverPrivate == NULL || sInfo == NULL)
175 return -1;
176 else
177 return 0;
178 }
179
180 static INLINE void
181 dri_copy_version(struct dri1_api_version *dst,
182 const struct __DRIversionRec *src)
183 {
184 dst->major = src->major;
185 dst->minor = src->minor;
186 dst->patch_level = src->patch;
187 }
188
189 static const __DRIconfig **
190 dri_init_screen(__DRIscreenPrivate * sPriv)
191 {
192 struct dri_screen *screen;
193 const __DRIconfig **configs;
194 struct dri1_create_screen_arg arg;
195
196 dri_init_extensions(NULL);
197
198 screen = CALLOC_STRUCT(dri_screen);
199 if (!screen)
200 return NULL;
201
202 screen->sPriv = sPriv;
203 screen->fd = sPriv->fd;
204 screen->drmLock = (drmLock *) & sPriv->pSAREA->lock;
205
206 sPriv->private = (void *)screen;
207 sPriv->extensions = dri_screen_extensions;
208
209 arg.base.mode = DRM_CREATE_DRI1;
210 arg.lf = &dri1_lf;
211 arg.ddx_info = sPriv->pDevPriv;
212 arg.ddx_info_size = sPriv->devPrivSize;
213 arg.sarea = sPriv->pSAREA;
214 dri_copy_version(&arg.ddx_version, &sPriv->ddx_version);
215 dri_copy_version(&arg.dri_version, &sPriv->dri_version);
216 dri_copy_version(&arg.drm_version, &sPriv->drm_version);
217 arg.api = NULL;
218
219 screen->pipe_screen = drm_api_hooks.create_screen(screen->fd, &arg.base);
220
221 if (!screen->pipe_screen || !arg.api) {
222 debug_printf("%s: failed to create dri1 screen\n", __FUNCTION__);
223 goto out_no_screen;
224 }
225
226 __dri1_api_hooks = arg.api;
227
228 screen->pipe_screen->flush_frontbuffer = dri1_flush_frontbuffer;
229 driParseOptionInfo(&screen->optionCache,
230 __driConfigOptions, __driNConfigOptions);
231
232 /**
233 * FIXME: If the driver supports format conversion swapbuffer blits, we might
234 * want to support other color bit depths than the server is currently
235 * using.
236 */
237
238 configs = dri_fill_in_modes(screen, sPriv->fbBPP);
239 if (!configs)
240 goto out_no_configs;
241
242 return configs;
243 out_no_configs:
244 screen->pipe_screen->destroy(screen->pipe_screen);
245 out_no_screen:
246 FREE(screen);
247 return NULL;
248 }
249
250 /**
251 * This is the driver specific part of the createNewScreen entry point.
252 *
253 * Returns the __GLcontextModes supported by this driver.
254 */
255 static const __DRIconfig **
256 dri_init_screen2(__DRIscreenPrivate * sPriv)
257 {
258 struct dri_screen *screen;
259 struct drm_create_screen_arg arg;
260
261 /* Set up dispatch table to cope with all known extensions */
262 dri_init_extensions(NULL);
263
264 screen = CALLOC_STRUCT(dri_screen);
265 if (!screen)
266 goto fail;
267
268 screen->sPriv = sPriv;
269 screen->fd = sPriv->fd;
270 sPriv->private = (void *)screen;
271 sPriv->extensions = dri_screen_extensions;
272 arg.mode = DRM_CREATE_NORMAL;
273
274 screen->pipe_screen = drm_api_hooks.create_screen(screen->fd, &arg);
275 if (!screen->pipe_screen) {
276 debug_printf("%s: failed to create pipe_screen\n", __FUNCTION__);
277 goto fail;
278 }
279
280 /* We need to hook in here */
281 screen->pipe_screen->flush_frontbuffer = dri_flush_frontbuffer;
282
283 driParseOptionInfo(&screen->optionCache,
284 __driConfigOptions, __driNConfigOptions);
285
286 return dri_fill_in_modes(screen, 32);
287 fail:
288 return NULL;
289 }
290
291 static void
292 dri_destroy_screen(__DRIscreenPrivate * sPriv)
293 {
294 struct dri_screen *screen = dri_screen(sPriv);
295
296 screen->pipe_screen->destroy(screen->pipe_screen);
297 FREE(screen);
298 sPriv->private = NULL;
299 }
300
301 PUBLIC const struct __DriverAPIRec driDriverAPI = {
302 .InitScreen = dri_init_screen,
303 .DestroyScreen = dri_destroy_screen,
304 .CreateContext = dri_create_context,
305 .DestroyContext = dri_destroy_context,
306 .CreateBuffer = dri_create_buffer,
307 .DestroyBuffer = dri_destroy_buffer,
308 .SwapBuffers = dri_swap_buffers,
309 .MakeCurrent = dri_make_current,
310 .UnbindContext = dri_unbind_context,
311 .GetSwapInfo = dri_get_swap_info,
312 .GetDrawableMSC = driDrawableGetMSC32,
313 .WaitForMSC = driWaitForMSC32,
314 .CopySubBuffer = dri_copy_sub_buffer,
315 .InitScreen = dri_init_screen,
316 .InitScreen2 = dri_init_screen2,
317 };
318
319 /* vim: set sw=3 ts=8 sts=3 expandtab: */