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