From 030d44bbd0a6d43bcb57953a603b89df10587f39 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Sun, 1 Nov 2026 20:16:44 +0700 Subject: [PATCH] fix(android): avoid Sony snapshot crash when locking screen --- .../com/zarz/mornye/ImpellerCompatibility.kt | 6 +- .../zarz/mornye/ImpellerCompatibilityTest.kt | 14 ++++ lib/widgets/lyric_blur.dart | 28 +++++++- lib/widgets/settled_blur.dart | 18 ++++- test/lyric_blur_test.dart | 65 +++++++++++++++++++ test/now_playing_lyrics_transition_test.dart | 1 + test/settled_blur_test.dart | 60 +++++++++++++++++ 7 files changed, 184 insertions(+), 8 deletions(-) diff --git a/android/app/src/main/kotlin/com/zarz/mornye/ImpellerCompatibility.kt b/android/app/src/main/kotlin/com/zarz/mornye/ImpellerCompatibility.kt index 17a83136d..be9fc9778 100644 --- a/android/app/src/main/kotlin/com/zarz/mornye/ImpellerCompatibility.kt +++ b/android/app/src/main/kotlin/com/zarz/mornye/ImpellerCompatibility.kt @@ -58,7 +58,9 @@ internal fun hasKnownImpellerRasterCrash(sdk: Int, manufacturer: String, model: (sdk == 35 && manufacturer.equals("xiaomi", ignoreCase = true) && model.equals("25053PC47I", ignoreCase = true)) || (sdk == 36 && manufacturer.equals("samsung", ignoreCase = true) && - model.equals("SM-S711B", ignoreCase = true)) + model.equals("SM-S711B", ignoreCase = true)) || + (sdk == 33 && manufacturer.equals("sony", ignoreCase = true) && + model.equals("SOG04", ignoreCase = true)) internal fun flutterRendererShellArgs(): Array = if (shouldDisableImpeller()) arrayOf("--enable-impeller=false") else emptyArray() @@ -70,7 +72,7 @@ internal fun shouldDisableImpeller(): Boolean { val device = Build.DEVICE.lowercase(Locale.ROOT) val manufacturer = Build.MANUFACTURER.lowercase(Locale.ROOT) - // Reported crashes in Impeller SaveLayer and Samsung's Vulkan QueueSubmit. + // Reported Impeller crashes in SaveLayer, Vulkan submission and GLES snapshots. // Keep these workarounds scoped to the reported device and Android version. if (hasKnownImpellerRasterCrash(Build.VERSION.SDK_INT, manufacturer, model)) { android.util.Log.i("Mornye", "Using Skia for a known Impeller raster crash on $model") diff --git a/android/app/src/test/kotlin/com/zarz/mornye/ImpellerCompatibilityTest.kt b/android/app/src/test/kotlin/com/zarz/mornye/ImpellerCompatibilityTest.kt index 8700bca1d..efc55e9a9 100644 --- a/android/app/src/test/kotlin/com/zarz/mornye/ImpellerCompatibilityTest.kt +++ b/android/app/src/test/kotlin/com/zarz/mornye/ImpellerCompatibilityTest.kt @@ -17,6 +17,20 @@ class ImpellerCompatibilityTest { assertTrue(hasKnownImpellerRasterCrash(36, "SAMSUNG", "sm-s711b")) } + @Test + fun sonySnapshotCrashUsesFallbackOnTheReportedDeviceAndAndroidVersion() { + assertTrue(hasKnownImpellerRasterCrash(33, "Sony", "SOG04")) + assertTrue(hasKnownImpellerRasterCrash(33, "SONY", "sog04")) + } + + @Test + fun sonyFallbackDoesNotIncludeOtherDevicesOrAndroidVersions() { + assertFalse(hasKnownImpellerRasterCrash(32, "Sony", "SOG04")) + assertFalse(hasKnownImpellerRasterCrash(34, "Sony", "SOG04")) + assertFalse(hasKnownImpellerRasterCrash(33, "Sony", "SOG05")) + assertFalse(hasKnownImpellerRasterCrash(33, "Other", "SOG04")) + } + @Test fun samsungFallbackDoesNotIncludeOtherDevicesOrAndroidVersions() { assertFalse(hasKnownImpellerRasterCrash(35, "samsung", "SM-S711B")) diff --git a/lib/widgets/lyric_blur.dart b/lib/widgets/lyric_blur.dart index c0ea8af36..f0160b6c6 100644 --- a/lib/widgets/lyric_blur.dart +++ b/lib/widgets/lyric_blur.dart @@ -79,7 +79,8 @@ class _BlurSnapshot extends SingleChildRenderObjectWidget { /// Paints its child blurred by [sigma], from a cached image once the child /// has not changed for a frame. With a zero [sigma] it is a plain repaint /// boundary. -class RenderLyricBlurSnapshot extends RenderProxyBox { +class RenderLyricBlurSnapshot extends RenderProxyBox + with WidgetsBindingObserver { RenderLyricBlurSnapshot(this._sigma, this._devicePixelRatio, this._source); double _sigma; @@ -114,6 +115,9 @@ class RenderLyricBlurSnapshot extends RenderProxyBox { @visibleForTesting ui.Image? get debugSnapshot => _snapshot; + bool get _canSnapshot => + WidgetsBinding.instance.lifecycleState == AppLifecycleState.resumed; + // Keeps the line on its own layer, like the boundary a live blur needs. @override bool get isRepaintBoundary => true; @@ -141,7 +145,7 @@ class RenderLyricBlurSnapshot extends RenderProxyBox { (_sigma * 3 * _devicePixelRatio).ceil() / _devicePixelRatio, ); final live = _live.layer; - if (_snapshot == null && _unchanged && live != null) { + if (_canSnapshot && _snapshot == null && _unchanged && live != null) { // The previous frame's live layer still holds the same, unchanged line. _snapshot = live.toImageSync(bounds, pixelRatio: _devicePixelRatio); _live.layer = null; @@ -158,9 +162,12 @@ class RenderLyricBlurSnapshot extends RenderProxyBox { final layer = _live.layer ??= ImageFilterLayer(); layer.imageFilter = ui.ImageFilter.blur(sigmaX: _sigma, sigmaY: _sigma); context.pushLayer(layer, super.paint, offset); + if (!_canSnapshot) return; _unchanged = true; SchedulerBinding.instance.addPostFrameCallback((_) { - if (!attached || !_unchanged || _snapshot != null) return; + if (!attached || !_canSnapshot || !_unchanged || _snapshot != null) { + return; + } _repaintingForSnapshot = true; markNeedsPaint(); _repaintingForSnapshot = false; @@ -172,8 +179,23 @@ class RenderLyricBlurSnapshot extends RenderProxyBox { _snapshot = null; } + @override + void attach(PipelineOwner owner) { + super.attach(owner); + WidgetsBinding.instance.addObserver(this); + markNeedsPaint(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + // A hidden surface may lose its GPU context. Rebuild its cache on resume. + _live.layer = null; + markNeedsPaint(); + } + @override void detach() { + WidgetsBinding.instance.removeObserver(this); _unchanged = false; _live.layer = null; _disposeSnapshot(); diff --git a/lib/widgets/settled_blur.dart b/lib/widgets/settled_blur.dart index 4e578965b..94d9bbe35 100644 --- a/lib/widgets/settled_blur.dart +++ b/lib/widgets/settled_blur.dart @@ -60,7 +60,7 @@ class SettledBlur extends SingleChildRenderObjectWidget { /// Paints its child blurred by [sigma]: live while the child changes, then /// from one image with [pixelRatio] image pixels per logical pixel. With a /// zero [sigma] it is a plain repaint boundary. -class RenderSettledBlur extends RenderProxyBox { +class RenderSettledBlur extends RenderProxyBox with WidgetsBindingObserver { RenderSettledBlur({ required double sigma, required double pixelRatio, @@ -103,6 +103,9 @@ class RenderSettledBlur extends RenderProxyBox { @visibleForTesting ui.Image? get debugImage => _image; + bool get _canSnapshot => + WidgetsBinding.instance.lifecycleState == AppLifecycleState.resumed; + // Keeps the blur on its own layer, so moving it reuses that layer. @override bool get isRepaintBoundary => true; @@ -142,7 +145,7 @@ class RenderSettledBlur extends RenderProxyBox { } void _scheduleCheck() { - if (_checkScheduled) return; + if (_checkScheduled || !_canSnapshot) return; _checkScheduled = true; SchedulerBinding.instance.addPostFrameCallback(_check); } @@ -153,7 +156,7 @@ class RenderSettledBlur extends RenderProxyBox { void _check(Duration _) { _checkScheduled = false; final live = _live.layer; - if (!attached || live == null || _image != null) return; + if (!attached || !_canSnapshot || live == null || _image != null) return; if (!live.attached) { // Off screen: it paints live again, and settles, once it is back. markNeedsPaint(); @@ -202,12 +205,21 @@ class RenderSettledBlur extends RenderProxyBox { @override void attach(PipelineOwner owner) { super.attach(owner); + WidgetsBinding.instance.addObserver(this); // Moved boxes repaint, as their old layer may hold an unchecked live blur. markNeedsPaint(); } + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + // A hidden surface may lose its GPU context. Rebuild its cache on resume. + _live.layer = null; + markNeedsPaint(); + } + @override void detach() { + WidgetsBinding.instance.removeObserver(this); _live.layer = null; _disposeImage(); super.detach(); diff --git a/test/lyric_blur_test.dart b/test/lyric_blur_test.dart index 7b4d66a6e..7f8e61026 100644 --- a/test/lyric_blur_test.dart +++ b/test/lyric_blur_test.dart @@ -137,6 +137,71 @@ Future> _pixels(WidgetTester tester) async { } void main() { + final binding = TestWidgetsFlutterBinding.ensureInitialized(); + setUp(() { + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + }); + + for (final state in AppLifecycleState.values) { + if (state == AppLifecycleState.resumed) continue; + testWidgets('releases the snapshot while $state and rebuilds on resume', ( + tester, + ) async { + addTearDown( + () => tester.binding.handleAppLifecycleStateChanged( + AppLifecycleState.resumed, + ), + ); + final before = await _settle(tester, const _Line().build()); + + tester.binding.handleAppLifecycleStateChanged(state); + expect(before.debugDisposed, isTrue); + for (var frame = 0; frame < 3; frame++) { + tester.binding.scheduleForcedFrame(); + await tester.pump(); + expect(_snapshot(tester), isNull); + } + expect(tester.binding.hasScheduledFrame, isFalse); + + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await tester.pump(); + expect(_snapshot(tester), isNull); + expect(_hasLiveBlur(tester), isTrue); + await tester.pump(); + final after = _snapshot(tester)!; + expect(after, isNot(same(before))); + expect(after.debugDisposed, isFalse); + }); + } + + testWidgets('a pending snapshot repaint stops when focus is lost mid-frame', ( + tester, + ) async { + addTearDown( + () => tester.binding.handleAppLifecycleStateChanged( + AppLifecycleState.resumed, + ), + ); + tester.binding.addPostFrameCallback((_) { + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + }); + await tester.pumpWidget(const _Line().build()); + for (var frame = 0; frame < 3; frame++) { + tester.binding.scheduleFrame(); + await tester.pump(); + expect(_snapshot(tester), isNull); + } + expect(tester.binding.hasScheduledFrame, isFalse); + + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await tester.pump(); + await tester.pump(); + expect(_snapshot(tester), isNotNull); + await tester.pumpWidget(const SizedBox()); + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + expect(tester.takeException(), isNull); + }); + testWidgets('a line blurs live until its blur settles', (tester) async { await tester.pumpWidget(const _Line(sigma: 2, settled: false).build()); await tester.pump(); diff --git a/test/now_playing_lyrics_transition_test.dart b/test/now_playing_lyrics_transition_test.dart index 54edcd48b..3ab9ef027 100644 --- a/test/now_playing_lyrics_transition_test.dart +++ b/test/now_playing_lyrics_transition_test.dart @@ -2364,6 +2364,7 @@ void main() { testWidgets( 'Mornye lyrics keep the active line sharp with bold leading-aligned text', (tester) async { + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); await pumpNowPlaying( tester, theme: MornyeTheme.build(Brightness.light), diff --git a/test/settled_blur_test.dart b/test/settled_blur_test.dart index 63faafe0b..976dcbf9b 100644 --- a/test/settled_blur_test.dart +++ b/test/settled_blur_test.dart @@ -225,6 +225,66 @@ double _meanDifference(Uint8List a, Uint8List b) { } void main() { + final binding = TestWidgetsFlutterBinding.ensureInitialized(); + setUp(() { + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + }); + + for (final state in AppLifecycleState.values) { + if (state == AppLifecycleState.resumed) continue; + testWidgets('releases the snapshot while $state and rebuilds on resume', ( + tester, + ) async { + addTearDown( + () => tester.binding.handleAppLifecycleStateChanged( + AppLifecycleState.resumed, + ), + ); + final before = await _settle(tester, _scene(const _Pattern())); + + tester.binding.handleAppLifecycleStateChanged(state); + expect(before.debugDisposed, isTrue); + for (var frame = 0; frame < 3; frame++) { + tester.binding.scheduleForcedFrame(); + await tester.pump(); + expect(_image(tester), isNull); + } + expect(tester.binding.hasScheduledFrame, isFalse); + + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await tester.pump(); + final after = await _settleFrames(tester); + expect(after, isNot(same(before))); + expect(after.debugDisposed, isFalse); + }); + } + + testWidgets('a pending snapshot check stops when focus is lost mid-frame', ( + tester, + ) async { + addTearDown( + () => tester.binding.handleAppLifecycleStateChanged( + AppLifecycleState.resumed, + ), + ); + tester.binding.addPostFrameCallback((_) { + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + }); + await tester.pumpWidget(_scene(const _Pattern())); + for (var frame = 0; frame < 3; frame++) { + await _frame(tester); + expect(_image(tester), isNull); + } + expect(tester.binding.hasScheduledFrame, isFalse); + + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await tester.pump(); + await _settleFrames(tester); + await tester.pumpWidget(const SizedBox()); + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + expect(tester.takeException(), isNull); + }); + testWidgets('blurs live, then draws one image once a frame shows it ' 'unchanged', (tester) async { final image = await _settle(tester, _scene(const _Pattern()));