Added comments about GLES 3.0 support with 2.0 references.
[freeglut] / src / egl / fg_window_egl.c
1 /*
2  * freeglut_display_android.c
3  *
4  * Window management methods for EGL
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 int fghChooseConfig(EGLConfig* config) {
30   const EGLint attribs[] = {
31     EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
32 #ifdef GL_ES_VERSION_2_0
33     /*
34      * Khronos does not specify a EGL_OPENGL_ES3_BIT outside of the OpenGL extension "EGL_KHR_create_context". There are numerous references on the internet that
35      * say to use EGL_OPENGL_ES3_BIT, followed by many saying they can't find it in any headers. In fact, the offical updated specification for EGL does not have
36      * any references to OpenGL ES 3.0. Tests have shown that EGL_OPENGL_ES2_BIT will work with ES 3.0.
37      */
38     EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
39 #elif GL_VERSION_ES_CM_1_0 || GL_VERSION_ES_CL_1_0 || GL_VERSION_ES_CM_1_1 || GL_VERSION_ES_CL_1_1
40     EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
41 #else
42     EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
43 #endif
44 #ifdef TARGET_HOST_BLACKBERRY
45     /* Only 888 and 565 seem to work. Based on
46        http://qt.gitorious.org/qt/qtbase/source/893deb1a93021cdfabe038cdf1869de33a60cbc9:src/plugins/platforms/qnx/qqnxglcontext.cpp and
47        https://twitter.com/BlackBerryDev/status/380720927475912706 */
48     EGL_BLUE_SIZE, 8,
49     EGL_GREEN_SIZE, 8,
50     EGL_RED_SIZE, 8,
51 #else
52     EGL_BLUE_SIZE, 1,
53     EGL_GREEN_SIZE, 1,
54     EGL_RED_SIZE, 1,
55 #endif
56     EGL_ALPHA_SIZE, (fgState.DisplayMode & GLUT_ALPHA) ? 1 : 0,
57     EGL_DEPTH_SIZE, (fgState.DisplayMode & GLUT_DEPTH) ? 1 : 0,
58     EGL_STENCIL_SIZE, (fgState.DisplayMode & GLUT_STENCIL) ? 1 : 0,
59     EGL_SAMPLE_BUFFERS, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? 1 : 0,
60     EGL_SAMPLES, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? fgState.SampleNumber : 0,
61     EGL_NONE
62   };
63
64   EGLint num_config;
65   if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
66                attribs, config, 1, &num_config)) {
67     fgWarning("eglChooseConfig: error %x\n", eglGetError());
68     return 0;
69   }
70
71   return 1;
72 }
73
74 /**
75  * Initialize an EGL context for the current display.
76  */
77 EGLContext fghCreateNewContextEGL( SFG_Window* window ) {
78   EGLContext context;
79
80   EGLDisplay eglDisplay = fgDisplay.pDisplay.egl.Display;
81   EGLConfig eglConfig = window->Window.pContext.egl.Config;
82
83   /* Ensure OpenGLES 2.0 context */
84   static EGLint ctx_attribs[] = {
85 #ifdef GL_ES_VERSION_2_0
86     EGL_CONTEXT_CLIENT_VERSION, 2,
87 #elif GL_VERSION_ES_CM_1_0 || GL_VERSION_ES_CL_1_0 || GL_VERSION_ES_CM_1_1 || GL_VERSION_ES_CL_1_1
88     EGL_CONTEXT_CLIENT_VERSION, 1,
89 #endif
90     EGL_NONE
91   };
92 #ifdef GL_ES_VERSION_2_0
93   /*
94    * As GLES 3.0 is backwards compatible with GLES 2.0, we set 2.0 as default unless the user states a different version.
95    * This updates the context attributes and lets us check that the correct version was set when we query it after creation.
96    */
97   int gles2Ver = fgState.MajorVersion <= 2 ? 2 : fgState.MajorVersion;
98   ctx_attribs[1] = gles2Ver;
99 #endif
100   context = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, ctx_attribs);
101   if (context == EGL_NO_CONTEXT) {
102     fgWarning("Cannot initialize EGL context, err=%x\n", eglGetError());
103     fghContextCreationError();
104   }
105   EGLint ver = -1;
106   eglQueryContext(fgDisplay.pDisplay.egl.Display, context, EGL_CONTEXT_CLIENT_VERSION, &ver);
107 #ifdef GL_ES_VERSION_2_0
108   if (ver != gles2Ver)
109 #else
110   if (ver != 1)
111 #endif
112     fgError("Wrong GLES major version: %d\n", ver);
113
114   return context;
115 }
116
117 void fgPlatformSetWindow ( SFG_Window *window )
118 {
119   if ( window != fgStructure.CurrentWindow && window) {
120     if (eglMakeCurrent(fgDisplay.pDisplay.egl.Display,
121                window->Window.pContext.egl.Surface,
122                window->Window.pContext.egl.Surface,
123                window->Window.Context) == EGL_FALSE)
124       fgError("eglMakeCurrent: err=%x\n", eglGetError());
125   }
126 }
127
128 /*
129  * Really opens a window when handle is available
130  */
131 void fghPlatformOpenWindowEGL( SFG_Window* window )
132 {
133   EGLDisplay display = fgDisplay.pDisplay.egl.Display;
134   EGLConfig  config  = window->Window.pContext.egl.Config;
135
136   EGLSurface surface = eglCreateWindowSurface(display, config, window->Window.Handle, NULL);
137   if (surface == EGL_NO_SURFACE)
138     fgError("Cannot create EGL window surface, err=%x\n", eglGetError());
139   window->Window.pContext.egl.Surface = surface;
140
141   fgPlatformSetWindow(window);
142
143   /* EGLint w, h; */
144   /* eglQuerySurface(display, surface, EGL_WIDTH, &w); */
145   /* eglQuerySurface(display, surface, EGL_HEIGHT, &h); */
146
147 }
148
149 /*
150  * Closes a window, destroying the frame and OpenGL context
151  */
152 void fghPlatformCloseWindowEGL( SFG_Window* window )
153 {
154   /* Based on fg_window_mswin fgPlatformCloseWindow */
155   if( fgStructure.CurrentWindow == window )
156     eglMakeCurrent(fgDisplay.pDisplay.egl.Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
157
158   if (window->Window.Context != EGL_NO_CONTEXT) {
159     /* Step through the list of windows. If the rendering context is not being used by another window, then delete it */
160     {
161       GLboolean used = GL_FALSE;
162       SFG_Window *iter;
163
164       for( iter = (SFG_Window*)fgStructure.Windows.First;
165            iter && used == GL_FALSE;
166            iter = (SFG_Window*)iter->Node.Next)
167       {
168         if( (iter->Window.Context == window->Window.Context) &&
169             (iter != window) )
170           used = GL_TRUE;
171       }
172
173       if( !used )
174         eglDestroyContext(fgDisplay.pDisplay.egl.Display, window->Window.Context);
175     }
176     window->Window.Context = EGL_NO_CONTEXT;
177   }
178
179   if (window->Window.pContext.egl.Surface != EGL_NO_SURFACE) {
180     eglDestroySurface(fgDisplay.pDisplay.egl.Display, window->Window.pContext.egl.Surface);
181     window->Window.pContext.egl.Surface = EGL_NO_SURFACE;
182   }
183 }