update source for tizen_2.1
[sdk/emulator/qemu.git] / tizen / src / hw / gloffscreen_wgl.c
1 /*
2  *  Offscreen OpenGL abstraction layer - WGL (windows) specific
3  *
4  *  Copyright (c) 2010 Intel Corporation
5  *  Written by: 
6  *    Gordon Williams <gordon.williams@collabora.co.uk>
7  *    Ian Molton <ian.molton@collabora.co.uk>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 #ifdef _WIN32
28 #include "gloffscreen.h"
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <windows.h>
35 #include <wingdi.h>
36 #include <GL/gl.h>
37 #include <GL/glext.h>
38 #include "GL/wglext.h"
39
40 #define MAX_ATTRIBS 12 /*Max attributes of pixel format we need*/
41
42 #ifdef MANGLE_OPENGL_SYMBOLS
43 #include "gl_mangled.h"
44 #endif
45
46 /* In Windows, you must create a window *before* you can create a pbuffer or
47  * get a context. So we create a hidden Window on startup (see glo_init/GloMain).
48  *
49  * Also, you can't share contexts that have different pixel formats, so we can't just
50  * create a new context from the window. We must create a whole new PBuffer just for
51  * a context :(
52  */
53
54 struct GloMain {
55     HINSTANCE             hInstance;
56     HDC                   hDC;
57     HWND                  hWnd; /* Our hidden window */
58     HGLRC                 hContext;
59 };
60 struct GloMain glo;
61 int glo_inited = 0;
62 int Render_texture_support = 0;
63
64 struct _GloContext {
65     int                   formatFlags;
66
67     /* Pixel format returned by wglChoosePixelFormat */
68     int                   wglPixelFormat;
69     /* We need a pbuffer to make a context of the right pixelformat :( */
70     HPBUFFERARB           hPBuffer; 
71     HDC                   hDC;
72     HGLRC                 hContext;
73 };
74
75 struct _GloSurface {
76     GLuint                width;
77     GLuint                height;  
78
79     GloContext           *context;
80     HPBUFFERARB           hPBuffer;
81     HDC                   hDC;
82 };
83
84 #define GLO_WINDOW_CLASS "QEmuGLClass"
85 #define DEFAULT_DEPTH_BUFFER (16)
86
87 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
88 PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB;
89 PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB;
90 PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB;
91 PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB;
92 PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB;
93 PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB;
94 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;
95
96 /* ------------------------------------------------------------------------ */
97
98 extern const char *glo_glXQueryExtensionsString(void);
99
100 extern void glo_surface_getcontents_readpixels(int formatFlags, int stride,
101                                     int bpp, int width, int height, void *data);
102
103 /* ------------------------------------------------------------------------ */
104
105 int glo_initialised(void) {
106     return glo_inited;
107 }
108
109 /* Sanity test of the host GL capabilities to see whether the gl offscreen
110  * could be well supported
111  */
112 int glo_sanity_test (void) {
113     PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
114     PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB;
115     PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB;
116     PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB;
117     PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB;
118
119     wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
120     wglGetPbufferDCARB = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB");
121     wglReleasePbufferDCARB = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB");
122     wglCreatePbufferARB = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB");
123     wglDestroyPbufferARB = (PFNWGLDESTROYPBUFFERARBPROC)wglGetProcAddress("wglDestroyPbufferARB");
124     if (!wglChoosePixelFormatARB ||
125         !wglGetPbufferDCARB ||
126         !wglReleasePbufferDCARB ||
127         !wglCreatePbufferARB ||
128         !wglDestroyPbufferARB) {
129         fprintf (stderr, "Unable to load the required WGL extensions\n");
130         return 1;
131     }
132     // check the shader support. It is for mcompositor to run.
133
134     if (!wglGetProcAddress("glShaderSource")) {
135         fprintf (stderr, "Unable to find shader support\n");
136         return 1;
137     }
138     return 0;
139 }
140
141 /* Initialise gloffscreen */
142 int glo_init(void) {
143     WNDCLASSEX wcx;
144     PIXELFORMATDESCRIPTOR pfd;
145         char *ext_str;
146
147     if (glo_inited) {
148         printf( "gloffscreen already inited\n" );
149         //exit( EXIT_FAILURE );
150                 return 1;
151     }
152
153     glo.hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
154
155     wcx.cbSize = sizeof(wcx);
156     wcx.style = 0;
157     wcx.lpfnWndProc = DefWindowProc;
158     wcx.cbClsExtra = 0;
159     wcx.cbWndExtra = 0;
160     wcx.hInstance = glo.hInstance;
161     wcx.hIcon = NULL;
162     wcx.hCursor = NULL;
163     wcx.hbrBackground = NULL;
164     wcx.lpszMenuName =  NULL;
165     wcx.lpszClassName = GLO_WINDOW_CLASS;
166     wcx.hIconSm = NULL;
167     RegisterClassEx(&wcx);
168     glo.hWnd = CreateWindow(
169         GLO_WINDOW_CLASS,
170         "QEmuGL",
171         0,0,0,0,0,
172         (HWND)NULL, (HMENU)NULL,
173         glo.hInstance,
174         (LPVOID) NULL);
175
176     if (!glo.hWnd) {
177         printf( "Unable to create window\n" );
178         //exit( EXIT_FAILURE );
179                 return 1;
180     }
181     glo.hDC = GetDC(glo.hWnd);
182
183     memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
184     pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
185     pfd.nVersion = 1;
186     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
187     pfd.iPixelType = PFD_TYPE_RGBA;
188     pfd.cColorBits = 24;
189     pfd.iLayerType = PFD_MAIN_PLANE;
190     unsigned int pixelFormat = ChoosePixelFormat(glo.hDC, &pfd);
191     DescribePixelFormat(glo.hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
192     if (!SetPixelFormat(glo.hDC, pixelFormat, &pfd))
193         return 1;
194
195     glo.hContext = wglCreateContext(glo.hDC);
196     if (glo.hContext == NULL) {
197         printf( "Unable to create GL context\n" );
198         //exit( EXIT_FAILURE );
199                 return 1;
200     }
201     wglMakeCurrent(glo.hDC, glo.hContext);
202
203     // FIXME GW
204     // Need to share lists AND copy state
205
206     // load in the extensions we need
207     //const char        *ext = wglGetExtensionsStringARB(hdc);
208     //"WGL_ARB_pixel_format" "WGL_ARB_pbuffer"
209
210     wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
211     wglGetPbufferDCARB = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB");
212     wglReleasePbufferDCARB = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB");
213     wglCreatePbufferARB = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB");
214     wglDestroyPbufferARB = (PFNWGLDESTROYPBUFFERARBPROC)wglGetProcAddress("wglDestroyPbufferARB");
215     wglBindTexImageARB = (PFNWGLBINDTEXIMAGEARBPROC)wglGetProcAddress("wglBindTexImageARB");
216     wglReleaseTexImageARB = (PFNWGLRELEASETEXIMAGEARBPROC)wglGetProcAddress("wglReleaseTexImageARB");
217         wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
218
219     if (!wglChoosePixelFormatARB ||
220         !wglGetPbufferDCARB ||
221         !wglReleasePbufferDCARB ||
222         !wglCreatePbufferARB ||
223         !wglDestroyPbufferARB ||
224                 !wglGetExtensionsStringARB ) {
225         printf( "Unable to load the required WGL extensions\n" );
226         //exit( EXIT_FAILURE );
227                 return 1;
228     }
229
230         if(wglGetExtensionsStringARB)
231         {
232                 ext_str = wglGetExtensionsStringARB(wglGetCurrentDC());
233                 if(ext_str == NULL)
234                 {
235                         fprintf(stderr, "Failed tp get extensions string!\n");
236                         Render_texture_support = 0;
237                 }
238                 else
239                 {
240                         if(strstr(ext_str, "WGL_ARB_render_texture") != NULL)
241                         {
242                                 fprintf(stdout, "WGL_ARB_render_texture supported!\n");
243                                 Render_texture_support = 1;
244                         }
245                         else
246                         {
247                                 fprintf(stderr, "WGL_ARB_render_texture not supported!\n");
248                                 Render_texture_support = 0;
249                         }
250                 }
251         }
252
253
254     if ( !wglBindTexImageARB ||
255          !wglReleaseTexImageARB )
256         fprintf (stderr, "Warning: no [Bind|Release]TexImageARB extensions.\n");
257
258     glo_inited = 1;
259
260         return 0;
261 }
262
263 /* Uninitialise gloffscreen */
264 void glo_kill(void) {
265     if (glo.hContext) {
266         wglMakeCurrent(NULL, NULL);
267         wglDeleteContext(glo.hContext);
268         glo.hContext = NULL;
269     }
270     if (glo.hDC) {
271         ReleaseDC(glo.hWnd, glo.hDC);
272         glo.hDC = NULL;
273     }
274     if (glo.hWnd) {
275         DestroyWindow(glo.hWnd);
276         glo.hWnd = NULL;
277     }
278     UnregisterClass(GLO_WINDOW_CLASS, glo.hInstance);
279 }
280
281 const char *glo_glXQueryExtensionsString(void) {
282     return "";
283 }
284
285
286 static const char *STANDARD_GL_FUNCTIONS ={
287 /* Miscellaneous */
288 "glClearIndex\0"                       
289 "glClearColor\0"                       
290 "glClear\0"                            
291 "glIndexMask\0"                        
292 "glColorMask\0"                        
293 "glAlphaFunc\0"                        
294 "glBlendFunc\0"                        
295 "glLogicOp\0"                          
296 "glCullFace\0"                         
297 "glFrontFace\0"                        
298 "glPointSize\0"                        
299 "glLineWidth\0"                        
300 "glLineStipple\0"                      
301 "glPolygonMode\0"                      
302 "glPolygonOffset\0"                    
303 "glPolygonStipple\0"                   
304 "glGetPolygonStipple\0"                
305 "glEdgeFlag\0"                         
306 "glEdgeFlagv\0"                        
307 "glScissor\0"                          
308 "glClipPlane\0"                        
309 "glGetClipPlane\0"                     
310 "glDrawBuffer\0"                       
311 "glReadBuffer\0"                       
312 "glEnable\0"                           
313 "glDisable\0"                          
314 "glIsEnabled\0"                        
315 "glEnableClientState\0"                
316 "glDisableClientState\0"               
317 "glGetBooleanv\0"                      
318 "glGetDoublev\0"                       
319 "glGetFloatv\0"                        
320 "glGetIntegerv\0"                      
321 "glPushAttrib\0"                       
322 "glPopAttrib\0"                        
323 "glPushClientAttrib\0"                 
324 "glPopClientAttrib\0"                  
325 "glRenderMode\0"                       
326 "glGetError\0"                         
327 "glGetString\0"                        
328 "glFinish\0"                           
329 "glFlush\0"                            
330 "glHint\0"                             
331 /* Depth Buffer */
332 "glClearDepth\0"                       
333 "glDepthFunc\0"                        
334 "glDepthMask\0"                        
335 "glDepthRange\0"                       
336 /* Accumulation Buffer */
337 "glClearAccum\0"                       
338 "glAccum\0"                            
339 /* Transformation */
340 "glMatrixMode\0"                       
341 "glOrtho\0"                            
342 "glFrustum\0"                          
343 "glViewport\0"                         
344 "glPushMatrix\0"                       
345 "glPopMatrix\0"                        
346 "glLoadIdentity\0"                     
347 "glLoadMatrixd\0"                      
348 "glLoadMatrixf\0"                      
349 "glMultMatrixd\0"                      
350 "glMultMatrixf\0"                      
351 "glRotated\0"                          
352 "glRotatef\0"                          
353 "glScaled\0"                           
354 "glScalef\0"                           
355 "glTranslated\0"                       
356 "glTranslatef\0"                       
357 /* Display Lists */
358 "glIsList\0"                           
359 "glDeleteLists\0"                      
360 "glGenLists\0"                         
361 "glNewList\0"                          
362 "glEndList\0"                          
363 "glCallList\0"                         
364 "glCallLists\0"                        
365 "glListBase\0"                         
366 /* Drawing Functions */
367 "glBegin\0"                            
368 "glEnd\0"                              
369 "glVertex2d\0"                         
370 "glVertex2f\0"                         
371 "glVertex2i\0"                         
372 "glVertex2s\0"                         
373 "glVertex3d\0"                         
374 "glVertex3f\0"                         
375 "glVertex3i\0"                         
376 "glVertex3s\0"                         
377 "glVertex4d\0"                         
378 "glVertex4f\0"                         
379 "glVertex4i\0"                         
380 "glVertex4s\0"                         
381 "glVertex2dv\0"                        
382 "glVertex2fv\0"                        
383 "glVertex2iv\0"                        
384 "glVertex2sv\0"                        
385 "glVertex3dv\0"                        
386 "glVertex3fv\0"                        
387 "glVertex3iv\0"                        
388 "glVertex3sv\0"                        
389 "glVertex4dv\0"                        
390 "glVertex4fv\0"                        
391 "glVertex4iv\0"                        
392 "glVertex4sv\0"                        
393 "glNormal3b\0"                         
394 "glNormal3d\0"                         
395 "glNormal3f\0"                         
396 "glNormal3i\0"                         
397 "glNormal3s\0"                         
398 "glNormal3bv\0"                        
399 "glNormal3dv\0"                        
400 "glNormal3fv\0"                        
401 "glNormal3iv\0"                        
402 "glNormal3sv\0"                        
403 "glIndexd\0"                           
404 "glIndexf\0"                           
405 "glIndexi\0"                           
406 "glIndexs\0"                           
407 "glIndexub\0"                          
408 "glIndexdv\0"                          
409 "glIndexfv\0"                          
410 "glIndexiv\0"                          
411 "glIndexsv\0"                          
412 "glIndexubv\0"                         
413 "glColor3b\0"                          
414 "glColor3d\0"                          
415 "glColor3f\0"                          
416 "glColor3i\0"                          
417 "glColor3s\0"                          
418 "glColor3ub\0"                         
419 "glColor3ui\0"                         
420 "glColor3us\0"                         
421 "glColor4b\0"                          
422 "glColor4d\0"                          
423 "glColor4f\0"                          
424 "glColor4i\0"                          
425 "glColor4s\0"                          
426 "glColor4ub\0"                         
427 "glColor4ui\0"                         
428 "glColor4us\0"                         
429 "glColor3bv\0"                         
430 "glColor3dv\0"                         
431 "glColor3fv\0"                         
432 "glColor3iv\0"                         
433 "glColor3sv\0"                         
434 "glColor3ubv\0"                        
435 "glColor3uiv\0"                        
436 "glColor3usv\0"                        
437 "glColor4bv\0"                         
438 "glColor4dv\0"                         
439 "glColor4fv\0"                         
440 "glColor4iv\0"                         
441 "glColor4sv\0"                         
442 "glColor4ubv\0"                        
443 "glColor4uiv\0"                        
444 "glColor4usv\0"                        
445 "glTexCoord1d\0"                       
446 "glTexCoord1f\0"                       
447 "glTexCoord1i\0"                       
448 "glTexCoord1s\0"                       
449 "glTexCoord2d\0"                       
450 "glTexCoord2f\0"                       
451 "glTexCoord2i\0"                       
452 "glTexCoord2s\0"                       
453 "glTexCoord3d\0"                       
454 "glTexCoord3f\0"                       
455 "glTexCoord3i\0"                       
456 "glTexCoord3s\0"                       
457 "glTexCoord4d\0"                       
458 "glTexCoord4f\0"                       
459 "glTexCoord4i\0"                       
460 "glTexCoord4s\0"                       
461 "glTexCoord1dv\0"                      
462 "glTexCoord1fv\0"                      
463 "glTexCoord1iv\0"                      
464 "glTexCoord1sv\0"                      
465 "glTexCoord2dv\0"                      
466 "glTexCoord2fv\0"                      
467 "glTexCoord2iv\0"                      
468 "glTexCoord2sv\0"                      
469 "glTexCoord3dv\0"                      
470 "glTexCoord3fv\0"                      
471 "glTexCoord3iv\0"                      
472 "glTexCoord3sv\0"                      
473 "glTexCoord4dv\0"                      
474 "glTexCoord4fv\0"                      
475 "glTexCoord4iv\0"                      
476 "glTexCoord4sv\0"                      
477 "glRasterPos2d\0"                      
478 "glRasterPos2f\0"                      
479 "glRasterPos2i\0"                      
480 "glRasterPos2s\0"                      
481 "glRasterPos3d\0"                      
482 "glRasterPos3f\0"                      
483 "glRasterPos3i\0"                      
484 "glRasterPos3s\0"                      
485 "glRasterPos4d\0"                      
486 "glRasterPos4f\0"                      
487 "glRasterPos4i\0"                      
488 "glRasterPos4s\0"                      
489 "glRasterPos2dv\0"                     
490 "glRasterPos2fv\0"                     
491 "glRasterPos2iv\0"                     
492 "glRasterPos2sv\0"                     
493 "glRasterPos3dv\0"                     
494 "glRasterPos3fv\0"                     
495 "glRasterPos3iv\0"                     
496 "glRasterPos3sv\0"                     
497 "glRasterPos4dv\0"                     
498 "glRasterPos4fv\0"                     
499 "glRasterPos4iv\0"                     
500 "glRasterPos4sv\0"                     
501 "glRectd\0"                            
502 "glRectf\0"                            
503 "glRecti\0"                            
504 "glRects\0"                            
505 "glRectdv\0"                           
506 "glRectfv\0"                           
507 "glRectiv\0"                           
508 "glRectsv\0"                           
509 /* Lighting */
510 "glShadeModel\0"                       
511 "glLightf\0"                           
512 "glLighti\0"                           
513 "glLightfv\0"                          
514 "glLightiv\0"                          
515 "glGetLightfv\0"                       
516 "glGetLightiv\0"                       
517 "glLightModelf\0"                      
518 "glLightModeli\0"                      
519 "glLightModelfv\0"                     
520 "glLightModeliv\0"                     
521 "glMaterialf\0"                        
522 "glMateriali\0"                        
523 "glMaterialfv\0"                       
524 "glMaterialiv\0"                       
525 "glGetMaterialfv\0"                    
526 "glGetMaterialiv\0"                    
527 "glColorMaterial\0"                    
528 /* Raster functions */
529 "glPixelZoom\0"                        
530 "glPixelStoref\0"                      
531 "glPixelStorei\0"                      
532 "glPixelTransferf\0"                   
533 "glPixelTransferi\0"                   
534 "glPixelMapfv\0"                       
535 "glPixelMapuiv\0"                      
536 "glPixelMapusv\0"                      
537 "glGetPixelMapfv\0"                    
538 "glGetPixelMapuiv\0"                   
539 "glGetPixelMapusv\0"                   
540 "glBitmap\0"                           
541 "glReadPixels\0"                       
542 "glDrawPixels\0"                       
543 "glCopyPixels\0"                       
544 /* Stenciling */
545 "glStencilFunc\0"                      
546 "glStencilMask\0"                      
547 "glStencilOp\0"                        
548 "glClearStencil\0"                     
549 /* Texture mapping */
550 "glTexGend\0"                          
551 "glTexGenf\0"                          
552 "glTexGeni\0"                          
553 "glTexGendv\0"                         
554 "glTexGenfv\0"                         
555 "glTexGeniv\0"                         
556 "glGetTexGendv\0"                      
557 "glGetTexGenfv\0"                      
558 "glGetTexGeniv\0"                      
559 "glTexEnvf\0"                          
560 "glTexEnvi\0"                          
561 "glTexEnvfv\0"                         
562 "glTexEnviv\0"                         
563 "glGetTexEnvfv\0"                      
564 "glGetTexEnviv\0"                      
565 "glTexParameterf\0"                    
566 "glTexParameteri\0"                    
567 "glTexParameterfv\0"                   
568 "glTexParameteriv\0"                   
569 "glGetTexParameterfv\0"                
570 "glGetTexParameteriv\0"                
571 "glGetTexLevelParameterfv\0"           
572 "glGetTexLevelParameteriv\0"           
573 "glTexImage1D\0"                       
574 "glTexImage2D\0"                       
575 "glGetTexImage\0"                      
576 /* Evaluators */
577 "glMap1d\0"                            
578 "glMap1f\0"                            
579 "glMap2d\0"                            
580 "glMap2f\0"                            
581 "glGetMapdv\0"                         
582 "glGetMapfv\0"                         
583 "glGetMapiv\0"                         
584 "glEvalCoord1d\0"                      
585 "glEvalCoord1f\0"                      
586 "glEvalCoord1dv\0"                     
587 "glEvalCoord1fv\0"                     
588 "glEvalCoord2d\0"                      
589 "glEvalCoord2f\0"                      
590 "glEvalCoord2dv\0"                     
591 "glEvalCoord2fv\0"                     
592 "glMapGrid1d\0"                        
593 "glMapGrid1f\0"                        
594 "glMapGrid2d\0"                        
595 "glMapGrid2f\0"                        
596 "glEvalPoint1\0"                       
597 "glEvalPoint2\0"                       
598 "glEvalMesh1\0"                        
599 "glEvalMesh2\0"                        
600 /* Fog */
601 "glFogf\0"                             
602 "glFogi\0"                             
603 "glFogfv\0"                            
604 "glFogiv\0"                            
605 /* Selection and Feedback */
606 "glFeedbackBuffer\0"                   
607 "glPassThrough\0"                      
608 "glSelectBuffer\0"                     
609 "glInitNames\0"                        
610 "glLoadName\0"                         
611 "glPushName\0"                         
612 "glPopName\0"                          
613 /* 1.1 functions */
614 /* texture objects */
615 "glGenTextures\0"                      
616 "glDeleteTextures\0"                   
617 "glBindTexture\0"                      
618 "glPrioritizeTextures\0"               
619 "glAreTexturesResident\0"              
620 "glIsTexture\0"                        
621 /* texture mapping */
622 "glTexSubImage1D\0"                    
623 "glTexSubImage2D\0"                    
624 "glCopyTexImage1D\0"                   
625 "glCopyTexImage2D\0"                   
626 "glCopyTexSubImage1D\0"                
627 "glCopyTexSubImage2D\0"                
628 /* vertex arrays */
629 "glVertexPointer\0"                    
630 "glNormalPointer\0"                    
631 "glColorPointer\0"                     
632 "glIndexPointer\0"                     
633 "glTexCoordPointer\0"                  
634 "glEdgeFlagPointer\0"                  
635 "glGetPointerv\0"                      
636 "glArrayElement\0"                     
637 "glDrawArrays\0"                       
638 "glDrawElements\0"                     
639 "glInterleavedArrays\0"                
640 /* GLX */
641 "glXChooseVisual\0"
642 "glXQueryExtensionsString\0"
643 "glXQueryServerString\0"
644 "glXGetClientString\0"
645 "glXCreateContext\0"
646 "glXCreateNewContext\0"
647 "glXCopyContext\0"
648 "glXDestroyContext\0"
649 "glXQueryVersion\0"
650 "glXMakeCurrent\0"
651 "glXSwapBuffers\0"
652 "glXGetConfig\0"
653 "glXQueryExtension\0"
654 "glXChooseFBConfig\0"
655 "glXGetFBConfigs\0"
656 "glXGetFBConfigAttrib\0"
657 "glXQueryContext\0"
658 "glXQueryDrawable\0"
659 "glXGetVisualFromFBConfig\0"
660 "glXIsDirect\0"
661 "glXCreatePixmap\0"
662 "glXDestroyPixmap\0"
663 "glXCreatePbuffer\n"
664 "glXDestroyPbuffer\n"
665 "\0"
666 };
667
668 /* Like wglGetProcAddress/glxGetProcAddress */
669 void *glo_getprocaddress(const char *procName) {
670     HGLRC oldCtx;
671     HDC oldDC;
672     if (!glo_inited)
673         glo_init();
674
675     oldCtx = wglGetCurrentContext();
676     oldDC = wglGetCurrentDC();
677     if (oldDC!=glo.hDC || oldCtx!=glo.hContext)
678         wglMakeCurrent(glo.hDC, glo.hContext);
679
680     void *procAddr = wglGetProcAddress(procName);
681
682     if (oldDC!=glo.hDC || oldCtx!=glo.hContext)
683         wglMakeCurrent(oldDC, oldCtx);
684
685     /* wgl doesn't know about the glx functions - but
686     we never call these anyway (they're implemented in
687     opengl_exec), so all we need to do is return a nunzero value...
688
689     But we also have to check for 'standard' GL function names
690     too as wgl doesn't return those either! */
691         /* Caller in opengl_exec.c may query some base GL API, then call them
692         * directly. On windows, wglGetProcAddress usually return NULL in such
693         * case, then we return 1, which casue segfault when accessing (void*)1. We
694         * should call base GL API directly instead of GET_EXT_PTR + ptr_func_*.
695         * TODO: add LoadLibrary + GetProcAddress as call back.
696         */ 
697     if (procAddr==0) {
698         const char *p = STANDARD_GL_FUNCTIONS;
699         while (*p) {
700             if (!strcmp(procName, p)) {
701                 procAddr = (void*)1;
702                 break;
703             }
704             // skip to the next '0' and then just over it
705             while (*p) p++;
706             p++;
707         }
708     }
709
710     /*printf("wglGetProcAddress '%s' -> %p\n", procName, procAddr);
711     fflush(stdout);*/
712
713     return procAddr;
714 }
715 void glo_surface_updatecontents(GloSurface *surface) {
716         /* NOT IMPLEMENTED YET. */
717         printf("glo_surface_updatecontents() is not implemented for windows. \n");
718 }
719
720 /* ------------------------------------------------------------------------ */
721
722 /* Create a light-weight context just for creating surface */
723 GloContext *__glo_context_create(int formatFlags) {
724     GloContext *context;
725     // pixel format attributes
726         int                      pf_attri[2*MAX_ATTRIBS];
727     float        pf_attrf[] = {0, 0};
728     unsigned int numReturned = 0;
729     int          pb_attr[] = { 0 };
730     int          rgbaBits[4];
731         int                      index = 0;
732
733         /*Initlized array because it needs to be terminated by 0*/
734         for(index = 0; index < 2*MAX_ATTRIBS; index ++)
735                 pf_attri[index] = 0;
736
737         index = 0;
738         pf_attri[2*index] = WGL_SUPPORT_OPENGL_ARB;
739         pf_attri[2*index +1 ] = TRUE;
740         index ++;
741
742         pf_attri[2*index] = WGL_DRAW_TO_PBUFFER_ARB;
743         pf_attri[2*index +1 ] = TRUE;
744         index ++;
745
746         pf_attri[2*index] = WGL_RED_BITS_ARB;
747         pf_attri[2*index +1 ] = 8;
748         index ++;
749
750         pf_attri[2*index] = WGL_GREEN_BITS_ARB;
751         pf_attri[2*index +1 ] = 8;
752         index ++;
753
754         pf_attri[2*index] = WGL_BLUE_BITS_ARB;
755         pf_attri[2*index +1 ] = 8;
756         index ++;
757
758         pf_attri[2*index] = WGL_ALPHA_BITS_ARB;
759         pf_attri[2*index +1 ] = 8;
760         index ++;
761
762         pf_attri[2*index] = WGL_DEPTH_BITS_ARB;
763         pf_attri[2*index +1 ] = 0;
764         index ++;
765
766         pf_attri[2*index] = WGL_STENCIL_BITS_ARB;
767         pf_attri[2*index +1 ] = 0;
768         index ++;
769
770         pf_attri[2*index] = WGL_DOUBLE_BUFFER_ARB;
771         pf_attri[2*index +1 ] = FALSE;
772         index ++;
773
774         if(Render_texture_support)
775         {
776                 fprintf(stdout, "Render to texture supported, add attributes in array!\n");
777                 pf_attri[2*index] = WGL_DRAW_TO_PBUFFER_ARB;
778                 pf_attri[2*index +1 ] = TRUE;
779                 index ++;
780
781                 pf_attri[2*index] = WGL_BIND_TO_TEXTURE_RGBA_ARB;
782                 pf_attri[2*index +1 ] = TRUE;
783
784         } else {
785                 fprintf(stderr, "Render to Texture not supported, disable attributes in array!\n");
786         }
787         
788
789
790     if (!glo_inited)
791         glo_init();
792
793     context = (GloContext*)malloc(sizeof(GloContext));
794     memset(context, 0, sizeof(GloContext));
795     context->formatFlags = formatFlags;
796
797     // set up the surface format from the flags we were given
798     glo_flags_get_rgba_bits(context->formatFlags, rgbaBits);
799     pf_attri[5]  = rgbaBits[0];
800     pf_attri[7]  = rgbaBits[1];
801     pf_attri[9]  = rgbaBits[2];
802     pf_attri[11] = rgbaBits[3];
803     pf_attri[13] = glo_flags_get_depth_bits(context->formatFlags);
804     pf_attri[15] = glo_flags_get_stencil_bits(context->formatFlags);
805
806     // find out what pixel format to use
807     wglChoosePixelFormatARB( glo.hDC, pf_attri, pf_attrf, 1, &context->wglPixelFormat, &numReturned);
808     if( numReturned == 0 ) {
809         printf( "No matching configs found.\n" );
810         //exit( EXIT_FAILURE );
811                 return NULL;
812     }
813
814     // We create a tiny pbuffer - just so we can make a context of the right pixel format
815     context->hPBuffer = wglCreatePbufferARB( glo.hDC, context->wglPixelFormat, 
816                                              16, 16, pb_attr );
817     if( !context->hPBuffer ) {
818         printf( "Couldn't create the PBuffer\n" );
819         //exit( EXIT_FAILURE );
820                 return NULL;
821     }
822     context->hDC      = wglGetPbufferDCARB( context->hPBuffer );
823     if( !context->hDC ) {
824         printf( "Couldn't create the DC\n" );
825         //exit( EXIT_FAILURE );
826                 return NULL;
827     }
828
829         return context;
830 }
831
832 /* Create an OpenGL context for a certain pixel format. formatflags are from the GLO_ constants */
833 GloContext *glo_context_create(int formatFlags, GloContext *shareLists) {
834
835     GloContext *context = __glo_context_create(formatFlags);
836
837         if (!context) {
838                 return NULL;
839         }
840
841     context->hContext = wglCreateContext(context->hDC);
842     if (context->hContext == NULL) {
843         printf( "Unable to create GL context\n" );
844         //exit( EXIT_FAILURE );
845                 return NULL;
846     }
847
848     if (shareLists) {
849         // Need to share lists...
850         wglShareLists(shareLists->hContext, context->hContext);
851     }
852
853     return context;
854 }
855
856 /* Destroy a previouslu created OpenGL context */
857 void glo_context_destroy(GloContext *context) {
858     if (!context) return;
859
860     wglMakeCurrent( NULL, NULL );
861     if( context->hPBuffer != NULL ) {
862         wglReleasePbufferDCARB( context->hPBuffer, context->hDC );
863         wglDestroyPbufferARB( context->hPBuffer );
864     }
865     if( context->hDC != NULL ) {
866         ReleaseDC( glo.hWnd, context->hDC );
867     }
868     if (context->hContext) {
869         wglDeleteContext(context->hContext);
870     }
871     free(context);
872 }
873
874 /* ------------------------------------------------------------------------ */
875
876 /* Update the context in surface and handle previous context */
877 void glo_surface_update_context(GloSurface *surface, GloContext *context, int free_flags)
878 {
879     /* If previous context is light-weight context, just free it. If previous
880      * context is valid one binded with surface via MakeCurrent, we need unbind
881      * from original glstate */
882
883     if ( surface->context )
884     {
885                 if ( free_flags ) /* light-weight context */
886             g_free(surface->context);
887     }
888     surface->context = context;
889 }
890
891 /* Create a surface with given width and height, formatflags are from the
892  * GLO_ constants */
893 GloSurface *glo_surface_create(int width, int height, GloContext *context) {
894     GloSurface           *surface;
895     int                   pb_attr[] = {
896         /* Need following 2 to support surface as texture */
897         WGL_TEXTURE_FORMAT_ARB,  WGL_TEXTURE_RGBA_ARB,
898         WGL_TEXTURE_TARGET_ARB,  WGL_TEXTURE_2D_ARB,
899         0
900     };
901     
902     // Create the p-buffer...
903     surface = (GloSurface*)malloc(sizeof(GloSurface));
904     memset(surface, 0, sizeof(GloSurface));
905     surface->width = width;
906     surface->height = height;
907     surface->context = context;
908
909     surface->hPBuffer = wglCreatePbufferARB( glo.hDC, context->wglPixelFormat, 
910                                              surface->width, surface->height, pb_attr );
911     if( !surface->hPBuffer ) {
912         printf( "Couldn't create the PBuffer\n" );
913         //exit( EXIT_FAILURE );
914                 return NULL;
915     }
916     surface->hDC      = wglGetPbufferDCARB( surface->hPBuffer );
917     if( !surface->hDC ) {
918         printf( "Couldn't create the DC\n" );
919         //exit( EXIT_FAILURE );
920                 return NULL;
921     }
922
923     return surface;
924 }
925
926 /* Destroy the given surface */
927 void glo_surface_destroy(GloSurface *surface) {
928     if (!surface) return;
929
930     wglMakeCurrent( NULL, NULL );
931     if( surface->hPBuffer != NULL ) {
932         wglReleasePbufferDCARB( surface->hPBuffer, surface->hDC );
933         wglDestroyPbufferARB( surface->hPBuffer );
934     }
935     if( surface->hDC != NULL ) {
936         ReleaseDC( glo.hWnd, surface->hDC );
937     }
938     free(surface);
939 }
940
941 /* Make the given surface current */
942 int glo_surface_makecurrent(GloSurface *surface) {
943   if (surface) {
944       return wglMakeCurrent( surface->hDC, surface->context->hContext );
945   } else {
946       return wglMakeCurrent( NULL, NULL );
947   }
948 }
949
950 /* Get the contents of the given surface */
951 void glo_surface_getcontents(GloSurface *surface, int stride, int bpp, void *data) {
952
953   if (!surface)
954       return;
955   // Compatible / fallback method.
956   glo_surface_getcontents_readpixels(surface->context->formatFlags,
957                                         stride, bpp, surface->width,
958                                         surface->height, data);
959 }
960
961 /* Return the width and height of the given surface */
962 void glo_surface_get_size(GloSurface *surface, int *width, int *height) {
963     if (width)
964         *width = surface->width;
965     if (height)
966         *height = surface->height;
967 }
968
969 /* Bind the surface as texture */
970 void glo_surface_as_texture(GloContext *ctxt, GloSurface *surface)
971 {
972 #if 0
973     int glFormat, glType;
974     glo_surface_updatecontents(surface);
975     /*XXX: change the fixed target: GL_TEXTURE_2D*/
976     glo_flags_get_readpixel_type(surface->context->formatFlags, &glFormat, &glType);
977     fprintf(stderr, "surface_as_texture:teximage:width=%d,height=%d,glFormat=0x%x,glType=0x%x.\n", surface->width, surface->height, glFormat, glType);
978     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->width, surface->height, 0, glFormat, glType, surface->image->data);
979 #else
980         if(!Render_texture_support)
981         {
982                 //fprintf(stderr, "Render to texture not supported on this machine, just return!\n");
983                 return ;
984         }
985
986     if (!wglBindTexImageARB)
987     {
988         fprintf (stderr, "wglBindTexImageEXT not supported! Can't emulate glEGLImageTargetTexture2DOES!\n");
989         return;
990     }
991
992     if ( !wglBindTexImageARB(surface->hPBuffer, WGL_FRONT_LEFT_ARB) )
993     {
994         fprintf(stderr, "wglBindTexImageARBr error=%d.\n", glGetError());
995     }
996
997 #endif
998 }
999 void glo_surface_release_texture(GloSurface *surface)
1000 {
1001     if(!Render_texture_support)
1002                 return ;
1003
1004     if (!wglReleaseTexImageARB)
1005     {
1006         fprintf (stderr, "wglReleaseTexImageARB not supported! Can't emulate glEGLImageTargetTexture2DOES!\n");
1007         return;
1008     }
1009
1010     if ( !wglReleaseTexImageARB(surface->hPBuffer, WGL_FRONT_LEFT_ARB) )
1011     {
1012         fprintf(stderr, "wglBindTexImageARBr error=%d.\n", glGetError());
1013     }
1014
1015 }
1016
1017 #endif