st/wgl: eliminate implicit cast warning
[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 return wglUseFontBitmapsW(hdc, first, count, listBase);
218 }
219
220 WINGDIAPI BOOL APIENTRY
221 wglShareLists(
222 HGLRC hglrc1,
223 HGLRC hglrc2 )
224 {
225 return DrvShareLists((DHGLRC)(UINT_PTR)hglrc1,
226 (DHGLRC)(UINT_PTR)hglrc2);
227 }
228
229 WINGDIAPI BOOL APIENTRY
230 wglUseFontBitmapsW(
231 HDC hdc,
232 DWORD first,
233 DWORD count,
234 DWORD listBase )
235 {
236 GLYPHMETRICS gm;
237 MAT2 tra;
238 FIXED one, minus_one, zero;
239 void *buffer = NULL;
240 BOOL result = TRUE;
241
242 one.value = 1;
243 one.fract = 0;
244 minus_one.value = -1;
245 minus_one.fract = 0;
246 zero.value = 0;
247 zero.fract = 0;
248
249 tra.eM11 = one;
250 tra.eM22 = minus_one;
251 tra.eM12 = tra.eM21 = zero;
252
253 for (int i = 0; i < count; i++) {
254 DWORD size = GetGlyphOutline(hdc, first + i, GGO_BITMAP, &gm, 0,
255 NULL, &tra);
256
257 glNewList(listBase + i, GL_COMPILE);
258
259 if (size != GDI_ERROR) {
260 if (size == 0) {
261 glBitmap(0, 0, (GLfloat)-gm.gmptGlyphOrigin.x,
262 (GLfloat)gm.gmptGlyphOrigin.y,
263 (GLfloat)gm.gmCellIncX,
264 (GLfloat)gm.gmCellIncY, NULL);
265 }
266 else {
267 buffer = realloc(buffer, size);
268 size = GetGlyphOutline(hdc, first + i, GGO_BITMAP, &gm,
269 size, buffer, &tra);
270
271 glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
272 -gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y,
273 gm.gmCellIncX, gm.gmCellIncY, buffer);
274 }
275 }
276 else {
277 result = FALSE;
278 }
279
280 glEndList();
281 }
282
283 free(buffer);
284
285 return result;
286 }
287
288 WINGDIAPI BOOL APIENTRY
289 wglUseFontOutlinesA(
290 HDC hdc,
291 DWORD first,
292 DWORD count,
293 DWORD listBase,
294 FLOAT deviation,
295 FLOAT extrusion,
296 int format,
297 LPGLYPHMETRICSFLOAT lpgmf )
298 {
299 (void) hdc;
300 (void) first;
301 (void) count;
302 (void) listBase;
303 (void) deviation;
304 (void) extrusion;
305 (void) format;
306 (void) lpgmf;
307
308 assert( 0 );
309
310 return FALSE;
311 }
312
313 WINGDIAPI BOOL APIENTRY
314 wglUseFontOutlinesW(
315 HDC hdc,
316 DWORD first,
317 DWORD count,
318 DWORD listBase,
319 FLOAT deviation,
320 FLOAT extrusion,
321 int format,
322 LPGLYPHMETRICSFLOAT lpgmf )
323 {
324 (void) hdc;
325 (void) first;
326 (void) count;
327 (void) listBase;
328 (void) deviation;
329 (void) extrusion;
330 (void) format;
331 (void) lpgmf;
332
333 assert( 0 );
334
335 return FALSE;
336 }
337
338 WINGDIAPI BOOL APIENTRY
339 wglDescribeLayerPlane(
340 HDC hdc,
341 int iPixelFormat,
342 int iLayerPlane,
343 UINT nBytes,
344 LPLAYERPLANEDESCRIPTOR plpd )
345 {
346 return DrvDescribeLayerPlane(hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
347 }
348
349 WINGDIAPI int APIENTRY
350 wglSetLayerPaletteEntries(
351 HDC hdc,
352 int iLayerPlane,
353 int iStart,
354 int cEntries,
355 CONST COLORREF *pcr )
356 {
357 return DrvSetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);
358 }
359
360 WINGDIAPI int APIENTRY
361 wglGetLayerPaletteEntries(
362 HDC hdc,
363 int iLayerPlane,
364 int iStart,
365 int cEntries,
366 COLORREF *pcr )
367 {
368 return DrvGetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);
369 }
370
371 WINGDIAPI BOOL APIENTRY
372 wglRealizeLayerPalette(
373 HDC hdc,
374 int iLayerPlane,
375 BOOL bRealize )
376 {
377 (void) hdc;
378 (void) iLayerPlane;
379 (void) bRealize;
380
381 assert( 0 );
382
383 return FALSE;
384 }
385
386
387 /* When this library is used as a opengl32.dll drop-in replacement, ensure we
388 * use the wglCreate/Destroy entrypoints above, and not the true opengl32.dll,
389 * which could happen if this library's name is not opengl32.dll exactly.
390 *
391 * For example, Qt 5.4 bundles this as opengl32sw.dll:
392 * https://blog.qt.io/blog/2014/11/27/qt-weekly-21-dynamic-opengl-implementation-loading-in-qt-5-4/
393 */
394 static void
395 overrideOpenGL32EntryPoints(void)
396 {
397 wglCreateContext_func = &wglCreateContext;
398 wglDeleteContext_func = &wglDeleteContext;
399 }