1 /**************************************************************************
2 *
3 * Copyright 2008 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 #include "state_tracker/st_context.h"
29
30 #include <windows.h>
31
32 #include "glapi/glapi.h"
33 #include "util/u_debug.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "util/u_driconf.h"
37 #include "util/driconf.h"
38 #include "pipe/p_screen.h"
39
40 #include "stw_device.h"
41 #include "stw_winsys.h"
42 #include "stw_pixelformat.h"
43 #include "stw_gdishim.h"
44 #include "gldrv.h"
45 #include "stw_tls.h"
46 #include "stw_framebuffer.h"
47 #include "stw_st.h"
48
49
50 struct stw_device *stw_dev = NULL;
51
52 static int
stw_get_param(struct pipe_frontend_screen * fscreen,enum st_manager_param param)53 stw_get_param(struct pipe_frontend_screen *fscreen,
54 enum st_manager_param param)
55 {
56 switch (param) {
57 case ST_MANAGER_BROKEN_INVALIDATE:
58 /*
59 * Force framebuffer validation on glViewport.
60 *
61 * Certain applications, like Rhinoceros 4, uses glReadPixels
62 * exclusively (never uses SwapBuffers), so framebuffers never get
63 * resized unless we check on glViewport.
64 */
65 return 1;
66 default:
67 return 0;
68 }
69 }
70
71
72 /** Get the refresh rate for the monitor, in Hz */
73 static int
get_refresh_rate(void)74 get_refresh_rate(void)
75 {
76 #ifndef _GAMING_XBOX
77 DEVMODE devModes;
78
79 if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devModes)) {
80 /* clamp the value, just in case we get garbage */
81 return CLAMP(devModes.dmDisplayFrequency, 30, 120);
82 }
83 else {
84 /* reasonable default */
85 return 60;
86 }
87 #else
88 return 60;
89 #endif /* _GAMING_XBOX */
90 }
91
92 static bool
init_screen(const struct stw_winsys * stw_winsys,HDC hdc)93 init_screen(const struct stw_winsys *stw_winsys, HDC hdc)
94 {
95 struct pipe_screen *screen = stw_winsys->create_screen(hdc);
96 if (!screen)
97 return false;
98
99 if (stw_winsys->get_adapter_luid)
100 stw_winsys->get_adapter_luid(screen, hdc, &stw_dev->AdapterLuid);
101
102 stw_dev->fscreen->screen = screen;
103 stw_dev->screen = screen;
104 stw_dev->zink = !memcmp(screen->get_name(screen), "zink", 4);
105
106 stw_dev->max_2d_length = screen->get_param(screen,
107 PIPE_CAP_MAX_TEXTURE_2D_SIZE);
108 return true;
109 }
110
111 static const driOptionDescription gallium_driconf[] = {
112 #include "pipe-loader/driinfo_gallium.h"
113 };
114
115 static void
init_options()116 init_options()
117 {
118 const char *driver_name = stw_dev->stw_winsys->get_name ? stw_dev->stw_winsys->get_name() : NULL;
119 driParseOptionInfo(&stw_dev->option_info, gallium_driconf, ARRAY_SIZE(gallium_driconf));
120 driParseConfigFiles(&stw_dev->option_cache, &stw_dev->option_info, 0,
121 driver_name ? driver_name : "", NULL, NULL, NULL, 0, NULL, 0);
122
123 u_driconf_fill_st_options(&stw_dev->st_options, &stw_dev->option_cache);
124 }
125
126 char *
stw_get_config_xml(void)127 stw_get_config_xml(void)
128 {
129 return driGetOptionsXml(gallium_driconf, ARRAY_SIZE(gallium_driconf));
130 }
131
132 bool
stw_init(const struct stw_winsys * stw_winsys)133 stw_init(const struct stw_winsys *stw_winsys)
134 {
135 static struct stw_device stw_dev_storage;
136
137 if (debug_get_bool_option("WGL_DISABLE_ERROR_DIALOGS", false))
138 debug_disable_win32_error_dialogs();
139
140 assert(!stw_dev);
141
142 stw_tls_init();
143
144 stw_dev = &stw_dev_storage;
145 memset(stw_dev, 0, sizeof(*stw_dev));
146
147 stw_dev->stw_winsys = stw_winsys;
148
149 stw_dev->fscreen = CALLOC_STRUCT(pipe_frontend_screen);
150 if (!stw_dev->fscreen)
151 goto error1;
152
153 stw_dev->fscreen->get_param = stw_get_param;
154
155 InitializeCriticalSection(&stw_dev->screen_mutex);
156 InitializeCriticalSection(&stw_dev->ctx_mutex);
157 InitializeCriticalSection(&stw_dev->fb_mutex);
158
159 stw_dev->ctx_table = handle_table_create();
160 if (!stw_dev->ctx_table) {
161 goto error1;
162 }
163
164 /* env var override for WGL_EXT_swap_control, useful for testing/debugging */
165 const char *s = os_get_option("WGL_SWAP_INTERVAL");
166 if (s) {
167 stw_dev->swap_interval = atoi(s);
168 }
169 stw_dev->refresh_rate = get_refresh_rate();
170
171 stw_dev->initialized = true;
172
173 return true;
174
175 error1:
176 FREE(stw_dev->fscreen);
177
178 stw_dev = NULL;
179 return false;
180 }
181
182 bool
stw_init_screen(HDC hdc)183 stw_init_screen(HDC hdc)
184 {
185 EnterCriticalSection(&stw_dev->screen_mutex);
186
187 if (!stw_dev->screen_initialized) {
188 stw_dev->screen_initialized = true;
189 if (!init_screen(stw_dev->stw_winsys, hdc)) {
190 LeaveCriticalSection(&stw_dev->screen_mutex);
191 return false;
192 }
193 init_options();
194 stw_pixelformat_init();
195 }
196
197 LeaveCriticalSection(&stw_dev->screen_mutex);
198 return stw_dev->screen != NULL;
199 }
200
201 struct stw_device *
stw_get_device(void)202 stw_get_device(void)
203 {
204 return stw_dev;
205 }
206
207 bool
stw_init_thread(void)208 stw_init_thread(void)
209 {
210 return stw_tls_init_thread();
211 }
212
213
214 void
stw_cleanup_thread(void)215 stw_cleanup_thread(void)
216 {
217 stw_tls_cleanup_thread();
218 }
219
220
221 void
stw_cleanup(void)222 stw_cleanup(void)
223 {
224 DHGLRC dhglrc;
225
226 debug_printf("%s\n", __func__);
227
228 if (!stw_dev)
229 return;
230
231 /*
232 * Abort cleanup if there are still active contexts. In some situations
233 * this DLL may be unloaded before the DLL that is using GL contexts is.
234 */
235 stw_lock_contexts(stw_dev);
236 dhglrc = handle_table_get_first_handle(stw_dev->ctx_table);
237 stw_unlock_contexts(stw_dev);
238 if (dhglrc) {
239 debug_printf("%s: contexts still active -- cleanup aborted\n", __func__);
240 stw_dev = NULL;
241 return;
242 }
243
244 free(stw_dev->st_options.force_gl_vendor);
245 free(stw_dev->st_options.force_gl_renderer);
246 free(stw_dev->st_options.mesa_extension_override);
247 driDestroyOptionCache(&stw_dev->option_cache);
248 driDestroyOptionInfo(&stw_dev->option_info);
249
250 handle_table_destroy(stw_dev->ctx_table);
251
252 stw_framebuffer_cleanup();
253
254 DeleteCriticalSection(&stw_dev->fb_mutex);
255 DeleteCriticalSection(&stw_dev->ctx_mutex);
256 DeleteCriticalSection(&stw_dev->screen_mutex);
257
258 st_screen_destroy(stw_dev->fscreen);
259 FREE(stw_dev->fscreen);
260
261 stw_dev->screen->destroy(stw_dev->screen);
262
263 stw_tls_cleanup();
264
265 util_dynarray_fini(&stw_dev->pixelformats);
266
267 stw_dev = NULL;
268 }
269
270
271 void APIENTRY
DrvSetCallbackProcs(INT nProcs,PROC * pProcs)272 DrvSetCallbackProcs(INT nProcs, PROC *pProcs)
273 {
274 size_t size;
275
276 if (stw_dev == NULL)
277 return;
278
279 size = MIN2(nProcs * sizeof *pProcs, sizeof stw_dev->callbacks);
280 memcpy(&stw_dev->callbacks, pProcs, size);
281
282 return;
283 }
284
285
286 BOOL APIENTRY
DrvValidateVersion(ULONG ulVersion)287 DrvValidateVersion(ULONG ulVersion)
288 {
289 /* ulVersion is the version reported by the KMD:
290 * - via D3DKMTQueryAdapterInfo(KMTQAITYPE_UMOPENGLINFO) on WDDM,
291 * - or ExtEscape on XPDM and can be used to ensure the KMD and OpenGL ICD
292 * versions match.
293 *
294 * We should get the expected version number from the winsys, but for now
295 * ignore it.
296 */
297 (void)ulVersion;
298 return true;
299 }
300