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