From 3dea8d75876d256b7ff605ea5c28bed8df387fcc Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:07:56 +0700 Subject: [PATCH] perf(android): reuse the cover copies when only the media item's text changes Lock screen lyrics send the playing item again with each lyric line as its title. Every send copied the unchanged cover twice at 512 px for the session and once at 256 px for the notification (about 2.3 MB on the main thread), and refreshed the playback state only to add the media ID it already had. The copies of the current cover are now kept while it is the same bitmap and still intact, and the refresh is skipped when the state already names the item. --- third_party/audio_service/MORNYE_PATCHES.md | 18 ++++ .../com/ryanheise/audioservice/ArtCopies.java | 55 ++++++++++++ .../ryanheise/audioservice/AudioService.java | 21 +++-- .../ryanheise/audioservice/MediaArtwork.java | 26 ++++-- .../ryanheise/audioservice/ArtCopiesTest.java | 89 +++++++++++++++++++ 5 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/ArtCopies.java create mode 100644 third_party/audio_service/android/src/test/java/com/ryanheise/audioservice/ArtCopiesTest.java diff --git a/third_party/audio_service/MORNYE_PATCHES.md b/third_party/audio_service/MORNYE_PATCHES.md index ccd3f36c6..63533bc0e 100644 --- a/third_party/audio_service/MORNYE_PATCHES.md +++ b/third_party/audio_service/MORNYE_PATCHES.md @@ -100,6 +100,22 @@ from the upstream package are not vendored. their pixels; the notification's 256 px large icon is now scaled from the 512 px bitmap instead of from up to 1023 px (owner-approved). The app's instrumented `MediaControlMetadataTest` covers the 512 px bound. +- Android: reuse the session's and notification's copies of the current + cover while it is the same bitmap. The app's lock screen lyrics send the + playing item again with each lyric line as its title, and each send copied + the cover twice at 512 px for the session (`ALBUM_ART`, `DISPLAY_ICON`) + and once at 256 px for the notification's large icon, about 2.3 MB on the + main thread per line. `ArtCopies` (new, covered by `ArtCopiesTest.java` + under `android/src/test`) keeps one copy per key for the current + `artBitmap`; `MediaArtwork.forSession(metadata, copies)` and + `MediaArtwork.copy(source, size, copies, key)` use it, the one-argument + `forSession` (used by `MediaControlMetadataTest`) still copies. Each key + still gets its own copy, a recycled copy is made again, a recycled cover + has none (as before), another cover drops the kept copies without + recycling them, and `onDestroy` clears them. `publishMetadata` also skips + its playback state refresh when the state already names the item's media + ID, since the refresh only adds that ID. The notification is still + rebuilt, as its title changes. - Darwin: `setMediaItem` reuses the last `MPMediaItemArtwork` while its `artCacheFile` has the same path, device, inode, size and modification time (`artworkForFile:`), instead of reading the file into a new image and @@ -179,6 +195,7 @@ and Android Auto instead of disguising them as unrelated custom actions. Compared against the pub.dev 0.18.19 package in the local pub cache: - `android/build.gradle.kts`, `android/gradle/wrapper/gradle-wrapper.properties` +- `android/src/main/java/com/ryanheise/audioservice/ArtCopies.java` (new) - `android/src/main/java/com/ryanheise/audioservice/AudioService.java` - `android/src/main/java/com/ryanheise/audioservice/AudioServicePlugin.java` - `android/src/main/java/com/ryanheise/audioservice/MediaArtwork.java` (new) @@ -186,6 +203,7 @@ Compared against the pub.dev 0.18.19 package in the local pub cache: - `android/src/main/java/com/ryanheise/audioservice/PinnedMetadataCache.java` (new) - `android/src/main/java/com/ryanheise/audioservice/RecentArtCache.java` (new) - `android/src/main/java/com/ryanheise/audioservice/QueueItemIndex.java` (new) +- `android/src/test/java/com/ryanheise/audioservice/ArtCopiesTest.java` (new) - `android/src/test/java/com/ryanheise/audioservice/PinnedMetadataCacheTest.java` (new) - `android/src/test/java/com/ryanheise/audioservice/RecentArtCacheTest.java` (new) - `android/src/test/java/com/ryanheise/audioservice/QueueItemIndexTest.java` (new) diff --git a/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/ArtCopies.java b/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/ArtCopies.java new file mode 100644 index 000000000..e6589d28f --- /dev/null +++ b/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/ArtCopies.java @@ -0,0 +1,55 @@ +package com.ryanheise.audioservice; + +import java.util.HashMap; +import java.util.Map; + +/** + * The copies the session and notification take of the current cover, kept + * while the cover is the same object, so an update that changes only text + * (a lock screen lyric line) reuses them instead of copying the art again. + * Nothing is recycled: the session and notification may still show a copy. + */ +final class ArtCopies { + interface Copier { + T copy(T source); + } + + interface Check { + boolean usable(T value); + } + + private final Check usable; + private final Map copies = new HashMap<>(); + private T source; + + ArtCopies(Check usable) { + this.usable = usable; + } + + /** The copy of source for key: a kept one while it is intact, else a new one. */ + synchronized T get(T source, String key, Copier copier) { + if (source == null || !usable.usable(source)) return null; + if (source != this.source) { + copies.clear(); + this.source = source; + } + T copy = copies.get(key); + if (copy != null && usable.usable(copy)) return copy; + copy = copier.copy(source); + if (copy == null) { + copies.remove(key); + } else { + copies.put(key, copy); + } + return copy; + } + + synchronized void clear() { + copies.clear(); + source = null; + } + + synchronized int size() { + return copies.size(); + } +} diff --git a/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java b/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java index 80e5cac26..8040bf136 100644 --- a/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java +++ b/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java @@ -74,6 +74,7 @@ public class AudioService extends MediaBrowserServiceCompat { public static final int KEYCODE_BYPASS_PAUSE = KeyEvent.KEYCODE_MEDIA_RECORD; public static final int MAX_COMPACT_ACTIONS = 3; private static final int ART_CACHE_ENTRIES = 3; + private static final String NOTIFICATION_ART = "notification"; private static final long AUTO_ENABLED_ACTIONS = PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY @@ -297,6 +298,8 @@ public class AudioService extends MediaBrowserServiceCompat { private int[] compactActionIndices; private MediaMetadataCompat mediaMetadata; private Bitmap artBitmap; + // The session's and notification's copies of the current cover. + private final ArtCopies artCopies = MediaArtwork.newCopies(); private String notificationChannelId; private RecentArtCache artBitmapCache; private boolean playing = false; @@ -393,6 +396,7 @@ public class AudioService extends MediaBrowserServiceCompat { queue = Collections.emptyList(); controls.clear(); artBitmapCache.evictAll(); + artCopies.clear(); compactActionIndices = null; releaseMediaSession(); ServiceCompat.stopForeground(this, config.androidResumeOnClick @@ -688,7 +692,7 @@ public class AudioService extends MediaBrowserServiceCompat { if (description.getDescription() != null) builder.setSubText(description.getDescription()); if (artBitmap != null) - builder.setLargeIcon(MediaArtwork.copy(artBitmap, 256)); + builder.setLargeIcon(MediaArtwork.copy(artBitmap, 256, artCopies, NOTIFICATION_ART)); } if (config.androidNotificationClickStartsActivity) builder.setContentIntent(mediaSession.getController().getSessionActivity()); @@ -887,7 +891,7 @@ public class AudioService extends MediaBrowserServiceCompat { if (destroyed || mediaSession == null) return; mediaMetadata = MediaArtwork.sanitize(mediaMetadata); try { - mediaSession.setMetadata(MediaArtwork.forSession(mediaMetadata)); + mediaSession.setMetadata(MediaArtwork.forSession(mediaMetadata, artCopies)); } catch (IllegalArgumentException | IllegalStateException error) { Log.w("AudioService", "Artwork rejected by media session; publishing text metadata", error); mediaMetadata = MediaArtwork.withoutArtwork(mediaMetadata); @@ -899,15 +903,22 @@ public class AudioService extends MediaBrowserServiceCompat { this.mediaMetadata = mediaMetadata; artBitmap = bitmap != null && !bitmap.isRecycled() ? bitmap : null; PlaybackStateCompat state = mediaSession.getController().getPlaybackState(); - if (state != null) { + String mediaId = mediaMetadata.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID); + // The same item again (a lock screen lyric line) leaves the state as it is. + if (state != null && !hasMediaId(state, mediaId)) { Bundle extras = state.getExtras() == null ? new Bundle() : new Bundle(state.getExtras()); - extras.putString(MediaConstants.PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID, - mediaMetadata.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID)); + extras.putString(MediaConstants.PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID, mediaId); mediaSession.setPlaybackState(new PlaybackStateCompat.Builder(state).setExtras(extras).build()); } updateNotification(); } + private static boolean hasMediaId(PlaybackStateCompat state, String mediaId) { + Bundle extras = state.getExtras(); + return mediaId != null && extras != null + && mediaId.equals(extras.getString(MediaConstants.PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID)); + } + private MediaMetadataCompat putArtToMetadata(MediaMetadataCompat mediaMetadata, Bitmap bitmap) { return new MediaMetadataCompat.Builder(mediaMetadata) .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap) diff --git a/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/MediaArtwork.java b/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/MediaArtwork.java index 2488e15fa..c8f16bf73 100644 --- a/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/MediaArtwork.java +++ b/third_party/audio_service/android/src/main/java/com/ryanheise/audioservice/MediaArtwork.java @@ -50,19 +50,33 @@ final class MediaArtwork { } static MediaMetadataCompat sanitize(MediaMetadataCompat metadata) { - return transform(metadata, false, false); + return transform(metadata, false, false, null); } static MediaMetadataCompat forSession(MediaMetadataCompat metadata) { - return transform(metadata, true, false); + return transform(metadata, true, false, null); + } + + /** Like {@link #forSession(MediaMetadataCompat)}, reusing the copies kept in copies. */ + static MediaMetadataCompat forSession(MediaMetadataCompat metadata, ArtCopies copies) { + return transform(metadata, true, false, copies); } static MediaMetadataCompat withoutArtwork(MediaMetadataCompat metadata) { - return transform(metadata, false, true); + return transform(metadata, false, true, null); + } + + /** The cover's copy for key, kept in copies while the cover stays the same. */ + static Bitmap copy(Bitmap source, int maxDimension, ArtCopies copies, String key) { + return copies.get(source, key, bitmap -> copy(bitmap, maxDimension)); + } + + static ArtCopies newCopies() { + return new ArtCopies<>(bitmap -> !bitmap.isRecycled()); } private static MediaMetadataCompat transform(MediaMetadataCompat metadata, - boolean copyArtwork, boolean omit) { + boolean copyArtwork, boolean omit, ArtCopies copies) { MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(metadata); Bundle values = metadata.getBundle(); for (String key : values.keySet()) { @@ -72,7 +86,9 @@ final class MediaArtwork { // Each key gets its own copy: some frameworks consume a bitmap // while scaling it, before visiting another key with the same art. builder.putBitmap(key, omit || bitmap.isRecycled() ? null - : copyArtwork ? copy(bitmap, MAX_DIMENSION) : bitmap); + : !copyArtwork ? bitmap + : copies == null ? copy(bitmap, MAX_DIMENSION) + : copy(bitmap, MAX_DIMENSION, copies, key)); } return builder.build(); } diff --git a/third_party/audio_service/android/src/test/java/com/ryanheise/audioservice/ArtCopiesTest.java b/third_party/audio_service/android/src/test/java/com/ryanheise/audioservice/ArtCopiesTest.java new file mode 100644 index 000000000..5961b5328 --- /dev/null +++ b/third_party/audio_service/android/src/test/java/com/ryanheise/audioservice/ArtCopiesTest.java @@ -0,0 +1,89 @@ +package com.ryanheise.audioservice; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +public class ArtCopiesTest { + /** A cover or a copy of one; recycled ones are unusable. */ + private static final class Art { + final String name; + boolean recycled; + + Art(String name) { + this.name = name; + } + } + + private final List copied = new ArrayList<>(); + private final ArtCopies copies = new ArtCopies<>(art -> !art.recycled); + + private Art copy(Art source, String key) { + return copies.get(source, key, art -> { + copied.add(art.name + "@" + key); + return new Art(art.name + "@" + key); + }); + } + + @Test + public void lyricLinesOverTheSameCoverCopyItOncePerKey() { + Art cover = new Art("cover"); + Set shown = new HashSet<>(); + for (int line = 0; line < 100; line++) { + shown.add(copy(cover, "album")); + shown.add(copy(cover, "icon")); + shown.add(copy(cover, "notification")); + } + assertEquals(3, shown.size()); + assertEquals(3, copied.size()); + assertNotSame(copy(cover, "album"), copy(cover, "icon")); + } + + @Test + public void anotherCoverGetsNewCopiesAndDropsTheOldOnes() { + Art first = new Art("first"); + Art album = copy(first, "album"); + Art second = new Art("second"); + Art next = copy(second, "album"); + assertNotSame(album, next); + assertEquals(1, copies.size()); + assertNotSame(album, copy(first, "album")); + assertEquals(3, copied.size()); + assertEquals(false, album.recycled); + } + + @Test + public void aRecycledCopyIsMadeAgainAndARecycledCoverHasNone() { + Art cover = new Art("cover"); + Art album = copy(cover, "album"); + album.recycled = true; + Art again = copy(cover, "album"); + assertNotSame(album, again); + assertSame(again, copy(cover, "album")); + assertEquals(2, copied.size()); + + cover.recycled = true; + assertNull(copy(cover, "album")); + assertNull(copies.get(null, "album", art -> art)); + assertEquals(2, copied.size()); + } + + @Test + public void aFailedCopyIsTriedAgainAndClearForgetsEverything() { + Art cover = new Art("cover"); + assertNull(copies.get(cover, "album", art -> null)); + assertEquals(0, copies.size()); + Art album = copy(cover, "album"); + assertSame(album, copy(cover, "album")); + copies.clear(); + assertEquals(0, copies.size()); + assertNotSame(album, copy(cover, "album")); + } +}