xref: /OK3568_Linux_fs/external/xserver/hw/xwin/glx/glshim.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * File: glshim.c
3*4882a593Smuzhiyun  * Purpose: GL shim which redirects to a specified DLL
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) Jon TURNEY 2013
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
8*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
9*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
10*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
12*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included in
15*4882a593Smuzhiyun  * all copies or substantial portions of the Software.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20*4882a593Smuzhiyun  * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21*4882a593Smuzhiyun  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22*4882a593Smuzhiyun  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun    A GL shim which redirects to a specified DLL
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun    XWin is statically linked with this, rather than the system libGL, so that
30*4882a593Smuzhiyun    GL calls can be directed to mesa cygGL-1.dll, or cygnativeGLthunk.dll
31*4882a593Smuzhiyun    (which contains cdecl-to-stdcall thunks to the native openGL32.dll)
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #ifdef HAVE_XWIN_CONFIG_H
35*4882a593Smuzhiyun #include <xwin-config.h>
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define GL_GLEXT_LEGACY
39*4882a593Smuzhiyun #define GL_GLEXT_PROTOTYPES
40*4882a593Smuzhiyun #include <GL/gl.h>
41*4882a593Smuzhiyun #undef GL_ARB_imaging
42*4882a593Smuzhiyun #undef GL_VERSION_1_3
43*4882a593Smuzhiyun #include <GL/glext.h>
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #include <X11/Xwindows.h>
46*4882a593Smuzhiyun #include <os.h>
47*4882a593Smuzhiyun #include "glwindows.h"
48*4882a593Smuzhiyun #include <glx/glxserver.h>
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun extern void *glXGetProcAddressARB(const char *);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun static HMODULE hMod = NULL;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun   Implement the __glGetProcAddress function by just using GetProcAddress() on the selected DLL
56*4882a593Smuzhiyun */
glXGetProcAddressARB(const char * symbol)57*4882a593Smuzhiyun void *glXGetProcAddressARB(const char *symbol)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun     void *proc;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun     /* Default to the mesa GL implementation if one hasn't been selected yet */
62*4882a593Smuzhiyun     if (!hMod)
63*4882a593Smuzhiyun         glWinSelectImplementation(0);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun     proc = GetProcAddress(hMod, symbol);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun     if (glxWinDebugSettings.enableGLcallTrace)
68*4882a593Smuzhiyun         ErrorF("glXGetProcAddressARB: Resolved '%s' in %p to %p\n", symbol, hMod, proc);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun     return proc;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun   Select a GL implementation DLL
75*4882a593Smuzhiyun */
glWinSelectImplementation(int native)76*4882a593Smuzhiyun int glWinSelectImplementation(int native)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun     const char *dllname;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun     if (native) {
81*4882a593Smuzhiyun         dllname = "cygnativeGLthunk.dll";
82*4882a593Smuzhiyun     }
83*4882a593Smuzhiyun     else {
84*4882a593Smuzhiyun         dllname = "cygGL-1.dll";
85*4882a593Smuzhiyun     }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun     hMod = LoadLibraryEx(dllname, NULL, 0);
88*4882a593Smuzhiyun     if (hMod == NULL) {
89*4882a593Smuzhiyun         ErrorF("glWinSelectGLimplementation: Could not load '%s'\n", dllname);
90*4882a593Smuzhiyun         return -1;
91*4882a593Smuzhiyun     }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun     ErrorF("glWinSelectGLimplementation: Loaded '%s'\n", dllname);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun     /* Connect __glGetProcAddress() to our implementation of glXGetProcAddressARB() above */
96*4882a593Smuzhiyun     __glXsetGetProcAddress((glx_gpa_proc)glXGetProcAddressARB);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun     return 0;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #define RESOLVE_RET(proctype, symbol, retval) \
102*4882a593Smuzhiyun     proctype proc = (proctype)glXGetProcAddressARB(symbol);   \
103*4882a593Smuzhiyun     if (proc == NULL) return retval;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #define RESOLVE(proctype, symbol) RESOLVE_RET(proctype, symbol,)
106*4882a593Smuzhiyun #define RESOLVED_PROC proc
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /* Include generated shims for direct linkage to GL functions which are in the ABI */
109*4882a593Smuzhiyun #include "generated_gl_shim.ic"
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun   Special wrapper for glAddSwapHintRectWIN for copySubBuffers
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun   Only used with native GL if the GL_WIN_swap_hint extension is present, so we enable
115*4882a593Smuzhiyun   GLX_MESA_copy_sub_buffer
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun typedef void (__stdcall * PFNGLADDSWAPHINTRECTWIN) (GLint x, GLint y,
118*4882a593Smuzhiyun                                                     GLsizei width,
119*4882a593Smuzhiyun                                                     GLsizei height);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun void
glAddSwapHintRectWINWrapper(GLint x,GLint y,GLsizei width,GLsizei height)122*4882a593Smuzhiyun glAddSwapHintRectWINWrapper(GLint x, GLint y, GLsizei width,
123*4882a593Smuzhiyun                             GLsizei height)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun     RESOLVE(PFNGLADDSWAPHINTRECTWIN, "glAddSwapHintRectWIN");
126*4882a593Smuzhiyun     RESOLVED_PROC(x, y, width, height);
127*4882a593Smuzhiyun }
128