st/wgl: pseudo-implementation of WGL_EXT_swap_control
[mesa.git] / src / gallium / state_trackers / wgl / stw_wgl.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 *
31 * Fake WGL API implementation.
32 *
33 * These functions implement the WGL API, on top of the ICD DDI, so that the
34 * resulting DLL can be used as a drop-in replacement for the system's
35 * opengl32.dll.
36 *
37 * These functions never get called for ICD drivers, which use exclusively the
38 * ICD DDI, i.e., the Drv* entrypoints.
39 */
40
41 #include <windows.h>
42
43 #include "util/u_debug.h"
44 #include "stw_icd.h"
45 #include "stw_context.h"
46 #include "stw_pixelformat.h"
47 #include "stw_wgl.h"
48 #include "stw_ext_context.h"
49
50
51 static void
52 overrideOpenGL32EntryPoints(void);
53
54 WINGDIAPI BOOL APIENTRY
55 wglCopyContext(
56 HGLRC hglrcSrc,
57 HGLRC hglrcDst,
58 UINT mask )
59 {
60 return DrvCopyContext( (DHGLRC)(UINT_PTR)hglrcSrc,
61 (DHGLRC)(UINT_PTR)hglrcDst,
62 mask );
63 }
64
65 WINGDIAPI HGLRC APIENTRY
66 wglCreateContext(
67 HDC hdc )
68 {
69 overrideOpenGL32EntryPoints();
70 return (HGLRC) DrvCreateContext(hdc);
71 }
72
73 WINGDIAPI HGLRC APIENTRY
74 wglCreateLayerContext(
75 HDC hdc,
76 int iLayerPlane )
77 {
78 overrideOpenGL32EntryPoints();
79 return (HGLRC) DrvCreateLayerContext( hdc, iLayerPlane );
80 }
81
82 WINGDIAPI BOOL APIENTRY
83 wglDeleteContext(
84 HGLRC hglrc )
85 {
86 return DrvDeleteContext((DHGLRC)(UINT_PTR)hglrc );
87 }
88
89
90 WINGDIAPI HGLRC APIENTRY
91 wglGetCurrentContext( VOID )
92 {
93 return (HGLRC)(UINT_PTR)stw_get_current_context();
94 }
95
96 WINGDIAPI HDC APIENTRY
97 wglGetCurrentDC( VOID )
98 {
99 return stw_get_current_dc();
100 }
101
102 WINGDIAPI HDC APIENTRY
103 wglGetCurrentReadDCARB( VOID )
104 {
105 return stw_get_current_read_dc();
106 }
107
108
109 WINGDIAPI BOOL APIENTRY
110 wglMakeCurrent(
111 HDC hdc,
112 HGLRC hglrc )
113 {
114 return DrvSetContext( hdc, (DHGLRC)(UINT_PTR)hglrc, NULL ) ? TRUE : FALSE;
115 }
116
117
118 WINGDIAPI BOOL APIENTRY
119 wglSwapBuffers(
120 HDC hdc )
121 {
122 return DrvSwapBuffers( hdc );
123 }
124
125
126 WINGDIAPI DWORD WINAPI
127 wglSwapMultipleBuffers(UINT n,
128 CONST WGLSWAP *ps)
129 {
130 UINT i;
131
132 for (i =0; i < n; ++i)
133 wglSwapBuffers(ps->hdc);
134
135 return 0;
136 }
137
138
139 WINGDIAPI BOOL APIENTRY
140 wglSwapLayerBuffers(
141 HDC hdc,
142 UINT fuPlanes )
143 {
144 return DrvSwapLayerBuffers( hdc, fuPlanes );
145 }
146
147 WINGDIAPI PROC APIENTRY
148 wglGetProcAddress(
149 LPCSTR lpszProc )
150 {
151 return DrvGetProcAddress( lpszProc );
152 }
153
154
155 WINGDIAPI int APIENTRY
156 wglChoosePixelFormat(
157 HDC hdc,
158 CONST PIXELFORMATDESCRIPTOR *ppfd )
159 {
160 if (ppfd->nSize != sizeof( PIXELFORMATDESCRIPTOR ) || ppfd->nVersion != 1)
161 return 0;
162 if (ppfd->iPixelType != PFD_TYPE_RGBA)
163 return 0;
164 if (!(ppfd->dwFlags & PFD_DRAW_TO_WINDOW))
165 return 0;
166 if (!(ppfd->dwFlags & PFD_SUPPORT_OPENGL))
167 return 0;
168 if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP)
169 return 0;
170 if (ppfd->dwFlags & PFD_SUPPORT_GDI)
171 return 0;
172 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE) && (ppfd->dwFlags & PFD_STEREO))
173 return 0;
174
175 return stw_pixelformat_choose( hdc, ppfd );
176 }
177
178 WINGDIAPI int APIENTRY
179 wglDescribePixelFormat(
180 HDC hdc,
181 int iPixelFormat,
182 UINT nBytes,
183 LPPIXELFORMATDESCRIPTOR ppfd )
184 {
185 return DrvDescribePixelFormat( hdc, iPixelFormat, nBytes, ppfd );
186 }
187
188 WINGDIAPI int APIENTRY
189 wglGetPixelFormat(
190 HDC hdc )
191 {
192 return stw_pixelformat_get( hdc );
193 }
194
195 WINGDIAPI BOOL APIENTRY
196 wglSetPixelFormat(
197 HDC hdc,
198 int iPixelFormat,
199 const PIXELFORMATDESCRIPTOR *ppfd )
200 {
201 /* SetPixelFormat (hence wglSetPixelFormat) must not touch ppfd, per
202 * http://msdn.microsoft.com/en-us/library/dd369049(v=vs.85).aspx
203 */
204 (void) ppfd;
205
206 return DrvSetPixelFormat( hdc, iPixelFormat );
207 }
208
209
210 WINGDIAPI BOOL APIENTRY
211 wglUseFontBitmapsA(
212 HDC hdc,
213 DWORD first,
214 DWORD count,
215 DWORD listBase )
216 {
217 (void) hdc;
218 (void) first;
219 (void) count;
220 (void) listBase;
221
222 assert( 0 );
223
224 return FALSE;
225 }
226
227 WINGDIAPI BOOL APIENTRY
228 wglShareLists(
229 HGLRC hglrc1,
230 HGLRC hglrc2 )
231 {
232 return DrvShareLists((DHGLRC)(UINT_PTR)hglrc1,
233 (DHGLRC)(UINT_PTR)hglrc2);
234 }
235
236 WINGDIAPI BOOL APIENTRY
237 wglUseFontBitmapsW(
238 HDC hdc,
239 DWORD first,
240 DWORD count,
241 DWORD listBase )
242 {
243 (void) hdc;
244 (void) first;
245 (void) count;
246 (void) listBase;
247
248 assert( 0 );
249
250 return FALSE;
251 }
252
253 WINGDIAPI BOOL APIENTRY
254 wglUseFontOutlinesA(
255 HDC hdc,
256 DWORD first,
257 DWORD count,
258 DWORD listBase,
259 FLOAT deviation,
260 FLOAT extrusion,
261 int format,
262 LPGLYPHMETRICSFLOAT lpgmf )
263 {
264 (void) hdc;
265 (void) first;
266 (void) count;
267 (void) listBase;
268 (void) deviation;
269 (void) extrusion;
270 (void) format;
271 (void) lpgmf;
272
273 assert( 0 );
274
275 return FALSE;
276 }
277
278 WINGDIAPI BOOL APIENTRY
279 wglUseFontOutlinesW(
280 HDC hdc,
281 DWORD first,
282 DWORD count,
283 DWORD listBase,
284 FLOAT deviation,
285 FLOAT extrusion,
286 int format,
287 LPGLYPHMETRICSFLOAT lpgmf )
288 {
289 (void) hdc;
290 (void) first;
291 (void) count;
292 (void) listBase;
293 (void) deviation;
294 (void) extrusion;
295 (void) format;
296 (void) lpgmf;
297
298 assert( 0 );
299
300 return FALSE;
301 }
302
303 WINGDIAPI BOOL APIENTRY
304 wglDescribeLayerPlane(
305 HDC hdc,
306 int iPixelFormat,
307 int iLayerPlane,
308 UINT nBytes,
309 LPLAYERPLANEDESCRIPTOR plpd )
310 {
311 return DrvDescribeLayerPlane(hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
312 }
313
314 WINGDIAPI int APIENTRY
315 wglSetLayerPaletteEntries(
316 HDC hdc,
317 int iLayerPlane,
318 int iStart,
319 int cEntries,
320 CONST COLORREF *pcr )
321 {
322 return DrvSetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);
323 }
324
325 WINGDIAPI int APIENTRY
326 wglGetLayerPaletteEntries(
327 HDC hdc,
328 int iLayerPlane,
329 int iStart,
330 int cEntries,
331 COLORREF *pcr )
332 {
333 return DrvGetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);
334 }
335
336 WINGDIAPI BOOL APIENTRY
337 wglRealizeLayerPalette(
338 HDC hdc,
339 int iLayerPlane,
340 BOOL bRealize )
341 {
342 (void) hdc;
343 (void) iLayerPlane;
344 (void) bRealize;
345
346 assert( 0 );
347
348 return FALSE;
349 }
350
351
352 /* When this library is used as a opengl32.dll drop-in replacement, ensure we
353 * use the wglCreate/Destroy entrypoints above, and not the true opengl32.dll,
354 * which could happen if this library's name is not opengl32.dll exactly.
355 *
356 * For example, Qt 5.4 bundles this as opengl32sw.dll:
357 * https://blog.qt.io/blog/2014/11/27/qt-weekly-21-dynamic-opengl-implementation-loading-in-qt-5-4/
358 */
359 static void
360 overrideOpenGL32EntryPoints(void)
361 {
362 wglCreateContext_func = &wglCreateContext;
363 wglDeleteContext_func = &wglDeleteContext;
364 }