f3b922af90c218c6a8adc7d31d0d7a3c6309d6fa
[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
29 /*
30  * Queries the GL context about some attributes
31  */
32 static int fgPlatformGetConfig( int attribute )
33 {
34   int returnValue = 0;
35   int result;  /*  Not checked  */
36
37   if( fgStructure.CurrentWindow )
38       result = eglGetConfigAttrib( fgDisplay.pDisplay.egl.Display,
39                                    fgStructure.CurrentWindow->Window.pContext.egl.Config,
40                                    attribute,
41                                    &returnValue );
42
43   return returnValue;
44 }
45
46 int fghPlatformGlutGetEGL ( GLenum eWhat )
47 {
48     int nsamples = 0;
49
50     switch( eWhat )
51     {
52     /*
53      * The window/context specific queries are handled mostly by
54      * fgPlatformGetConfig().
55      */
56     case GLUT_WINDOW_NUM_SAMPLES:
57         glGetIntegerv(GL_SAMPLES, &nsamples);
58         return nsamples;
59
60     /*
61      * The rest of GLX queries under X are general enough to use a macro to
62      * check them
63      */
64 #   define EGL_QUERY(a,b) case a: return fgPlatformGetConfig( b );
65
66     EGL_QUERY( GLUT_WINDOW_BUFFER_SIZE,         EGL_BUFFER_SIZE         );
67     EGL_QUERY( GLUT_WINDOW_STENCIL_SIZE,        EGL_STENCIL_SIZE        );
68     EGL_QUERY( GLUT_WINDOW_DEPTH_SIZE,          EGL_DEPTH_SIZE          );
69     EGL_QUERY( GLUT_WINDOW_RED_SIZE,            EGL_RED_SIZE            );
70     EGL_QUERY( GLUT_WINDOW_GREEN_SIZE,          EGL_GREEN_SIZE          );
71     EGL_QUERY( GLUT_WINDOW_BLUE_SIZE,           EGL_BLUE_SIZE           );
72     EGL_QUERY( GLUT_WINDOW_ALPHA_SIZE,          EGL_ALPHA_SIZE          );
73
74 #   undef EGL_QUERY
75
76     /* I do not know yet if there will be a fgChooseVisual() function for Win32 */
77     case GLUT_DISPLAY_MODE_POSSIBLE:
78     {
79         /*  We should not have to call fghChooseConfig again here.  */
80         EGLConfig config;
81         return fghChooseConfig(&config);
82     }
83
84     /* This is system-dependant */
85     case GLUT_WINDOW_FORMAT_ID:
86         if( fgStructure.CurrentWindow == NULL )
87             return 0;
88         return fgPlatformGetConfig( EGL_NATIVE_VISUAL_ID );
89
90     default:
91         fgWarning( "glutGet(): missing enum handle %d", eWhat );
92         break;
93     }
94
95     return -1;
96 }
97
98 int* fgPlatformGlutGetModeValues(GLenum eWhat, int *size)
99 {
100   int *array;
101
102   int attributes[9];
103   int attribute_name = 0;
104
105   array = NULL;
106   *size = 0;
107   
108   switch (eWhat)
109     {
110     case GLUT_AUX:
111       break;
112
113     case GLUT_MULTISAMPLE:
114       attributes[0] = EGL_BUFFER_SIZE;
115       attributes[1] = EGL_DONT_CARE;
116       attributes[2] = EGL_SAMPLE_BUFFERS;
117       attributes[3] = 1;
118       attributes[4] = EGL_SAMPLES;
119       attributes[5] = 1;
120       attributes[6] = EGL_NONE;
121       
122       attribute_name = EGL_SAMPLES;
123
124       EGLConfig* configArray;
125       EGLConfig* config;
126       EGLint configArraySize = 0;
127  
128       /* Get number of available configs */
129       if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
130                            attributes, NULL, 0,
131                            &configArraySize))
132         break;
133       configArray = calloc(configArraySize, sizeof(EGLConfig));
134       
135       if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
136                            attributes, configArray, configArraySize,
137                            &configArraySize))
138         break;
139       
140       /* We get results in ascending order */
141       {
142         int * temp_array;
143         int previous_value;
144         int i;
145         
146         array = malloc(sizeof(int) * configArraySize);
147         previous_value = 0;
148
149         for (i = 0; i < configArraySize; i++) {
150           int value;
151           eglGetConfigAttrib(fgDisplay.pDisplay.egl.Display,
152                              configArray[i], attribute_name, &value);
153           if (value > previous_value)
154             {
155               previous_value = value;
156               temp_array[*size] = value;
157               (*size)++;
158             }
159         }
160         
161         array = realloc(array, sizeof(int) * (*size));
162       }
163       free(configArray);
164       break;
165
166     default:
167       break;
168     }
169
170   return array;
171 }