1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 2017 BlackBerry Limited
4 
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.
8 
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:
12 
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.
20 */
21 
22 #include "../../SDL_internal.h"
23 #include "sdl_qnx.h"
24 
25 static EGLDisplay   egl_disp;
26 
27 /**
28  * Detertmines the pixel format to use based on the current display and EGL
29  * configuration.
30  * @param   egl_conf    EGL configuration to use
31  * @return  A SCREEN_FORMAT* constant for the pixel format to use
32  */
33 static int
chooseFormat(EGLConfig egl_conf)34 chooseFormat(EGLConfig egl_conf)
35 {
36     EGLint buffer_bit_depth;
37     EGLint alpha_bit_depth;
38 
39     eglGetConfigAttrib(egl_disp, egl_conf, EGL_BUFFER_SIZE, &buffer_bit_depth);
40     eglGetConfigAttrib(egl_disp, egl_conf, EGL_ALPHA_SIZE, &alpha_bit_depth);
41 
42     switch (buffer_bit_depth) {
43         case 32:
44             return SCREEN_FORMAT_RGBX8888;
45         case 24:
46             return SCREEN_FORMAT_RGB888;
47         case 16:
48             switch (alpha_bit_depth) {
49                 case 4:
50                     return SCREEN_FORMAT_RGBX4444;
51                 case 1:
52                     return SCREEN_FORMAT_RGBA5551;
53                 default:
54                     return SCREEN_FORMAT_RGB565;
55             }
56         default:
57             return 0;
58     }
59 }
60 
61 /**
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
66  */
67 int
glGetConfig(EGLConfig * pconf,int * pformat)68 glGetConfig(EGLConfig *pconf, int *pformat)
69 {
70     EGLConfig egl_conf = (EGLConfig)0;
71     EGLConfig *egl_configs;
72     EGLint egl_num_configs;
73     EGLint val;
74     EGLBoolean rc;
75     EGLint i;
76 
77     // Determine the numbfer of configurations.
78     rc = eglGetConfigs(egl_disp, NULL, 0, &egl_num_configs);
79     if (rc != EGL_TRUE) {
80         return -1;
81     }
82 
83     if (egl_num_configs == 0) {
84         return -1;
85     }
86 
87     // Allocate enough memory for all configurations.
88     egl_configs = malloc(egl_num_configs * sizeof(*egl_configs));
89     if (egl_configs == NULL) {
90         return -1;
91     }
92 
93     // Get the list of configurations.
94     rc = eglGetConfigs(egl_disp, egl_configs, egl_num_configs,
95                        &egl_num_configs);
96     if (rc != EGL_TRUE) {
97         free(egl_configs);
98         return -1;
99     }
100 
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)) {
105             continue;
106         }
107 
108         eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_RENDERABLE_TYPE, &val);
109         if (!(val & EGL_OPENGL_ES2_BIT)) {
110             continue;
111         }
112 
113         eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_DEPTH_SIZE, &val);
114         if (val == 0) {
115             continue;
116         }
117 
118         egl_conf = egl_configs[i];
119         break;
120     }
121 
122     free(egl_configs);
123     *pconf = egl_conf;
124     *pformat = chooseFormat(egl_conf);
125 
126     return 0;
127 }
128 
129 /**
130  * Initializes the EGL library.
131  * @param   _THIS
132  * @param   name    unused
133  * @return  0 if successful, -1 on error
134  */
135 int
glLoadLibrary(_THIS,const char * name)136 glLoadLibrary(_THIS, const char *name)
137 {
138     EGLNativeDisplayType    disp_id = EGL_DEFAULT_DISPLAY;
139 
140     egl_disp = eglGetDisplay(disp_id);
141     if (egl_disp == EGL_NO_DISPLAY) {
142         return -1;
143     }
144 
145     if (eglInitialize(egl_disp, NULL, NULL) == EGL_FALSE) {
146         return -1;
147     }
148 
149     return 0;
150 }
151 
152 /**
153  * Finds the address of an EGL extension function.
154  * @param   proc    Function name
155  * @return  Function address
156  */
157 void *
glGetProcAddress(_THIS,const char * proc)158 glGetProcAddress(_THIS, const char *proc)
159 {
160     return eglGetProcAddress(proc);
161 }
162 
163 /**
164  * Associates the given window with the necessary EGL structures for drawing and
165  * displaying content.
166  * @param   _THIS
167  * @param   window  The SDL window to create the context for
168  * @return  A pointer to the created context, if successful, NULL on error
169  */
170 SDL_GLContext
glCreateContext(_THIS,SDL_Window * window)171 glCreateContext(_THIS, SDL_Window *window)
172 {
173     window_impl_t   *impl = (window_impl_t *)window->driverdata;
174     EGLContext      context;
175     EGLSurface      surface;
176 
177     struct {
178         EGLint client_version[2];
179         EGLint none;
180     } egl_ctx_attr = {
181         .client_version = { EGL_CONTEXT_CLIENT_VERSION, 2 },
182         .none = EGL_NONE
183     };
184 
185     struct {
186         EGLint render_buffer[2];
187         EGLint none;
188     } egl_surf_attr = {
189         .render_buffer = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER },
190         .none = EGL_NONE
191     };
192 
193     context = eglCreateContext(egl_disp, impl->conf, EGL_NO_CONTEXT,
194                                (EGLint *)&egl_ctx_attr);
195     if (context == EGL_NO_CONTEXT) {
196         return NULL;
197     }
198 
199     surface = eglCreateWindowSurface(egl_disp, impl->conf, impl->window,
200                                      (EGLint *)&egl_surf_attr);
201     if (surface == EGL_NO_SURFACE) {
202         return NULL;
203     }
204 
205     eglMakeCurrent(egl_disp, surface, surface, context);
206 
207     impl->surface = surface;
208     return context;
209 }
210 
211 /**
212  * Sets a new value for the number of frames to display before swapping buffers.
213  * @param   _THIS
214  * @param   interval    New interval value
215  * @return  0 if successful, -1 on error
216  */
217 int
glSetSwapInterval(_THIS,int interval)218 glSetSwapInterval(_THIS, int interval)
219 {
220     if (eglSwapInterval(egl_disp, interval) != EGL_TRUE) {
221         return -1;
222     }
223 
224     return 0;
225 }
226 
227 /**
228  * Swaps the EGL buffers associated with the given window
229  * @param   _THIS
230  * @param   window  Window to swap buffers for
231  * @return  0 if successful, -1 on error
232  */
233 int
glSwapWindow(_THIS,SDL_Window * window)234 glSwapWindow(_THIS, SDL_Window *window)
235 {
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;
239 }
240 
241 /**
242  * Makes the given context the current one for drawing operations.
243  * @param   _THIS
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
247  */
248 int
glMakeCurrent(_THIS,SDL_Window * window,SDL_GLContext context)249 glMakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
250 {
251     window_impl_t   *impl;
252     EGLSurface      surface = NULL;
253 
254     if (window) {
255         impl = (window_impl_t *)window->driverdata;
256         surface = impl->surface;
257     }
258 
259     if (eglMakeCurrent(egl_disp, surface, surface, context) != EGL_TRUE) {
260         return -1;
261     }
262 
263     return 0;
264 }
265 
266 /**
267  * Destroys a context.
268  * @param   _THIS
269  * @param   context The context to destroy
270  */
271 void
glDeleteContext(_THIS,SDL_GLContext context)272 glDeleteContext(_THIS, SDL_GLContext context)
273 {
274     eglDestroyContext(egl_disp, context);
275 }
276 
277 /**
278  * Terminates access to the EGL library.
279  * @param   _THIS
280  */
281 void
glUnloadLibrary(_THIS)282 glUnloadLibrary(_THIS)
283 {
284     eglTerminate(egl_disp);
285 }
286