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 gallium frontend.
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#include <GL/gl.h>
43
44#include "util/u_debug.h"
45#include "gldrv.h"
46#include "stw_context.h"
47#include "stw_pixelformat.h"
48#include "stw_wgl.h"
49#include "stw_ext_context.h"
50
51WINGDIAPI BOOL APIENTRY
52wglCopyContext(
53   HGLRC hglrcSrc,
54   HGLRC hglrcDst,
55   UINT mask )
56{
57   return DrvCopyContext( (DHGLRC)(UINT_PTR)hglrcSrc,
58                          (DHGLRC)(UINT_PTR)hglrcDst,
59                          mask );
60}
61
62WINGDIAPI HGLRC APIENTRY
63wglCreateContext(
64   HDC hdc )
65{
66   stw_override_opengl32_entry_points(&wglCreateContext, &wglDeleteContext);
67   return (HGLRC)(UINT_PTR)DrvCreateContext(hdc);
68}
69
70WINGDIAPI HGLRC APIENTRY
71wglCreateLayerContext(
72   HDC hdc,
73   int iLayerPlane )
74{
75   stw_override_opengl32_entry_points(&wglCreateContext, &wglDeleteContext);
76   return (HGLRC)(UINT_PTR)DrvCreateLayerContext( hdc, iLayerPlane );
77}
78
79WINGDIAPI BOOL APIENTRY
80wglDeleteContext(
81   HGLRC hglrc )
82{
83   return DrvDeleteContext((DHGLRC)(UINT_PTR)hglrc );
84}
85
86
87WINGDIAPI HGLRC APIENTRY
88wglGetCurrentContext( VOID )
89{
90   return (HGLRC)(UINT_PTR)stw_get_current_context();
91}
92
93WINGDIAPI HDC APIENTRY
94wglGetCurrentDC( VOID )
95{
96   return stw_get_current_dc();
97}
98
99
100WINGDIAPI BOOL APIENTRY
101wglMakeCurrent(
102   HDC hdc,
103   HGLRC hglrc )
104{
105   return DrvSetContext( hdc, (DHGLRC)(UINT_PTR)hglrc, NULL ) ? TRUE : FALSE;
106}
107
108
109WINGDIAPI BOOL APIENTRY
110wglSwapBuffers(
111   HDC hdc )
112{
113   return DrvSwapBuffers( hdc );
114}
115
116
117WINGDIAPI DWORD WINAPI
118wglSwapMultipleBuffers(UINT n,
119                       CONST WGLSWAP *ps)
120{
121   UINT i;
122
123   for (i =0; i < n; ++i)
124      wglSwapBuffers(ps->hdc);
125
126   return 0;
127}
128
129
130WINGDIAPI BOOL APIENTRY
131wglSwapLayerBuffers(
132   HDC hdc,
133   UINT fuPlanes )
134{
135   return DrvSwapLayerBuffers( hdc, fuPlanes );
136}
137
138WINGDIAPI PROC APIENTRY
139wglGetProcAddress(
140    LPCSTR lpszProc )
141{
142   return DrvGetProcAddress( lpszProc );
143}
144
145
146WINGDIAPI int APIENTRY
147wglChoosePixelFormat(
148   HDC hdc,
149   CONST PIXELFORMATDESCRIPTOR *ppfd )
150{
151   if (ppfd->nSize != sizeof( PIXELFORMATDESCRIPTOR ) || ppfd->nVersion != 1)
152      return 0;
153   if (ppfd->iPixelType != PFD_TYPE_RGBA)
154      return 0;
155   if (!(ppfd->dwFlags & PFD_DRAW_TO_WINDOW))
156      return 0;
157   if (!(ppfd->dwFlags & PFD_SUPPORT_OPENGL))
158      return 0;
159   if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP)
160      return 0;
161   if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE) && (ppfd->dwFlags & PFD_STEREO))
162      return 0;
163
164   return stw_pixelformat_choose( hdc, ppfd );
165}
166
167WINGDIAPI int APIENTRY
168wglDescribePixelFormat(
169   HDC hdc,
170   int iPixelFormat,
171   UINT nBytes,
172   LPPIXELFORMATDESCRIPTOR ppfd )
173{
174   return DrvDescribePixelFormat( hdc, iPixelFormat, nBytes, ppfd );
175}
176
177WINGDIAPI int APIENTRY
178wglGetPixelFormat(
179   HDC hdc )
180{
181   return stw_pixelformat_get( hdc );
182}
183
184WINGDIAPI BOOL APIENTRY
185wglSetPixelFormat(
186   HDC hdc,
187   int iPixelFormat,
188   const PIXELFORMATDESCRIPTOR *ppfd )
189{
190    /* SetPixelFormat (hence wglSetPixelFormat) must not touch ppfd, per
191     * http://msdn.microsoft.com/en-us/library/dd369049(v=vs.85).aspx
192     */
193   (void) ppfd;
194
195   return DrvSetPixelFormat( hdc, iPixelFormat );
196}
197
198
199WINGDIAPI BOOL APIENTRY
200wglUseFontBitmapsA(
201   HDC hdc,
202   DWORD first,
203   DWORD count,
204   DWORD listBase )
205{
206   return wglUseFontBitmapsW(hdc, first, count, listBase);
207}
208
209WINGDIAPI BOOL APIENTRY
210wglShareLists(
211   HGLRC hglrc1,
212   HGLRC hglrc2 )
213{
214   return DrvShareLists((DHGLRC)(UINT_PTR)hglrc1,
215                        (DHGLRC)(UINT_PTR)hglrc2);
216}
217
218WINGDIAPI BOOL APIENTRY
219wglUseFontBitmapsW(
220   HDC hdc,
221   DWORD first,
222   DWORD count,
223   DWORD listBase )
224{
225   GLYPHMETRICS gm;
226   MAT2 tra;
227   FIXED one, minus_one, zero;
228   void *buffer = NULL;
229   BOOL result = TRUE;
230
231   one.value = 1;
232   one.fract = 0;
233   minus_one.value = -1;
234   minus_one.fract = 0;
235   zero.value = 0;
236   zero.fract = 0;
237
238   tra.eM11 = one;
239   tra.eM22 = minus_one;
240   tra.eM12 = tra.eM21 = zero;
241
242   for (int i = 0; i < count; i++) {
243      DWORD size = GetGlyphOutline(hdc, first + i, GGO_BITMAP, &gm, 0,
244                                   NULL, &tra);
245
246      glNewList(listBase + i, GL_COMPILE);
247
248      if (size != GDI_ERROR) {
249         if (size == 0) {
250            glBitmap(0, 0, (GLfloat)-gm.gmptGlyphOrigin.x,
251                     (GLfloat)gm.gmptGlyphOrigin.y,
252                     (GLfloat)gm.gmCellIncX,
253                     (GLfloat)gm.gmCellIncY, NULL);
254         }
255         else {
256            buffer = realloc(buffer, size);
257            size = GetGlyphOutline(hdc, first + i, GGO_BITMAP, &gm,
258                                   size, buffer, &tra);
259
260            glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
261                     -gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y,
262                     gm.gmCellIncX, gm.gmCellIncY, buffer);
263         }
264      }
265      else {
266         result = FALSE;
267      }
268
269      glEndList();
270   }
271
272   free(buffer);
273
274   return result;
275}
276
277WINGDIAPI BOOL APIENTRY
278wglUseFontOutlinesA(
279   HDC hdc,
280   DWORD first,
281   DWORD count,
282   DWORD listBase,
283   FLOAT deviation,
284   FLOAT extrusion,
285   int format,
286   LPGLYPHMETRICSFLOAT lpgmf )
287{
288   (void) hdc;
289   (void) first;
290   (void) count;
291   (void) listBase;
292   (void) deviation;
293   (void) extrusion;
294   (void) format;
295   (void) lpgmf;
296
297   assert( 0 );
298
299   return FALSE;
300}
301
302WINGDIAPI BOOL APIENTRY
303wglUseFontOutlinesW(
304   HDC hdc,
305   DWORD first,
306   DWORD count,
307   DWORD listBase,
308   FLOAT deviation,
309   FLOAT extrusion,
310   int format,
311   LPGLYPHMETRICSFLOAT lpgmf )
312{
313   (void) hdc;
314   (void) first;
315   (void) count;
316   (void) listBase;
317   (void) deviation;
318   (void) extrusion;
319   (void) format;
320   (void) lpgmf;
321
322   assert( 0 );
323
324   return FALSE;
325}
326
327WINGDIAPI BOOL APIENTRY
328wglDescribeLayerPlane(
329   HDC hdc,
330   int iPixelFormat,
331   int iLayerPlane,
332   UINT nBytes,
333   LPLAYERPLANEDESCRIPTOR plpd )
334{
335   return DrvDescribeLayerPlane(hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
336}
337
338WINGDIAPI int APIENTRY
339wglSetLayerPaletteEntries(
340   HDC hdc,
341   int iLayerPlane,
342   int iStart,
343   int cEntries,
344   CONST COLORREF *pcr )
345{
346   return DrvSetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);
347}
348
349WINGDIAPI int APIENTRY
350wglGetLayerPaletteEntries(
351   HDC hdc,
352   int iLayerPlane,
353   int iStart,
354   int cEntries,
355   COLORREF *pcr )
356{
357   return DrvGetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);
358}
359
360WINGDIAPI BOOL APIENTRY
361wglRealizeLayerPalette(
362   HDC hdc,
363   int iLayerPlane,
364   BOOL bRealize )
365{
366   (void) hdc;
367   (void) iLayerPlane;
368   (void) bRealize;
369
370   assert( 0 );
371
372   return FALSE;
373}
374
375
376