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

Commit f950c2af authored by Yasin Kilicdere's avatar Yasin Kilicdere Committed by Android (Google) Code Review
Browse files

Merge changes I1d6e556e,I51580b66 into main

* changes:
  Optimise loading wallpaper from disk to memory using chunks.
  Add traces to the code where wallpaper is loaded during a user switch.
parents 06f9a781 b90f1d3c
Loading
Loading
Loading
Loading
+17 −9
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.StrictMode;
import android.os.SystemProperties;
import android.os.Trace;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
@@ -91,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;
@@ -614,11 +614,14 @@ public class WallpaperManager {
                ColorManagementProxy cmProxy) {
            if (mService != null) {
                try {
                    Trace.beginSection("WPMS.isWallpaperSupported");
                    if (!mService.isWallpaperSupported(context.getOpPackageName())) {
                        return null;
                    }
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                } finally {
                    Trace.endSection();
                }
            }
            synchronized (this) {
@@ -629,6 +632,7 @@ public class WallpaperManager {
                mCachedWallpaper = null;
                Bitmap currentWallpaper = null;
                try {
                    Trace.beginSection("WPMS.getCurrentWallpaperLocked");
                    currentWallpaper = getCurrentWallpaperLocked(
                            context, which, userId, hardware, cmProxy);
                } catch (OutOfMemoryError e) {
@@ -654,6 +658,8 @@ public class WallpaperManager {
                        // Post-O apps really most sincerely need the permission.
                        throw e;
                    }
                } finally {
                    Trace.endSection();
                }
                if (currentWallpaper != null) {
                    mCachedWallpaper = new CachedWallpaper(currentWallpaper, userId, which);
@@ -732,19 +738,15 @@ public class WallpaperManager {

            try {
                Bundle params = new Bundle();
                Trace.beginSection("WPMS.getWallpaperWithFeature_" + which);
                ParcelFileDescriptor pfd = mService.getWallpaperWithFeature(
                        context.getOpPackageName(), context.getAttributionTag(), this, which,
                        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());
                    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);
@@ -764,13 +766,18 @@ public class WallpaperManager {
        }

        private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
            Trace.beginSection("WPMS.getDefaultWallpaper_" + which);
            Bitmap defaultWallpaper = mDefaultWallpaper;
            if (defaultWallpaper == null || defaultWallpaper.isRecycled()) {
                defaultWallpaper = null;
                Trace.beginSection("WPMS.openDefaultWallpaper");
                try (InputStream is = openDefaultWallpaper(context, which)) {
                    Trace.endSection();
                    if (is != null) {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        Trace.beginSection("WPMS.decodeStream");
                        defaultWallpaper = BitmapFactory.decodeStream(is, null, options);
                        Trace.endSection();
                    }
                } catch (OutOfMemoryError | IOException e) {
                    Log.w(TAG, "Can't decode stream", e);
@@ -779,6 +786,7 @@ public class WallpaperManager {
            synchronized (this) {
                mDefaultWallpaper = defaultWallpaper;
            }
            Trace.endSection();
            return defaultWallpaper;
        }

+17 −1
Original line number Diff line number Diff line
@@ -317,10 +317,11 @@ public class ImageWallpaper extends WallpaperService {
        }

        private void loadWallpaperAndDrawFrameInternal() {
            Trace.beginSection("ImageWallpaper.CanvasEngine#loadWallpaper");
            Trace.beginSection("WPMS.ImageWallpaper.CanvasEngine#loadWallpaper");
            boolean loadSuccess = false;
            Bitmap bitmap;
            try {
                Trace.beginSection("WPMS.getBitmapAsUser");
                bitmap = mWallpaperManager.getBitmapAsUser(
                        mUserTracker.getUserId(), false, getSourceFlag(), true);
                if (bitmap != null
@@ -333,15 +334,22 @@ public class ImageWallpaper extends WallpaperService {
                // be loaded, we will go into a cycle. Don't do a build where the
                // default wallpaper can't be loaded.
                Log.w(TAG, "Unable to load wallpaper!", exception);
                Trace.beginSection("WPMS.clearWallpaper");
                mWallpaperManager.clearWallpaper(getWallpaperFlags(), mUserTracker.getUserId());
                Trace.endSection();

                try {
                    Trace.beginSection("WPMS.getBitmapAsUser_defaultWallpaper");
                    bitmap = mWallpaperManager.getBitmapAsUser(
                            mUserTracker.getUserId(), false, getSourceFlag(), true);
                } catch (RuntimeException | OutOfMemoryError e) {
                    Log.w(TAG, "Unable to load default wallpaper!", e);
                    bitmap = null;
                } finally {
                    Trace.endSection();
                }
            } finally {
                Trace.endSection();
            }

            if (bitmap == null) {
@@ -355,15 +363,23 @@ public class ImageWallpaper extends WallpaperService {
                loadSuccess = true;
                // recycle the previously loaded bitmap
                if (mBitmap != null) {
                    Trace.beginSection("WPMS.mBitmap.recycle");
                    mBitmap.recycle();
                    Trace.endSection();
                }
                mBitmap = bitmap;
                Trace.beginSection("WPMS.wallpaperSupportsWcg");
                mWideColorGamut = mWallpaperManager.wallpaperSupportsWcg(getSourceFlag());
                Trace.endSection();

                // +2 usages for the color extraction and the delayed unload.
                mBitmapUsages += 2;
                Trace.beginSection("WPMS.recomputeColorExtractorMiniBitmap");
                recomputeColorExtractorMiniBitmap();
                Trace.endSection();
                Trace.beginSection("WPMS.drawFrameInternal");
                drawFrameInternal();
                Trace.endSection();

                /*
                 * after loading, the bitmap will be unloaded after all these conditions: