3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * WGL stuff. Added by Oleg Letsinsky, ajl@ultersys.ru
22 * Some things originated from the 3Dfx WGL functions
26 * This file contains the implementation of the wgl* functions for
27 * Mesa on Windows. Since these functions are provided by Windows in
28 * GDI/OpenGL, we must supply our versions that work with Mesa here.
34 #include "main/config.h"
35 #include "glapi/glapi.h"
36 #include "swrast/swrast.h"
37 #include "GL/wmesa.h" /* protos for wmesa* functions */
40 * Pixel Format Descriptors
43 /* Extend the PFD to include DB flag */
44 struct __pixelformat__
46 PIXELFORMATDESCRIPTOR pfd
;
47 GLboolean doubleBuffered
;
52 /* These are the PFD's supported by this driver. */
53 struct __pixelformat__ pfd
[] =
56 /* Double Buffer, alpha */
59 sizeof(PIXELFORMATDESCRIPTOR
), 1,
60 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
61 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
69 DEFAULT_SOFTWARE_DEPTH_BITS
, 8,
75 /* Single Buffer, alpha */
78 sizeof(PIXELFORMATDESCRIPTOR
), 1,
79 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
88 DEFAULT_SOFTWARE_DEPTH_BITS
, 8,
95 /* Double Buffer, no alpha */
98 sizeof(PIXELFORMATDESCRIPTOR
), 1,
99 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
100 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
108 DEFAULT_SOFTWARE_DEPTH_BITS
, 8,
114 /* Single Buffer, no alpha */
117 sizeof(PIXELFORMATDESCRIPTOR
), 1,
118 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
127 DEFAULT_SOFTWARE_DEPTH_BITS
, 8,
135 int npfd
= sizeof(pfd
) / sizeof(pfd
[0]);
146 #define MESAWGL_CTX_MAX_COUNT 20
148 static MesaWglCtx wgl_ctx
[MESAWGL_CTX_MAX_COUNT
];
150 static unsigned ctx_count
= 0;
151 static int ctx_current
= -1;
152 static unsigned curPFD
= 0;
154 static HDC CurrentHDC
= 0;
157 WINGDIAPI HGLRC GLAPIENTRY
wglCreateContext(HDC hdc
)
161 for(i
=0;i
<MESAWGL_CTX_MAX_COUNT
;i
++) {
162 wgl_ctx
[i
].ctx
= NULL
;
165 for( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
166 if ( wgl_ctx
[i
].ctx
== NULL
) {
168 WMesaCreateContext(hdc
, NULL
, (GLboolean
)GL_TRUE
,
169 (GLboolean
) (pfd
[curPFD
-1].doubleBuffered
?
171 (GLboolean
)(pfd
[curPFD
-1].pfd
.cAlphaBits
?
172 GL_TRUE
: GL_FALSE
) );
173 if (wgl_ctx
[i
].ctx
== NULL
)
176 return ((HGLRC
)wgl_ctx
[i
].ctx
);
183 WINGDIAPI BOOL GLAPIENTRY
wglDeleteContext(HGLRC hglrc
)
186 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
187 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
){
188 WMesaMakeCurrent((WMesaContext
) hglrc
, NULL
);
189 WMesaDestroyContext(wgl_ctx
[i
].ctx
);
190 wgl_ctx
[i
].ctx
= NULL
;
199 WINGDIAPI HGLRC GLAPIENTRY
wglGetCurrentContext(VOID
)
204 return (HGLRC
) wgl_ctx
[ctx_current
].ctx
;
207 WINGDIAPI HDC GLAPIENTRY
wglGetCurrentDC(VOID
)
212 WINGDIAPI BOOL GLAPIENTRY
wglMakeCurrent(HDC hdc
, HGLRC hglrc
)
218 if (!hdc
|| !hglrc
) {
219 WMesaMakeCurrent(NULL
, NULL
);
224 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
225 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
) {
226 WMesaMakeCurrent( (WMesaContext
) hglrc
, hdc
);
235 WINGDIAPI
int GLAPIENTRY
wglChoosePixelFormat(HDC hdc
,
237 PIXELFORMATDESCRIPTOR
*ppfd
)
239 int i
,best
= -1,bestdelta
= 0x7FFFFFFF,delta
;
242 if(ppfd
->nSize
!= sizeof(PIXELFORMATDESCRIPTOR
) || ppfd
->nVersion
!= 1)
247 for(i
= 0; i
< npfd
;i
++)
251 (ppfd
->dwFlags
& PFD_DRAW_TO_WINDOW
) &&
252 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_WINDOW
))
255 (ppfd
->dwFlags
& PFD_DRAW_TO_BITMAP
) &&
256 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_BITMAP
))
259 (ppfd
->dwFlags
& PFD_SUPPORT_GDI
) &&
260 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_GDI
))
263 (ppfd
->dwFlags
& PFD_SUPPORT_OPENGL
) &&
264 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_OPENGL
))
267 !(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
268 ((ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
269 (pfd
[i
].pfd
.dwFlags
& PFD_DOUBLEBUFFER
)))
272 !(ppfd
->dwFlags
& PFD_STEREO_DONTCARE
) &&
273 ((ppfd
->dwFlags
& PFD_STEREO
) !=
274 (pfd
[i
].pfd
.dwFlags
& PFD_STEREO
)))
276 if(ppfd
->iPixelType
!= pfd
[i
].pfd
.iPixelType
)
278 if(ppfd
->cAlphaBits
!= pfd
[i
].pfd
.cAlphaBits
)
280 if(delta
< bestdelta
)
296 WINGDIAPI
int GLAPIENTRY
wglDescribePixelFormat(HDC hdc
,
299 LPPIXELFORMATDESCRIPTOR ppfd
)
305 if(iPixelFormat
< 1 || iPixelFormat
> npfd
||
306 nBytes
!= sizeof(PIXELFORMATDESCRIPTOR
))
311 *ppfd
= pfd
[iPixelFormat
- 1].pfd
;
315 WINGDIAPI PROC GLAPIENTRY
wglGetProcAddress(LPCSTR lpszProc
)
317 PROC p
= (PROC
) _glapi_get_proc_address((const char *) lpszProc
);
325 WINGDIAPI
int GLAPIENTRY
wglGetPixelFormat(HDC hdc
)
335 WINGDIAPI BOOL GLAPIENTRY
wglSetPixelFormat(HDC hdc
,int iPixelFormat
,
336 const PIXELFORMATDESCRIPTOR
*ppfd
)
340 /* SetPixelFormat (hence wglSetPixelFormat) must not touch ppfd, per
341 * http://msdn.microsoft.com/en-us/library/dd369049(v=vs.85).aspx
345 if(iPixelFormat
< 1 || iPixelFormat
> npfd
) {
349 curPFD
= iPixelFormat
;
353 WINGDIAPI BOOL GLAPIENTRY
wglSwapBuffers(HDC hdc
)
355 WMesaSwapBuffers(hdc
);
359 static FIXED
FixedFromDouble(double d
)
361 long l
= (long) (d
* 65536L);
362 return *(FIXED
*) (void *) &l
;
367 ** This is cribbed from FX/fxwgl.c, and seems to implement support
368 ** for bitmap fonts where the wglUseFontBitmapsA() code implements
369 ** support for outline fonts. In combination they hopefully give
370 ** fairly generic support for fonts.
372 static BOOL
wglUseFontBitmaps_FX(HDC fontDevice
, DWORD firstChar
,
373 DWORD numChars
, DWORD listBase
)
375 #define VERIFY(a) (void)(a)
383 VERIFY(GetTextMetrics(fontDevice
, &metric
));
385 dibInfo
= (BITMAPINFO
*) calloc(sizeof(BITMAPINFO
) + sizeof(RGBQUAD
), 1);
386 dibInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
387 dibInfo
->bmiHeader
.biPlanes
= 1;
388 dibInfo
->bmiHeader
.biBitCount
= 1;
389 dibInfo
->bmiHeader
.biCompression
= BI_RGB
;
391 bitDevice
= CreateCompatibleDC(fontDevice
);
393 /* Swap fore and back colors so the bitmap has the right polarity */
394 tempColor
= GetBkColor(bitDevice
);
395 SetBkColor(bitDevice
, GetTextColor(bitDevice
));
396 SetTextColor(bitDevice
, tempColor
);
398 /* Place chars based on base line */
399 VERIFY(SetTextAlign(bitDevice
, TA_BASELINE
) != GDI_ERROR
? 1 : 0);
401 for(i
= 0; i
< (int)numChars
; i
++) {
404 int charWidth
,charHeight
,bmapWidth
,bmapHeight
,numBytes
,res
;
409 curChar
= (char)(i
+ firstChar
);
411 /* Find how high/wide this character is */
412 VERIFY(GetTextExtentPoint32(bitDevice
, &curChar
, 1, &size
));
414 /* Create the output bitmap */
416 charHeight
= size
.cy
;
417 /* Round up to the next multiple of 32 bits */
418 bmapWidth
= ((charWidth
+ 31) / 32) * 32;
419 bmapHeight
= charHeight
;
420 bitObject
= CreateCompatibleBitmap(bitDevice
,
423 /* VERIFY(bitObject); */
425 /* Assign the output bitmap to the device */
426 origBmap
= SelectObject(bitDevice
, bitObject
);
427 (void) VERIFY(origBmap
);
429 VERIFY( PatBlt( bitDevice
, 0, 0, bmapWidth
, bmapHeight
,BLACKNESS
) );
431 /* Use our source font on the device */
432 VERIFY(SelectObject(bitDevice
, GetCurrentObject(fontDevice
,OBJ_FONT
)));
434 /* Draw the character */
435 VERIFY(TextOut(bitDevice
, 0, metric
.tmAscent
, &curChar
, 1));
437 /* Unselect our bmap object */
438 VERIFY(SelectObject(bitDevice
, origBmap
));
440 /* Convert the display dependant representation to a 1 bit deep DIB */
441 numBytes
= (bmapWidth
* bmapHeight
) / 8;
442 bmap
= malloc(numBytes
);
443 dibInfo
->bmiHeader
.biWidth
= bmapWidth
;
444 dibInfo
->bmiHeader
.biHeight
= bmapHeight
;
445 res
= GetDIBits(bitDevice
, bitObject
, 0, bmapHeight
, bmap
,
450 /* Create the GL object */
451 glNewList(i
+ listBase
, GL_COMPILE
);
452 glBitmap(bmapWidth
, bmapHeight
, 0.0, (GLfloat
)metric
.tmDescent
,
453 (GLfloat
)charWidth
, 0.0,
458 /* Destroy the bmap object */
459 DeleteObject(bitObject
);
461 /* Deallocate the bitmap data */
466 VERIFY(DeleteDC(bitDevice
));
474 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsA(HDC hdc
, DWORD first
,
475 DWORD count
, DWORD listBase
)
489 font_list
= listBase
;
491 mat
.eM11
= FixedFromDouble(1);
492 mat
.eM12
= FixedFromDouble(0);
493 mat
.eM21
= FixedFromDouble(0);
494 mat
.eM22
= FixedFromDouble(-1);
496 memset(&gm
,0,sizeof(gm
));
499 ** If we can't get the glyph outline, it may be because this is a fixed
500 ** font. Try processing it that way.
502 if( GetGlyphOutline(hdc
, first
, GGO_BITMAP
, &gm
, 0, NULL
, &mat
)
504 return wglUseFontBitmaps_FX( hdc
, first
, count
, listBase
);
508 ** Otherwise process all desired characters.
510 for (i
= 0; i
< (int)count
; i
++) {
513 glNewList( font_list
+i
, GL_COMPILE
);
515 /* allocate space for the bitmap/outline */
516 size
= GetGlyphOutline(hdc
, first
+ i
, GGO_BITMAP
,
518 if (size
== GDI_ERROR
) {
520 err
= GetLastError();
525 hBits
= GlobalAlloc(GHND
, size
+1);
526 lpBits
= GlobalLock(hBits
);
529 GetGlyphOutline(hdc
, /* handle to device context */
530 first
+ i
, /* character to query */
531 GGO_BITMAP
, /* format of data to return */
532 &gm
, /* ptr to structure for metrics*/
533 size
, /* size of buffer for data */
534 lpBits
, /* pointer to buffer for data */
535 &mat
/* pointer to transformation */
536 /* matrix structure */
539 if (err
== GDI_ERROR
) {
544 err
= GetLastError();
549 glBitmap(gm
.gmBlackBoxX
,gm
.gmBlackBoxY
,
550 (GLfloat
)-gm
.gmptGlyphOrigin
.x
,
551 (GLfloat
)gm
.gmptGlyphOrigin
.y
,
552 (GLfloat
)gm
.gmCellIncX
,
553 (GLfloat
)gm
.gmCellIncY
,
554 (const GLubyte
* )lpBits
);
565 WINGDIAPI BOOL GLAPIENTRY
wglShareLists(HGLRC hglrc1
,
568 WMesaShareLists((WMesaContext
)hglrc1
, (WMesaContext
)hglrc2
);
574 /* NOT IMPLEMENTED YET */
575 WINGDIAPI BOOL GLAPIENTRY
wglCopyContext(HGLRC hglrcSrc
,
579 (void) hglrcSrc
; (void) hglrcDst
; (void) mask
;
583 WINGDIAPI HGLRC GLAPIENTRY
wglCreateLayerContext(HDC hdc
,
587 if (iLayerPlane
== 0)
588 return wglCreateContext( hdc
);
593 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsW(HDC hdc
,
598 (void) hdc
; (void) first
; (void) count
; (void) listBase
;
602 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesA(HDC hdc
,
609 LPGLYPHMETRICSFLOAT lpgmf
)
611 (void) hdc
; (void) first
; (void) count
;
612 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
618 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesW(HDC hdc
,
625 LPGLYPHMETRICSFLOAT lpgmf
)
627 (void) hdc
; (void) first
; (void) count
;
628 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
634 WINGDIAPI BOOL GLAPIENTRY
wglDescribeLayerPlane(HDC hdc
,
638 LPLAYERPLANEDESCRIPTOR plpd
)
640 (void) hdc
; (void) iPixelFormat
; (void) iLayerPlane
;
641 (void) nBytes
; (void) plpd
;
646 WINGDIAPI
int GLAPIENTRY
wglSetLayerPaletteEntries(HDC hdc
,
652 (void) hdc
; (void) iLayerPlane
; (void) iStart
;
653 (void) cEntries
; (void) pcr
;
658 WINGDIAPI
int GLAPIENTRY
wglGetLayerPaletteEntries(HDC hdc
,
664 (void) hdc
; (void) iLayerPlane
; (void) iStart
; (void) cEntries
; (void) pcr
;
669 WINGDIAPI BOOL GLAPIENTRY
wglRealizeLayerPalette(HDC hdc
,
673 (void) hdc
; (void) iLayerPlane
; (void) bRealize
;
678 WINGDIAPI BOOL GLAPIENTRY
wglSwapLayerBuffers(HDC hdc
,
681 (void) hdc
; (void) fuPlanes
;
686 WINGDIAPI
const char * GLAPIENTRY
wglGetExtensionsStringARB(HDC hdc
)
688 return "WGL_ARB_extensions_string";