1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Android: Stabilize camera lifecycle handling

- Pause camera feeds during lifecycle transitions to avoid crashes
- Refresh camera metadata after rotation to keep orientation accurate
This commit is contained in:
KOGA Mitsuhiro
2025-10-21 07:35:54 +09:00
parent 9cd297b6f2
commit 4483871cd3
6 changed files with 284 additions and 29 deletions

View File

@@ -170,6 +170,7 @@ class Godot private constructor(val context: Context) {
*/
private var renderViewInitialized = false
private var primaryHost: GodotHost? = null
private var currentConfig = context.resources.configuration
/**
* Tracks whether we're in the RESUMED lifecycle state.
@@ -757,6 +758,13 @@ class Godot private constructor(val context: Context) {
darkMode = newDarkMode
GodotLib.onNightModeChanged()
}
if (currentConfig.orientation != newConfig.orientation) {
runOnRenderThread {
GodotLib.onScreenRotationChange()
}
}
currentConfig = newConfig
}
/**

View File

@@ -295,6 +295,11 @@ public class GodotLib {
*/
public static native void onRendererPaused();
/**
* Invoked when the screen orientation changes.
*/
static native void onScreenRotationChange();
/**
* @return true if input must be dispatched from the render thread. If false, input is
* dispatched from the UI thread.

View File

@@ -52,6 +52,13 @@
#include "main/main.h"
#include "servers/rendering/rendering_server.h"
#include "modules/modules_enabled.gen.h" // For camera.
#ifdef MODULE_CAMERA_ENABLED
#include "modules/camera/camera_android.h"
#include "servers/camera/camera_server.h"
#endif
#ifndef XR_DISABLED
#include "servers/xr/xr_server.h"
#endif // XR_DISABLED
@@ -593,6 +600,12 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNI
// We force redraw to ensure we render at least once when resuming the app.
Main::force_redraw();
#ifdef MODULE_CAMERA_ENABLED
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
if (camera_android) {
camera_android->handle_resume();
}
#endif // MODULE_CAMERA_ENABLED
if (os_android->get_main_loop()) {
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
}
@@ -603,11 +616,31 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIE
return;
}
#ifdef MODULE_CAMERA_ENABLED
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
if (camera_android) {
camera_android->handle_pause();
}
#endif // MODULE_CAMERA_ENABLED
if (os_android->get_main_loop()) {
os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
}
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onScreenRotationChange(JNIEnv *env, jclass clazz) {
if (step.get() <= STEP_SETUP) {
return;
}
#ifdef MODULE_CAMERA_ENABLED
CameraAndroid *camera_android = Object::cast_to<CameraAndroid>(CameraServer::get_singleton());
if (camera_android) {
camera_android->handle_rotation_change();
}
#endif // MODULE_CAMERA_ENABLED
}
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz) {
Input *input = Input::get_singleton();
if (input) {

View File

@@ -72,6 +72,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_hardwareKeyboardConne
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_filePickerCallback(JNIEnv *env, jclass clazz, jboolean p_ok, jobjectArray p_selected_paths);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onScreenRotationChange(JNIEnv *env, jclass clazz);
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz);
JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getProjectResourceDir(JNIEnv *env, jclass clazz);
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_isEditorHint(JNIEnv *env, jclass clazz);