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