1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2009-2010 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 17bf215546Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 18bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 21bf215546Sopenharmony_ci * 22bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 23bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 24bf215546Sopenharmony_ci * of the Software. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci * 27bf215546Sopenharmony_ci **************************************************************************/ 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci/** 30bf215546Sopenharmony_ci * @file 31bf215546Sopenharmony_ci * Softpipe/LLVMpipe support. 32bf215546Sopenharmony_ci * 33bf215546Sopenharmony_ci * @author Jose Fonseca <jfonseca@vmware.com> 34bf215546Sopenharmony_ci */ 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#include <windows.h> 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci#include "util/u_debug.h" 40bf215546Sopenharmony_ci#include "util/debug.h" 41bf215546Sopenharmony_ci#include "stw_winsys.h" 42bf215546Sopenharmony_ci#include "stw_device.h" 43bf215546Sopenharmony_ci#include "gdi/gdi_sw_winsys.h" 44bf215546Sopenharmony_ci#include "pipe/p_screen.h" 45bf215546Sopenharmony_ci#include "pipe/p_context.h" 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci#ifdef GALLIUM_SOFTPIPE 48bf215546Sopenharmony_ci#include "softpipe/sp_texture.h" 49bf215546Sopenharmony_ci#include "softpipe/sp_screen.h" 50bf215546Sopenharmony_ci#include "softpipe/sp_public.h" 51bf215546Sopenharmony_ci#endif 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci#ifdef GALLIUM_LLVMPIPE 54bf215546Sopenharmony_ci#include "llvmpipe/lp_texture.h" 55bf215546Sopenharmony_ci#include "llvmpipe/lp_screen.h" 56bf215546Sopenharmony_ci#include "llvmpipe/lp_public.h" 57bf215546Sopenharmony_ci#endif 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci#ifdef GALLIUM_D3D12 60bf215546Sopenharmony_ci#include "d3d12/wgl/d3d12_wgl_public.h" 61bf215546Sopenharmony_ci#endif 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci#ifdef GALLIUM_ZINK 64bf215546Sopenharmony_ci#include "zink/zink_public.h" 65bf215546Sopenharmony_ci#endif 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci#ifdef GALLIUM_LLVMPIPE 68bf215546Sopenharmony_cistatic boolean use_llvmpipe = FALSE; 69bf215546Sopenharmony_ci#endif 70bf215546Sopenharmony_ci#ifdef GALLIUM_D3D12 71bf215546Sopenharmony_cistatic boolean use_d3d12 = FALSE; 72bf215546Sopenharmony_ci#endif 73bf215546Sopenharmony_ci#ifdef GALLIUM_ZINK 74bf215546Sopenharmony_cistatic boolean use_zink = FALSE; 75bf215546Sopenharmony_ci#endif 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_cistatic const char *created_driver_name = NULL; 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_cistatic struct pipe_screen * 80bf215546Sopenharmony_ciwgl_screen_create_by_name(HDC hDC, const char* driver, struct sw_winsys *winsys) 81bf215546Sopenharmony_ci{ 82bf215546Sopenharmony_ci struct pipe_screen* screen = NULL; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci#ifdef GALLIUM_LLVMPIPE 85bf215546Sopenharmony_ci if (strcmp(driver, "llvmpipe") == 0) { 86bf215546Sopenharmony_ci screen = llvmpipe_create_screen(winsys); 87bf215546Sopenharmony_ci if (screen) 88bf215546Sopenharmony_ci use_llvmpipe = TRUE; 89bf215546Sopenharmony_ci } 90bf215546Sopenharmony_ci#endif 91bf215546Sopenharmony_ci#ifdef GALLIUM_D3D12 92bf215546Sopenharmony_ci if (strcmp(driver, "d3d12") == 0) { 93bf215546Sopenharmony_ci screen = d3d12_wgl_create_screen(winsys, hDC); 94bf215546Sopenharmony_ci if (screen) 95bf215546Sopenharmony_ci use_d3d12 = TRUE; 96bf215546Sopenharmony_ci } 97bf215546Sopenharmony_ci#endif 98bf215546Sopenharmony_ci#ifdef GALLIUM_ZINK 99bf215546Sopenharmony_ci if (strcmp(driver, "zink") == 0) { 100bf215546Sopenharmony_ci screen = zink_create_screen(winsys, NULL); 101bf215546Sopenharmony_ci if (screen) 102bf215546Sopenharmony_ci use_zink = TRUE; 103bf215546Sopenharmony_ci } 104bf215546Sopenharmony_ci#endif 105bf215546Sopenharmony_ci#ifdef GALLIUM_SOFTPIPE 106bf215546Sopenharmony_ci if (strcmp(driver, "softpipe") == 0) { 107bf215546Sopenharmony_ci screen = softpipe_create_screen(winsys); 108bf215546Sopenharmony_ci } 109bf215546Sopenharmony_ci#endif 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci return screen; 112bf215546Sopenharmony_ci} 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_cistatic struct pipe_screen * 115bf215546Sopenharmony_ciwgl_screen_create(HDC hDC) 116bf215546Sopenharmony_ci{ 117bf215546Sopenharmony_ci struct sw_winsys *winsys; 118bf215546Sopenharmony_ci UNUSED bool sw_only = env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false); 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci winsys = gdi_create_sw_winsys(); 121bf215546Sopenharmony_ci if (!winsys) 122bf215546Sopenharmony_ci return NULL; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci const char *const drivers[] = { 125bf215546Sopenharmony_ci debug_get_option("GALLIUM_DRIVER", ""), 126bf215546Sopenharmony_ci#ifdef GALLIUM_D3D12 127bf215546Sopenharmony_ci sw_only ? "" : "d3d12", 128bf215546Sopenharmony_ci#endif 129bf215546Sopenharmony_ci#ifdef GALLIUM_ZINK 130bf215546Sopenharmony_ci sw_only ? "" : "zink", 131bf215546Sopenharmony_ci#endif 132bf215546Sopenharmony_ci#if defined(GALLIUM_LLVMPIPE) 133bf215546Sopenharmony_ci "llvmpipe", 134bf215546Sopenharmony_ci#endif 135bf215546Sopenharmony_ci#if defined(GALLIUM_SOFTPIPE) 136bf215546Sopenharmony_ci "softpipe", 137bf215546Sopenharmony_ci#endif 138bf215546Sopenharmony_ci }; 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_ci /* If the default driver screen creation fails, fall back to the next option in the 141bf215546Sopenharmony_ci * sorted list. Don't do this if GALLIUM_DRIVER is specified. 142bf215546Sopenharmony_ci */ 143bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(drivers); ++i) { 144bf215546Sopenharmony_ci struct pipe_screen* screen = wgl_screen_create_by_name(hDC, drivers[i], winsys); 145bf215546Sopenharmony_ci if (screen) { 146bf215546Sopenharmony_ci created_driver_name = drivers[i]; 147bf215546Sopenharmony_ci return screen; 148bf215546Sopenharmony_ci } 149bf215546Sopenharmony_ci if (i == 0 && drivers[i][0] != '\0') 150bf215546Sopenharmony_ci break; 151bf215546Sopenharmony_ci } 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci winsys->destroy(winsys); 154bf215546Sopenharmony_ci return NULL; 155bf215546Sopenharmony_ci} 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_cistatic void 159bf215546Sopenharmony_ciwgl_present(struct pipe_screen *screen, 160bf215546Sopenharmony_ci struct pipe_context *ctx, 161bf215546Sopenharmony_ci struct pipe_resource *res, 162bf215546Sopenharmony_ci HDC hDC) 163bf215546Sopenharmony_ci{ 164bf215546Sopenharmony_ci /* This will fail if any interposing layer (trace, debug, etc) has 165bf215546Sopenharmony_ci * been introduced between the gallium frontends and the pipe driver. 166bf215546Sopenharmony_ci * 167bf215546Sopenharmony_ci * Ideally this would get replaced with a call to 168bf215546Sopenharmony_ci * pipe_screen::flush_frontbuffer(). 169bf215546Sopenharmony_ci * 170bf215546Sopenharmony_ci * Failing that, it may be necessary for intervening layers to wrap 171bf215546Sopenharmony_ci * other structs such as this stw_winsys as well... 172bf215546Sopenharmony_ci */ 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci struct sw_winsys *winsys = NULL; 175bf215546Sopenharmony_ci struct sw_displaytarget *dt = NULL; 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci#ifdef GALLIUM_LLVMPIPE 178bf215546Sopenharmony_ci if (use_llvmpipe) { 179bf215546Sopenharmony_ci winsys = llvmpipe_screen(screen)->winsys; 180bf215546Sopenharmony_ci dt = llvmpipe_resource(res)->dt; 181bf215546Sopenharmony_ci gdi_sw_display(winsys, dt, hDC); 182bf215546Sopenharmony_ci return; 183bf215546Sopenharmony_ci } 184bf215546Sopenharmony_ci#endif 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci#ifdef GALLIUM_D3D12 187bf215546Sopenharmony_ci if (use_d3d12) { 188bf215546Sopenharmony_ci d3d12_wgl_present(screen, ctx, res, hDC); 189bf215546Sopenharmony_ci return; 190bf215546Sopenharmony_ci } 191bf215546Sopenharmony_ci#endif 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci#ifdef GALLIUM_ZINK 194bf215546Sopenharmony_ci if (use_zink) { 195bf215546Sopenharmony_ci screen->flush_frontbuffer(screen, ctx, res, 0, 0, hDC, NULL); 196bf215546Sopenharmony_ci return; 197bf215546Sopenharmony_ci } 198bf215546Sopenharmony_ci#endif 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci#ifdef GALLIUM_SOFTPIPE 201bf215546Sopenharmony_ci winsys = softpipe_screen(screen)->winsys, 202bf215546Sopenharmony_ci dt = softpipe_resource(res)->dt, 203bf215546Sopenharmony_ci gdi_sw_display(winsys, dt, hDC); 204bf215546Sopenharmony_ci#endif 205bf215546Sopenharmony_ci} 206bf215546Sopenharmony_ci 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci#if WINVER >= 0xA00 209bf215546Sopenharmony_cistatic boolean 210bf215546Sopenharmony_ciwgl_get_adapter_luid(struct pipe_screen* screen, 211bf215546Sopenharmony_ci HDC hDC, 212bf215546Sopenharmony_ci LUID* adapter_luid) 213bf215546Sopenharmony_ci{ 214bf215546Sopenharmony_ci if (!stw_dev || !stw_dev->callbacks.pfnGetAdapterLuid) 215bf215546Sopenharmony_ci return false; 216bf215546Sopenharmony_ci 217bf215546Sopenharmony_ci stw_dev->callbacks.pfnGetAdapterLuid(hDC, adapter_luid); 218bf215546Sopenharmony_ci return true; 219bf215546Sopenharmony_ci} 220bf215546Sopenharmony_ci#endif 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_cistatic unsigned 224bf215546Sopenharmony_ciwgl_get_pfd_flags(struct pipe_screen *screen) 225bf215546Sopenharmony_ci{ 226bf215546Sopenharmony_ci#ifdef GALLIUM_D3D12 227bf215546Sopenharmony_ci if (use_d3d12) 228bf215546Sopenharmony_ci return d3d12_wgl_get_pfd_flags(screen); 229bf215546Sopenharmony_ci#endif 230bf215546Sopenharmony_ci return stw_pfd_gdi_support; 231bf215546Sopenharmony_ci} 232bf215546Sopenharmony_ci 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_cistatic struct stw_winsys_framebuffer * 235bf215546Sopenharmony_ciwgl_create_framebuffer(struct pipe_screen *screen, 236bf215546Sopenharmony_ci HWND hWnd, 237bf215546Sopenharmony_ci int iPixelFormat) 238bf215546Sopenharmony_ci{ 239bf215546Sopenharmony_ci#ifdef GALLIUM_D3D12 240bf215546Sopenharmony_ci if (use_d3d12) 241bf215546Sopenharmony_ci return d3d12_wgl_create_framebuffer(screen, hWnd, iPixelFormat); 242bf215546Sopenharmony_ci#endif 243bf215546Sopenharmony_ci return NULL; 244bf215546Sopenharmony_ci} 245bf215546Sopenharmony_ci 246bf215546Sopenharmony_cistatic const char * 247bf215546Sopenharmony_ciwgl_get_name(void) 248bf215546Sopenharmony_ci{ 249bf215546Sopenharmony_ci return created_driver_name; 250bf215546Sopenharmony_ci} 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci 253bf215546Sopenharmony_cistatic const struct stw_winsys stw_winsys = { 254bf215546Sopenharmony_ci &wgl_screen_create, 255bf215546Sopenharmony_ci &wgl_present, 256bf215546Sopenharmony_ci#if WINVER >= 0xA00 257bf215546Sopenharmony_ci &wgl_get_adapter_luid, 258bf215546Sopenharmony_ci#else 259bf215546Sopenharmony_ci NULL, /* get_adapter_luid */ 260bf215546Sopenharmony_ci#endif 261bf215546Sopenharmony_ci NULL, /* shared_surface_open */ 262bf215546Sopenharmony_ci NULL, /* shared_surface_close */ 263bf215546Sopenharmony_ci NULL, /* compose */ 264bf215546Sopenharmony_ci &wgl_get_pfd_flags, 265bf215546Sopenharmony_ci &wgl_create_framebuffer, 266bf215546Sopenharmony_ci &wgl_get_name, 267bf215546Sopenharmony_ci}; 268bf215546Sopenharmony_ci 269bf215546Sopenharmony_ci 270bf215546Sopenharmony_ciEXTERN_C BOOL WINAPI 271bf215546Sopenharmony_ciDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 272bf215546Sopenharmony_ci 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_ciBOOL WINAPI 275bf215546Sopenharmony_ciDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 276bf215546Sopenharmony_ci{ 277bf215546Sopenharmony_ci switch (fdwReason) { 278bf215546Sopenharmony_ci case DLL_PROCESS_ATTACH: 279bf215546Sopenharmony_ci stw_init(&stw_winsys); 280bf215546Sopenharmony_ci stw_init_thread(); 281bf215546Sopenharmony_ci break; 282bf215546Sopenharmony_ci 283bf215546Sopenharmony_ci case DLL_THREAD_ATTACH: 284bf215546Sopenharmony_ci stw_init_thread(); 285bf215546Sopenharmony_ci break; 286bf215546Sopenharmony_ci 287bf215546Sopenharmony_ci case DLL_THREAD_DETACH: 288bf215546Sopenharmony_ci stw_cleanup_thread(); 289bf215546Sopenharmony_ci break; 290bf215546Sopenharmony_ci 291bf215546Sopenharmony_ci case DLL_PROCESS_DETACH: 292bf215546Sopenharmony_ci if (lpvReserved == NULL) { 293bf215546Sopenharmony_ci // We're being unloaded from the process. 294bf215546Sopenharmony_ci stw_cleanup_thread(); 295bf215546Sopenharmony_ci stw_cleanup(); 296bf215546Sopenharmony_ci } else { 297bf215546Sopenharmony_ci // Process itself is terminating, and all threads and modules are 298bf215546Sopenharmony_ci // being detached. 299bf215546Sopenharmony_ci // 300bf215546Sopenharmony_ci // The order threads (including llvmpipe rasterizer threads) are 301bf215546Sopenharmony_ci // destroyed can not be relied up, so it's not safe to cleanup. 302bf215546Sopenharmony_ci // 303bf215546Sopenharmony_ci // However global destructors (e.g., LLVM's) will still be called, and 304bf215546Sopenharmony_ci // if Microsoft OPENGL32.DLL's DllMain is called after us, it will 305bf215546Sopenharmony_ci // still try to invoke DrvDeleteContext to destroys all outstanding, 306bf215546Sopenharmony_ci // so set stw_dev to NULL to return immediately if that happens. 307bf215546Sopenharmony_ci stw_dev = NULL; 308bf215546Sopenharmony_ci } 309bf215546Sopenharmony_ci break; 310bf215546Sopenharmony_ci } 311bf215546Sopenharmony_ci return TRUE; 312bf215546Sopenharmony_ci} 313