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