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