summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Yasin Kilicdere <tyk@google.com> 2024-01-04 17:39:33 +0000
committer Yasin Kilicdere <tyk@google.com> 2024-01-05 15:35:44 +0000
commit16253de0e32a4522e16812ec98449fe080d7c61e (patch)
treec5d6e74348899bc36860175bbe9097669534b479
parentbf4d2e055c9de9a712666ed862bd0724513e7cb8 (diff)
Optimise loading wallpaper from disk, by skipping unnecessary steps.
Before this optimisation, target user's image wallpaper was being read from disk to memory one byte at a time, which was causing too much CPU consumption and slowness. With this CL, that process is avoided by providing the input stream to ImageDecoder.createSource directly. This CL decreases the average core (without the animations) user switch duration by 36%, from 982.82ms to 633.50ms on a high end tablet device. Impact is expected to be higher on lower end devices. And the standard deviation (spikiness) decreased by 39% from 246.21ms to 151.80ms. This CL also avoids the extra memory consumtion. Which was twice the size of the file. go/optimise-loadwallpaper-during-userswitch-2 Bug: 316331405 Test: Perfetto trace comparison Flag: NONE Change-Id: Idfd8ceaaf9fe3930db2057a6b9adc204991f5139
-rw-r--r--core/java/android/app/WallpaperManager.java34
1 files changed, 14 insertions, 20 deletions
diff --git a/core/java/android/app/WallpaperManager.java b/core/java/android/app/WallpaperManager.java
index c200db30e0e2..62db90f79091 100644
--- a/core/java/android/app/WallpaperManager.java
+++ b/core/java/android/app/WallpaperManager.java
@@ -92,7 +92,6 @@ import com.android.internal.R;
import libcore.io.IoUtils;
import java.io.BufferedInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -745,26 +744,21 @@ public class WallpaperManager {
params, userId, /* getCropped = */ true);
Trace.endSection();
- if (pfd != null) {
- try (BufferedInputStream bis = new BufferedInputStream(
- new ParcelFileDescriptor.AutoCloseInputStream(pfd))) {
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int data;
- while ((data = bis.read()) != -1) {
- baos.write(data);
+ if (pfd == null) {
+ return null;
+ }
+ try (InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pfd)) {
+ ImageDecoder.Source src = ImageDecoder.createSource(context.getResources(), is);
+ return ImageDecoder.decodeBitmap(src, ((decoder, info, source) -> {
+ // Mutable and hardware config can't be set at the same time.
+ decoder.setMutableRequired(!hardware);
+ // Let's do color management
+ if (cmProxy != null) {
+ cmProxy.doColorManagement(decoder, info);
}
- ImageDecoder.Source src = ImageDecoder.createSource(baos.toByteArray());
- return ImageDecoder.decodeBitmap(src, ((decoder, info, source) -> {
- // Mutable and hardware config can't be set at the same time.
- decoder.setMutableRequired(!hardware);
- // Let's do color management
- if (cmProxy != null) {
- cmProxy.doColorManagement(decoder, info);
- }
- }));
- } catch (OutOfMemoryError | IOException e) {
- Log.w(TAG, "Can't decode file", e);
- }
+ }));
+ } catch (OutOfMemoryError | IOException e) {
+ Log.w(TAG, "Can't decode file", e);
}
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();