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 = { .dmSize = sizeof(DEVMODE) };
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 DRI_CONF_SECTION("WGL")
115 DRI_CONF_WGL_FRAME_LATENCY(2)
116 DRI_CONF_WGL_SWAP_INTERVAL(1)
117 DRI_CONF_SECTION_END
118 };
119
120 static void
init_options()121 init_options()
122 {
123 const char *driver_name = stw_dev->stw_winsys->get_name ? stw_dev->stw_winsys->get_name() : NULL;
124 driParseOptionInfo(&stw_dev->option_info, gallium_driconf, ARRAY_SIZE(gallium_driconf));
125 driParseConfigFiles(&stw_dev->option_cache, &stw_dev->option_info, 0,
126 driver_name ? driver_name : "", NULL, NULL, NULL, 0, NULL, 0);
127
128 u_driconf_fill_st_options(&stw_dev->st_options, &stw_dev->option_cache);
129
130 stw_dev->swap_interval = driQueryOptioni(&stw_dev->option_cache, "wgl_swap_interval");
131 }
132
133 char *
stw_get_config_xml(void)134 stw_get_config_xml(void)
135 {
136 return driGetOptionsXml(gallium_driconf, ARRAY_SIZE(gallium_driconf));
137 }
138
139 bool
stw_init(const struct stw_winsys * stw_winsys)140 stw_init(const struct stw_winsys *stw_winsys)
141 {
142 static struct stw_device stw_dev_storage;
143
144 if (debug_get_bool_option("WGL_DISABLE_ERROR_DIALOGS", false))
145 debug_disable_win32_error_dialogs();
146
147 assert(!stw_dev);
148
149 stw_tls_init();
150
151 stw_dev = &stw_dev_storage;
152 memset(stw_dev, 0, sizeof(*stw_dev));
153
154 stw_dev->stw_winsys = stw_winsys;
155
156 stw_dev->fscreen = CALLOC_STRUCT(pipe_frontend_screen);
157 if (!stw_dev->fscreen)
158 goto error1;
159
160 stw_dev->fscreen->get_param = stw_get_param;
161
162 InitializeCriticalSection(&stw_dev->screen_mutex);
163 InitializeCriticalSection(&stw_dev->ctx_mutex);
164 InitializeCriticalSection(&stw_dev->fb_mutex);
165
166 stw_dev->ctx_table = handle_table_create();
167 if (!stw_dev->ctx_table) {
168 goto error1;
169 }
170
171 stw_dev->refresh_rate = get_refresh_rate();
172
173 stw_dev->initialized = true;
174
175 return true;
176
177 error1:
178 FREE(stw_dev->fscreen);
179
180 stw_dev = NULL;
181 return false;
182 }
183
184 bool
stw_init_screen(HDC hdc)185 stw_init_screen(HDC hdc)
186 {
187 EnterCriticalSection(&stw_dev->screen_mutex);
188
189 if (!stw_dev->screen_initialized) {
190 stw_dev->screen_initialized = true;
191 if (!init_screen(stw_dev->stw_winsys, hdc)) {
192 LeaveCriticalSection(&stw_dev->screen_mutex);
193 return false;
194 }
195 init_options();
196 stw_pixelformat_init();
197 }
198
199 LeaveCriticalSection(&stw_dev->screen_mutex);
200 return stw_dev->screen != NULL;
201 }
202
203 struct stw_device *
stw_get_device(void)204 stw_get_device(void)
205 {
206 return stw_dev;
207 }
208
209 bool
stw_init_thread(void)210 stw_init_thread(void)
211 {
212 return stw_tls_init_thread();
213 }
214
215
216 void
stw_cleanup_thread(void)217 stw_cleanup_thread(void)
218 {
219 stw_tls_cleanup_thread();
220 }
221
222
223 void
stw_cleanup(void)224 stw_cleanup(void)
225 {
226 DHGLRC dhglrc;
227
228 debug_printf("%s\n", __func__);
229
230 if (!stw_dev)
231 return;
232
233 /*
234 * Abort cleanup if there are still active contexts. In some situations
235 * this DLL may be unloaded before the DLL that is using GL contexts is.
236 */
237 stw_lock_contexts(stw_dev);
238 dhglrc = handle_table_get_first_handle(stw_dev->ctx_table);
239 stw_unlock_contexts(stw_dev);
240 if (dhglrc) {
241 debug_printf("%s: contexts still active -- cleanup aborted\n", __func__);
242 stw_dev = NULL;
243 return;
244 }
245
246 free(stw_dev->st_options.force_gl_vendor);
247 free(stw_dev->st_options.force_gl_renderer);
248 free(stw_dev->st_options.mesa_extension_override);
249 driDestroyOptionCache(&stw_dev->option_cache);
250 driDestroyOptionInfo(&stw_dev->option_info);
251
252 handle_table_destroy(stw_dev->ctx_table);
253
254 stw_framebuffer_cleanup();
255
256 DeleteCriticalSection(&stw_dev->fb_mutex);
257 DeleteCriticalSection(&stw_dev->ctx_mutex);
258 DeleteCriticalSection(&stw_dev->screen_mutex);
259
260 st_screen_destroy(stw_dev->fscreen);
261 FREE(stw_dev->fscreen);
262
263 if (stw_dev->screen)
264 stw_dev->screen->destroy(stw_dev->screen);
265
266 stw_tls_cleanup();
267
268 util_dynarray_fini(&stw_dev->pixelformats);
269
270 stw_dev = NULL;
271 }
272
273
274 void APIENTRY
DrvSetCallbackProcs(INT nProcs,PROC * pProcs)275 DrvSetCallbackProcs(INT nProcs, PROC *pProcs)
276 {
277 size_t size;
278
279 if (stw_dev == NULL)
280 return;
281
282 size = MIN2(nProcs * sizeof *pProcs, sizeof stw_dev->callbacks);
283 memcpy(&stw_dev->callbacks, pProcs, size);
284
285 return;
286 }
287
288
289 BOOL APIENTRY
DrvValidateVersion(ULONG ulVersion)290 DrvValidateVersion(ULONG ulVersion)
291 {
292 /* ulVersion is the version reported by the KMD:
293 * - via D3DKMTQueryAdapterInfo(KMTQAITYPE_UMOPENGLINFO) on WDDM,
294 * - or ExtEscape on XPDM and can be used to ensure the KMD and OpenGL ICD
295 * versions match.
296 *
297 * We should get the expected version number from the winsys, but for now
298 * ignore it.
299 */
300 (void)ulVersion;
301 return true;
302 }
303