2 Simple DirectMedia Layer
3 Copyright (C) 2017 BlackBerry Limited
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 #include "../../SDL_internal.h"
25 static EGLDisplay egl_disp;
28 * Detertmines the pixel format to use based on the current display and EGL
30 * @param egl_conf EGL configuration to use
31 * @return A SCREEN_FORMAT* constant for the pixel format to use
34 chooseFormat(EGLConfig egl_conf)
36 EGLint buffer_bit_depth;
37 EGLint alpha_bit_depth;
39 eglGetConfigAttrib(egl_disp, egl_conf, EGL_BUFFER_SIZE, &buffer_bit_depth);
40 eglGetConfigAttrib(egl_disp, egl_conf, EGL_ALPHA_SIZE, &alpha_bit_depth);
42 switch (buffer_bit_depth) {
44 return SCREEN_FORMAT_RGBX8888;
46 return SCREEN_FORMAT_RGB888;
48 switch (alpha_bit_depth) {
50 return SCREEN_FORMAT_RGBX4444;
52 return SCREEN_FORMAT_RGBA5551;
54 return SCREEN_FORMAT_RGB565;
62 * Enumerates the supported EGL configurations and chooses a suitable one.
63 * @param[out] pconf The chosen configuration
64 * @param[out] pformat The chosen pixel format
65 * @return 0 if successful, -1 on error
68 glGetConfig(EGLConfig *pconf, int *pformat)
70 EGLConfig egl_conf = (EGLConfig)0;
71 EGLConfig *egl_configs;
72 EGLint egl_num_configs;
77 // Determine the numbfer of configurations.
78 rc = eglGetConfigs(egl_disp, NULL, 0, &egl_num_configs);
83 if (egl_num_configs == 0) {
87 // Allocate enough memory for all configurations.
88 egl_configs = malloc(egl_num_configs * sizeof(*egl_configs));
89 if (egl_configs == NULL) {
93 // Get the list of configurations.
94 rc = eglGetConfigs(egl_disp, egl_configs, egl_num_configs,
101 // Find a good configuration.
102 for (i = 0; i < egl_num_configs; i++) {
103 eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_SURFACE_TYPE, &val);
104 if (!(val & EGL_WINDOW_BIT)) {
108 eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_RENDERABLE_TYPE, &val);
109 if (!(val & EGL_OPENGL_ES2_BIT)) {
113 eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_DEPTH_SIZE, &val);
118 egl_conf = egl_configs[i];
124 *pformat = chooseFormat(egl_conf);
130 * Initializes the EGL library.
133 * @return 0 if successful, -1 on error
136 glLoadLibrary(_THIS, const char *name)
138 EGLNativeDisplayType disp_id = EGL_DEFAULT_DISPLAY;
140 egl_disp = eglGetDisplay(disp_id);
141 if (egl_disp == EGL_NO_DISPLAY) {
145 if (eglInitialize(egl_disp, NULL, NULL) == EGL_FALSE) {
153 * Finds the address of an EGL extension function.
154 * @param proc Function name
155 * @return Function address
158 glGetProcAddress(_THIS, const char *proc)
160 return eglGetProcAddress(proc);
164 * Associates the given window with the necessary EGL structures for drawing and
165 * displaying content.
167 * @param window The SDL window to create the context for
168 * @return A pointer to the created context, if successful, NULL on error
171 glCreateContext(_THIS, SDL_Window *window)
173 window_impl_t *impl = (window_impl_t *)window->driverdata;
178 EGLint client_version[2];
181 .client_version = { EGL_CONTEXT_CLIENT_VERSION, 2 },
186 EGLint render_buffer[2];
189 .render_buffer = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER },
193 context = eglCreateContext(egl_disp, impl->conf, EGL_NO_CONTEXT,
194 (EGLint *)&egl_ctx_attr);
195 if (context == EGL_NO_CONTEXT) {
199 surface = eglCreateWindowSurface(egl_disp, impl->conf, impl->window,
200 (EGLint *)&egl_surf_attr);
201 if (surface == EGL_NO_SURFACE) {
205 eglMakeCurrent(egl_disp, surface, surface, context);
207 impl->surface = surface;
212 * Sets a new value for the number of frames to display before swapping buffers.
214 * @param interval New interval value
215 * @return 0 if successful, -1 on error
218 glSetSwapInterval(_THIS, int interval)
220 if (eglSwapInterval(egl_disp, interval) != EGL_TRUE) {
228 * Swaps the EGL buffers associated with the given window
230 * @param window Window to swap buffers for
231 * @return 0 if successful, -1 on error
234 glSwapWindow(_THIS, SDL_Window *window)
236 /* !!! FIXME: should we migrate this all over to use SDL_egl.c? */
237 window_impl_t *impl = (window_impl_t *)window->driverdata;
238 return eglSwapBuffers(egl_disp, impl->surface) == EGL_TRUE ? 0 : -1;
242 * Makes the given context the current one for drawing operations.
244 * @param window SDL window associated with the context (maybe NULL)
245 * @param context The context to activate
246 * @return 0 if successful, -1 on error
249 glMakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
252 EGLSurface surface = NULL;
255 impl = (window_impl_t *)window->driverdata;
256 surface = impl->surface;
259 if (eglMakeCurrent(egl_disp, surface, surface, context) != EGL_TRUE) {
267 * Destroys a context.
269 * @param context The context to destroy
272 glDeleteContext(_THIS, SDL_GLContext context)
274 eglDestroyContext(egl_disp, context);
278 * Terminates access to the EGL library.
282 glUnloadLibrary(_THIS)
284 eglTerminate(egl_disp);