BlackBerry and Android -Added ability for developer to choose OpenGL ES version ...
[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     EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
34 #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
35     EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
36 #else
37     EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
38 #endif
39 #ifdef TARGET_HOST_BLACKBERRY
40     /* Only 888 and 565 seem to work. Based on
41        http://qt.gitorious.org/qt/qtbase/source/893deb1a93021cdfabe038cdf1869de33a60cbc9:src/plugins/platforms/qnx/qqnxglcontext.cpp and
42        https://twitter.com/BlackBerryDev/status/380720927475912706 */
43     EGL_BLUE_SIZE, 8,
44     EGL_GREEN_SIZE, 8,
45     EGL_RED_SIZE, 8,
46 #else
47     EGL_BLUE_SIZE, 1,
48     EGL_GREEN_SIZE, 1,
49     EGL_RED_SIZE, 1,
50 #endif
51     EGL_ALPHA_SIZE, (fgState.DisplayMode & GLUT_ALPHA) ? 1 : 0,
52     EGL_DEPTH_SIZE, (fgState.DisplayMode & GLUT_DEPTH) ? 1 : 0,
53     EGL_STENCIL_SIZE, (fgState.DisplayMode & GLUT_STENCIL) ? 1 : 0,
54     EGL_SAMPLE_BUFFERS, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? 1 : 0,
55     EGL_SAMPLES, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? fgState.SampleNumber : 0,
56     EGL_NONE
57   };
58
59   EGLint num_config;
60   if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
61                attribs, config, 1, &num_config)) {
62     fgWarning("eglChooseConfig: error %x\n", eglGetError());
63     return 0;
64   }
65
66   return 1;
67 }
68
69 /**
70  * Initialize an EGL context for the current display.
71  */
72 EGLContext fghCreateNewContextEGL( SFG_Window* window ) {
73   EGLContext context;
74
75   EGLDisplay eglDisplay = fgDisplay.pDisplay.egl.Display;
76   EGLConfig eglConfig = window->Window.pContext.egl.Config;
77
78   /* Ensure OpenGLES 2.0 context */
79   static EGLint ctx_attribs[] = {
80 #ifdef GL_ES_VERSION_2_0
81     EGL_CONTEXT_CLIENT_VERSION, 2,
82 #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
83     EGL_CONTEXT_CLIENT_VERSION, 1,
84 #endif
85     EGL_NONE
86   };
87 #ifdef GL_ES_VERSION_2_0
88   int gles2Ver = fgState.MajorVersion <= 2 ? 2 : fgState.MajorVersion;
89   ctx_attribs[1] = gles2Ver;
90 #endif
91   context = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, ctx_attribs);
92   if (context == EGL_NO_CONTEXT) {
93     fgWarning("Cannot initialize EGL context, err=%x\n", eglGetError());
94     fghContextCreationError();
95   }
96   EGLint ver = -1;
97   eglQueryContext(fgDisplay.pDisplay.egl.Display, context, EGL_CONTEXT_CLIENT_VERSION, &ver);
98 #ifdef GL_ES_VERSION_2_0
99   if (ver != gles2Ver)
100 #else
101   if (ver != 1)
102 #endif
103     fgError("Wrong GLES major version: %d\n", ver);
104
105   return context;
106 }
107
108 void fgPlatformSetWindow ( SFG_Window *window )
109 {
110   if ( window != fgStructure.CurrentWindow && window) {
111     if (eglMakeCurrent(fgDisplay.pDisplay.egl.Display,
112                window->Window.pContext.egl.Surface,
113                window->Window.pContext.egl.Surface,
114                window->Window.Context) == EGL_FALSE)
115       fgError("eglMakeCurrent: err=%x\n", eglGetError());
116   }
117 }
118
119 /*
120  * Really opens a window when handle is available
121  */
122 void fghPlatformOpenWindowEGL( SFG_Window* window )
123 {
124   EGLDisplay display = fgDisplay.pDisplay.egl.Display;
125   EGLConfig  config  = window->Window.pContext.egl.Config;
126
127   EGLSurface surface = eglCreateWindowSurface(display, config, window->Window.Handle, NULL);
128   if (surface == EGL_NO_SURFACE)
129     fgError("Cannot create EGL window surface, err=%x\n", eglGetError());
130   window->Window.pContext.egl.Surface = surface;
131
132   fgPlatformSetWindow(window);
133
134   /* EGLint w, h; */
135   /* eglQuerySurface(display, surface, EGL_WIDTH, &w); */
136   /* eglQuerySurface(display, surface, EGL_HEIGHT, &h); */
137
138 }
139
140 /*
141  * Closes a window, destroying the frame and OpenGL context
142  */
143 void fghPlatformCloseWindowEGL( SFG_Window* window )
144 {
145   /* Based on fg_window_mswin fgPlatformCloseWindow */
146   if( fgStructure.CurrentWindow == window )
147     eglMakeCurrent(fgDisplay.pDisplay.egl.Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
148
149   if (window->Window.Context != EGL_NO_CONTEXT) {
150     /* Step through the list of windows. If the rendering context is not being used by another window, then delete it */
151     {
152       GLboolean used = GL_FALSE;
153       SFG_Window *iter;
154
155       for( iter = (SFG_Window*)fgStructure.Windows.First;
156            iter && used == GL_FALSE;
157            iter = (SFG_Window*)iter->Node.Next)
158       {
159         if( (iter->Window.Context == window->Window.Context) &&
160             (iter != window) )
161           used = GL_TRUE;
162       }
163
164       if( !used )
165         eglDestroyContext(fgDisplay.pDisplay.egl.Display, window->Window.Context);
166     }
167     window->Window.Context = EGL_NO_CONTEXT;
168   }
169
170   if (window->Window.pContext.egl.Surface != EGL_NO_SURFACE) {
171     eglDestroySurface(fgDisplay.pDisplay.egl.Display, window->Window.pContext.egl.Surface);
172     window->Window.pContext.egl.Surface = EGL_NO_SURFACE;
173   }
174 }