xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/wgl/stw_ext_context.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2011 Morgan Armand <[email protected]>
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 "stw_gdishim.h"
34 #include "gldrv.h"
35 #include "stw_context.h"
36 #include "stw_device.h"
37 #include "stw_ext_context.h"
38 
39 #include "util/u_debug.h"
40 
41 
42 static wglCreateContext_t g_pfnwglCreateContext = NULL;
43 static wglDeleteContext_t g_pfnwglDeleteContext = NULL;
44 
45 /* When this library is used as a opengl32.dll drop-in replacement, ensure we
46  * use the wglCreate/Destroy entrypoints above, and not the true opengl32.dll,
47  * which could happen if this library's name is not opengl32.dll exactly.
48  *
49  * For example, Qt 5.4 bundles this as opengl32sw.dll:
50  * https://blog.qt.io/blog/2014/11/27/qt-weekly-21-dynamic-opengl-implementation-loading-in-qt-5-4/
51  */
52 void
stw_override_opengl32_entry_points(wglCreateContext_t create,wglDeleteContext_t delete)53 stw_override_opengl32_entry_points(wglCreateContext_t create, wglDeleteContext_t delete)
54 {
55    g_pfnwglCreateContext = create;
56    g_pfnwglDeleteContext = delete;
57 }
58 
59 
60 /**
61  * The implementation of this function is tricky.  The OPENGL32.DLL library
62  * remaps the context IDs returned by our stw_create_context_attribs()
63  * function to different values returned to the caller of wglCreateContext().
64  * That is, DHGLRC (driver) handles are not equivalent to HGLRC (public)
65  * handles.
66  *
67  * So we need to generate a new HGLRC ID here.  We do that by calling
68  * the regular wglCreateContext() function.  Then, we replace the newly-
69  * created stw_context with a new stw_context that reflects the arguments
70  * to this function.
71  */
72 HGLRC WINAPI
wglCreateContextAttribsARB(HDC hDC,HGLRC hShareContext,const int * attribList)73 wglCreateContextAttribsARB(HDC hDC, HGLRC hShareContext, const int *attribList)
74 {
75    HGLRC context;
76 
77    int majorVersion = 1, minorVersion = 0, layerPlane = 0;
78    int contextFlags = 0x0;
79    int profileMask = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
80    int resetStrategy = WGL_NO_RESET_NOTIFICATION_ARB;
81    int i;
82    BOOL done = false;
83    const int contextFlagsAll = (WGL_CONTEXT_DEBUG_BIT_ARB |
84                                 WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB |
85                                 WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB);
86 
87    if (!stw_dev)
88       return NULL;
89 
90    /* parse attrib_list */
91    if (attribList) {
92       for (i = 0; !done && attribList[i]; i++) {
93          switch (attribList[i]) {
94          case WGL_CONTEXT_MAJOR_VERSION_ARB:
95             majorVersion = attribList[++i];
96             break;
97          case WGL_CONTEXT_MINOR_VERSION_ARB:
98             minorVersion = attribList[++i];
99             break;
100          case WGL_CONTEXT_LAYER_PLANE_ARB:
101             layerPlane = attribList[++i];
102             break;
103          case WGL_CONTEXT_FLAGS_ARB:
104             contextFlags = attribList[++i];
105             break;
106          case WGL_CONTEXT_PROFILE_MASK_ARB:
107             profileMask = attribList[++i];
108             break;
109          case WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB:
110             resetStrategy = attribList[++i];
111             break;
112          case 0:
113             /* end of list */
114             done = true;
115             break;
116          default:
117             /* bad attribute */
118             SetLastError(ERROR_INVALID_PARAMETER);
119             return 0;
120          }
121       }
122    }
123 
124    /* check contextFlags */
125    if (contextFlags & ~contextFlagsAll) {
126       SetLastError(ERROR_INVALID_PARAMETER);
127       return NULL;
128    }
129 
130    /* check profileMask */
131    if (profileMask != WGL_CONTEXT_CORE_PROFILE_BIT_ARB &&
132        profileMask != WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB &&
133        profileMask != WGL_CONTEXT_ES_PROFILE_BIT_EXT) {
134       SetLastError(ERROR_INVALID_PROFILE_ARB);
135       return NULL;
136    }
137 
138    /* check version (generate ERROR_INVALID_VERSION_ARB if bad) */
139    if (majorVersion <= 0 ||
140        minorVersion < 0 ||
141        (profileMask != WGL_CONTEXT_ES_PROFILE_BIT_EXT &&
142         ((majorVersion == 1 && minorVersion > 5) ||
143          (majorVersion == 2 && minorVersion > 1) ||
144          (majorVersion == 3 && minorVersion > 3) ||
145          (majorVersion == 4 && minorVersion > 6) ||
146          majorVersion > 4)) ||
147        (profileMask == WGL_CONTEXT_ES_PROFILE_BIT_EXT &&
148         ((majorVersion == 1 && minorVersion > 1) ||
149          (majorVersion == 2 && minorVersion > 0) ||
150          (majorVersion == 3 && minorVersion > 1) ||
151          majorVersion > 3))) {
152       SetLastError(ERROR_INVALID_VERSION_ARB);
153       return NULL;
154    }
155 
156    if ((contextFlags & WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB) &&
157        majorVersion < 3) {
158       SetLastError(ERROR_INVALID_VERSION_ARB);
159       return 0;
160    }
161 
162    if (resetStrategy != WGL_NO_RESET_NOTIFICATION_ARB &&
163        resetStrategy != WGL_LOSE_CONTEXT_ON_RESET_ARB) {
164       SetLastError(ERROR_INVALID_PARAMETER);
165       return NULL;
166    }
167 
168    wglCreateContext_t pfnwglCreateContext;
169    wglDeleteContext_t pfnwglDeleteContext;
170 
171    if (stw_dev->callbacks.pfnGetDhglrc) {
172       /* Used as an ICD.
173        *
174        * Get pointers to OPENGL32.DLL's wglCreate/DeleteContext() functions
175        */
176       HMODULE opengl_lib = GetModuleHandleA("opengl32.dll");
177       if (!opengl_lib) {
178          _debug_printf("wgl: GetModuleHandleA(\"opengl32.dll\") failed\n");
179          return NULL;
180       }
181 
182       pfnwglCreateContext = (wglCreateContext_t)
183          GetProcAddress(opengl_lib, "wglCreateContext");
184       if (!pfnwglCreateContext) {
185          _debug_printf("wgl: failed to get wglCreateContext()\n");
186          return NULL;
187       }
188 
189       pfnwglDeleteContext = (wglDeleteContext_t)
190          GetProcAddress(opengl_lib, "wglDeleteContext");
191       if (!pfnwglDeleteContext) {
192          _debug_printf("wgl: failed to get wglDeleteContext()\n");
193          return NULL;
194       }
195    } else {
196       /* Used as opengl32.dll drop-in alternative. */
197       assert(g_pfnwglCreateContext != NULL);
198       assert(g_pfnwglDeleteContext != NULL);
199       pfnwglCreateContext = g_pfnwglCreateContext;
200       pfnwglDeleteContext = g_pfnwglDeleteContext;
201    }
202 
203    /* Call wglCreateContext to get a valid context ID */
204    context = pfnwglCreateContext(hDC);
205 
206    if (context) {
207       /* Now replace the context we just created with a new one that reflects
208        * the attributes passed to this function.
209        */
210       DHGLRC dhglrc, c, share_dhglrc = 0;
211 
212       /* Convert public HGLRC to driver DHGLRC */
213       if (stw_dev && stw_dev->callbacks.pfnGetDhglrc) {
214          dhglrc = stw_dev->callbacks.pfnGetDhglrc(context);
215          if (hShareContext)
216             share_dhglrc = stw_dev->callbacks.pfnGetDhglrc(hShareContext);
217       }
218       else {
219          /* not using ICD */
220          dhglrc = (DHGLRC)(INT_PTR)context;
221          share_dhglrc = (DHGLRC)(INT_PTR)hShareContext;
222       }
223 
224       struct stw_context *share_stw = stw_lookup_context(share_dhglrc);
225 
226       const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info_from_hdc(hDC);
227       if (!pfi)
228          return 0;
229 
230       struct stw_context *stw_ctx = stw_create_context_attribs(hDC, layerPlane, share_stw,
231                                                                stw_dev->fscreen,
232                                                                majorVersion, minorVersion,
233                                                                contextFlags, profileMask, pfi,
234                                                                resetStrategy);
235 
236       if (!stw_ctx) {
237          pfnwglDeleteContext(context);
238          return NULL;
239       }
240 
241       c = stw_create_context_handle(stw_ctx, dhglrc);
242       if (!c) {
243          stw_destroy_context(stw_ctx);
244          pfnwglDeleteContext(context);
245          context = NULL;
246       }
247    }
248 
249    return context;
250 }
251 
252 
253 /** Defined by WGL_ARB_make_current_read */
254 BOOL APIENTRY
wglMakeContextCurrentARB(HDC hDrawDC,HDC hReadDC,HGLRC hglrc)255 wglMakeContextCurrentARB(HDC hDrawDC, HDC hReadDC, HGLRC hglrc)
256 {
257    DHGLRC dhglrc = 0;
258 
259    if (stw_dev && stw_dev->callbacks.pfnGetDhglrc) {
260       /* Convert HGLRC to DHGLRC */
261       dhglrc = stw_dev->callbacks.pfnGetDhglrc(hglrc);
262    } else {
263       /* not using ICD */
264       dhglrc = (DHGLRC)(INT_PTR)hglrc;
265    }
266 
267    return stw_make_current_by_handles(hDrawDC, hReadDC, dhglrc);
268 }
269 
270 HDC APIENTRY
wglGetCurrentReadDCARB(VOID)271 wglGetCurrentReadDCARB(VOID)
272 {
273    return stw_get_current_read_dc();
274 }
275