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