b57f96f2e872a809f4b05b56004d96b8ec9c1fe3
[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 "stw_icd.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 boolean
67 stw_init(const struct stw_winsys *stw_winsys)
68 {
69 static struct stw_device stw_dev_storage;
70 struct pipe_screen *screen;
71
72 debug_disable_error_message_boxes();
73
74 debug_printf("%s\n", __FUNCTION__);
75
76 assert(!stw_dev);
77
78 stw_tls_init();
79
80 stw_dev = &stw_dev_storage;
81 memset(stw_dev, 0, sizeof(*stw_dev));
82
83 #ifdef DEBUG
84 stw_dev->memdbg_no = debug_memory_begin();
85 #endif
86
87 stw_dev->stw_winsys = stw_winsys;
88
89 stw_dev->stapi = stw_st_create_api();
90 stw_dev->smapi = CALLOC_STRUCT(st_manager);
91 if (!stw_dev->stapi || !stw_dev->smapi)
92 goto error1;
93
94 screen = stw_winsys->create_screen();
95 if(!screen)
96 goto error1;
97
98 if(stw_winsys->get_adapter_luid)
99 stw_winsys->get_adapter_luid(screen, &stw_dev->AdapterLuid);
100
101 stw_dev->smapi->screen = screen;
102 stw_dev->smapi->get_param = stw_get_param;
103 stw_dev->screen = screen;
104
105 stw_dev->max_2d_levels =
106 screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
107 stw_dev->max_2d_length = 1 << (stw_dev->max_2d_levels - 1);
108
109 InitializeCriticalSection(&stw_dev->ctx_mutex);
110 InitializeCriticalSection(&stw_dev->fb_mutex);
111
112 stw_dev->ctx_table = handle_table_create();
113 if (!stw_dev->ctx_table) {
114 goto error1;
115 }
116
117 stw_pixelformat_init();
118
119 stw_dev->initialized = true;
120
121 return TRUE;
122
123 error1:
124 FREE(stw_dev->smapi);
125 if (stw_dev->stapi)
126 stw_dev->stapi->destroy(stw_dev->stapi);
127
128 stw_dev = NULL;
129 return FALSE;
130 }
131
132
133 boolean
134 stw_init_thread(void)
135 {
136 return stw_tls_init_thread();
137 }
138
139
140 void
141 stw_cleanup_thread(void)
142 {
143 stw_tls_cleanup_thread();
144 }
145
146
147 void
148 stw_cleanup(void)
149 {
150 DHGLRC dhglrc;
151
152 debug_printf("%s\n", __FUNCTION__);
153
154 if (!stw_dev)
155 return;
156
157 /*
158 * Abort cleanup if there are still active contexts. In some situations
159 * this DLL may be unloaded before the DLL that is using GL contexts is.
160 */
161 stw_lock_contexts(stw_dev);
162 dhglrc = handle_table_get_first_handle(stw_dev->ctx_table);
163 stw_unlock_contexts(stw_dev);
164 if (dhglrc) {
165 debug_printf("%s: contexts still active -- cleanup aborted\n", __FUNCTION__);
166 stw_dev = NULL;
167 return;
168 }
169
170 handle_table_destroy(stw_dev->ctx_table);
171
172 stw_framebuffer_cleanup();
173
174 DeleteCriticalSection(&stw_dev->fb_mutex);
175 DeleteCriticalSection(&stw_dev->ctx_mutex);
176
177 FREE(stw_dev->smapi);
178 stw_dev->stapi->destroy(stw_dev->stapi);
179
180 stw_dev->screen->destroy(stw_dev->screen);
181
182 /* glapi is statically linked: we can call the local destroy function. */
183 #ifdef _GLAPI_NO_EXPORTS
184 _glapi_destroy_multithread();
185 #endif
186
187 #ifdef DEBUG
188 debug_memory_end(stw_dev->memdbg_no);
189 #endif
190
191 stw_tls_cleanup();
192
193 stw_dev = NULL;
194 }
195
196
197 void APIENTRY
198 DrvSetCallbackProcs(
199 INT nProcs,
200 PROC *pProcs )
201 {
202 size_t size;
203
204 if (stw_dev == NULL)
205 return;
206
207 size = MIN2(nProcs * sizeof *pProcs, sizeof stw_dev->callbacks);
208 memcpy(&stw_dev->callbacks, pProcs, size);
209
210 return;
211 }
212
213
214 BOOL APIENTRY
215 DrvValidateVersion(
216 ULONG ulVersion )
217 {
218 /* ulVersion is the version reported by the KMD:
219 * - via D3DKMTQueryAdapterInfo(KMTQAITYPE_UMOPENGLINFO) on WDDM,
220 * - or ExtEscape on XPDM and can be used to ensure the KMD and OpenGL ICD
221 * versions match.
222 *
223 * We should get the expected version number from the winsys, but for now
224 * ignore it.
225 */
226 (void)ulVersion;
227 return TRUE;
228 }