1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2011 Morgan Armand <morgan.devel@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <stdio.h>
26 #include <windows.h>
27 
28 #define WGL_WGLEXT_PROTOTYPES
29 
30 #include <GL/gl.h>
31 #include <GL/wglext.h>
32 
33 #include "gldrv.h"
34 #include "stw_context.h"
35 #include "stw_device.h"
36 #include "stw_ext_context.h"
37 
38 #include "util/u_debug.h"
39 
40 
41 static wglCreateContext_t wglCreateContext_func = 0;
42 static wglDeleteContext_t wglDeleteContext_func = 0;
43 
44 /* When this library is used as a opengl32.dll drop-in replacement, ensure we
45  * use the wglCreate/Destroy entrypoints above, and not the true opengl32.dll,
46  * which could happen if this library's name is not opengl32.dll exactly.
47  *
48  * For example, Qt 5.4 bundles this as opengl32sw.dll:
49  * https://blog.qt.io/blog/2014/11/27/qt-weekly-21-dynamic-opengl-implementation-loading-in-qt-5-4/
50  */
51 void
stw_override_opengl32_entry_points(wglCreateContext_t create,wglDeleteContext_t delete)52 stw_override_opengl32_entry_points(wglCreateContext_t create, wglDeleteContext_t delete)
53 {
54    wglCreateContext_func = create;
55    wglDeleteContext_func = delete;
56 }
57 
58 
59 /**
60  * The implementation of this function is tricky.  The OPENGL32.DLL library
61  * remaps the context IDs returned by our stw_create_context_attribs()
62  * function to different values returned to the caller of wglCreateContext().
63  * That is, DHGLRC (driver) handles are not equivalent to HGLRC (public)
64  * handles.
65  *
66  * So we need to generate a new HGLRC ID here.  We do that by calling
67  * the regular wglCreateContext() function.  Then, we replace the newly-
68  * created stw_context with a new stw_context that reflects the arguments
69  * to this function.
70  */
71 HGLRC WINAPI
wglCreateContextAttribsARB(HDC hDC,HGLRC hShareContext,const int * attribList)72 wglCreateContextAttribsARB(HDC hDC, HGLRC hShareContext, const int *attribList)
73 {
74    HGLRC context;
75 
76    int majorVersion = 1, minorVersion = 0, layerPlane = 0;
77    int contextFlags = 0x0;
78    int profileMask = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
79    int i;
80    BOOL done = FALSE;
81    const int contextFlagsAll = (WGL_CONTEXT_DEBUG_BIT_ARB |
82                                 WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
83 
84    /* parse attrib_list */
85    if (attribList) {
86       for (i = 0; !done && attribList[i]; i++) {
87          switch (attribList[i]) {
88          case WGL_CONTEXT_MAJOR_VERSION_ARB:
89             majorVersion = attribList[++i];
90             break;
91          case WGL_CONTEXT_MINOR_VERSION_ARB:
92             minorVersion = attribList[++i];
93             break;
94          case WGL_CONTEXT_LAYER_PLANE_ARB:
95             layerPlane = attribList[++i];
96             break;
97          case WGL_CONTEXT_FLAGS_ARB:
98             contextFlags = attribList[++i];
99             break;
100          case WGL_CONTEXT_PROFILE_MASK_ARB:
101             profileMask = attribList[++i];
102             break;
103          case 0:
104             /* end of list */
105             done = TRUE;
106             break;
107          default:
108             /* bad attribute */
109             SetLastError(ERROR_INVALID_PARAMETER);
110             return 0;
111          }
112       }
113    }
114 
115    /* check contextFlags */
116    if (contextFlags & ~contextFlagsAll) {
117       SetLastError(ERROR_INVALID_PARAMETER);
118       return NULL;
119    }
120 
121    /* check profileMask */
122    if (profileMask != WGL_CONTEXT_CORE_PROFILE_BIT_ARB &&
123        profileMask != WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB &&
124        profileMask != WGL_CONTEXT_ES_PROFILE_BIT_EXT) {
125       SetLastError(ERROR_INVALID_PROFILE_ARB);
126       return NULL;
127    }
128 
129    /* check version (generate ERROR_INVALID_VERSION_ARB if bad) */
130    if (majorVersion <= 0 ||
131        minorVersion < 0 ||
132        (profileMask != WGL_CONTEXT_ES_PROFILE_BIT_EXT &&
133         ((majorVersion == 1 && minorVersion > 5) ||
134          (majorVersion == 2 && minorVersion > 1) ||
135          (majorVersion == 3 && minorVersion > 3) ||
136          (majorVersion == 4 && minorVersion > 5) ||
137          majorVersion > 4)) ||
138        (profileMask == WGL_CONTEXT_ES_PROFILE_BIT_EXT &&
139         ((majorVersion == 1 && minorVersion > 1) ||
140          (majorVersion == 2 && minorVersion > 0) ||
141          (majorVersion == 3 && minorVersion > 1) ||
142          majorVersion > 3))) {
143       SetLastError(ERROR_INVALID_VERSION_ARB);
144       return NULL;
145    }
146 
147    if ((contextFlags & WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB) &&
148        majorVersion < 3) {
149       SetLastError(ERROR_INVALID_VERSION_ARB);
150       return 0;
151    }
152 
153    /* Get pointer to OPENGL32.DLL's wglCreate/DeleteContext() functions */
154    if (!wglCreateContext_func || !wglDeleteContext_func) {
155       /* Get the OPENGL32.DLL library */
156       HMODULE opengl_lib = GetModuleHandleA("opengl32.dll");
157       if (!opengl_lib) {
158          _debug_printf("wgl: GetModuleHandleA(\"opengl32.dll\") failed\n");
159          return 0;
160       }
161 
162       /* Get pointer to wglCreateContext() function */
163       wglCreateContext_func = (wglCreateContext_t)
164          GetProcAddress(opengl_lib, "wglCreateContext");
165       if (!wglCreateContext_func) {
166          _debug_printf("wgl: failed to get wglCreateContext()\n");
167          return 0;
168       }
169 
170       /* Get pointer to wglDeleteContext() function */
171       wglDeleteContext_func = (wglDeleteContext_t)
172          GetProcAddress(opengl_lib, "wglDeleteContext");
173       if (!wglDeleteContext_func) {
174          _debug_printf("wgl: failed to get wglDeleteContext()\n");
175          return 0;
176       }
177    }
178 
179    /* Call wglCreateContext to get a valid context ID */
180    context = wglCreateContext_func(hDC);
181 
182    if (context) {
183       /* Now replace the context we just created with a new one that reflects
184        * the attributes passed to this function.
185        */
186       DHGLRC dhglrc, c, share_dhglrc = 0;
187 
188       /* Convert public HGLRC to driver DHGLRC */
189       if (stw_dev && stw_dev->callbacks.pfnGetDhglrc) {
190          dhglrc = stw_dev->callbacks.pfnGetDhglrc(context);
191          if (hShareContext)
192             share_dhglrc = stw_dev->callbacks.pfnGetDhglrc(hShareContext);
193       }
194       else {
195          /* not using ICD */
196          dhglrc = (DHGLRC)(INT_PTR)context;
197          share_dhglrc = (DHGLRC)(INT_PTR)hShareContext;
198       }
199 
200       struct stw_context *share_stw = stw_lookup_context(share_dhglrc);
201 
202       struct stw_context *stw_ctx = stw_create_context_attribs(hDC, layerPlane, share_stw,
203                                                                majorVersion, minorVersion,
204                                                                contextFlags, profileMask, 0);
205 
206       if (!stw_ctx) {
207          wglDeleteContext_func(context);
208          return 0;
209       }
210 
211       c = stw_create_context_handle(stw_ctx, dhglrc);
212       if (!c) {
213          stw_destroy_context(stw_ctx);
214          wglDeleteContext_func(context);
215          context = 0;
216       }
217    }
218 
219    return context;
220 }
221 
222 
223 /** Defined by WGL_ARB_make_current_read */
224 BOOL APIENTRY
wglMakeContextCurrentARB(HDC hDrawDC,HDC hReadDC,HGLRC hglrc)225 wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc)
226 {
227    DHGLRC dhglrc = 0;
228 
229    if (stw_dev && stw_dev->callbacks.pfnGetDhglrc) {
230       /* Convert HGLRC to DHGLRC */
231       dhglrc = stw_dev->callbacks.pfnGetDhglrc(hglrc);
232    }
233 
234    return stw_make_current_by_handles(hDrawDC, hReadDC, dhglrc);
235 }
236 
237 HDC APIENTRY
wglGetCurrentReadDCARB(VOID)238 wglGetCurrentReadDCARB(VOID)
239 {
240    return stw_get_current_read_dc();
241 }
242