xref: /aosp_15_r20/external/libepoxy/src/dispatch_wgl.c (revision 706d0b42ae4182339789e08d473a0b312ecdc60f)
1*706d0b42SXin Li /*
2*706d0b42SXin Li  * Copyright © 2013 Intel Corporation
3*706d0b42SXin Li  *
4*706d0b42SXin Li  * Permission is hereby granted, free of charge, to any person obtaining a
5*706d0b42SXin Li  * copy of this software and associated documentation files (the "Software"),
6*706d0b42SXin Li  * to deal in the Software without restriction, including without limitation
7*706d0b42SXin Li  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*706d0b42SXin Li  * and/or sell copies of the Software, and to permit persons to whom the
9*706d0b42SXin Li  * Software is furnished to do so, subject to the following conditions:
10*706d0b42SXin Li  *
11*706d0b42SXin Li  * The above copyright notice and this permission notice (including the next
12*706d0b42SXin Li  * paragraph) shall be included in all copies or substantial portions of the
13*706d0b42SXin Li  * Software.
14*706d0b42SXin Li  *
15*706d0b42SXin Li  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*706d0b42SXin Li  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*706d0b42SXin Li  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*706d0b42SXin Li  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*706d0b42SXin Li  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*706d0b42SXin Li  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*706d0b42SXin Li  * IN THE SOFTWARE.
22*706d0b42SXin Li  */
23*706d0b42SXin Li 
24*706d0b42SXin Li #include <assert.h>
25*706d0b42SXin Li #include <string.h>
26*706d0b42SXin Li #include <stdio.h>
27*706d0b42SXin Li 
28*706d0b42SXin Li #include "dispatch_common.h"
29*706d0b42SXin Li 
30*706d0b42SXin Li static bool first_context_current = false;
31*706d0b42SXin Li static bool already_switched_to_dispatch_table = false;
32*706d0b42SXin Li 
33*706d0b42SXin Li /**
34*706d0b42SXin Li  * If we can determine the WGL extension support from the current
35*706d0b42SXin Li  * context, then return that, otherwise give the answer that will just
36*706d0b42SXin Li  * send us on to get_proc_address().
37*706d0b42SXin Li  */
38*706d0b42SXin Li bool
epoxy_conservative_has_wgl_extension(const char * ext)39*706d0b42SXin Li epoxy_conservative_has_wgl_extension(const char *ext)
40*706d0b42SXin Li {
41*706d0b42SXin Li     HDC hdc = wglGetCurrentDC();
42*706d0b42SXin Li 
43*706d0b42SXin Li     if (!hdc)
44*706d0b42SXin Li         return true;
45*706d0b42SXin Li 
46*706d0b42SXin Li     return epoxy_has_wgl_extension(hdc, ext);
47*706d0b42SXin Li }
48*706d0b42SXin Li 
49*706d0b42SXin Li bool
epoxy_has_wgl_extension(HDC hdc,const char * ext)50*706d0b42SXin Li epoxy_has_wgl_extension(HDC hdc, const char *ext)
51*706d0b42SXin Li  {
52*706d0b42SXin Li      PFNWGLGETEXTENSIONSSTRINGARBPROC getext;
53*706d0b42SXin Li 
54*706d0b42SXin Li      getext = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
55*706d0b42SXin Li      if (!getext) {
56*706d0b42SXin Li          fputs("Implementation unexpectedly missing "
57*706d0b42SXin Li                "WGL_ARB_extensions_string.  Probably a libepoxy bug.\n",
58*706d0b42SXin Li                stderr);
59*706d0b42SXin Li          return false;
60*706d0b42SXin Li      }
61*706d0b42SXin Li 
62*706d0b42SXin Li     return epoxy_extension_in_string(getext(hdc), ext);
63*706d0b42SXin Li }
64*706d0b42SXin Li 
65*706d0b42SXin Li /**
66*706d0b42SXin Li  * Does the work necessary to update the win32 per-thread dispatch
67*706d0b42SXin Li  * tables when wglMakeCurrent() is called.
68*706d0b42SXin Li  *
69*706d0b42SXin Li  * Right now, we use global function pointers until the second
70*706d0b42SXin Li  * MakeCurrent occurs, at which point we switch to dispatch tables.
71*706d0b42SXin Li  * This could be improved in the future to track a resolved dispatch
72*706d0b42SXin Li  * table per context and reuse it when the context is made current
73*706d0b42SXin Li  * again.
74*706d0b42SXin Li  */
75*706d0b42SXin Li void
epoxy_handle_external_wglMakeCurrent(void)76*706d0b42SXin Li epoxy_handle_external_wglMakeCurrent(void)
77*706d0b42SXin Li {
78*706d0b42SXin Li     if (!first_context_current) {
79*706d0b42SXin Li         first_context_current = true;
80*706d0b42SXin Li     } else {
81*706d0b42SXin Li         if (!already_switched_to_dispatch_table) {
82*706d0b42SXin Li             already_switched_to_dispatch_table = true;
83*706d0b42SXin Li             gl_switch_to_dispatch_table();
84*706d0b42SXin Li             wgl_switch_to_dispatch_table();
85*706d0b42SXin Li         }
86*706d0b42SXin Li 
87*706d0b42SXin Li         gl_init_dispatch_table();
88*706d0b42SXin Li         wgl_init_dispatch_table();
89*706d0b42SXin Li     }
90*706d0b42SXin Li }
91*706d0b42SXin Li 
92*706d0b42SXin Li /**
93*706d0b42SXin Li  * This global symbol is apparently looked up by Windows when loading
94*706d0b42SXin Li  * a DLL, but it doesn't declare the prototype.
95*706d0b42SXin Li  */
96*706d0b42SXin Li BOOL WINAPI
97*706d0b42SXin Li DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved);
98*706d0b42SXin Li 
99*706d0b42SXin Li BOOL WINAPI
DllMain(HINSTANCE dll,DWORD reason,LPVOID reserved)100*706d0b42SXin Li DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
101*706d0b42SXin Li {
102*706d0b42SXin Li     void *data;
103*706d0b42SXin Li 
104*706d0b42SXin Li     switch (reason) {
105*706d0b42SXin Li     case DLL_PROCESS_ATTACH:
106*706d0b42SXin Li         gl_tls_index = TlsAlloc();
107*706d0b42SXin Li         if (gl_tls_index == TLS_OUT_OF_INDEXES)
108*706d0b42SXin Li             return FALSE;
109*706d0b42SXin Li         wgl_tls_index = TlsAlloc();
110*706d0b42SXin Li         if (wgl_tls_index == TLS_OUT_OF_INDEXES)
111*706d0b42SXin Li             return FALSE;
112*706d0b42SXin Li 
113*706d0b42SXin Li         first_context_current = false;
114*706d0b42SXin Li 
115*706d0b42SXin Li         /* FALLTHROUGH */
116*706d0b42SXin Li 
117*706d0b42SXin Li     case DLL_THREAD_ATTACH:
118*706d0b42SXin Li         data = LocalAlloc(LPTR, gl_tls_size);
119*706d0b42SXin Li         TlsSetValue(gl_tls_index, data);
120*706d0b42SXin Li 
121*706d0b42SXin Li         data = LocalAlloc(LPTR, wgl_tls_size);
122*706d0b42SXin Li         TlsSetValue(wgl_tls_index, data);
123*706d0b42SXin Li 
124*706d0b42SXin Li         break;
125*706d0b42SXin Li 
126*706d0b42SXin Li     case DLL_THREAD_DETACH:
127*706d0b42SXin Li     case DLL_PROCESS_DETACH:
128*706d0b42SXin Li         data = TlsGetValue(gl_tls_index);
129*706d0b42SXin Li         LocalFree(data);
130*706d0b42SXin Li 
131*706d0b42SXin Li         data = TlsGetValue(wgl_tls_index);
132*706d0b42SXin Li         LocalFree(data);
133*706d0b42SXin Li 
134*706d0b42SXin Li         if (reason == DLL_PROCESS_DETACH) {
135*706d0b42SXin Li             TlsFree(gl_tls_index);
136*706d0b42SXin Li             TlsFree(wgl_tls_index);
137*706d0b42SXin Li         }
138*706d0b42SXin Li         break;
139*706d0b42SXin Li     }
140*706d0b42SXin Li 
141*706d0b42SXin Li     return TRUE;
142*706d0b42SXin Li }
143*706d0b42SXin Li 
144*706d0b42SXin Li WRAPPER_VISIBILITY (BOOL)
WRAPPER(epoxy_wglMakeCurrent)145*706d0b42SXin Li WRAPPER(epoxy_wglMakeCurrent)(HDC hdc, HGLRC hglrc)
146*706d0b42SXin Li {
147*706d0b42SXin Li     BOOL ret = epoxy_wglMakeCurrent_unwrapped(hdc, hglrc);
148*706d0b42SXin Li 
149*706d0b42SXin Li     epoxy_handle_external_wglMakeCurrent();
150*706d0b42SXin Li 
151*706d0b42SXin Li     return ret;
152*706d0b42SXin Li }
153*706d0b42SXin Li 
154*706d0b42SXin Li 
155*706d0b42SXin Li WRAPPER_VISIBILITY (BOOL)
WRAPPER(epoxy_wglMakeContextCurrentARB)156*706d0b42SXin Li WRAPPER(epoxy_wglMakeContextCurrentARB)(HDC hDrawDC,
157*706d0b42SXin Li                                         HDC hReadDC,
158*706d0b42SXin Li                                         HGLRC hglrc)
159*706d0b42SXin Li {
160*706d0b42SXin Li     BOOL ret = epoxy_wglMakeContextCurrentARB_unwrapped(hDrawDC, hReadDC,
161*706d0b42SXin Li                                                         hglrc);
162*706d0b42SXin Li 
163*706d0b42SXin Li     epoxy_handle_external_wglMakeCurrent();
164*706d0b42SXin Li 
165*706d0b42SXin Li     return ret;
166*706d0b42SXin Li }
167*706d0b42SXin Li 
168*706d0b42SXin Li 
169*706d0b42SXin Li WRAPPER_VISIBILITY (BOOL)
WRAPPER(epoxy_wglMakeContextCurrentEXT)170*706d0b42SXin Li WRAPPER(epoxy_wglMakeContextCurrentEXT)(HDC hDrawDC,
171*706d0b42SXin Li                                         HDC hReadDC,
172*706d0b42SXin Li                                         HGLRC hglrc)
173*706d0b42SXin Li {
174*706d0b42SXin Li     BOOL ret = epoxy_wglMakeContextCurrentEXT_unwrapped(hDrawDC, hReadDC,
175*706d0b42SXin Li                                                         hglrc);
176*706d0b42SXin Li 
177*706d0b42SXin Li     epoxy_handle_external_wglMakeCurrent();
178*706d0b42SXin Li 
179*706d0b42SXin Li     return ret;
180*706d0b42SXin Li }
181*706d0b42SXin Li 
182*706d0b42SXin Li 
183*706d0b42SXin Li WRAPPER_VISIBILITY (BOOL)
WRAPPER(epoxy_wglMakeAssociatedContextCurrentAMD)184*706d0b42SXin Li WRAPPER(epoxy_wglMakeAssociatedContextCurrentAMD)(HGLRC hglrc)
185*706d0b42SXin Li {
186*706d0b42SXin Li     BOOL ret = epoxy_wglMakeAssociatedContextCurrentAMD_unwrapped(hglrc);
187*706d0b42SXin Li 
188*706d0b42SXin Li     epoxy_handle_external_wglMakeCurrent();
189*706d0b42SXin Li 
190*706d0b42SXin Li     return ret;
191*706d0b42SXin Li }
192*706d0b42SXin Li 
193*706d0b42SXin Li PFNWGLMAKECURRENTPROC epoxy_wglMakeCurrent = epoxy_wglMakeCurrent_wrapped;
194*706d0b42SXin Li PFNWGLMAKECONTEXTCURRENTEXTPROC epoxy_wglMakeContextCurrentEXT = epoxy_wglMakeContextCurrentEXT_wrapped;
195*706d0b42SXin Li PFNWGLMAKECONTEXTCURRENTARBPROC epoxy_wglMakeContextCurrentARB = epoxy_wglMakeContextCurrentARB_wrapped;
196*706d0b42SXin Li PFNWGLMAKEASSOCIATEDCONTEXTCURRENTAMDPROC epoxy_wglMakeAssociatedContextCurrentEXT = epoxy_wglMakeAssociatedContextCurrentAMD_wrapped;
197