Skip to content

Commit d70e694

Browse files
committed
Support multi-threaded rendering with OpenGL on Android
1 parent a590be0 commit d70e694

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

platform/android/display_server_android.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,20 +484,13 @@ int64_t DisplayServerAndroid::window_get_native_handle(HandleType p_handle_type,
484484
}
485485
#ifdef GLES3_ENABLED
486486
case DISPLAY_HANDLE: {
487-
if (rendering_driver == "opengl3") {
488-
return reinterpret_cast<int64_t>(eglGetCurrentDisplay());
489-
}
490-
return 0;
487+
return reinterpret_cast<int64_t>(egl_display);
491488
}
492489
case OPENGL_CONTEXT: {
493-
if (rendering_driver == "opengl3") {
494-
return reinterpret_cast<int64_t>(eglGetCurrentContext());
495-
}
496-
return 0;
490+
return reinterpret_cast<int64_t>(egl_context);
497491
}
498492
case EGL_DISPLAY: {
499-
// @todo Find a way to get this from the Java side.
500-
return 0;
493+
return reinterpret_cast<int64_t>(egl_display);
501494
}
502495
case EGL_CONFIG: {
503496
// @todo Find a way to get this from the Java side.
@@ -792,6 +785,13 @@ DisplayServerAndroid::DisplayServerAndroid(const String &p_rendering_driver, Dis
792785
#if defined(GLES3_ENABLED)
793786
if (rendering_driver == "opengl3") {
794787
RasterizerGLES3::make_current(false);
788+
swap_own_buffers = OS::get_singleton()->is_separate_thread_rendering_enabled();
789+
// These will have been set via eglMakeCurrent() on the Java side. It would be nice to explicitly pass
790+
// them from Java, but there's no way to get the native handles with javax.microedition.khronos.egl;
791+
// we'd need to switch to android.opengl.
792+
egl_display = eglGetCurrentDisplay();
793+
egl_surface = eglGetCurrentSurface(EGL_DRAW);
794+
egl_context = eglGetCurrentContext();
795795
}
796796
#endif
797797

@@ -963,6 +963,28 @@ bool DisplayServerAndroid::should_swap_buffers() const {
963963

964964
void DisplayServerAndroid::swap_buffers() {
965965
swap_buffers_flag = true;
966+
967+
#ifdef GLES3_ENABLED
968+
if (swap_own_buffers) {
969+
eglSwapBuffers(egl_display, egl_surface);
970+
}
971+
#endif
972+
}
973+
974+
void DisplayServerAndroid::gl_window_make_current(DisplayServer::WindowID p_window_id) {
975+
#ifdef GLES3_ENABLED
976+
if (swap_own_buffers && egl_display && p_window_id == DisplayServer::MAIN_WINDOW_ID) {
977+
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
978+
}
979+
#endif
980+
}
981+
982+
void DisplayServerAndroid::release_rendering_thread() {
983+
#ifdef GLES3_ENABLED
984+
if (swap_own_buffers && egl_display) {
985+
eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
986+
}
987+
#endif
966988
}
967989

968990
void DisplayServerAndroid::set_native_icon(const String &p_filename) {

platform/android/display_server_android.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,21 @@ class DisplayServerAndroid : public DisplayServer {
7272

7373
bool keep_screen_on;
7474
bool swap_buffers_flag;
75+
bool swap_own_buffers = false;
7576

7677
CursorShape cursor_shape = CursorShape::CURSOR_ARROW;
7778

7879
#if defined(RD_ENABLED)
7980
RenderingContextDriver *rendering_context = nullptr;
8081
RenderingDevice *rendering_device = nullptr;
8182
#endif
83+
84+
#ifdef GLES3_ENABLED
85+
void *egl_display = nullptr;
86+
void *egl_surface = nullptr;
87+
void *egl_context = nullptr;
88+
#endif
89+
8290
NativeMenu *native_menu = nullptr;
8391

8492
ObjectID window_attached_instance_id;
@@ -193,6 +201,7 @@ class DisplayServerAndroid : public DisplayServer {
193201

194202
virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
195203
virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
204+
virtual void gl_window_make_current(DisplayServer::WindowID p_window_id) override;
196205

197206
virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
198207
virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
@@ -251,7 +260,9 @@ class DisplayServerAndroid : public DisplayServer {
251260

252261
void reset_swap_buffers_flag();
253262
bool should_swap_buffers() const;
263+
inline bool get_swap_own_buffers() const { return swap_own_buffers; }
254264
virtual void swap_buffers() override;
265+
virtual void release_rendering_thread() override;
255266

256267
virtual void set_native_icon(const String &p_filename) override;
257268
virtual void set_icon(const Ref<Image> &p_icon) override;

platform/android/java_godot_lib_jni.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env,
267267
Main::setup2(false); // The logo is shown in the next frame otherwise we run into rendering issues
268268
input_handler = new AndroidInputHandler();
269269
step.increment();
270-
return true;
270+
return !DisplayServerAndroid::get_singleton()->get_swap_own_buffers();
271271
}
272272

273273
if (step.get() == STEP_SHOW_LOGO) {
@@ -286,7 +286,7 @@ JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env,
286286
}
287287

288288
step.increment();
289-
return true;
289+
return !DisplayServerAndroid::get_singleton()->get_swap_own_buffers();
290290
}
291291

292292
if (step.get() == STEP_STARTED) {
@@ -305,12 +305,13 @@ JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env,
305305
DisplayServerAndroid::get_singleton()->process_magnetometer(magnetometer);
306306
DisplayServerAndroid::get_singleton()->process_gyroscope(gyroscope);
307307

308+
bool swap_own_buffers = DisplayServerAndroid::get_singleton()->get_swap_own_buffers();
308309
bool should_swap_buffers = false;
309310
if (os_android->main_loop_iterate(&should_swap_buffers)) {
310311
_terminate(env, false);
311312
}
312313

313-
return should_swap_buffers;
314+
return !swap_own_buffers && should_swap_buffers;
314315
}
315316

316317
// Called on the UI thread

0 commit comments

Comments
 (0)