6b980bd3ad326d3af9811d7f7756fcd3442357ff
[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 /**
30  * Initialize an EGL context for the current display.
31  */
32 void fghCreateContext( ) {
33   /*
34    * Here specify the attributes of the desired configuration.
35    * Below, we select an EGLConfig with at least 8 bits per color
36    * component compatible with on-screen windows
37    */
38   /* Ensure OpenGLES 2.0 context */
39   printf("DisplayMode: %d (DEPTH %d)\n", fgState.DisplayMode, (fgState.DisplayMode & GLUT_DEPTH));
40   const EGLint attribs[] = {
41     EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
42     EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
43     EGL_BLUE_SIZE, 1,
44     EGL_GREEN_SIZE, 1,
45     EGL_RED_SIZE, 1,
46     EGL_ALPHA_SIZE, (fgState.DisplayMode & GLUT_ALPHA) ? 1 : 0,
47     EGL_DEPTH_SIZE, (fgState.DisplayMode & GLUT_DEPTH) ? 1 : 0,
48     EGL_STENCIL_SIZE, (fgState.DisplayMode & GLUT_STENCIL) ? 1 : 0,
49     EGL_SAMPLE_BUFFERS, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? 1 : 0,
50     EGL_SAMPLES, (fgState.DisplayMode & GLUT_MULTISAMPLE) ? fgState.SampleNumber : 0,
51     EGL_NONE
52   };
53
54   EGLint format;
55   EGLint numConfigs;
56   EGLConfig config;
57   EGLContext context;
58
59   EGLDisplay eglDisplay = fgDisplay.pDisplay.eglDisplay;
60
61   /* TODO : apply DisplayMode */
62   /*        (GLUT_DEPTH already applied in attribs[] above) */
63
64   /* Here, the application chooses the configuration it desires. In this
65    * sample, we have a very simplified selection process, where we pick
66    * the first EGLConfig that matches our criteria */
67   eglChooseConfig(eglDisplay, attribs, &config, 1, &numConfigs);
68
69   /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
70    * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
71    * As soon as we picked a EGLConfig, we can safely reconfigure the
72    * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
73   eglGetConfigAttrib(eglDisplay, config, EGL_NATIVE_VISUAL_ID, &format);
74
75   /* Default, but doesn't hurt */
76   eglBindAPI(EGL_OPENGL_ES_API);
77
78   /* Ensure OpenGLES 2.0 context */
79   static const EGLint ctx_attribs[] = {
80     EGL_CONTEXT_CLIENT_VERSION, 2,
81     EGL_NONE
82   };
83   context = eglCreateContext(eglDisplay, config, EGL_NO_CONTEXT, ctx_attribs);
84   if (context == EGL_NO_CONTEXT) {
85     fgWarning("Cannot initialize EGL context, err=%x\n", eglGetError());
86     fghContextCreationError();
87   }
88   EGLint ver = -1;
89   eglQueryContext(fgDisplay.pDisplay.eglDisplay, context, EGL_CONTEXT_CLIENT_VERSION, &ver);
90   if (ver != 2)
91     fgError("Wrong GLES major version: %d\n", ver);
92
93   fgDisplay.pDisplay.eglContext = context;
94   fgDisplay.pDisplay.eglContextConfig = config;
95   fgDisplay.pDisplay.eglContextFormat = format;
96 }
97
98 /*
99  * Really opens a window when handle is available
100  */
101 EGLSurface fghEGLPlatformOpenWindow( EGLNativeWindowType handle )
102 {
103   EGLDisplay display = fgDisplay.pDisplay.eglDisplay;
104   EGLContext context = fgDisplay.pDisplay.eglContext;
105   EGLConfig  config  = fgDisplay.pDisplay.eglContextConfig;
106
107   EGLSurface surface = eglCreateWindowSurface(display, config, handle, NULL);
108   if (surface == EGL_NO_SURFACE)
109     fgError("Cannot create EGL window surface, err=%x\n", eglGetError());
110   if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
111     fgError("eglMakeCurrent: err=%x\n", eglGetError());
112
113   //EGLint w, h;
114   //eglQuerySurface(display, surface, EGL_WIDTH, &w);
115   //eglQuerySurface(display, surface, EGL_HEIGHT, &h);
116
117   return surface;
118 }
119
120 /*
121  * Closes a window, destroying the frame and OpenGL context
122  */
123 void fgPlatformCloseWindow( SFG_Window* window )
124 {
125   if (window->Window.pContext.eglSurface != EGL_NO_SURFACE) {
126     eglDestroySurface(fgDisplay.pDisplay.eglDisplay, window->Window.pContext.eglSurface);
127     window->Window.pContext.eglSurface = EGL_NO_SURFACE;
128   }
129 }