EGL is always double-buffered
[freeglut] / src / egl / fg_state_egl.c
1 /*
2  * fg_state_egl.c
3  *
4  * EGL-specific freeglut state query methods.
5  *
6  * Copyright (C) 2012  Sylvain Beucler
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions 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 MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <GL/freeglut.h>
27 #include "fg_internal.h"
28 #include "egl/fg_window_egl.h"
29
30 /*
31  * Queries the GL context about some attributes
32  */
33 static int fgPlatformGetConfig( int attribute )
34 {
35   int returnValue = 0;
36   int result __fg_unused;  /*  Not checked  */
37
38   if( fgStructure.CurrentWindow )
39       result = eglGetConfigAttrib( fgDisplay.pDisplay.egl.Display,
40                                    fgStructure.CurrentWindow->Window.pContext.egl.Config,
41                                    attribute,
42                                    &returnValue );
43
44   return returnValue;
45 }
46
47 int fghPlatformGlutGetEGL ( GLenum eWhat )
48 {
49     int nsamples = 0;
50
51     switch( eWhat )
52     {
53     /*
54      * The window/context specific queries are handled mostly by
55      * fgPlatformGetConfig().
56      */
57     case GLUT_WINDOW_NUM_SAMPLES:
58         glGetIntegerv(GL_SAMPLES, &nsamples);
59         return nsamples;
60
61     /*
62      * The rest of GLX queries under X are general enough to use a macro to
63      * check them
64      */
65 #   define EGL_QUERY(a,b) case a: return fgPlatformGetConfig( b );
66
67     EGL_QUERY( GLUT_WINDOW_BUFFER_SIZE,         EGL_BUFFER_SIZE         );
68     EGL_QUERY( GLUT_WINDOW_STENCIL_SIZE,        EGL_STENCIL_SIZE        );
69     EGL_QUERY( GLUT_WINDOW_DEPTH_SIZE,          EGL_DEPTH_SIZE          );
70     EGL_QUERY( GLUT_WINDOW_RED_SIZE,            EGL_RED_SIZE            );
71     EGL_QUERY( GLUT_WINDOW_GREEN_SIZE,          EGL_GREEN_SIZE          );
72     EGL_QUERY( GLUT_WINDOW_BLUE_SIZE,           EGL_BLUE_SIZE           );
73     EGL_QUERY( GLUT_WINDOW_ALPHA_SIZE,          EGL_ALPHA_SIZE          );
74
75 #   undef EGL_QUERY
76
77     /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
78     case GLUT_DISPLAY_MODE_POSSIBLE:
79     {
80         /*  We should not have to call fghChooseConfig again here.  */
81         EGLConfig config;
82         return fghChooseConfig(&config);
83     }
84
85     /* This is system-dependant */
86     case GLUT_WINDOW_FORMAT_ID:
87         if( fgStructure.CurrentWindow == NULL )
88             return 0;
89         return fgPlatformGetConfig( EGL_NATIVE_VISUAL_ID );
90
91     case GLUT_WINDOW_DOUBLEBUFFER:
92         return 1; /* EGL is always double-buffered */
93
94     default:
95         fgWarning( "glutGet(): missing enum handle %d", eWhat );
96         break;
97     }
98
99     return -1;
100 }
101
102 int* fgPlatformGlutGetModeValues(GLenum eWhat, int *size)
103 {
104   int *array;
105
106   int attributes[9];
107   int attribute_name = 0;
108
109   array = NULL;
110   *size = 0;
111   
112   switch (eWhat)
113     {
114     case GLUT_AUX:
115       break;
116
117     case GLUT_MULTISAMPLE:
118       attributes[0] = EGL_BUFFER_SIZE;
119       attributes[1] = EGL_DONT_CARE;
120       attributes[2] = EGL_SAMPLE_BUFFERS;
121       attributes[3] = 1;
122       attributes[4] = EGL_SAMPLES;
123       attributes[5] = 1;
124       attributes[6] = EGL_NONE;
125       
126       attribute_name = EGL_SAMPLES;
127
128       EGLConfig* configArray;
129       EGLint configArraySize = 0;
130  
131       /* Get number of available configs */
132       if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
133                            attributes, NULL, 0,
134                            &configArraySize))
135         break;
136       configArray = calloc(configArraySize, sizeof(EGLConfig));
137       
138       if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
139                            attributes, configArray, configArraySize,
140                            &configArraySize))
141         break;
142       
143       /* We get results in ascending order */
144       {
145         int previous_value = 0;
146         int i;
147         
148         array = malloc(sizeof(int) * configArraySize);
149
150         for (i = 0; i < configArraySize; i++) {
151           int value = 0;
152           eglGetConfigAttrib(fgDisplay.pDisplay.egl.Display,
153                              configArray[i], attribute_name, &value);
154           if (value > previous_value)
155             {
156               previous_value = value;
157               array[*size] = value;
158               (*size)++;
159             }
160         }
161         
162         array = realloc(array, sizeof(int) * (*size));
163       }
164       free(configArray);
165       break;
166
167     default:
168       break;
169     }
170
171   return array;
172 }