Skip to content

Commit 54488e0

Browse files
committed
Fix race conditions in MetalRedrawer (#2672)
Await when all scheduled tasks in async rendering queue are completed before clearing resources. Move surface and renderTarget closing to the critical section. Fixes: https://youtrack.jetbrains.com/issue/CMP-8144/Crash-when-enabling-parallel-rendering-in-MetalRedrawer-for-iOS Fixes: https://youtrack.jetbrains.com/issue/CMP-7598/Skia-crash-on-iOS-GrResourceCachenotifyARefCntReachedZero ## Release Notes ### Fixes - iOS - Fix the crash that may occur when the Compose container disposes.
1 parent d9437a3 commit 54488e0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

compose/ui/ui/src/uikitMain/kotlin/androidx/compose/ui/window/MetalRedrawer.uikit.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ internal class MetalRedrawer(
277277
caDisplayLink?.invalidate()
278278
caDisplayLink = null
279279

280+
// Wait until all scheduled rendering tasks are completed to eliminate race conditions
281+
// when clearing the resources
282+
if (useSeparateRenderThreadWhenPossible) {
283+
trace("MetalRedrawer:dispose:waitForAsyncRenderingTasks") {
284+
dispatch_sync(renderingDispatchQueue) {}
285+
}
286+
}
287+
280288
pictureRecorder.close()
281289
context.close()
282290
}
@@ -430,6 +438,9 @@ internal class MetalRedrawer(
430438
picture.close()
431439
surface.flushAndSubmit()
432440

441+
surface.close()
442+
renderTarget.close()
443+
433444
if (useSeparateRenderThreadWhenPossible) {
434445
dispatch_semaphore_signal(drawCanvasSemaphore)
435446
}
@@ -469,9 +480,6 @@ internal class MetalRedrawer(
469480
}
470481
}
471482

472-
surface.close()
473-
renderTarget.close()
474-
475483
// Track current inflight command buffers to synchronously wait for their schedule in case app goes background
476484
inflightCommandBuffers.add(commandBuffer)
477485

0 commit comments

Comments
 (0)