Improve Vulkan Android surface lifecycle (#4323)

This commit is contained in:
Adrian Cojocaru
2026-06-05 19:59:36 +03:00
committed by GitHub
parent a770b79551
commit 269d1bc910
7 changed files with 52 additions and 37 deletions

View File

@@ -249,10 +249,23 @@ void MapRenderer::onSurfaceCreated(JNIEnv& env, const jni::Object<AndroidSurface
// Lock as the initialization can come from the main thread or the GL thread first
std::scoped_lock lock(initialisationMutex);
// The android system will have already destroyed the underlying
// GL resources if this is not the first initialization and an
// attempt to clean them up will fail
UniqueANativeWindow window_ = nullptr;
if (surface) {
window_ = std::unique_ptr<ANativeWindow, std::function<void(ANativeWindow*)>>(
ANativeWindow_fromSurface(&env, reinterpret_cast<jobject>(surface.get())),
[](ANativeWindow* window_) { ANativeWindow_release(window_); });
}
if (backend) {
if (backend->createSurface(window_.get())) {
window = std::move(window_);
return;
}
// The android system will have already destroyed the underlying
// GL resources if this is not the first initialization and an
// attempt to clean them up will fail
gfx::BackendScope backendGuard{backend->getImpl()};
backend->markContextLost();
}
@@ -264,13 +277,7 @@ void MapRenderer::onSurfaceCreated(JNIEnv& env, const jni::Object<AndroidSurface
// Reset in opposite order
renderer.reset();
backend.reset();
window.reset();
if (surface) {
window = std::unique_ptr<ANativeWindow, std::function<void(ANativeWindow*)>>(
ANativeWindow_fromSurface(&env, reinterpret_cast<jobject>(surface.get())),
[](ANativeWindow* window_) { ANativeWindow_release(window_); });
}
window = std::move(window_);
// Create the new backend and renderer
backend = AndroidRendererBackend::Create(window.get());
@@ -316,7 +323,11 @@ void MapRenderer::onRendererReset(JNIEnv&) {
// needs to be called on GL thread
void MapRenderer::onSurfaceDestroyed(JNIEnv&) {
resetRenderer();
if (backend) {
backend->destroySurface();
}
window.reset();
}
void MapRenderer::setSwapBehaviorFlush(JNIEnv&, jboolean flush) {