Cache soft focus shadows and compare TV rendering performance

This commit is contained in:
Elijah Kuntz 2026-10-19 10:01:28 -07:00
parent 1f69422354
commit 5bb15fa69e
11 changed files with 85 additions and 17 deletions

View file

@ -1,5 +1,6 @@
package tv.luma.launcher;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.*;
import android.view.*;
@ -12,6 +13,12 @@ final class AppTile extends LinearLayout {
private final TextView label;
private boolean motion = true;
private String labelMode = "always";
// One small shared texture replaces live elevation-shadow geometry on every focus frame.
private static Bitmap shadowMask;
private final Paint shadowPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
private final RectF shadowBounds = new RectF();
private float shadowAmount;
private ValueAnimator shadowAnimation;
interface Focus {
void changed(boolean hasFocus);
@ -25,6 +32,15 @@ final class AppTile extends LinearLayout {
setClickable(true);
setClipChildren(false);
setClipToPadding(false);
setWillNotDraw(false);
if (shadowMask == null) {
shadowMask = Bitmap.createBitmap(192, 122, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(shadowMask);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.WHITE);
p.setShadowLayer(8, 0, 4, Color.WHITE);
canvas.drawRoundRect(new RectF(16, 16, 176, 106), 12, 12, p);
}
setContentDescription(name);
setPadding(Ui.dp(c, 5), Ui.dp(c, 7), Ui.dp(c, 5), Ui.dp(c, 7));
art = new ArtView(c, name);
@ -48,8 +64,20 @@ final class AppTile extends LinearLayout {
label.setTextColor(f ? Ui.WHITE : Ui.MUTED);
updateLabel();
art.focus = f;
// Only the focused tile needs a shadow; avoid overlapping parent/child shadows.
art.animate().translationZ(Ui.dp(c, f ? 6 : 0)).setDuration(motion ? 160 : 0).start();
if (shadowAnimation != null) shadowAnimation.cancel();
if (motion) {
shadowAnimation = ValueAnimator.ofFloat(shadowAmount, f ? 1 : 0);
shadowAnimation.setDuration(160);
shadowAnimation.addUpdateListener(
a -> {
shadowAmount = (float) a.getAnimatedValue();
invalidate();
});
shadowAnimation.start();
} else {
shadowAmount = f ? 1 : 0;
invalidate();
}
art.invalidate();
if (focus != null) focus.changed(f);
});
@ -78,15 +106,28 @@ final class AppTile extends LinearLayout {
}
void setAccent(int color) {
if (android.os.Build.VERSION.SDK_INT >= 28) {
art.setOutlineAmbientShadowColor(color);
art.setOutlineSpotShadowColor(color);
}
shadowPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (shadowAmount <= 0 || art.getWidth() == 0) return;
float inset = Ui.dp(getContext(), 2);
float scale = (art.getWidth() - 2 * inset) / 160f;
float left = art.getLeft() + inset - 16 * scale;
float top = art.getTop() + inset - 16 * scale;
shadowBounds.set(left, top, left + 192 * scale, top + 122 * scale);
shadowPaint.setAlpha(Math.round(150 * shadowAmount));
canvas.drawBitmap(shadowMask, null, shadowBounds, shadowPaint);
}
void stopAnimations() {
animate().cancel();
art.animate().cancel();
if (shadowAnimation != null) shadowAnimation.cancel();
shadowAmount = hasFocus() ? 1 : 0;
}
void bitmap(Bitmap b) {