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

Commit b90f1d3c authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

Optimise loading wallpaper from disk to memory using chunks.

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 optimised
by reading in 8 KB chunks via using the readAllBytes() function.

This CL decreases the average core (without the animations) user
switch duration by 32%, from 815.69ms to 556.66ms on a high end tablet
device. Impact is expected to be higher on lower end devices.

This CL also halves the memory consumtion since byte array is no more
getting duplicated.

Bug: 316331405
Test: Perfetto trace comparison
Flag: NONE
Change-Id: I1d6e556e51b0f81e18174f77edfc85ef7d8bb62a
parent 0e97a0b2
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
@@ -90,7 +90,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;
@@ -744,14 +743,8 @@ public class WallpaperManager {
                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());
                    try (InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pfd)) {
                        ImageDecoder.Source src = ImageDecoder.createSource(is.readAllBytes());
                        return ImageDecoder.decodeBitmap(src, ((decoder, info, source) -> {
                            // Mutable and hardware config can't be set at the same time.
                            decoder.setMutableRequired(!hardware);