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