Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 16253de0 authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

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
parent bf4d2e05
Loading
Loading
Loading
Loading
+14 −20
Original line number Diff line number Diff line
@@ -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,15 +744,11 @@ 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);
                        }
                        ImageDecoder.Source src = ImageDecoder.createSource(baos.toByteArray());
                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);
@@ -765,7 +760,6 @@ public class WallpaperManager {
                } catch (OutOfMemoryError | IOException e) {
                    Log.w(TAG, "Can't decode file", e);
                }
                }
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }