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