2 * freeglut_display_android.c
4 * Window management methods for EGL
6 * Copyright (C) 2012 Sylvain Beucler
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
26 #include <GL/freeglut.h>
27 #include "fg_internal.h"
29 int fghChooseConfig(EGLConfig* config) {
32 attribs[i++] = EGL_SURFACE_TYPE;
33 attribs[i++] = EGL_WINDOW_BIT;
34 if (fgState.MajorVersion >= 2) {
36 * 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
37 * 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
38 * any references to OpenGL ES 3.0. Tests have shown that EGL_OPENGL_ES2_BIT will work with ES 3.0.
40 attribs[i++] = EGL_RENDERABLE_TYPE;
41 attribs[i++] = EGL_OPENGL_ES2_BIT;
43 attribs[i++] = EGL_RENDERABLE_TYPE;
44 attribs[i++] = EGL_OPENGL_ES_BIT;
46 /* Technically it's possible to request a standard OpenGL (non-ES)
47 context, but currently our build system assumes EGL => GLES */
48 /* attribs[i++] = EGL_RENDERABLE_TYPE; */
49 /* attribs[i++] = EGL_OPENGL_BIT; */
50 #ifdef TARGET_HOST_BLACKBERRY
51 /* Only 888 and 565 seem to work. Based on
52 http://qt.gitorious.org/qt/qtbase/source/893deb1a93021cdfabe038cdf1869de33a60cbc9:src/plugins/platforms/qnx/qqnxglcontext.cpp and
53 https://twitter.com/BlackBerryDev/status/380720927475912706 */
54 attribs[i++] = EGL_BLUE_SIZE; attribs[i++] = 8;
55 attribs[i++] = EGL_GREEN_SIZE; attribs[i++] = 8;
56 attribs[i++] = EGL_RED_SIZE; attribs[i++] = 8;
58 attribs[i++] = EGL_BLUE_SIZE; attribs[i++] = 1;
59 attribs[i++] = EGL_GREEN_SIZE; attribs[i++] = 1;
60 attribs[i++] = EGL_RED_SIZE; attribs[i++] = 1;
62 attribs[i++] = EGL_ALPHA_SIZE;
63 attribs[i++] = (fgState.DisplayMode & GLUT_ALPHA) ? 1 : 0;
64 attribs[i++] = EGL_DEPTH_SIZE;
65 attribs[i++] = (fgState.DisplayMode & GLUT_DEPTH) ? 1 : 0;
66 attribs[i++] = EGL_STENCIL_SIZE;
67 attribs[i++] = (fgState.DisplayMode & GLUT_STENCIL) ? 1 : 0;
68 attribs[i++] = EGL_SAMPLE_BUFFERS;
69 attribs[i++] = (fgState.DisplayMode & GLUT_MULTISAMPLE) ? 1 : 0;
70 attribs[i++] = EGL_SAMPLES;
71 attribs[i++] = (fgState.DisplayMode & GLUT_MULTISAMPLE) ? fgState.SampleNumber : 0;
72 attribs[i++] = EGL_NONE;
75 if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
76 attribs, config, 1, &num_config)) {
77 fgWarning("eglChooseConfig: error %x\n", eglGetError());
85 * Initialize an EGL context for the current display.
87 EGLContext fghCreateNewContextEGL( SFG_Window* window ) {
91 EGLDisplay eglDisplay = fgDisplay.pDisplay.egl.Display;
92 EGLConfig eglConfig = window->Window.pContext.egl.Config;
94 /* On GLES, user specifies the target version with glutInitContextVersion */
95 EGLint ctx_attribs[32];
97 ctx_attribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
98 ctx_attribs[i++] = fgState.MajorVersion;
99 ctx_attribs[i++] = EGL_NONE;
101 context = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, ctx_attribs);
102 if (context == EGL_NO_CONTEXT) {
103 fgWarning("Cannot initialize EGL context, err=%x\n", eglGetError());
104 fghContextCreationError();
107 eglQueryContext(fgDisplay.pDisplay.egl.Display, context, EGL_CONTEXT_CLIENT_VERSION, &ver);
108 if (ver != fgState.MajorVersion)
109 fgError("Wrong GLES major version: %d\n", ver);
114 void fgPlatformSetWindow ( SFG_Window *window )
116 if ( window != fgStructure.CurrentWindow && window) {
117 if (eglMakeCurrent(fgDisplay.pDisplay.egl.Display,
118 window->Window.pContext.egl.Surface,
119 window->Window.pContext.egl.Surface,
120 window->Window.Context) == EGL_FALSE)
121 fgError("eglMakeCurrent: err=%x\n", eglGetError());
126 * Really opens a window when handle is available
128 void fghPlatformOpenWindowEGL( SFG_Window* window )
130 EGLDisplay display = fgDisplay.pDisplay.egl.Display;
131 EGLConfig config = window->Window.pContext.egl.Config;
133 EGLSurface surface = eglCreateWindowSurface(display, config, window->Window.Handle, NULL);
134 if (surface == EGL_NO_SURFACE)
135 fgError("Cannot create EGL window surface, err=%x\n", eglGetError());
136 window->Window.pContext.egl.Surface = surface;
138 fgPlatformSetWindow(window);
141 /* eglQuerySurface(display, surface, EGL_WIDTH, &w); */
142 /* eglQuerySurface(display, surface, EGL_HEIGHT, &h); */
147 * Closes a window, destroying the frame and OpenGL context
149 void fghPlatformCloseWindowEGL( SFG_Window* window )
151 /* Based on fg_window_mswin fgPlatformCloseWindow */
152 if( fgStructure.CurrentWindow == window )
153 eglMakeCurrent(fgDisplay.pDisplay.egl.Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
155 if (window->Window.Context != EGL_NO_CONTEXT) {
156 /* Step through the list of windows. If the rendering context is not being used by another window, then delete it */
158 GLboolean used = GL_FALSE;
161 for( iter = (SFG_Window*)fgStructure.Windows.First;
162 iter && used == GL_FALSE;
163 iter = (SFG_Window*)iter->Node.Next)
165 if( (iter->Window.Context == window->Window.Context) &&
171 eglDestroyContext(fgDisplay.pDisplay.egl.Display, window->Window.Context);
173 window->Window.Context = EGL_NO_CONTEXT;
176 if (window->Window.pContext.egl.Surface != EGL_NO_SURFACE) {
177 eglDestroySurface(fgDisplay.pDisplay.egl.Display, window->Window.pContext.egl.Surface);
178 window->Window.pContext.egl.Surface = EGL_NO_SURFACE;