Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/egl/egl_manager.cpp
9973 views
1
/**************************************************************************/
2
/* egl_manager.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "egl_manager.h"
32
33
#include "core/crypto/crypto_core.h"
34
#include "core/io/dir_access.h"
35
#include "drivers/gles3/rasterizer_gles3.h"
36
37
#ifdef EGL_ENABLED
38
39
#if defined(EGL_STATIC)
40
41
#define GLAD_EGL_VERSION_1_5 true
42
43
#ifdef EGL_EXT_platform_base
44
#define GLAD_EGL_EXT_platform_base 1
45
#endif
46
47
#define KHRONOS_STATIC 1
48
extern "C" EGLAPI void EGLAPIENTRY eglSetBlobCacheFuncsANDROID(EGLDisplay dpy, EGLSetBlobFuncANDROID set, EGLGetBlobFuncANDROID get);
49
extern "C" EGLAPI EGLDisplay EGLAPIENTRY eglGetPlatformDisplayEXT(EGLenum platform, void *native_display, const EGLint *attrib_list);
50
#undef KHRONOS_STATIC
51
52
#endif // defined(EGL_STATIC)
53
54
#ifndef EGL_EXT_platform_base
55
#define GLAD_EGL_EXT_platform_base 0
56
#endif
57
58
#ifdef WINDOWS_ENABLED
59
// Unofficial ANGLE extension: EGL_ANGLE_surface_orientation
60
#ifndef EGL_OPTIMAL_SURFACE_ORIENTATION_ANGLE
61
#define EGL_OPTIMAL_SURFACE_ORIENTATION_ANGLE 0x33A7
62
#define EGL_SURFACE_ORIENTATION_ANGLE 0x33A8
63
#define EGL_SURFACE_ORIENTATION_INVERT_X_ANGLE 0x0001
64
#define EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE 0x0002
65
#endif
66
#endif
67
68
// Creates and caches a GLDisplay. Returns -1 on error.
69
int EGLManager::_get_gldisplay_id(void *p_display) {
70
// Look for a cached GLDisplay.
71
for (unsigned int i = 0; i < displays.size(); i++) {
72
if (displays[i].display == p_display) {
73
return i;
74
}
75
}
76
77
// We didn't find any, so we'll have to create one, along with its own
78
// EGLDisplay and EGLContext.
79
GLDisplay new_gldisplay;
80
new_gldisplay.display = p_display;
81
82
if (GLAD_EGL_VERSION_1_5) {
83
Vector<EGLAttrib> attribs = _get_platform_display_attributes();
84
new_gldisplay.egl_display = eglGetPlatformDisplay(_get_platform_extension_enum(), new_gldisplay.display, (attribs.size() > 0) ? attribs.ptr() : nullptr);
85
} else if (GLAD_EGL_EXT_platform_base) {
86
#ifdef EGL_EXT_platform_base
87
// eglGetPlatformDisplayEXT wants its attributes as EGLint, so we'll truncate
88
// what we already have. It's a bit naughty but I'm really not sure what else
89
// we could do here.
90
Vector<EGLint> attribs;
91
for (const EGLAttrib &attrib : _get_platform_display_attributes()) {
92
attribs.push_back((EGLint)attrib);
93
}
94
95
new_gldisplay.egl_display = eglGetPlatformDisplayEXT(_get_platform_extension_enum(), new_gldisplay.display, (attribs.size() > 0) ? attribs.ptr() : nullptr);
96
#endif // EGL_EXT_platform_base
97
} else {
98
NativeDisplayType *native_display_type = (NativeDisplayType *)new_gldisplay.display;
99
new_gldisplay.egl_display = eglGetDisplay(*native_display_type);
100
}
101
102
ERR_FAIL_COND_V(eglGetError() != EGL_SUCCESS, -1);
103
104
ERR_FAIL_COND_V_MSG(new_gldisplay.egl_display == EGL_NO_DISPLAY, -1, "Can't create an EGL display.");
105
106
if (!eglInitialize(new_gldisplay.egl_display, nullptr, nullptr)) {
107
ERR_FAIL_V_MSG(-1, "Can't initialize an EGL display.");
108
}
109
110
if (!eglBindAPI(_get_platform_api_enum())) {
111
ERR_FAIL_V_MSG(-1, "OpenGL not supported.");
112
}
113
114
Error err = _gldisplay_create_context(new_gldisplay);
115
116
if (err != OK) {
117
eglTerminate(new_gldisplay.egl_display);
118
ERR_FAIL_V(-1);
119
}
120
121
#ifdef EGL_ANDROID_blob_cache
122
#if defined(EGL_STATIC)
123
bool has_blob_cache = true;
124
#else
125
bool has_blob_cache = (eglSetBlobCacheFuncsANDROID != nullptr);
126
#endif
127
if (has_blob_cache && !shader_cache_dir.is_empty()) {
128
eglSetBlobCacheFuncsANDROID(new_gldisplay.egl_display, &EGLManager::_set_cache, &EGLManager::_get_cache);
129
}
130
#endif
131
132
#ifdef WINDOWS_ENABLED
133
String client_extensions_string = eglQueryString(new_gldisplay.egl_display, EGL_EXTENSIONS);
134
if (eglGetError() == EGL_SUCCESS) {
135
Vector<String> egl_extensions = client_extensions_string.split(" ");
136
137
if (egl_extensions.has("EGL_ANGLE_surface_orientation")) {
138
new_gldisplay.has_EGL_ANGLE_surface_orientation = true;
139
print_verbose("EGL: EGL_ANGLE_surface_orientation is supported.");
140
}
141
}
142
#endif
143
144
displays.push_back(new_gldisplay);
145
146
// Return the new GLDisplay's ID.
147
return displays.size() - 1;
148
}
149
150
#ifdef EGL_ANDROID_blob_cache
151
String EGLManager::shader_cache_dir;
152
153
void EGLManager::_set_cache(const void *p_key, EGLsizeiANDROID p_key_size, const void *p_value, EGLsizeiANDROID p_value_size) {
154
String name = CryptoCore::b64_encode_str((const uint8_t *)p_key, p_key_size).replace_char('/', '_');
155
String path = shader_cache_dir.path_join(name) + ".cache";
156
157
Error err = OK;
158
Ref<FileAccess> file = FileAccess::open(path, FileAccess::WRITE, &err);
159
if (err != OK) {
160
return;
161
}
162
file->store_buffer((const uint8_t *)p_value, p_value_size);
163
}
164
165
EGLsizeiANDROID EGLManager::_get_cache(const void *p_key, EGLsizeiANDROID p_key_size, void *p_value, EGLsizeiANDROID p_value_size) {
166
String name = CryptoCore::b64_encode_str((const uint8_t *)p_key, p_key_size).replace_char('/', '_');
167
String path = shader_cache_dir.path_join(name) + ".cache";
168
169
Error err = OK;
170
Ref<FileAccess> file = FileAccess::open(path, FileAccess::READ, &err);
171
if (err != OK) {
172
return 0;
173
}
174
EGLsizeiANDROID len = file->get_length();
175
if (len <= p_value_size) {
176
file->get_buffer((uint8_t *)p_value, len);
177
}
178
return len;
179
}
180
#endif
181
182
Error EGLManager::_gldisplay_create_context(GLDisplay &p_gldisplay) {
183
EGLint attribs[] = {
184
EGL_RED_SIZE,
185
1,
186
EGL_BLUE_SIZE,
187
1,
188
EGL_GREEN_SIZE,
189
1,
190
EGL_DEPTH_SIZE,
191
24,
192
EGL_NONE,
193
};
194
195
EGLint attribs_layered[] = {
196
EGL_RED_SIZE,
197
8,
198
EGL_GREEN_SIZE,
199
8,
200
EGL_GREEN_SIZE,
201
8,
202
EGL_ALPHA_SIZE,
203
8,
204
EGL_DEPTH_SIZE,
205
24,
206
EGL_NONE,
207
};
208
209
EGLint config_count = 0;
210
211
if (OS::get_singleton()->is_layered_allowed()) {
212
eglChooseConfig(p_gldisplay.egl_display, attribs_layered, &p_gldisplay.egl_config, 1, &config_count);
213
} else {
214
eglChooseConfig(p_gldisplay.egl_display, attribs, &p_gldisplay.egl_config, 1, &config_count);
215
}
216
217
ERR_FAIL_COND_V(eglGetError() != EGL_SUCCESS, ERR_BUG);
218
ERR_FAIL_COND_V(config_count == 0, ERR_UNCONFIGURED);
219
220
Vector<EGLint> context_attribs = _get_platform_context_attribs();
221
p_gldisplay.egl_context = eglCreateContext(p_gldisplay.egl_display, p_gldisplay.egl_config, EGL_NO_CONTEXT, (context_attribs.size() > 0) ? context_attribs.ptr() : nullptr);
222
ERR_FAIL_COND_V_MSG(p_gldisplay.egl_context == EGL_NO_CONTEXT, ERR_CANT_CREATE, vformat("Can't create an EGL context. Error code: %d", eglGetError()));
223
224
return OK;
225
}
226
227
Error EGLManager::open_display(void *p_display) {
228
int gldisplay_id = _get_gldisplay_id(p_display);
229
if (gldisplay_id < 0) {
230
return ERR_CANT_CREATE;
231
} else {
232
return OK;
233
}
234
}
235
236
int EGLManager::display_get_native_visual_id(void *p_display) {
237
int gldisplay_id = _get_gldisplay_id(p_display);
238
ERR_FAIL_COND_V(gldisplay_id < 0, ERR_CANT_CREATE);
239
240
GLDisplay gldisplay = displays[gldisplay_id];
241
242
EGLint native_visual_id = -1;
243
244
if (!eglGetConfigAttrib(gldisplay.egl_display, gldisplay.egl_config, EGL_NATIVE_VISUAL_ID, &native_visual_id)) {
245
ERR_FAIL_V(-1);
246
}
247
248
return native_visual_id;
249
}
250
251
Error EGLManager::window_create(DisplayServer::WindowID p_window_id, void *p_display, void *p_native_window, int p_width, int p_height) {
252
int gldisplay_id = _get_gldisplay_id(p_display);
253
ERR_FAIL_COND_V(gldisplay_id < 0, ERR_CANT_CREATE);
254
255
GLDisplay &gldisplay = displays[gldisplay_id];
256
257
// In order to ensure a fast lookup, make sure we got enough elements in the
258
// windows local vector to use the window id as an index.
259
if (p_window_id >= (int)windows.size()) {
260
windows.resize(p_window_id + 1);
261
}
262
263
GLWindow &glwindow = windows[p_window_id];
264
glwindow.gldisplay_id = gldisplay_id;
265
266
Vector<EGLAttrib> egl_attribs;
267
268
#ifdef WINDOWS_ENABLED
269
if (gldisplay.has_EGL_ANGLE_surface_orientation) {
270
EGLint optimal_orientation;
271
if (eglGetConfigAttrib(gldisplay.egl_display, gldisplay.egl_config, EGL_OPTIMAL_SURFACE_ORIENTATION_ANGLE, &optimal_orientation)) {
272
// We only need to support inverting Y for optimizing ANGLE on D3D11.
273
if (optimal_orientation & EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE && !(optimal_orientation & EGL_SURFACE_ORIENTATION_INVERT_X_ANGLE)) {
274
egl_attribs.push_back(EGL_SURFACE_ORIENTATION_ANGLE);
275
egl_attribs.push_back(EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE);
276
}
277
} else {
278
ERR_PRINT(vformat("Failed to get EGL_OPTIMAL_SURFACE_ORIENTATION_ANGLE, error: 0x%08X", eglGetError()));
279
}
280
}
281
282
if (!egl_attribs.is_empty()) {
283
egl_attribs.push_back(EGL_NONE);
284
}
285
#endif
286
287
if (GLAD_EGL_VERSION_1_5) {
288
glwindow.egl_surface = eglCreatePlatformWindowSurface(gldisplay.egl_display, gldisplay.egl_config, p_native_window, egl_attribs.ptr());
289
} else {
290
EGLNativeWindowType *native_window_type = (EGLNativeWindowType *)p_native_window;
291
glwindow.egl_surface = eglCreateWindowSurface(gldisplay.egl_display, gldisplay.egl_config, *native_window_type, nullptr);
292
}
293
294
if (glwindow.egl_surface == EGL_NO_SURFACE) {
295
return ERR_CANT_CREATE;
296
}
297
298
glwindow.initialized = true;
299
300
#ifdef WINDOWS_ENABLED
301
if (gldisplay.has_EGL_ANGLE_surface_orientation) {
302
EGLint orientation;
303
if (eglQuerySurface(gldisplay.egl_display, glwindow.egl_surface, EGL_SURFACE_ORIENTATION_ANGLE, &orientation)) {
304
if (orientation & EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE && !(orientation & EGL_SURFACE_ORIENTATION_INVERT_X_ANGLE)) {
305
glwindow.flipped_y = true;
306
print_verbose("EGL: Using optimal surface orientation: Invert Y");
307
}
308
} else {
309
ERR_PRINT(vformat("Failed to get EGL_SURFACE_ORIENTATION_ANGLE, error: 0x%08X", eglGetError()));
310
}
311
}
312
#endif
313
314
window_make_current(p_window_id);
315
316
return OK;
317
}
318
319
void EGLManager::window_destroy(DisplayServer::WindowID p_window_id) {
320
ERR_FAIL_INDEX(p_window_id, (int)windows.size());
321
322
GLWindow &glwindow = windows[p_window_id];
323
324
if (!glwindow.initialized) {
325
return;
326
}
327
328
glwindow.initialized = false;
329
330
ERR_FAIL_INDEX(glwindow.gldisplay_id, (int)displays.size());
331
GLDisplay &gldisplay = displays[glwindow.gldisplay_id];
332
333
if (glwindow.egl_surface != EGL_NO_SURFACE) {
334
eglDestroySurface(gldisplay.egl_display, glwindow.egl_surface);
335
glwindow.egl_surface = nullptr;
336
}
337
}
338
339
void EGLManager::release_current() {
340
if (!current_window) {
341
return;
342
}
343
344
GLDisplay &current_display = displays[current_window->gldisplay_id];
345
346
eglMakeCurrent(current_display.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
347
}
348
349
void EGLManager::swap_buffers() {
350
if (!current_window) {
351
return;
352
}
353
354
if (!current_window->initialized) {
355
WARN_PRINT("Current OpenGL window is uninitialized!");
356
return;
357
}
358
359
GLDisplay &current_display = displays[current_window->gldisplay_id];
360
361
eglSwapBuffers(current_display.egl_display, current_window->egl_surface);
362
}
363
364
void EGLManager::window_make_current(DisplayServer::WindowID p_window_id) {
365
if (p_window_id == DisplayServer::INVALID_WINDOW_ID) {
366
return;
367
}
368
369
GLWindow &glwindow = windows[p_window_id];
370
371
if (&glwindow == current_window || !glwindow.initialized) {
372
return;
373
}
374
375
current_window = &glwindow;
376
377
GLDisplay &current_display = displays[current_window->gldisplay_id];
378
379
eglMakeCurrent(current_display.egl_display, current_window->egl_surface, current_window->egl_surface, current_display.egl_context);
380
381
#ifdef WINDOWS_ENABLED
382
RasterizerGLES3::set_screen_flipped_y(glwindow.flipped_y);
383
#endif
384
}
385
386
void EGLManager::set_use_vsync(bool p_use) {
387
// We need an active window to get a display to set the vsync.
388
if (!current_window) {
389
return;
390
}
391
392
GLDisplay &disp = displays[current_window->gldisplay_id];
393
394
int swap_interval = p_use ? 1 : 0;
395
396
if (!eglSwapInterval(disp.egl_display, swap_interval)) {
397
WARN_PRINT("Could not set V-Sync mode.");
398
}
399
400
use_vsync = p_use;
401
}
402
403
bool EGLManager::is_using_vsync() const {
404
return use_vsync;
405
}
406
407
EGLContext EGLManager::get_context(DisplayServer::WindowID p_window_id) {
408
GLWindow &glwindow = windows[p_window_id];
409
410
if (!glwindow.initialized) {
411
return EGL_NO_CONTEXT;
412
}
413
414
GLDisplay &display = displays[glwindow.gldisplay_id];
415
416
return display.egl_context;
417
}
418
419
EGLDisplay EGLManager::get_display(DisplayServer::WindowID p_window_id) {
420
GLWindow &glwindow = windows[p_window_id];
421
422
if (!glwindow.initialized) {
423
return EGL_NO_CONTEXT;
424
}
425
426
GLDisplay &display = displays[glwindow.gldisplay_id];
427
428
return display.egl_display;
429
}
430
431
EGLConfig EGLManager::get_config(DisplayServer::WindowID p_window_id) {
432
GLWindow &glwindow = windows[p_window_id];
433
434
if (!glwindow.initialized) {
435
return nullptr;
436
}
437
438
GLDisplay &display = displays[glwindow.gldisplay_id];
439
440
return display.egl_config;
441
}
442
443
Error EGLManager::initialize(void *p_native_display) {
444
#if defined(GLAD_ENABLED) && !defined(EGL_STATIC)
445
// Loading EGL with a new display gets us just the bare minimum API. We'll then
446
// have to temporarily get a proper display and reload EGL once again to
447
// initialize everything else.
448
if (!gladLoaderLoadEGL(EGL_NO_DISPLAY)) {
449
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Can't load EGL dynamic library.");
450
}
451
452
EGLDisplay tmp_display = EGL_NO_DISPLAY;
453
454
if (GLAD_EGL_EXT_platform_base) {
455
#ifdef EGL_EXT_platform_base
456
// eglGetPlatformDisplayEXT wants its attributes as EGLint.
457
Vector<EGLint> attribs;
458
for (const EGLAttrib &attrib : _get_platform_display_attributes()) {
459
attribs.push_back((EGLint)attrib);
460
}
461
tmp_display = eglGetPlatformDisplayEXT(_get_platform_extension_enum(), p_native_display, attribs.ptr());
462
#endif // EGL_EXT_platform_base
463
} else {
464
WARN_PRINT("EGL: EGL_EXT_platform_base not found during init, using default platform.");
465
EGLNativeDisplayType *native_display_type = (EGLNativeDisplayType *)p_native_display;
466
tmp_display = eglGetDisplay(*native_display_type);
467
}
468
469
if (tmp_display == EGL_NO_DISPLAY) {
470
eglTerminate(tmp_display);
471
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Can't get a valid initial EGL display.");
472
}
473
474
eglInitialize(tmp_display, nullptr, nullptr);
475
476
int version = gladLoaderLoadEGL(tmp_display);
477
if (!version) {
478
eglTerminate(tmp_display);
479
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Can't load EGL dynamic library.");
480
}
481
482
int major = GLAD_VERSION_MAJOR(version);
483
int minor = GLAD_VERSION_MINOR(version);
484
485
print_verbose(vformat("Loaded EGL %d.%d", major, minor));
486
487
ERR_FAIL_COND_V_MSG(!GLAD_EGL_VERSION_1_4, ERR_UNAVAILABLE, vformat("EGL version is too old! %d.%d < 1.4", major, minor));
488
489
eglTerminate(tmp_display);
490
#endif
491
492
#ifdef EGL_ANDROID_blob_cache
493
shader_cache_dir = Engine::get_singleton()->get_shader_cache_path();
494
if (shader_cache_dir.is_empty()) {
495
shader_cache_dir = "user://";
496
}
497
Error err = OK;
498
Ref<DirAccess> da = DirAccess::open(shader_cache_dir);
499
if (da.is_null()) {
500
ERR_PRINT("EGL: Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
501
shader_cache_dir = String();
502
} else {
503
err = da->change_dir(String("shader_cache").path_join("EGL"));
504
if (err != OK) {
505
err = da->make_dir_recursive(String("shader_cache").path_join("EGL"));
506
}
507
if (err != OK) {
508
ERR_PRINT("EGL: Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
509
shader_cache_dir = String();
510
} else {
511
shader_cache_dir = shader_cache_dir.path_join(String("shader_cache").path_join("EGL"));
512
}
513
}
514
#endif
515
516
String client_extensions_string = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
517
518
// If the above method fails, we don't support client extensions, so there's nothing to check.
519
if (eglGetError() == EGL_SUCCESS) {
520
const char *platform = _get_platform_extension_name();
521
if (!client_extensions_string.split(" ").has(platform)) {
522
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, vformat("EGL platform extension \"%s\" not found.", platform));
523
}
524
}
525
526
return OK;
527
}
528
529
EGLManager::EGLManager() {
530
}
531
532
EGLManager::~EGLManager() {
533
for (unsigned int i = 0; i < displays.size(); i++) {
534
eglTerminate(displays[i].egl_display);
535
}
536
}
537
538
#endif // EGL_ENABLED
539
540