Optimize portable tile rendering and add optional memory saver

This commit is contained in:
Elijah Kuntz 2026-10-19 10:36:30 -07:00
parent 5bb15fa69e
commit 25ea43bea7
18 changed files with 918 additions and 63 deletions

View file

@ -6,8 +6,8 @@ android {
applicationId 'tv.luma.launcher'
minSdk 26
targetSdk 34
versionCode 10
versionName '1.1.7'
versionCode 11
versionName '1.1.8'
}
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8; targetCompatibility JavaVersion.VERSION_1_8 }
}

View file

@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tv.luma.launcher" android:versionCode="10" android:versionName="1.1.7">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tv.luma.launcher" android:versionCode="11" android:versionName="1.1.8">
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="34" />
<uses-feature android:name="android.software.leanback" android:required="true" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />

View file

@ -68,6 +68,8 @@ final class AppRepository {
d.setBounds(97, 34, 191, 128);
}
d.draw(c);
// The app image was painted over an opaque color; let HWUI skip alpha blending.
b.setHasAlpha(false);
icons.put(app.pkg, b);
return b;
} catch (Exception e) {

View file

@ -34,7 +34,7 @@ final class AppTile extends LinearLayout {
setClipToPadding(false);
setWillNotDraw(false);
if (shadowMask == null) {
shadowMask = Bitmap.createBitmap(192, 122, Bitmap.Config.ARGB_8888);
shadowMask = Bitmap.createBitmap(192, 122, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(shadowMask);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.WHITE);
@ -106,7 +106,7 @@ final class AppTile extends LinearLayout {
}
void setAccent(int color) {
shadowPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
shadowPaint.setColor(color);
invalidate();
}
@ -131,8 +131,7 @@ final class AppTile extends LinearLayout {
}
void bitmap(Bitmap b) {
art.bitmap = b;
art.invalidate();
art.image(b);
}
boolean hasImage() {
@ -147,6 +146,9 @@ final class AppTile extends LinearLayout {
private static final class ArtView extends View {
final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
final RectF rect = new RectF();
final Paint imagePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
final Matrix imageMatrix = new Matrix();
BitmapShader imageShader;
final Path clip = new Path();
Bitmap bitmap;
boolean focus;
@ -157,19 +159,6 @@ final class AppTile extends LinearLayout {
super(c);
fallback = s;
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
setElevation(0);
setOutlineProvider(
new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(
2,
2,
Math.max(3, view.getWidth() - 2),
Math.max(3, view.getHeight() - 2),
Ui.dp(c, 12));
}
});
}
protected void onSizeChanged(int w, int h, int ow, int oh) {
@ -180,27 +169,38 @@ final class AppTile extends LinearLayout {
h - Ui.dp(getContext(), 2));
clip.reset();
clip.addRoundRect(rect, Ui.dp(getContext(), 12), Ui.dp(getContext(), 12), Path.Direction.CW);
positionImage();
}
void image(Bitmap b) {
bitmap = b;
imageShader =
b == null ? null : new BitmapShader(b, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
imagePaint.setShader(imageShader);
positionImage();
invalidate();
}
private void positionImage() {
if (bitmap == null || imageShader == null || rect.width() <= 0) return;
float scale = Math.max(rect.width() / bitmap.getWidth(), rect.height() / bitmap.getHeight());
imageMatrix.setScale(scale, scale);
imageMatrix.postTranslate(
rect.centerX() - bitmap.getWidth() * scale / 2,
rect.centerY() - bitmap.getHeight() * scale / 2);
imageShader.setLocalMatrix(imageMatrix);
}
protected void onDraw(Canvas c) {
c.save();
c.clipPath(clip);
p.setColor(0xFF1B263B);
c.drawRect(rect, p);
if (bitmap == null || bitmap.hasAlpha()) {
p.setColor(0xFF1B263B);
c.drawRoundRect(rect, Ui.dp(getContext(), 12), Ui.dp(getContext(), 12), p);
}
if (bitmap != null) {
float scale =
Math.max(rect.width() / bitmap.getWidth(), rect.height() / bitmap.getHeight());
float w = bitmap.getWidth() * scale, h = bitmap.getHeight() * scale;
c.drawBitmap(
bitmap,
null,
new RectF(
rect.centerX() - w / 2,
rect.centerY() - h / 2,
rect.centerX() + w / 2,
rect.centerY() + h / 2),
p);
c.drawRoundRect(rect, Ui.dp(getContext(), 12), Ui.dp(getContext(), 12), imagePaint);
} else {
c.save();
c.clipPath(clip);
p.setColor(Ui.WHITE);
p.setTextSize(Ui.dp(getContext(), 17));
p.setTypeface(Typeface.create("sans-serif-medium", 0));
@ -210,8 +210,11 @@ final class AppTile extends LinearLayout {
rect.centerX(),
rect.centerY() + Ui.dp(getContext(), 5),
p);
c.restore();
}
if (progress > 0) {
c.save();
c.clipPath(clip);
p.setColor(0xA0080D18);
c.drawRect(rect.left, rect.bottom - Ui.dp(getContext(), 4), rect.right, rect.bottom, p);
p.setColor(Ui.GOLD);
@ -221,8 +224,8 @@ final class AppTile extends LinearLayout {
rect.left + rect.width() * progress,
rect.bottom,
p);
c.restore();
}
c.restore();
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(Ui.dp(getContext(), focus ? 1.5f : 1));
p.setColor(focus ? 0xDBF4E8D8 : 0x26FFFFFF);

View file

@ -1277,6 +1277,16 @@ public final class MainActivity extends Activity {
});
}
});
action(
p,
"Memory saver · " + on(prefs.getBoolean("memorySaver", false)),
"Uses less memory for background colors; animation speed varies by device",
() -> {
boolean enabled = !prefs.getBoolean("memorySaver", false);
prefs.edit().putBoolean("memorySaver", enabled).apply();
scene.setMemorySaver(enabled);
appearance();
});
action(
p,
"Focus animations · " + on(motion()),

View file

@ -21,19 +21,24 @@ final class SceneView extends View {
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1),
new ThreadPoolExecutor.DiscardOldestPolicy());
private Bitmap wallpaper, mediaArt, frame, previousFrame;
private Bitmap wallpaper, mediaArt, frame, previousFrame, mask, previousMask;
private final Paint tintPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
private float mix = 1;
private boolean motion = true, active = true, dirty = true;
private volatile int generation;
private int accent = 0xFF5B4125;
private int accent = 0xFF5B4125, targetAccent = accent;
private String scene = "alpine";
private float dim = .24f;
private ValueAnimator animator;
private ValueAnimator animator, colorAnimator;
private boolean useTintMask;
SceneView(Context c) {
super(c);
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
renderer.allowCoreThreadTimeOut(true);
useTintMask =
c.getSharedPreferences("luma", Context.MODE_PRIVATE).getBoolean("memorySaver", false);
tintPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
}
static Bitmap decodeWallpaper(Context c) {
@ -74,6 +79,20 @@ final class SceneView extends View {
requestRender();
}
void setMemorySaver(boolean enabled) {
if (useTintMask == enabled) return;
stopAnimations();
useTintMask = enabled;
// Full-color and mask bases encode different pixel values; retain source artwork for
// rebuilding.
generation++;
renderer.getQueue().clear();
frame = previousFrame = mask = previousMask = null;
dirty = true;
requestRender();
invalidate();
}
void setMotion(boolean enabled) {
motion = enabled;
if (!enabled) stopAnimations();
@ -87,9 +106,34 @@ final class SceneView extends View {
}
void accent(int color, boolean animate) {
if (accent == color) return;
accent = color;
requestRender();
if (!useTintMask) {
if (accent == color) return;
accent = targetAccent = color;
requestRender();
return;
}
if (targetAccent == color) return;
targetAccent = color;
if (colorAnimator != null) colorAnimator.cancel();
if (!animate || !motion || frame == null) {
accent = color;
invalidate();
return;
}
final int from = accent;
colorAnimator = ValueAnimator.ofFloat(0, 1);
colorAnimator.setDuration(280);
colorAnimator.addUpdateListener(
a -> {
float f = (float) a.getAnimatedValue();
accent =
Color.rgb(
Math.round(Color.red(from) * (1 - f) + Color.red(color) * f),
Math.round(Color.green(from) * (1 - f) + Color.green(color) * f),
Math.round(Color.blue(from) * (1 - f) + Color.blue(color) * f));
invalidate();
});
colorAnimator.start();
}
@Override
@ -107,14 +151,16 @@ final class SceneView extends View {
final int h = Math.max(1, Math.round(getHeight() * scale));
final Bitmap art = mediaArt != null ? mediaArt : scene.equals("alpine") ? wallpaper : null;
final String theme = scene;
final int color = accent;
final int color = useTintMask ? Color.BLACK : accent;
final boolean masked = useTintMask;
final float darkness = dim;
renderer.execute(
() -> {
if (ticket != generation) return;
Bitmap composed;
Bitmap composed, tint;
try {
composed = compose(w, h, art, theme, darkness, color);
tint = masked ? makeMask(w, h, art != null && !art.isRecycled()) : null;
} catch (OutOfMemoryError e) {
return;
}
@ -123,11 +169,14 @@ final class SceneView extends View {
() -> {
if (ticket != generation || !active) return;
dirty = false;
stopAnimations();
stopImageAnimation();
previousFrame = frame;
previousMask = mask;
frame = composed;
mask = tint;
if (!motion || previousFrame == null) {
previousFrame = null;
previousMask = null;
mix = 1;
invalidate();
return;
@ -145,6 +194,7 @@ final class SceneView extends View {
@Override
public void onAnimationEnd(Animator animation) {
previousFrame = null;
previousMask = null;
mix = 1;
invalidate();
}
@ -211,17 +261,70 @@ final class SceneView extends View {
return result;
}
/** The protected accent contribution is independent of the wallpaper RGB. */
private static Bitmap makeMask(int width, int height, boolean artwork) {
float scale = Math.min(1f, Math.min(320f / width, 180f / height));
int w = Math.max(1, Math.round(width * scale)), h = Math.max(1, Math.round(height * scale));
Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(result);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setShader(
new RadialGradient(
w * .76f,
h * .25f,
w * .8f,
new int[] {0xA0FFFFFF, 0x00FFFFFF},
null,
Shader.TileMode.CLAMP));
p.setAlpha(artwork ? 65 : 150);
c.drawRect(0, 0, w, h, p);
p.setAlpha(255);
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
p.setShader(
new LinearGradient(
0,
0,
w * .88f,
0,
new int[] {0xE0FFFFFF, 0x9CFFFFFF, 0x00FFFFFF},
new float[] {0, .48f, 1},
Shader.TileMode.CLAMP));
c.drawRect(0, 0, w, h, p);
p.setShader(
new LinearGradient(
0,
h * .24f,
0,
h,
new int[] {0x00FFFFFF, 0xA8FFFFFF, 0xFCFFFFFF, 0xFFFFFFFF},
new float[] {0, .47f, .82f, 1},
Shader.TileMode.CLAMP));
c.drawRect(0, 0, w, h, p);
return result;
}
@Override
protected void onDraw(Canvas c) {
destination.set(0, 0, getWidth(), getHeight());
if (previousFrame != null) c.drawBitmap(previousFrame, null, destination, paint);
if (frame == null) {
c.drawColor(Ui.BG);
return;
}
paint.setAlpha(previousFrame == null ? 255 : (int) (mix * 255));
if (previousFrame != null) c.drawBitmap(previousFrame, null, destination, paint);
paint.setAlpha(previousFrame == null ? 255 : (int) (255 * mix));
c.drawBitmap(frame, null, destination, paint);
paint.setAlpha(255);
if (useTintMask) {
if (previousFrame != null) drawMask(c, previousMask, 1 - mix);
drawMask(c, mask, previousFrame == null ? 1 : mix);
}
}
private void drawMask(Canvas c, Bitmap image, float opacity) {
if (image == null || opacity <= 0) return;
tintPaint.setColor(accent);
tintPaint.setAlpha(Math.round(255 * opacity));
c.drawBitmap(image, null, destination, tintPaint);
}
@Override
@ -229,12 +332,20 @@ final class SceneView extends View {
return true;
}
void stopAnimations() {
private void stopImageAnimation() {
if (animator != null) animator.cancel();
previousFrame = null;
previousMask = null;
mix = 1;
}
void stopAnimations() {
stopImageAnimation();
if (colorAnimator != null) colorAnimator.cancel();
accent = targetAccent;
invalidate();
}
void pause() {
active = false;
generation++;
@ -252,6 +363,7 @@ final class SceneView extends View {
wallpaper = null;
mediaArt = null;
frame = null;
mask = null;
dirty = true;
invalidate();
}