1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
36 #include "stw_device.h"
37 #include "stw_winsys.h"
38 #include "stw_pixelformat.h"
41 #include "stw_framebuffer.h"
45 struct stw_device
*stw_dev
= NULL
;
48 stw_get_param(struct st_manager
*smapi
,
49 enum st_manager_param param
)
52 case ST_MANAGER_BROKEN_INVALIDATE
:
54 * Force framebuffer validation on glViewport.
56 * Certain applications, like Rhinoceros 4, uses glReadPixels
57 * exclusively (never uses SwapBuffers), so framebuffers never get
58 * resized unless we check on glViewport.
67 /** Get the refresh rate for the monitor, in Hz */
69 get_refresh_rate(void)
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);
78 /* reasonable default */
85 stw_init(const struct stw_winsys
*stw_winsys
)
87 static struct stw_device stw_dev_storage
;
88 struct pipe_screen
*screen
;
90 debug_disable_error_message_boxes();
92 debug_printf("%s\n", __FUNCTION__
);
98 stw_dev
= &stw_dev_storage
;
99 memset(stw_dev
, 0, sizeof(*stw_dev
));
102 stw_dev
->memdbg_no
= debug_memory_begin();
105 stw_dev
->stw_winsys
= stw_winsys
;
107 stw_dev
->stapi
= stw_st_create_api();
108 stw_dev
->smapi
= CALLOC_STRUCT(st_manager
);
109 if (!stw_dev
->stapi
|| !stw_dev
->smapi
)
112 screen
= stw_winsys
->create_screen();
116 if (stw_winsys
->get_adapter_luid
)
117 stw_winsys
->get_adapter_luid(screen
, &stw_dev
->AdapterLuid
);
119 stw_dev
->smapi
->screen
= screen
;
120 stw_dev
->smapi
->get_param
= stw_get_param
;
121 stw_dev
->screen
= screen
;
123 stw_dev
->max_2d_levels
= util_last_bit(screen
->get_param(screen
,
124 PIPE_CAP_MAX_TEXTURE_2D_SIZE
));
125 stw_dev
->max_2d_length
= 1 << (stw_dev
->max_2d_levels
- 1);
127 InitializeCriticalSection(&stw_dev
->ctx_mutex
);
128 InitializeCriticalSection(&stw_dev
->fb_mutex
);
130 stw_dev
->ctx_table
= handle_table_create();
131 if (!stw_dev
->ctx_table
) {
135 stw_pixelformat_init();
137 /* env var override for WGL_EXT_swap_control, useful for testing/debugging */
138 const char *s
= os_get_option("WGL_SWAP_INTERVAL");
140 stw_dev
->swap_interval
= atoi(s
);
142 stw_dev
->refresh_rate
= get_refresh_rate();
144 stw_dev
->initialized
= true;
149 FREE(stw_dev
->smapi
);
151 stw_dev
->stapi
->destroy(stw_dev
->stapi
);
159 stw_init_thread(void)
161 return stw_tls_init_thread();
166 stw_cleanup_thread(void)
168 stw_tls_cleanup_thread();
177 debug_printf("%s\n", __FUNCTION__
);
183 * Abort cleanup if there are still active contexts. In some situations
184 * this DLL may be unloaded before the DLL that is using GL contexts is.
186 stw_lock_contexts(stw_dev
);
187 dhglrc
= handle_table_get_first_handle(stw_dev
->ctx_table
);
188 stw_unlock_contexts(stw_dev
);
190 debug_printf("%s: contexts still active -- cleanup aborted\n", __FUNCTION__
);
195 handle_table_destroy(stw_dev
->ctx_table
);
197 stw_framebuffer_cleanup();
199 DeleteCriticalSection(&stw_dev
->fb_mutex
);
200 DeleteCriticalSection(&stw_dev
->ctx_mutex
);
202 if (stw_dev
->smapi
->destroy
)
203 stw_dev
->smapi
->destroy(stw_dev
->smapi
);
205 FREE(stw_dev
->smapi
);
206 stw_dev
->stapi
->destroy(stw_dev
->stapi
);
208 stw_dev
->screen
->destroy(stw_dev
->screen
);
210 /* glapi is statically linked: we can call the local destroy function. */
211 #ifdef _GLAPI_NO_EXPORTS
212 _glapi_destroy_multithread();
216 debug_memory_end(stw_dev
->memdbg_no
);
226 DrvSetCallbackProcs(INT nProcs
, PROC
*pProcs
)
233 size
= MIN2(nProcs
* sizeof *pProcs
, sizeof stw_dev
->callbacks
);
234 memcpy(&stw_dev
->callbacks
, pProcs
, size
);
241 DrvValidateVersion(ULONG ulVersion
)
243 /* ulVersion is the version reported by the KMD:
244 * - via D3DKMTQueryAdapterInfo(KMTQAITYPE_UMOPENGLINFO) on WDDM,
245 * - or ExtEscape on XPDM and can be used to ensure the KMD and OpenGL ICD
248 * We should get the expected version number from the winsys, but for now