Optimizing some slow calls on the main thread

Change-Id: I4eed5f7e5bf1316556d6985a610918fc709a7471
This commit is contained in:
Sunny Goyal committed 2020-01-28 22:18:14 +00:00
1 parent fc86a9c75c
commit 09b03c55df
8 files changed
+56 -37

No files matched your search

@@ -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);
}