1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (c) 2011-2017 - Daniel De Matteis
3  *
4  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
5  *  of the GNU General Public License as published by the Free Software Found-
6  *  ation, either version 3 of the License, or (at your option) any later version.
7  *
8  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  *  PURPOSE.  See the GNU General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License along with RetroArch.
13  *  If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #ifdef _MSC_VER
17 #pragma comment(lib, "libEGL")
18 #endif
19 
20 #include <stdlib.h>
21 
22 #include <retro_assert.h>
23 
24 #ifdef HAVE_CONFIG_H
25 #include "../../config.h"
26 #endif
27 
28 #ifdef HAVE_OPENGL
29 #include "gl_common.h"
30 #endif
31 
32 #include "egl_common.h"
33 #include "../../verbosity.h"
34 #include "../../frontend/frontend_driver.h"
35 
36 /* TODO/FIXME - globals */
37 bool g_egl_inited    = false;
38 
39 unsigned g_egl_major = 0;
40 unsigned g_egl_minor = 0;
41 
42 #if defined(HAVE_DYNAMIC) && defined(HAVE_DYNAMIC_EGL)
43 #include <dynamic/dylib.h>
44 
45 typedef EGLBoolean(* PFN_EGL_QUERY_SURFACE)(
46       EGLDisplay dpy,
47       EGLSurface surface,
48       EGLint attribute,
49       EGLint *value);
50 typedef void *(* PFN_EGL_GET_PROC_ADDRESS)(const char *procname);
51 typedef EGLSurface(*PFN_EGL_CREATE_WINDOW_SURFACE) (EGLDisplay dpy,
52 					    EGLConfig config,
53 					    EGLNativeWindowType win,
54 					    const EGLint * attrib_list);
55 typedef EGLContext(*PFN_EGL_CREATE_CONTEXT)(EGLDisplay dpy, EGLConfig config,
56 				      EGLContext share_context,
57 				      const EGLint * attrib_list);
58 typedef EGLBoolean(*PFN_EGL_GET_CONFIGS) (EGLDisplay dpy, EGLConfig * configs,
59 				   EGLint config_size, EGLint * num_config);
60 typedef EGLint(*PFN_EGL_GET_ERROR) (void);
61 typedef EGLDisplay(*PFN_EGL_GET_DISPLAY) (EGLNativeDisplayType display_id);
62 typedef EGLBoolean(*PFN_EGL_CHOOSE_CONFIG) (EGLDisplay dpy,
63 				     const EGLint * attrib_list,
64 				     EGLConfig * configs,
65 				     EGLint config_size, EGLint * num_config);
66 typedef EGLBoolean(*PFN_EGL_TERMINATE)(EGLDisplay dpy);
67 typedef EGLBoolean(*PFN_EGL_INITIALIZE)(EGLDisplay dpy, EGLint * major,
68 				   EGLint * minor);
69 typedef EGLBoolean(*PFN_EGL_BIND_API) (EGLenum api);
70 typedef EGLBoolean(*PFN_EGL_MAKE_CURRENT) (EGLDisplay dpy, EGLSurface draw,
71 				    EGLSurface read, EGLContext ctx);
72 typedef EGLBoolean(*PFN_EGL_DESTROY_SURFACE) (EGLDisplay dpy, EGLSurface surface);
73 typedef EGLBoolean(*PFN_EGL_DESTROY_CONTEXT) (EGLDisplay dpy, EGLContext ctx);
74 typedef EGLContext(*PFN_EGL_GET_CURRENT_CONTEXT) (void);
75 typedef const char *(*PFN_EGL_QUERY_STRING) (EGLDisplay dpy, EGLint name);
76 typedef EGLBoolean(*PFN_EGL_GET_CONFIG_ATTRIB) (EGLDisplay dpy,
77 					EGLConfig config,
78 					EGLint attribute, EGLint * value);
79 typedef EGLBoolean(*PFN_EGL_SWAP_BUFFERS) (EGLDisplay dpy, EGLSurface surface);
80 typedef EGLBoolean(*PFN_EGL_SWAP_INTERVAL) (EGLDisplay dpy, EGLint interval);
81 
82 static PFN_EGL_QUERY_SURFACE             _egl_query_surface;
83 static PFN_EGL_GET_PROC_ADDRESS          _egl_get_proc_address;
84 static PFN_EGL_CREATE_WINDOW_SURFACE     _egl_create_window_surface;
85 static PFN_EGL_CREATE_CONTEXT            _egl_create_context;
86 static PFN_EGL_GET_CONFIGS               _egl_get_configs;
87 static PFN_EGL_GET_ERROR                 _egl_get_error;
88 static PFN_EGL_GET_DISPLAY               _egl_get_display;
89 static PFN_EGL_CHOOSE_CONFIG             _egl_choose_config;
90 static PFN_EGL_TERMINATE                 _egl_terminate;
91 static PFN_EGL_INITIALIZE                _egl_initialize;
92 static PFN_EGL_BIND_API                  _egl_bind_api;
93 static PFN_EGL_MAKE_CURRENT              _egl_make_current;
94 static PFN_EGL_DESTROY_SURFACE           _egl_destroy_surface;
95 static PFN_EGL_DESTROY_CONTEXT           _egl_destroy_context;
96 static PFN_EGL_GET_CURRENT_CONTEXT       _egl_get_current_context;
97 static PFN_EGL_QUERY_STRING              _egl_query_string;
98 static PFN_EGL_GET_CONFIG_ATTRIB         _egl_get_config_attrib;
99 static PFN_EGL_SWAP_BUFFERS              _egl_swap_buffers;
100 static PFN_EGL_SWAP_INTERVAL             _egl_swap_interval;
101 
102 #else
103 #define _egl_query_surface(a, b, c, d) eglQuerySurface(a, b, c, d)
104 #define _egl_get_proc_address(a) eglGetProcAddress(a)
105 #define _egl_create_window_surface(a, b, c, d) eglCreateWindowSurface(a, b, c, d)
106 #define _egl_create_context(a, b, c, d) eglCreateContext(a, b, c, d)
107 #define _egl_get_configs(a, b, c, d) eglGetConfigs(a, b, c, d)
108 #define _egl_get_display(a) eglGetDisplay(a)
109 #define _egl_choose_config(a, b, c, d, e) eglChooseConfig(a, b, c, d, e)
110 #define _egl_make_current(a, b, c, d) eglMakeCurrent(a, b, c, d)
111 #define _egl_initialize(a, b, c) eglInitialize(a, b, c)
112 #define _egl_destroy_surface(a, b) eglDestroySurface(a, b)
113 #define _egl_destroy_context(a, b) eglDestroyContext(a, b)
114 #define _egl_get_current_context() eglGetCurrentContext()
115 #define _egl_get_error() eglGetError()
116 #define _egl_terminate(dpy) eglTerminate(dpy)
117 #define _egl_bind_api(a) eglBindAPI(a)
118 #define _egl_query_string(a, b) eglQueryString(a, b)
119 #define _egl_get_config_attrib(a, b, c, d) eglGetConfigAttrib(a, b, c, d)
120 #define _egl_swap_buffers(a, b) eglSwapBuffers(a, b)
121 #define _egl_swap_interval(a, b) eglSwapInterval(a, b)
122 #endif
123 
egl_init_dll(void)124 bool egl_init_dll(void)
125 {
126 #if defined(HAVE_DYNAMIC) && defined(HAVE_DYNAMIC_EGL)
127    static dylib_t egl_dll;
128 
129    if (!egl_dll)
130    {
131       egl_dll = dylib_load("libEGL.dll");
132       if (egl_dll)
133       {
134          /* Setup function callbacks once */
135          _egl_query_surface         = (PFN_EGL_QUERY_SURFACE)dylib_proc(
136                egl_dll, "eglQuerySurface");
137          _egl_get_proc_address      = (PFN_EGL_GET_PROC_ADDRESS)dylib_proc(
138                egl_dll, "eglGetProcAddress");
139          _egl_create_window_surface = (PFN_EGL_CREATE_WINDOW_SURFACE)dylib_proc(
140                egl_dll, "eglCreateWindowSurface");
141          _egl_create_context        = (PFN_EGL_CREATE_CONTEXT)dylib_proc(
142                egl_dll, "eglCreateContext");
143          _egl_get_configs           = (PFN_EGL_GET_CONFIGS)dylib_proc(
144                egl_dll, "eglGetConfigs");
145          _egl_get_error             = (PFN_EGL_GET_ERROR)dylib_proc(
146                egl_dll, "eglGetError");
147          _egl_get_display           = (PFN_EGL_GET_DISPLAY)dylib_proc(
148                egl_dll, "eglGetDisplay");
149          _egl_choose_config         = (PFN_EGL_CHOOSE_CONFIG)dylib_proc(
150                egl_dll, "eglChooseConfig");
151          _egl_terminate             = (PFN_EGL_TERMINATE)dylib_proc(
152                egl_dll, "eglTerminate");
153          _egl_initialize            = (PFN_EGL_INITIALIZE)dylib_proc(
154                egl_dll, "eglInitialize");
155          _egl_bind_api              = (PFN_EGL_BIND_API)dylib_proc(
156                egl_dll, "eglBindAPI");
157          _egl_make_current          = (PFN_EGL_MAKE_CURRENT)dylib_proc(
158                egl_dll, "eglMakeCurrent");
159          _egl_destroy_surface       = (PFN_EGL_DESTROY_SURFACE)dylib_proc(
160                egl_dll, "eglDestroySurface");
161          _egl_destroy_context       = (PFN_EGL_DESTROY_CONTEXT)dylib_proc(
162                egl_dll, "eglDestroyContext");
163          _egl_get_current_context   = (PFN_EGL_GET_CURRENT_CONTEXT)dylib_proc(
164                egl_dll, "eglGetCurrentContext");
165          _egl_query_string          = (PFN_EGL_QUERY_STRING)dylib_proc(
166                egl_dll, "eglQueryString");
167          _egl_get_config_attrib     = (PFN_EGL_GET_CONFIG_ATTRIB)dylib_proc(
168                egl_dll, "eglGetConfigAttrib");
169          _egl_swap_buffers          = (PFN_EGL_SWAP_BUFFERS)dylib_proc(
170                egl_dll, "eglSwapBuffers");
171          _egl_swap_interval         = (PFN_EGL_SWAP_INTERVAL)dylib_proc(
172                egl_dll, "eglSwapInterval");
173       }
174    }
175 
176    if (egl_dll)
177       return true;
178 #endif
179    return false;
180 }
181 
egl_report_error(void)182 void egl_report_error(void)
183 {
184    EGLint    error = _egl_get_error();
185    const char *str = NULL;
186    switch (error)
187    {
188       case EGL_SUCCESS:
189          str = "EGL_SUCCESS";
190          break;
191 
192       case EGL_BAD_ACCESS:
193          str = "EGL_BAD_ACCESS";
194          break;
195 
196       case EGL_BAD_ALLOC:
197          str = "EGL_BAD_ALLOC";
198          break;
199 
200       case EGL_BAD_ATTRIBUTE:
201          str = "EGL_BAD_ATTRIBUTE";
202          break;
203 
204       case EGL_BAD_CONFIG:
205          str = "EGL_BAD_CONFIG";
206          break;
207 
208       case EGL_BAD_CONTEXT:
209          str = "EGL_BAD_CONTEXT";
210          break;
211 
212       case EGL_BAD_CURRENT_SURFACE:
213          str = "EGL_BAD_CURRENT_SURFACE";
214          break;
215 
216       case EGL_BAD_DISPLAY:
217          str = "EGL_BAD_DISPLAY";
218          break;
219 
220       case EGL_BAD_MATCH:
221          str = "EGL_BAD_MATCH";
222          break;
223 
224       case EGL_BAD_NATIVE_PIXMAP:
225          str = "EGL_BAD_NATIVE_PIXMAP";
226          break;
227 
228       case EGL_BAD_NATIVE_WINDOW:
229          str = "EGL_BAD_NATIVE_WINDOW";
230          break;
231 
232       case EGL_BAD_PARAMETER:
233          str = "EGL_BAD_PARAMETER";
234          break;
235 
236       case EGL_BAD_SURFACE:
237          str = "EGL_BAD_SURFACE";
238          break;
239 
240       default:
241          str = "Unknown";
242          break;
243    }
244 
245    RARCH_ERR("[EGL]: #0x%x, %s\n", (unsigned)error, str);
246 }
247 
egl_get_proc_address(const char * symbol)248 gfx_ctx_proc_t egl_get_proc_address(const char *symbol)
249 {
250    return _egl_get_proc_address(symbol);
251 }
252 
egl_terminate(EGLDisplay dpy)253 void egl_terminate(EGLDisplay dpy)
254 {
255    _egl_terminate(dpy);
256 }
257 
egl_get_config_attrib(EGLDisplay dpy,EGLConfig config,EGLint attribute,EGLint * value)258 bool egl_get_config_attrib(EGLDisplay dpy, EGLConfig config, EGLint attribute,
259       EGLint *value)
260 {
261    return _egl_get_config_attrib(dpy, config, attribute, value);
262 }
263 
egl_initialize(EGLDisplay dpy,EGLint * major,EGLint * minor)264 bool egl_initialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
265 {
266    return _egl_initialize(dpy, major, minor);
267 }
268 
egl_bind_api(EGLenum egl_api)269 bool egl_bind_api(EGLenum egl_api)
270 {
271    return _egl_bind_api(egl_api);
272 }
273 
egl_destroy(egl_ctx_data_t * egl)274 void egl_destroy(egl_ctx_data_t *egl)
275 {
276    if (egl->dpy)
277    {
278 #if defined HAVE_OPENGL
279 #if !defined(RARCH_MOBILE)
280       if (egl->ctx != EGL_NO_CONTEXT)
281       {
282          glFlush();
283          glFinish();
284       }
285 #endif
286 #endif
287 
288       _egl_make_current(egl->dpy,
289             EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
290       if (egl->ctx != EGL_NO_CONTEXT)
291          _egl_destroy_context(egl->dpy, egl->ctx);
292 
293       if (egl->hw_ctx != EGL_NO_CONTEXT)
294          _egl_destroy_context(egl->dpy, egl->hw_ctx);
295 
296       if (egl->surf != EGL_NO_SURFACE)
297          _egl_destroy_surface(egl->dpy, egl->surf);
298       egl_terminate(egl->dpy);
299    }
300 
301    /* Be as careful as possible in deinit.
302     * If we screw up, any TTY will not restore.
303     */
304 
305    egl->ctx     = EGL_NO_CONTEXT;
306    egl->hw_ctx  = EGL_NO_CONTEXT;
307    egl->surf    = EGL_NO_SURFACE;
308    egl->dpy     = EGL_NO_DISPLAY;
309    egl->config  = 0;
310    g_egl_inited  = false;
311 
312    frontend_driver_destroy_signal_handler_state();
313 }
314 
egl_bind_hw_render(egl_ctx_data_t * egl,bool enable)315 void egl_bind_hw_render(egl_ctx_data_t *egl, bool enable)
316 {
317    egl->use_hw_ctx = enable;
318 
319    if (egl->dpy  == EGL_NO_DISPLAY)
320       return;
321    if (egl->surf == EGL_NO_SURFACE)
322       return;
323 
324    _egl_make_current(egl->dpy, egl->surf,
325          egl->surf,
326          enable ? egl->hw_ctx : egl->ctx);
327 }
328 
egl_swap_buffers(void * data)329 void egl_swap_buffers(void *data)
330 {
331    egl_ctx_data_t *egl = (egl_ctx_data_t*)data;
332    if (  egl                         &&
333          egl->dpy  != EGL_NO_DISPLAY &&
334          egl->surf != EGL_NO_SURFACE
335          )
336       _egl_swap_buffers(egl->dpy, egl->surf);
337 }
338 
egl_set_swap_interval(egl_ctx_data_t * egl,int interval)339 void egl_set_swap_interval(egl_ctx_data_t *egl, int interval)
340 {
341    /* Can be called before initialization.
342     * Some contexts require that swap interval
343     * is known at startup time.
344     */
345    egl->interval = interval;
346 
347    if (egl->dpy  == EGL_NO_DISPLAY)
348       return;
349    if (!_egl_get_current_context())
350       return;
351 
352    if (!_egl_swap_interval(egl->dpy, interval))
353    {
354       RARCH_ERR("[EGL]: eglSwapInterval(%i) failed.\n", interval);
355       egl_report_error();
356    }
357 }
358 
egl_get_video_size(egl_ctx_data_t * egl,unsigned * width,unsigned * height)359 void egl_get_video_size(egl_ctx_data_t *egl, unsigned *width, unsigned *height)
360 {
361    *width  = 0;
362    *height = 0;
363 
364    if (egl->dpy != EGL_NO_DISPLAY && egl->surf != EGL_NO_SURFACE)
365    {
366       EGLint gl_width, gl_height;
367 
368       _egl_query_surface(egl->dpy, egl->surf, EGL_WIDTH, &gl_width);
369       _egl_query_surface(egl->dpy, egl->surf, EGL_HEIGHT, &gl_height);
370       *width  = gl_width;
371       *height = gl_height;
372    }
373 }
374 
check_egl_version(int minMajorVersion,int minMinorVersion)375 bool check_egl_version(int minMajorVersion, int minMinorVersion)
376 {
377    int count;
378    int major, minor;
379    const char *str = _egl_query_string(EGL_NO_DISPLAY, EGL_VERSION);
380 
381    if (!str)
382       return false;
383 
384    count = sscanf(str, "%d.%d", &major, &minor);
385    if (count != 2)
386       return false;
387 
388    if (major < minMajorVersion)
389       return false;
390 
391    if (major > minMajorVersion)
392       return true;
393 
394    if (minor >= minMinorVersion)
395       return true;
396 
397    return false;
398 }
399 
check_egl_client_extension(const char * name)400 bool check_egl_client_extension(const char *name)
401 {
402    size_t nameLen;
403    const char *str = _egl_query_string(EGL_NO_DISPLAY, EGL_EXTENSIONS);
404 
405    /* The EGL implementation doesn't support client extensions at all. */
406    if (!str)
407       return false;
408 
409    nameLen = strlen(name);
410    while (*str != '\0')
411    {
412       /* Use strspn and strcspn to find the start position and length of each
413        * token in the extension string. Using strtok could also work, but
414        * that would require allocating a copy of the string. */
415       size_t len = strcspn(str, " ");
416       if (len == nameLen && strncmp(str, name, nameLen) == 0)
417          return true;
418       str += len;
419       str += strspn(str, " ");
420    }
421 
422    return false;
423 }
424 
get_egl_display(EGLenum platform,void * native)425 static EGLDisplay get_egl_display(EGLenum platform, void *native)
426 {
427    if (platform != EGL_NONE)
428    {
429       /* If the client library supports at least EGL 1.5, then we can call
430        * eglGetPlatformDisplay. Otherwise, see if eglGetPlatformDisplayEXT
431        * is available. */
432 #if defined(EGL_VERSION_1_5)
433       if (check_egl_version(1, 5))
434       {
435          typedef EGLDisplay (EGLAPIENTRY * pfn_eglGetPlatformDisplay)
436             (EGLenum platform, void *native_display, const EGLAttrib *attrib_list);
437          pfn_eglGetPlatformDisplay ptr_eglGetPlatformDisplay;
438 
439          RARCH_LOG("[EGL] Found EGL client version >= 1.5, trying eglGetPlatformDisplay\n");
440          ptr_eglGetPlatformDisplay = (pfn_eglGetPlatformDisplay)
441             egl_get_proc_address("eglGetPlatformDisplay");
442 
443          if (ptr_eglGetPlatformDisplay)
444          {
445             EGLDisplay dpy = ptr_eglGetPlatformDisplay(platform, native, NULL);
446             if (dpy != EGL_NO_DISPLAY)
447                return dpy;
448          }
449       }
450 #endif /* defined(EGL_VERSION_1_5) */
451 
452 #if defined(EGL_EXT_platform_base)
453       if (check_egl_client_extension("EGL_EXT_platform_base"))
454       {
455          PFNEGLGETPLATFORMDISPLAYEXTPROC ptr_eglGetPlatformDisplayEXT;
456 
457          RARCH_LOG("[EGL] Found EGL_EXT_platform_base, trying eglGetPlatformDisplayEXT\n");
458          ptr_eglGetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
459             egl_get_proc_address("eglGetPlatformDisplayEXT");
460 
461          if (ptr_eglGetPlatformDisplayEXT)
462          {
463             EGLDisplay dpy = ptr_eglGetPlatformDisplayEXT(platform, native, NULL);
464             if (dpy != EGL_NO_DISPLAY)
465                return dpy;
466          }
467       }
468 #endif /* defined(EGL_EXT_platform_base) */
469    }
470 
471    /* Either the caller didn't provide a platform type, or the EGL
472     * implementation doesn't support eglGetPlatformDisplay. In this case, try
473     * eglGetDisplay and hope for the best. */
474    RARCH_LOG("[EGL] Falling back to eglGetDisplay\n");
475    return _egl_get_display((EGLNativeDisplayType) native);
476 }
477 
egl_get_native_visual_id(egl_ctx_data_t * egl,EGLint * value)478 bool egl_get_native_visual_id(egl_ctx_data_t *egl, EGLint *value)
479 {
480    if (!egl_get_config_attrib(egl->dpy, egl->config,
481          EGL_NATIVE_VISUAL_ID, value))
482    {
483       RARCH_ERR("[EGL]: egl_get_native_visual_id failed.\n");
484       return false;
485    }
486 
487    return true;
488 }
489 
egl_default_accept_config_cb(void * display_data,EGLDisplay dpy,EGLConfig config)490 bool egl_default_accept_config_cb(void *display_data, EGLDisplay dpy, EGLConfig config)
491 {
492    /* Makes sure we have 8 bit color. */
493    EGLint r, g, b;
494    if (!egl_get_config_attrib(dpy, config, EGL_RED_SIZE, &r))
495       return false;
496    if (!egl_get_config_attrib(dpy, config, EGL_GREEN_SIZE, &g))
497       return false;
498    if (!egl_get_config_attrib(dpy, config, EGL_BLUE_SIZE, &b))
499       return false;
500 
501    if (r != 8 || g != 8 || b != 8)
502       return false;
503 
504    return true;
505 }
506 
egl_init_context_common(egl_ctx_data_t * egl,EGLint * count,const EGLint * attrib_ptr,egl_accept_config_cb_t cb,void * display_data)507 bool egl_init_context_common(
508       egl_ctx_data_t *egl, EGLint *count,
509       const EGLint *attrib_ptr,
510       egl_accept_config_cb_t cb,
511       void *display_data)
512 {
513    EGLint i;
514    EGLint matched     = 0;
515    EGLConfig *configs = NULL;
516    if (!egl)
517       return false;
518 
519    if (!_egl_get_configs(egl->dpy, NULL, 0, count) || *count < 1)
520    {
521       RARCH_ERR("[EGL]: No configs to choose from.\n");
522       return false;
523    }
524 
525    configs = (EGLConfig*)malloc(*count * sizeof(*configs));
526    if (!configs)
527       return false;
528 
529    if (!_egl_choose_config(egl->dpy, attrib_ptr,
530             configs, *count, &matched) || !matched)
531    {
532       RARCH_ERR("[EGL]: No EGL configs with appropriate attributes.\n");
533       return false;
534    }
535 
536    for (i = 0; i < *count; i++)
537    {
538       if (!cb || cb(display_data, egl->dpy, configs[i]))
539       {
540          egl->config = configs[i];
541          break;
542       }
543    }
544 
545    free(configs);
546 
547    if (i == *count)
548    {
549       RARCH_ERR("[EGL]: No EGL config found which satifies requirements.\n");
550       return false;
551    }
552 
553    egl->major = g_egl_major;
554    egl->minor = g_egl_minor;
555 
556    return true;
557 }
558 
559 
egl_init_context(egl_ctx_data_t * egl,EGLenum platform,void * display_data,EGLint * major,EGLint * minor,EGLint * count,const EGLint * attrib_ptr,egl_accept_config_cb_t cb)560 bool egl_init_context(egl_ctx_data_t *egl,
561       EGLenum platform,
562       void *display_data,
563       EGLint *major, EGLint *minor,
564       EGLint *count, const EGLint *attrib_ptr,
565       egl_accept_config_cb_t cb)
566 {
567    EGLDisplay dpy     = get_egl_display(platform, display_data);
568 
569    if (dpy == EGL_NO_DISPLAY)
570    {
571       RARCH_ERR("[EGL]: Couldn't get EGL display.\n");
572       return false;
573    }
574 
575    egl->dpy = dpy;
576 
577    if (!egl_initialize(egl->dpy, major, minor))
578       return false;
579 
580    RARCH_LOG("[EGL]: EGL version: %d.%d\n", *major, *minor);
581 
582    return egl_init_context_common(egl, count, attrib_ptr, cb,
583          display_data);
584 }
585 
egl_create_context(egl_ctx_data_t * egl,const EGLint * egl_attribs)586 bool egl_create_context(egl_ctx_data_t *egl, const EGLint *egl_attribs)
587 {
588    EGLContext ctx = _egl_create_context(egl->dpy, egl->config, EGL_NO_CONTEXT,
589          egl_attribs);
590 
591    if (ctx == EGL_NO_CONTEXT)
592       return false;
593 
594    egl->ctx    = ctx;
595    egl->hw_ctx = NULL;
596 
597    if (egl->use_hw_ctx)
598    {
599       egl->hw_ctx = _egl_create_context(egl->dpy, egl->config, egl->ctx,
600             egl_attribs);
601       RARCH_LOG("[EGL]: Created shared context: %p.\n", (void*)egl->hw_ctx);
602 
603       if (egl->hw_ctx == EGL_NO_CONTEXT)
604          return false;
605    }
606 
607    return true;
608 }
609 
egl_create_surface(egl_ctx_data_t * egl,void * native_window)610 bool egl_create_surface(egl_ctx_data_t *egl, void *native_window)
611 {
612    EGLint window_attribs[] = {
613 	   EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
614 	   EGL_NONE,
615    };
616 
617    egl->surf = _egl_create_window_surface(egl->dpy, egl->config, (NativeWindowType)native_window, window_attribs);
618 
619    if (egl->surf == EGL_NO_SURFACE)
620       return false;
621 
622    /* Connect the context to the surface. */
623    if (!_egl_make_current(egl->dpy, egl->surf, egl->surf, egl->ctx))
624       return false;
625 
626    RARCH_LOG("[EGL]: Current context: %p.\n", (void*)_egl_get_current_context());
627 
628    return true;
629 }
630 
egl_has_config(egl_ctx_data_t * egl)631 bool egl_has_config(egl_ctx_data_t *egl)
632 {
633    if (!egl->config)
634    {
635       RARCH_ERR("[EGL]: No EGL configurations available.\n");
636       return false;
637    }
638    return true;
639 }
640