Merge branch 'origin' into glsl-compiler-1
[mesa.git] / src / mesa / drivers / windows / gdi / wgl.c
1 /* $Id: wgl.c,v 1.12 2006/03/30 07:58:24 kschultz Exp $ */
2
3 /*
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 */
19
20 /*
21 * File name : wgl.c
22 * WGL stuff. Added by Oleg Letsinsky, ajl@ultersys.ru
23 * Some things originated from the 3Dfx WGL functions
24 */
25
26 /*
27 * This file contains the implementation of the wgl* functions for
28 * Mesa on Windows. Since these functions are provided by Windows in
29 * GDI/OpenGL, we must supply our versions that work with Mesa here.
30 */
31
32
33 /* We're essentially building part of GDI here, so define this so that
34 * we get the right export linkage. */
35 #define _GDI32_
36 #include <windows.h>
37 #include "glapi.h"
38
39 #include "GL/wmesa.h" /* protos for wmesa* functions */
40
41 /*
42 * Pixel Format Descriptors
43 */
44
45 /* Extend the PFD to include DB flag */
46 struct __pixelformat__
47 {
48 PIXELFORMATDESCRIPTOR pfd;
49 GLboolean doubleBuffered;
50 };
51
52 /* These are the PFD's supported by this driver. */
53 struct __pixelformat__ pfd[] =
54 {
55 #if 0
56 /* Double Buffer, alpha */
57 {
58 {
59 sizeof(PIXELFORMATDESCRIPTOR), 1,
60 PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
61 PFD_GENERIC_FORMAT|PFD_DOUBLEBUFFER|PFD_SWAP_COPY,
62 PFD_TYPE_RGBA,
63 24,
64 8, 0,
65 8, 8,
66 8, 16,
67 8, 24,
68 0, 0, 0, 0, 0,
69 16, 8,
70 0, 0, 0,
71 0, 0, 0
72 },
73 GL_TRUE
74 },
75 /* Single Buffer, alpha */
76 {
77 {
78 sizeof(PIXELFORMATDESCRIPTOR), 1,
79 PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
80 PFD_GENERIC_FORMAT,
81 PFD_TYPE_RGBA,
82 24,
83 8, 0,
84 8, 8,
85 8, 16,
86 8, 24,
87 0, 0, 0, 0, 0,
88 16, 8,
89 0, 0, 0,
90 0, 0, 0
91 },
92 GL_FALSE
93 },
94 #endif
95 /* Double Buffer, no alpha */
96 {
97 {
98 sizeof(PIXELFORMATDESCRIPTOR), 1,
99 PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
100 PFD_GENERIC_FORMAT|PFD_DOUBLEBUFFER|PFD_SWAP_COPY,
101 PFD_TYPE_RGBA,
102 24,
103 8, 0,
104 8, 8,
105 8, 16,
106 0, 0,
107 0, 0, 0, 0, 0,
108 16, 8,
109 0, 0, 0,
110 0, 0, 0
111 },
112 GL_TRUE
113 },
114 /* Single Buffer, no alpha */
115 {
116 {
117 sizeof(PIXELFORMATDESCRIPTOR), 1,
118 PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
119 PFD_GENERIC_FORMAT,
120 PFD_TYPE_RGBA,
121 24,
122 8, 0,
123 8, 8,
124 8, 16,
125 0, 0,
126 0, 0, 0, 0, 0,
127 16, 8,
128 0, 0, 0,
129 0, 0, 0
130 },
131 GL_FALSE
132 },
133 };
134
135 int npfd = sizeof(pfd) / sizeof(pfd[0]);
136
137
138 /*
139 * Contexts
140 */
141
142 typedef struct {
143 WMesaContext ctx;
144 } MesaWglCtx;
145
146 #define MESAWGL_CTX_MAX_COUNT 20
147
148 static MesaWglCtx wgl_ctx[MESAWGL_CTX_MAX_COUNT];
149
150 static unsigned ctx_count = 0;
151 static int ctx_current = -1;
152 static unsigned curPFD = 0;
153
154 static HDC CurrentHDC = 0;
155
156
157 WINGDIAPI HGLRC GLAPIENTRY wglCreateContext(HDC hdc)
158 {
159 int i = 0;
160 if (!ctx_count) {
161 for(i=0;i<MESAWGL_CTX_MAX_COUNT;i++) {
162 wgl_ctx[i].ctx = NULL;
163 }
164 }
165 for( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ ) {
166 if ( wgl_ctx[i].ctx == NULL ) {
167 wgl_ctx[i].ctx =
168 WMesaCreateContext(hdc, NULL, (GLboolean)GL_TRUE,
169 (GLboolean) (pfd[curPFD-1].doubleBuffered ?
170 GL_TRUE : GL_FALSE),
171 (GLboolean)(pfd[curPFD-1].pfd.cAlphaBits ?
172 GL_TRUE : GL_FALSE) );
173 if (wgl_ctx[i].ctx == NULL)
174 break;
175 ctx_count++;
176 return ((HGLRC)wgl_ctx[i].ctx);
177 }
178 }
179 SetLastError(0);
180 return(NULL);
181 }
182
183 WINGDIAPI BOOL GLAPIENTRY wglDeleteContext(HGLRC hglrc)
184 {
185 int i;
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;
191 ctx_count--;
192 return(TRUE);
193 }
194 }
195 SetLastError(0);
196 return(FALSE);
197 }
198
199 WINGDIAPI HGLRC GLAPIENTRY wglGetCurrentContext(VOID)
200 {
201 if (ctx_current < 0)
202 return 0;
203 else
204 return (HGLRC) wgl_ctx[ctx_current].ctx;
205 }
206
207 WINGDIAPI HDC GLAPIENTRY wglGetCurrentDC(VOID)
208 {
209 return CurrentHDC;
210 }
211
212 WINGDIAPI BOOL GLAPIENTRY wglMakeCurrent(HDC hdc, HGLRC hglrc)
213 {
214 int i;
215
216 CurrentHDC = hdc;
217
218 if (!hdc || !hglrc) {
219 WMesaMakeCurrent(NULL, NULL);
220 ctx_current = -1;
221 return TRUE;
222 }
223
224 for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ ) {
225 if ( wgl_ctx[i].ctx == (WMesaContext) hglrc ) {
226 WMesaMakeCurrent( (WMesaContext) hglrc, hdc );
227 ctx_current = i;
228 return TRUE;
229 }
230 }
231 return FALSE;
232 }
233
234
235 WINGDIAPI int GLAPIENTRY wglChoosePixelFormat(HDC hdc,
236 CONST
237 PIXELFORMATDESCRIPTOR *ppfd)
238 {
239 int i,best = -1,bestdelta = 0x7FFFFFFF,delta;
240 (void) hdc;
241
242 if(ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR) || ppfd->nVersion != 1)
243 {
244 SetLastError(0);
245 return(0);
246 }
247 for(i = 0; i < npfd;i++)
248 {
249 delta = 0;
250 if(
251 (ppfd->dwFlags & PFD_DRAW_TO_WINDOW) &&
252 !(pfd[i].pfd.dwFlags & PFD_DRAW_TO_WINDOW))
253 continue;
254 if(
255 (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) &&
256 !(pfd[i].pfd.dwFlags & PFD_DRAW_TO_BITMAP))
257 continue;
258 if(
259 (ppfd->dwFlags & PFD_SUPPORT_GDI) &&
260 !(pfd[i].pfd.dwFlags & PFD_SUPPORT_GDI))
261 continue;
262 if(
263 (ppfd->dwFlags & PFD_SUPPORT_OPENGL) &&
264 !(pfd[i].pfd.dwFlags & PFD_SUPPORT_OPENGL))
265 continue;
266 if(
267 !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
268 ((ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
269 (pfd[i].pfd.dwFlags & PFD_DOUBLEBUFFER)))
270 continue;
271 if(
272 !(ppfd->dwFlags & PFD_STEREO_DONTCARE) &&
273 ((ppfd->dwFlags & PFD_STEREO) !=
274 (pfd[i].pfd.dwFlags & PFD_STEREO)))
275 continue;
276 if(ppfd->iPixelType != pfd[i].pfd.iPixelType)
277 delta++;
278 if(ppfd->cAlphaBits != pfd[i].pfd.cAlphaBits)
279 delta++;
280 if(delta < bestdelta)
281 {
282 best = i + 1;
283 bestdelta = delta;
284 if(bestdelta == 0)
285 break;
286 }
287 }
288 if(best == -1)
289 {
290 SetLastError(0);
291 return(0);
292 }
293 return(best);
294 }
295
296 WINGDIAPI int GLAPIENTRY wglDescribePixelFormat(HDC hdc,
297 int iPixelFormat,
298 UINT nBytes,
299 LPPIXELFORMATDESCRIPTOR ppfd)
300 {
301 (void) hdc;
302
303 if(ppfd == NULL)
304 return(npfd);
305 if(iPixelFormat < 1 || iPixelFormat > npfd ||
306 nBytes != sizeof(PIXELFORMATDESCRIPTOR))
307 {
308 SetLastError(0);
309 return(0);
310 }
311 *ppfd = pfd[iPixelFormat - 1].pfd;
312 return(npfd);
313 }
314
315 WINGDIAPI PROC GLAPIENTRY wglGetProcAddress(LPCSTR lpszProc)
316 {
317 PROC p = (PROC) _glapi_get_proc_address((const char *) lpszProc);
318 if (p)
319 return p;
320
321 SetLastError(0);
322 return(NULL);
323 }
324
325 WINGDIAPI int GLAPIENTRY wglGetPixelFormat(HDC hdc)
326 {
327 (void) hdc;
328 if(curPFD == 0) {
329 SetLastError(0);
330 return(0);
331 }
332 return(curPFD);
333 }
334
335 WINGDIAPI BOOL GLAPIENTRY wglSetPixelFormat(HDC hdc,int iPixelFormat,
336 PIXELFORMATDESCRIPTOR *ppfd)
337 {
338 (void) hdc;
339
340 if(iPixelFormat < 1 || iPixelFormat > npfd ||
341 ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR)) {
342 SetLastError(0);
343 return(FALSE);
344 }
345 curPFD = iPixelFormat;
346 return(TRUE);
347 }
348
349 WINGDIAPI BOOL GLAPIENTRY wglSwapBuffers(HDC hdc)
350 {
351 WMesaSwapBuffers(hdc);
352 return TRUE;
353 }
354
355 static FIXED FixedFromDouble(double d)
356 {
357 long l = (long) (d * 65536L);
358 return *(FIXED *) (void *) &l;
359 }
360
361
362 /*
363 ** This is cribbed from FX/fxwgl.c, and seems to implement support
364 ** for bitmap fonts where the wglUseFontBitmapsA() code implements
365 ** support for outline fonts. In combination they hopefully give
366 ** fairly generic support for fonts.
367 */
368 static BOOL wglUseFontBitmaps_FX(HDC fontDevice, DWORD firstChar,
369 DWORD numChars, DWORD listBase)
370 {
371 #define VERIFY(a) a
372
373 TEXTMETRIC metric;
374 BITMAPINFO *dibInfo;
375 HDC bitDevice;
376 COLORREF tempColor;
377 int i;
378
379 VERIFY(GetTextMetrics(fontDevice, &metric));
380
381 dibInfo = (BITMAPINFO *) calloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD), 1);
382 dibInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
383 dibInfo->bmiHeader.biPlanes = 1;
384 dibInfo->bmiHeader.biBitCount = 1;
385 dibInfo->bmiHeader.biCompression = BI_RGB;
386
387 bitDevice = CreateCompatibleDC(fontDevice);
388
389 // Swap fore and back colors so the bitmap has the right polarity
390 tempColor = GetBkColor(bitDevice);
391 SetBkColor(bitDevice, GetTextColor(bitDevice));
392 SetTextColor(bitDevice, tempColor);
393
394 // Place chars based on base line
395 VERIFY(SetTextAlign(bitDevice, TA_BASELINE) != GDI_ERROR ? 1 : 0);
396
397 for(i = 0; i < (int)numChars; i++) {
398 SIZE size;
399 char curChar;
400 int charWidth,charHeight,bmapWidth,bmapHeight,numBytes,res;
401 HBITMAP bitObject;
402 HGDIOBJ origBmap;
403 unsigned char *bmap;
404
405 curChar = (char)(i + firstChar);
406
407 // Find how high/wide this character is
408 VERIFY(GetTextExtentPoint32(bitDevice, &curChar, 1, &size));
409
410 // Create the output bitmap
411 charWidth = size.cx;
412 charHeight = size.cy;
413 // Round up to the next multiple of 32 bits
414 bmapWidth = ((charWidth + 31) / 32) * 32;
415 bmapHeight = charHeight;
416 bitObject = CreateCompatibleBitmap(bitDevice,
417 bmapWidth,
418 bmapHeight);
419 //VERIFY(bitObject);
420
421 // Assign the output bitmap to the device
422 origBmap = SelectObject(bitDevice, bitObject);
423 (void) VERIFY(origBmap);
424
425 VERIFY( PatBlt( bitDevice, 0, 0, bmapWidth, bmapHeight,BLACKNESS ) );
426
427 // Use our source font on the device
428 VERIFY(SelectObject(bitDevice, GetCurrentObject(fontDevice,OBJ_FONT)));
429
430 // Draw the character
431 VERIFY(TextOut(bitDevice, 0, metric.tmAscent, &curChar, 1));
432
433 // Unselect our bmap object
434 VERIFY(SelectObject(bitDevice, origBmap));
435
436 // Convert the display dependant representation to a 1 bit deep DIB
437 numBytes = (bmapWidth * bmapHeight) / 8;
438 bmap = malloc(numBytes);
439 dibInfo->bmiHeader.biWidth = bmapWidth;
440 dibInfo->bmiHeader.biHeight = bmapHeight;
441 res = GetDIBits(bitDevice, bitObject, 0, bmapHeight, bmap,
442 dibInfo,
443 DIB_RGB_COLORS);
444 //VERIFY(res);
445
446 // Create the GL object
447 glNewList(i + listBase, GL_COMPILE);
448 glBitmap(bmapWidth, bmapHeight, 0.0, (GLfloat)metric.tmDescent,
449 (GLfloat)charWidth, 0.0,
450 bmap);
451 glEndList();
452 // CheckGL();
453
454 // Destroy the bmap object
455 DeleteObject(bitObject);
456
457 // Deallocate the bitmap data
458 free(bmap);
459 }
460
461 // Destroy the DC
462 VERIFY(DeleteDC(bitDevice));
463
464 free(dibInfo);
465
466 return TRUE;
467 #undef VERIFY
468 }
469
470 WINGDIAPI BOOL GLAPIENTRY wglUseFontBitmapsA(HDC hdc, DWORD first,
471 DWORD count, DWORD listBase)
472 {
473 int i;
474 GLuint font_list;
475 DWORD size;
476 GLYPHMETRICS gm;
477 HANDLE hBits;
478 LPSTR lpBits;
479 MAT2 mat;
480 int success = TRUE;
481
482 if (count == 0)
483 return FALSE;
484
485 font_list = listBase;
486
487 mat.eM11 = FixedFromDouble(1);
488 mat.eM12 = FixedFromDouble(0);
489 mat.eM21 = FixedFromDouble(0);
490 mat.eM22 = FixedFromDouble(-1);
491
492 memset(&gm,0,sizeof(gm));
493
494 /*
495 ** If we can't get the glyph outline, it may be because this is a fixed
496 ** font. Try processing it that way.
497 */
498 if( GetGlyphOutline(hdc, first, GGO_BITMAP, &gm, 0, NULL, &mat)
499 == GDI_ERROR ) {
500 return wglUseFontBitmaps_FX( hdc, first, count, listBase );
501 }
502
503 /*
504 ** Otherwise process all desired characters.
505 */
506 for (i = 0; i < (int)count; i++) {
507 DWORD err;
508
509 glNewList( font_list+i, GL_COMPILE );
510
511 /* allocate space for the bitmap/outline */
512 size = GetGlyphOutline(hdc, first + i, GGO_BITMAP,
513 &gm, 0, NULL, &mat);
514 if (size == GDI_ERROR) {
515 glEndList( );
516 err = GetLastError();
517 success = FALSE;
518 continue;
519 }
520
521 hBits = GlobalAlloc(GHND, size+1);
522 lpBits = GlobalLock(hBits);
523
524 err =
525 GetGlyphOutline(hdc, /* handle to device context */
526 first + i, /* character to query */
527 GGO_BITMAP, /* format of data to return */
528 &gm, /* ptr to structure for metrics*/
529 size, /* size of buffer for data */
530 lpBits, /* pointer to buffer for data */
531 &mat /* pointer to transformation */
532 /* matrix structure */
533 );
534
535 if (err == GDI_ERROR) {
536 GlobalUnlock(hBits);
537 GlobalFree(hBits);
538
539 glEndList( );
540 err = GetLastError();
541 success = FALSE;
542 continue;
543 }
544
545 glBitmap(gm.gmBlackBoxX,gm.gmBlackBoxY,
546 (GLfloat)-gm.gmptGlyphOrigin.x,
547 (GLfloat)gm.gmptGlyphOrigin.y,
548 (GLfloat)gm.gmCellIncX,
549 (GLfloat)gm.gmCellIncY,
550 (const GLubyte * )lpBits);
551
552 GlobalUnlock(hBits);
553 GlobalFree(hBits);
554
555 glEndList( );
556 }
557
558 return success;
559 }
560
561
562
563 /* NOT IMPLEMENTED YET */
564 WINGDIAPI BOOL GLAPIENTRY wglCopyContext(HGLRC hglrcSrc,
565 HGLRC hglrcDst,
566 UINT mask)
567 {
568 (void) hglrcSrc; (void) hglrcDst; (void) mask;
569 return(FALSE);
570 }
571
572 WINGDIAPI HGLRC GLAPIENTRY wglCreateLayerContext(HDC hdc,
573 int iLayerPlane)
574 {
575 (void) hdc; (void) iLayerPlane;
576 SetLastError(0);
577 return(NULL);
578 }
579
580 WINGDIAPI BOOL GLAPIENTRY wglShareLists(HGLRC hglrc1,
581 HGLRC hglrc2)
582 {
583 (void) hglrc1; (void) hglrc2;
584 return(TRUE);
585 }
586
587
588 WINGDIAPI BOOL GLAPIENTRY wglUseFontBitmapsW(HDC hdc,
589 DWORD first,
590 DWORD count,
591 DWORD listBase)
592 {
593 (void) hdc; (void) first; (void) count; (void) listBase;
594 return FALSE;
595 }
596
597 WINGDIAPI BOOL GLAPIENTRY wglUseFontOutlinesA(HDC hdc,
598 DWORD first,
599 DWORD count,
600 DWORD listBase,
601 FLOAT deviation,
602 FLOAT extrusion,
603 int format,
604 LPGLYPHMETRICSFLOAT lpgmf)
605 {
606 (void) hdc; (void) first; (void) count;
607 (void) listBase; (void) deviation; (void) extrusion; (void) format;
608 (void) lpgmf;
609 SetLastError(0);
610 return(FALSE);
611 }
612
613 WINGDIAPI BOOL GLAPIENTRY wglUseFontOutlinesW(HDC hdc,
614 DWORD first,
615 DWORD count,
616 DWORD listBase,
617 FLOAT deviation,
618 FLOAT extrusion,
619 int format,
620 LPGLYPHMETRICSFLOAT lpgmf)
621 {
622 (void) hdc; (void) first; (void) count;
623 (void) listBase; (void) deviation; (void) extrusion; (void) format;
624 (void) lpgmf;
625 SetLastError(0);
626 return(FALSE);
627 }
628
629 WINGDIAPI BOOL GLAPIENTRY wglDescribeLayerPlane(HDC hdc,
630 int iPixelFormat,
631 int iLayerPlane,
632 UINT nBytes,
633 LPLAYERPLANEDESCRIPTOR plpd)
634 {
635 (void) hdc; (void) iPixelFormat; (void) iLayerPlane;
636 (void) nBytes; (void) plpd;
637 SetLastError(0);
638 return(FALSE);
639 }
640
641 WINGDIAPI int GLAPIENTRY wglSetLayerPaletteEntries(HDC hdc,
642 int iLayerPlane,
643 int iStart,
644 int cEntries,
645 CONST COLORREF *pcr)
646 {
647 (void) hdc; (void) iLayerPlane; (void) iStart;
648 (void) cEntries; (void) pcr;
649 SetLastError(0);
650 return(0);
651 }
652
653 WINGDIAPI int GLAPIENTRY wglGetLayerPaletteEntries(HDC hdc,
654 int iLayerPlane,
655 int iStart,
656 int cEntries,
657 COLORREF *pcr)
658 {
659 (void) hdc; (void) iLayerPlane; (void) iStart; (void) cEntries; (void) pcr;
660 SetLastError(0);
661 return(0);
662 }
663
664 WINGDIAPI BOOL GLAPIENTRY wglRealizeLayerPalette(HDC hdc,
665 int iLayerPlane,
666 BOOL bRealize)
667 {
668 (void) hdc; (void) iLayerPlane; (void) bRealize;
669 SetLastError(0);
670 return(FALSE);
671 }
672
673 WINGDIAPI BOOL GLAPIENTRY wglSwapLayerBuffers(HDC hdc,
674 UINT fuPlanes)
675 {
676 (void) hdc; (void) fuPlanes;
677 SetLastError(0);
678 return(FALSE);
679 }
680
681 WINGDIAPI const char * GLAPIENTRY wglGetExtensionsStringARB(HDC hdc)
682 {
683 return "WGL_ARB_extensions_string";
684 }