Context can be used for a menu rather than the current window
[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 fghChooseConfigEGL(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     EGL_BLUE_SIZE, 1,
40     EGL_GREEN_SIZE, 1,
41     EGL_RED_SIZE, 1,
42     EGL_ALPHA_SIZE, (fgState.DisplayMode & GLUT_ALPHA) ? 1 : 0,
43     EGL_DEPTH_SIZE, (fgState.DisplayMode & GLUT_DEPTH) ? 1 : 0,
44     EGL_STENCIL_SIZE, (fgState.DisplayMode & GLUT_STENCIL) ? 1 : 0,
45     EGL_SAMPLE_BUFFERS, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? 1 : 0,
46     EGL_SAMPLES, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? fgState.SampleNumber : 0,
47     EGL_NONE
48   };
49   
50   EGLint num_config;
51   if (!eglChooseConfig(fgDisplay.pDisplay.egl.Display,
52                        attribs, config, 1, &num_config)) {
53     fgWarn("eglChooseConfig: error %x\n", eglGetError());
54     return 0;
55   }
56
57   return 1;
58 }
59
60 /**
61  * Initialize an EGL context for the current display.
62  */
63 EGLContext fghCreateNewContextEGL( SFG_Window* window ) {
64   EGLContext context;
65
66   EGLDisplay eglDisplay = fgDisplay.pDisplay.egl.Display;
67   EGLConfig eglConfig = window->Window.pContext.egl.Config;
68
69   /* Ensure OpenGLES 2.0 context */
70   static const EGLint ctx_attribs[] = {
71 #ifdef GL_ES_VERSION_2_0
72     EGL_CONTEXT_CLIENT_VERSION, 2,
73 #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
74     EGL_CONTEXT_CLIENT_VERSION, 1,
75 #endif
76     EGL_NONE
77   };
78   context = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, ctx_attribs);
79   if (context == EGL_NO_CONTEXT) {
80     fgWarning("Cannot initialize EGL context, err=%x\n", eglGetError());
81     fghContextCreationError();
82   }
83   EGLint ver = -1;
84   eglQueryContext(fgDisplay.pDisplay.egl.Display, context, EGL_CONTEXT_CLIENT_VERSION, &ver);
85   if (ver != 2)
86     fgError("Wrong GLES major version: %d\n", ver);
87
88   return context;
89 }
90
91 /*
92  * Really opens a window when handle is available
93  */
94 void fghPlatformOpenWindowEGL( SFG_Window* window )
95 {
96   EGLDisplay display = fgDisplay.pDisplay.egl.Display;
97   EGLConfig  config  = window->Window.pContext.egl.Config;
98
99   EGLSurface surface = eglCreateWindowSurface(display, config, window->Window.Handle, NULL);
100   if (surface == EGL_NO_SURFACE)
101     fgError("Cannot create EGL window surface, err=%x\n", eglGetError());
102   if (eglMakeCurrent(display, surface, surface, window->Window.Context) == EGL_FALSE)
103     fgError("eglMakeCurrent: err=%x\n", eglGetError());
104
105   //EGLint w, h;
106   //eglQuerySurface(display, surface, EGL_WIDTH, &w);
107   //eglQuerySurface(display, surface, EGL_HEIGHT, &h);
108
109   window->Window.pContext.egl.Surface = surface;
110 }
111
112 /*
113  * Closes a window, destroying the frame and OpenGL context
114  */
115 void fghPlatformCloseWindowEGL( SFG_Window* window )
116 {
117   eglMakeCurrent(fgDisplay.pDisplay.egl.Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
118   if (window->Window.Context != EGL_NO_CONTEXT) {
119     eglDestroyContext(fgDisplay.pDisplay.egl.Display, window->Window.Context);
120     window->Window.Context = EGL_NO_CONTEXT;
121   }
122
123   if (window->Window.pContext.egl.Surface != EGL_NO_SURFACE) {
124     eglDestroySurface(fgDisplay.pDisplay.egl.Display, window->Window.pContext.egl.Surface);
125     window->Window.pContext.egl.Surface = EGL_NO_SURFACE;
126   }
127 }