Path: blob/21.2-virgl/src/gallium/frontends/wgl/stw_device.c
4561 views
/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <windows.h>2829#include "glapi/glapi.h"30#include "util/u_debug.h"31#include "util/u_math.h"32#include "util/u_memory.h"33#include "pipe/p_screen.h"3435#include "stw_device.h"36#include "stw_winsys.h"37#include "stw_pixelformat.h"38#include "gldrv.h"39#include "stw_tls.h"40#include "stw_framebuffer.h"41#include "stw_st.h"424344struct stw_device *stw_dev = NULL;4546static int47stw_get_param(struct st_manager *smapi,48enum st_manager_param param)49{50switch (param) {51case ST_MANAGER_BROKEN_INVALIDATE:52/*53* Force framebuffer validation on glViewport.54*55* Certain applications, like Rhinoceros 4, uses glReadPixels56* exclusively (never uses SwapBuffers), so framebuffers never get57* resized unless we check on glViewport.58*/59return 1;60default:61return 0;62}63}646566/** Get the refresh rate for the monitor, in Hz */67static int68get_refresh_rate(void)69{70DEVMODE devModes;7172if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devModes)) {73/* clamp the value, just in case we get garbage */74return CLAMP(devModes.dmDisplayFrequency, 30, 120);75}76else {77/* reasonable default */78return 60;79}80}8182static bool83init_screen(const struct stw_winsys *stw_winsys, HDC hdc)84{85struct pipe_screen *screen = stw_winsys->create_screen(hdc);86if (!screen)87return false;8889if (stw_winsys->get_adapter_luid)90stw_winsys->get_adapter_luid(screen, hdc, &stw_dev->AdapterLuid);9192stw_dev->smapi->screen = screen;93stw_dev->screen = screen;9495stw_dev->max_2d_length = screen->get_param(screen,96PIPE_CAP_MAX_TEXTURE_2D_SIZE);97return true;98}99100boolean101stw_init(const struct stw_winsys *stw_winsys)102{103static struct stw_device stw_dev_storage;104105debug_disable_error_message_boxes();106107assert(!stw_dev);108109stw_tls_init();110111stw_dev = &stw_dev_storage;112memset(stw_dev, 0, sizeof(*stw_dev));113114stw_dev->stw_winsys = stw_winsys;115116stw_dev->stapi = stw_st_create_api();117stw_dev->smapi = CALLOC_STRUCT(st_manager);118if (!stw_dev->stapi || !stw_dev->smapi)119goto error1;120121stw_dev->smapi->get_param = stw_get_param;122123InitializeCriticalSection(&stw_dev->screen_mutex);124InitializeCriticalSection(&stw_dev->ctx_mutex);125InitializeCriticalSection(&stw_dev->fb_mutex);126127stw_dev->ctx_table = handle_table_create();128if (!stw_dev->ctx_table) {129goto error1;130}131132/* env var override for WGL_EXT_swap_control, useful for testing/debugging */133const char *s = os_get_option("WGL_SWAP_INTERVAL");134if (s) {135stw_dev->swap_interval = atoi(s);136}137stw_dev->refresh_rate = get_refresh_rate();138139stw_dev->initialized = true;140141return TRUE;142143error1:144FREE(stw_dev->smapi);145if (stw_dev->stapi)146stw_dev->stapi->destroy(stw_dev->stapi);147148stw_dev = NULL;149return FALSE;150}151152boolean153stw_init_screen(HDC hdc)154{155EnterCriticalSection(&stw_dev->screen_mutex);156157if (!stw_dev->screen_initialized) {158stw_dev->screen_initialized = true;159if (!init_screen(stw_dev->stw_winsys, hdc)) {160LeaveCriticalSection(&stw_dev->screen_mutex);161return false;162}163stw_pixelformat_init();164}165166LeaveCriticalSection(&stw_dev->screen_mutex);167return stw_dev->screen != NULL;168}169170boolean171stw_init_thread(void)172{173return stw_tls_init_thread();174}175176177void178stw_cleanup_thread(void)179{180stw_tls_cleanup_thread();181}182183184void185stw_cleanup(void)186{187DHGLRC dhglrc;188189debug_printf("%s\n", __FUNCTION__);190191if (!stw_dev)192return;193194/*195* Abort cleanup if there are still active contexts. In some situations196* this DLL may be unloaded before the DLL that is using GL contexts is.197*/198stw_lock_contexts(stw_dev);199dhglrc = handle_table_get_first_handle(stw_dev->ctx_table);200stw_unlock_contexts(stw_dev);201if (dhglrc) {202debug_printf("%s: contexts still active -- cleanup aborted\n", __FUNCTION__);203stw_dev = NULL;204return;205}206207handle_table_destroy(stw_dev->ctx_table);208209stw_framebuffer_cleanup();210211DeleteCriticalSection(&stw_dev->fb_mutex);212DeleteCriticalSection(&stw_dev->ctx_mutex);213DeleteCriticalSection(&stw_dev->screen_mutex);214215if (stw_dev->smapi->destroy)216stw_dev->smapi->destroy(stw_dev->smapi);217218FREE(stw_dev->smapi);219stw_dev->stapi->destroy(stw_dev->stapi);220221stw_dev->screen->destroy(stw_dev->screen);222223/* glapi is statically linked: we can call the local destroy function. */224#ifdef _GLAPI_NO_EXPORTS225_glapi_destroy_multithread();226#endif227228stw_tls_cleanup();229230util_dynarray_fini(&stw_dev->pixelformats);231232stw_dev = NULL;233}234235236void APIENTRY237DrvSetCallbackProcs(INT nProcs, PROC *pProcs)238{239size_t size;240241if (stw_dev == NULL)242return;243244size = MIN2(nProcs * sizeof *pProcs, sizeof stw_dev->callbacks);245memcpy(&stw_dev->callbacks, pProcs, size);246247return;248}249250251BOOL APIENTRY252DrvValidateVersion(ULONG ulVersion)253{254/* ulVersion is the version reported by the KMD:255* - via D3DKMTQueryAdapterInfo(KMTQAITYPE_UMOPENGLINFO) on WDDM,256* - or ExtEscape on XPDM and can be used to ensure the KMD and OpenGL ICD257* versions match.258*259* We should get the expected version number from the winsys, but for now260* ignore it.261*/262(void)ulVersion;263return TRUE;264}265266267