glapi.c: misc coscmetic for FreeTSD
[mesa.git] / src / gallium / state_trackers / wgl / stw_device.c
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/glthread.h"
31 #include "util/u_debug.h"
32 #include "util/u_math.h"
33 #include "pipe/p_screen.h"
34 #include "state_tracker/st_public.h"
35
36 #ifdef DEBUG
37 #include "trace/tr_screen.h"
38 #include "trace/tr_texture.h"
39 #endif
40
41 #include "stw_device.h"
42 #include "stw_winsys.h"
43 #include "stw_pixelformat.h"
44 #include "stw_icd.h"
45 #include "stw_tls.h"
46 #include "stw_framebuffer.h"
47
48 #ifdef WIN32_THREADS
49 extern _glthread_Mutex OneTimeLock;
50 #endif
51
52
53 struct stw_device *stw_dev = NULL;
54
55
56 /**
57 * XXX: Dispatch pipe_screen::flush_front_buffer to our
58 * stw_winsys::flush_front_buffer.
59 */
60 static void
61 stw_flush_frontbuffer(struct pipe_screen *screen,
62 struct pipe_surface *surface,
63 void *context_private )
64 {
65 HDC hdc = (HDC)context_private;
66 struct stw_framebuffer *fb;
67
68 fb = stw_framebuffer_from_hdc( hdc );
69 if (!fb) {
70 /* fb can be NULL if window was destroyed already */
71 return;
72 }
73
74 stw_framebuffer_present_locked(hdc, fb, surface);
75 }
76
77
78 boolean
79 stw_init(const struct stw_winsys *stw_winsys)
80 {
81 static struct stw_device stw_dev_storage;
82 struct pipe_screen *screen;
83
84 debug_printf("%s\n", __FUNCTION__);
85
86 assert(!stw_dev);
87
88 stw_tls_init();
89
90 stw_dev = &stw_dev_storage;
91 memset(stw_dev, 0, sizeof(*stw_dev));
92
93 #ifdef DEBUG
94 stw_dev->memdbg_no = debug_memory_begin();
95 #endif
96
97 stw_dev->stw_winsys = stw_winsys;
98
99 #ifdef WIN32_THREADS
100 _glthread_INIT_MUTEX(OneTimeLock);
101 #endif
102
103 screen = stw_winsys->create_screen();
104 if(!screen)
105 goto error1;
106
107 if(stw_winsys->get_adapter_luid)
108 stw_winsys->get_adapter_luid(screen, &stw_dev->AdapterLuid);
109
110 #ifdef DEBUG
111 stw_dev->screen = trace_screen_create(screen);
112 stw_dev->trace_running = stw_dev->screen != screen ? TRUE : FALSE;
113 #else
114 stw_dev->screen = screen;
115 #endif
116
117 stw_dev->screen->flush_frontbuffer = &stw_flush_frontbuffer;
118
119 pipe_mutex_init( stw_dev->ctx_mutex );
120 pipe_mutex_init( stw_dev->fb_mutex );
121
122 stw_dev->ctx_table = handle_table_create();
123 if (!stw_dev->ctx_table) {
124 goto error1;
125 }
126
127 stw_pixelformat_init();
128
129 return TRUE;
130
131 error1:
132 stw_dev = NULL;
133 return FALSE;
134 }
135
136
137 boolean
138 stw_init_thread(void)
139 {
140 return stw_tls_init_thread();
141 }
142
143
144 void
145 stw_cleanup_thread(void)
146 {
147 stw_tls_cleanup_thread();
148 }
149
150
151 void
152 stw_cleanup(void)
153 {
154 DHGLRC dhglrc;
155
156 debug_printf("%s\n", __FUNCTION__);
157
158 if (!stw_dev)
159 return;
160
161 /*
162 * Abort cleanup if there are still active contexts. In some situations
163 * this DLL may be unloaded before the DLL that is using GL contexts is.
164 */
165 pipe_mutex_lock( stw_dev->ctx_mutex );
166 dhglrc = handle_table_get_first_handle(stw_dev->ctx_table);
167 pipe_mutex_unlock( stw_dev->ctx_mutex );
168 if (dhglrc) {
169 debug_printf("%s: contexts still active -- cleanup aborted\n", __FUNCTION__);
170 stw_dev = NULL;
171 return;
172 }
173
174 handle_table_destroy(stw_dev->ctx_table);
175
176 stw_framebuffer_cleanup();
177
178 pipe_mutex_destroy( stw_dev->fb_mutex );
179 pipe_mutex_destroy( stw_dev->ctx_mutex );
180
181 stw_dev->screen->destroy(stw_dev->screen);
182
183 #ifdef WIN32_THREADS
184 _glthread_DESTROY_MUTEX(OneTimeLock);
185
186 _glapi_destroy_multithread();
187 #endif
188
189 #ifdef DEBUG
190 debug_memory_end(stw_dev->memdbg_no);
191 #endif
192
193 stw_tls_cleanup();
194
195 stw_dev = NULL;
196 }
197
198
199 struct stw_context *
200 stw_lookup_context_locked( DHGLRC dhglrc )
201 {
202 if (dhglrc == 0)
203 return NULL;
204
205 if (stw_dev == NULL)
206 return NULL;
207
208 return (struct stw_context *) handle_table_get(stw_dev->ctx_table, dhglrc);
209 }
210
211
212 void APIENTRY
213 DrvSetCallbackProcs(
214 INT nProcs,
215 PROC *pProcs )
216 {
217 size_t size;
218
219 if (stw_dev == NULL)
220 return;
221
222 size = MIN2(nProcs * sizeof *pProcs, sizeof stw_dev->callbacks);
223 memcpy(&stw_dev->callbacks, pProcs, size);
224
225 return;
226 }
227
228
229 BOOL APIENTRY
230 DrvValidateVersion(
231 ULONG ulVersion )
232 {
233 /* TODO: get the expected version from the winsys */
234 return ulVersion == 1;
235 }