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