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