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

Commit 09b03c55 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Optimizing some slow calls on the main thread

Change-Id: I4eed5f7e5bf1316556d6985a610918fc709a7471
parent fc86a9c7
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -17,9 +17,13 @@ package com.android.launcher3.icons;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.os.Build.VERSION_CODES;

/**
 * Interface representing a bitmap draw operation.
@@ -29,6 +33,7 @@ public interface BitmapRenderer {
    boolean USE_HARDWARE_BITMAP = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;

    static Bitmap createSoftwareBitmap(int width, int height, BitmapRenderer renderer) {
        GraphicsUtils.noteNewBitmapCreated();
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        renderer.draw(new Canvas(result));
        return result;
@@ -40,11 +45,26 @@ public interface BitmapRenderer {
            return createSoftwareBitmap(width, height, renderer);
        }

        GraphicsUtils.noteNewBitmapCreated();
        Picture picture = new Picture();
        renderer.draw(picture.beginRecording(width, height));
        picture.endRecording();
        return Bitmap.createBitmap(picture);
    }

    /**
     * Returns a bitmap from subset of the source bitmap. The new bitmap may be the
     * same object as source, or a copy may have been made.
     */
    static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
        if (Build.VERSION.SDK_INT >= VERSION_CODES.O && source.getConfig() == Config.HARDWARE) {
            return createHardwareBitmap(width, height, c -> c.drawBitmap(source,
                    new Rect(x, y, x + width, y + height), new RectF(0, 0, width, height), null));
        } else {
            GraphicsUtils.noteNewBitmapCreated();
            return Bitmap.createBitmap(source, x, y, width, height);
        }
    }

    void draw(Canvas out);
}
+7 −2
Original line number Diff line number Diff line
@@ -21,11 +21,11 @@ import android.graphics.Region;
import android.graphics.RegionIterator;
import android.util.Log;

import androidx.annotation.ColorInt;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import androidx.annotation.ColorInt;

public class GraphicsUtils {

    private static final String TAG = "GraphicsUtils";
@@ -73,4 +73,9 @@ public class GraphicsUtils {
        }
        return area;
    }

    /**
     * Utility method to track new bitmap creation
     */
    public static void noteNewBitmapCreated() { }
}
+1 −4
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.launcher3.icons;
import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
@@ -135,9 +134,7 @@ public class ShadowGenerator {
            bounds.offsetTo(center - width / 2f, center - height / 2f);

            int size = center * 2;
            Bitmap result = Bitmap.createBitmap(size, size, Config.ARGB_8888);
            drawShadow(new Canvas(result));
            return result;
            return BitmapRenderer.createHardwareBitmap(size, size, this::drawShadow);
        }

        public void drawShadow(Canvas c) {
+4 −3
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ import com.android.launcher3.views.Transposable;
import com.android.launcher3.widget.PendingAddShortcutInfo;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
@@ -465,12 +464,14 @@ public final class Utilities {
    }

    public static SharedPreferences getPrefs(Context context) {
        return context.getSharedPreferences(
        // Use application context for shared preferences, so that we use a single cached instance
        return context.getApplicationContext().getSharedPreferences(
                LauncherFiles.SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE);
    }

    public static SharedPreferences getDevicePrefs(Context context) {
        return context.getSharedPreferences(
        // Use application context for shared preferences, so that we use a single cached instance
        return context.getApplicationContext().getSharedPreferences(
                LauncherFiles.DEVICE_PREFERENCES_KEY, Context.MODE_PRIVATE);
    }

+3 −4
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
@@ -79,6 +78,7 @@ import com.android.launcher3.folder.PreviewBackground;
import com.android.launcher3.graphics.DragPreviewProvider;
import com.android.launcher3.graphics.PreloadIconDrawable;
import com.android.launcher3.graphics.RotationMode;
import com.android.launcher3.icons.BitmapRenderer;
import com.android.launcher3.logging.UserEventDispatcher;
import com.android.launcher3.pageindicators.WorkspacePageIndicator;
import com.android.launcher3.popup.PopupContainerWithArrow;
@@ -2606,11 +2606,10 @@ public class Workspace extends PagedView<WorkspacePageIndicator>

        int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY);
        int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY);
        Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1],
                Bitmap.Config.ARGB_8888);
        layout.measure(width, height);
        layout.layout(0, 0, unScaledSize[0], unScaledSize[1]);
        layout.draw(new Canvas(b));
        Bitmap b = BitmapRenderer.createHardwareBitmap(
                unScaledSize[0], unScaledSize[1], layout::draw);
        layout.setVisibility(visibility);
        return b;
    }
Loading