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