Optimize cinematic rendering for integrated Android TVs

This commit is contained in:
Elijah Kuntz 2026-10-19 09:49:08 -07:00
parent dc5908cb3b
commit 1f69422354
15 changed files with 366 additions and 153 deletions

View file

@ -42,14 +42,14 @@ final class AppTile extends LinearLayout {
animate()
.scaleX(f ? 1.065f : 1f)
.scaleY(f ? 1.065f : 1f)
.translationZ(f ? Ui.dp(c, 8) : 0)
.setDuration(motion ? 160 : 0)
.setInterpolator(new DecelerateInterpolator())
.start();
label.setTextColor(f ? Ui.WHITE : Ui.MUTED);
updateLabel();
art.focus = f;
art.animate().translationZ(Ui.dp(c, f ? 8 : 1)).setDuration(motion ? 160 : 0).start();
// 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();
art.invalidate();
if (focus != null) focus.changed(f);
});
@ -116,7 +116,7 @@ final class AppTile extends LinearLayout {
super(c);
fallback = s;
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
setElevation(Ui.dp(c, 2));
setElevation(0);
setOutlineProvider(
new ViewOutlineProvider() {
@Override

View file

@ -308,6 +308,7 @@ public final class MainActivity extends Activity {
protected void onResume() {
super.onResume();
foreground = true;
scene.resume();
plex.resume();
restoreImages();
handler.removeCallbacks(minuteTick);
@ -358,7 +359,7 @@ public final class MainActivity extends Activity {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
handler.removeCallbacks(idleSaver);
stopSaver(false);
scene.stopAnimations();
scene.pause();
scroll.stopAnimation();
}
@ -401,6 +402,7 @@ public final class MainActivity extends Activity {
plex.cancel();
updater.cancel();
if (updateTask != null) updateTask.cancel(true);
scene.shutdown();
work.shutdownNow();
network.shutdownNow();
super.onDestroy();
@ -1857,7 +1859,7 @@ public final class MainActivity extends Activity {
handler.removeCallbacks(idleSaver);
handler.removeCallbacks(minuteTick);
handler.removeCallbacks(rotateScene);
scene.stopAnimations();
scene.pause();
scroll.stopAnimation();
saver = new SaverView(this);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
@ -1872,6 +1874,7 @@ public final class MainActivity extends Activity {
saver = null;
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (resume && foreground) {
scene.resume();
AppTile tile = tiles.get(selected);
if (tile != null) tile.requestFocus();
handler.removeCallbacks(minuteTick);

View file

@ -7,24 +7,33 @@ import android.content.Context;
import android.graphics.*;
import android.view.View;
import java.io.InputStream;
import java.util.concurrent.*;
/** Single sampled bitmap, cached shaders; nothing redraws when the screen is idle. */
/** Flatten cinematic effects once off the UI thread, then crossfade bounded opaque textures. */
final class SceneView extends View {
private final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
private Bitmap wallpaper, mediaArt, previousArt;
private float artMix = 1;
private boolean motion = true;
private ValueAnimator imageAnimator;
private final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
private final RectF destination = new RectF();
private int accent = 0xFF5B4125, from = accent, to = accent;
private ValueAnimator animator;
private Shader left, bottom, glow, base;
private final ThreadPoolExecutor renderer =
new ThreadPoolExecutor(
1,
1,
2,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1),
new ThreadPoolExecutor.DiscardOldestPolicy());
private Bitmap wallpaper, mediaArt, frame, previousFrame;
private float mix = 1;
private boolean motion = true, active = true, dirty = true;
private volatile int generation;
private int accent = 0xFF5B4125;
private String scene = "alpine";
private float dim = .24f;
private ValueAnimator animator;
SceneView(Context c) {
super(c);
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
renderer.allowCoreThreadTimeOut(true);
}
static Bitmap decodeWallpaper(Context c) {
@ -46,9 +55,9 @@ final class SceneView extends View {
}
void setWallpaper(Bitmap b) {
Bitmap previous = currentArt();
if (wallpaper == b) return;
wallpaper = b;
transitionArt(previous);
requestRender();
}
boolean hasWallpaper() {
@ -61,106 +70,125 @@ final class SceneView extends View {
void setMediaArt(Bitmap b) {
if (mediaArt == b) return;
Bitmap previous = currentArt();
mediaArt = b;
transitionArt(previous);
}
private Bitmap currentArt() {
return mediaArt != null ? mediaArt : scene.equals("alpine") ? wallpaper : null;
requestRender();
}
void setMotion(boolean enabled) {
motion = enabled;
if (!motion) stopAnimations();
}
private void transitionArt(Bitmap previous) {
if (imageAnimator != null) imageAnimator.cancel();
if (!motion || previous == currentArt()) {
previousArt = null;
artMix = 1;
invalidate();
return;
}
previousArt = previous;
artMix = 0;
imageAnimator = ValueAnimator.ofFloat(0, 1);
imageAnimator.setDuration(320);
imageAnimator.addUpdateListener(
a -> {
artMix = (float) a.getAnimatedValue();
invalidate();
});
imageAnimator.addListener(
new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
previousArt = null;
artMix = 1;
invalidate();
}
});
imageAnimator.start();
if (!enabled) stopAnimations();
}
void configure(String s, float darkness) {
Bitmap previous = currentArt();
if (scene.equals(s) && dim == darkness && frame != null) return;
scene = s;
dim = darkness;
shaders();
transitionArt(previous);
requestRender();
}
void accent(int color, boolean animate) {
if (animator != null) animator.cancel();
from = accent;
to = color;
if (!animate) {
accent = color;
shaders();
invalidate();
return;
}
animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(280);
animator.addUpdateListener(
a -> {
float f = (float) a.getAnimatedValue();
accent = blend(from, to, f);
glowShader();
invalidate();
});
animator.start();
}
private int blend(int a, int b, float f) {
return Color.rgb(
(int) (Color.red(a) * (1 - f) + Color.red(b) * f),
(int) (Color.green(a) * (1 - f) + Color.green(b) * f),
(int) (Color.blue(a) * (1 - f) + Color.blue(b) * f));
if (accent == color) return;
accent = color;
requestRender();
}
@Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
shaders();
requestRender();
}
private void glowShader() {
if (getWidth() > 0)
glow =
new RadialGradient(
getWidth() * .76f,
getHeight() * .25f,
getWidth() * .8f,
new int[] {(accent & 0x00FFFFFF) | 0xA0000000, 0x00000000},
private void requestRender() {
dirty = true;
final int ticket = ++generation;
renderer.getQueue().clear();
if (!active || getWidth() == 0 || getHeight() == 0) return;
float scale = Math.min(1f, Math.min(960f / getWidth(), 540f / getHeight()));
final int w = Math.max(1, Math.round(getWidth() * scale));
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 float darkness = dim;
renderer.execute(
() -> {
if (ticket != generation) return;
Bitmap composed;
try {
composed = compose(w, h, art, theme, darkness, color);
} catch (OutOfMemoryError e) {
return;
}
if (ticket != generation) return;
post(
() -> {
if (ticket != generation || !active) return;
dirty = false;
stopAnimations();
previousFrame = frame;
frame = composed;
if (!motion || previousFrame == null) {
previousFrame = null;
mix = 1;
invalidate();
return;
}
mix = 0;
animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(280);
animator.addUpdateListener(
a -> {
mix = (float) a.getAnimatedValue();
invalidate();
});
animator.addListener(
new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
previousFrame = null;
mix = 1;
invalidate();
}
});
animator.start();
});
});
}
private static Bitmap compose(int w, int h, Bitmap art, String scene, float dim, int accent) {
Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
result.setHasAlpha(false);
Canvas c = new Canvas(result);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
boolean hasArt = art != null && !art.isRecycled();
if (hasArt) {
float scale = Math.max(w / (float) art.getWidth(), h / (float) art.getHeight());
float aw = art.getWidth() * scale, ah = art.getHeight() * scale;
c.drawBitmap(art, null, new RectF((w - aw) / 2, (h - ah) / 2, (w + aw) / 2, (h + ah) / 2), p);
c.drawColor(((int) (dim * 255) << 24) | 0x00040810);
} else {
p.setShader(
new LinearGradient(
w,
0,
0,
h,
new int[] {scene.equals("aurora") ? 0xFF143B49 : 0xFF152037, 0xFF151A32, Ui.BG},
null,
Shader.TileMode.CLAMP);
}
private void shaders() {
if (getWidth() == 0) return;
float w = getWidth(), h = getHeight();
left =
Shader.TileMode.CLAMP));
c.drawRect(0, 0, w, h, p);
}
p.setShader(
new RadialGradient(
w * .76f,
h * .25f,
w * .8f,
new int[] {(accent & 0x00FFFFFF) | 0xA0000000, 0x00000000},
null,
Shader.TileMode.CLAMP));
p.setAlpha(hasArt ? 65 : 150);
c.drawRect(0, 0, w, h, p);
p.setAlpha(255);
p.setShader(
new LinearGradient(
0,
0,
@ -168,8 +196,9 @@ final class SceneView extends View {
0,
new int[] {0xE0080D18, 0x9C080D18, 0x00080D18},
new float[] {0, .48f, 1},
Shader.TileMode.CLAMP);
bottom =
Shader.TileMode.CLAMP));
c.drawRect(0, 0, w, h, p);
p.setShader(
new LinearGradient(
0,
h * .24f,
@ -177,60 +206,58 @@ final class SceneView extends View {
h,
new int[] {0x00080D18, 0xA8080D18, 0xFC080D18, 0xFF080D18},
new float[] {0, .47f, .82f, 1},
Shader.TileMode.CLAMP);
int top = scene.equals("aurora") ? 0xFF143B49 : 0xFF152037;
base =
new LinearGradient(
w, 0, 0, h, new int[] {top, 0xFF151A32, Ui.BG}, null, Shader.TileMode.CLAMP);
glowShader();
Shader.TileMode.CLAMP));
c.drawRect(0, 0, w, h, p);
return result;
}
@Override
protected void onDraw(Canvas c) {
if (base == null) shaders();
p.setShader(base);
c.drawRect(0, 0, getWidth(), getHeight(), p);
p.setShader(null);
Bitmap art = currentArt();
if (previousArt != null)
drawArt(c, previousArt, art == null ? (int) (255 * (1 - artMix)) : 255);
drawArt(c, art, (int) (255 * artMix));
if (art != null || previousArt != null) {
c.drawColor(((int) (dim * 255) << 24) | 0x00040810);
destination.set(0, 0, getWidth(), getHeight());
if (previousFrame != null) c.drawBitmap(previousFrame, null, destination, paint);
if (frame == null) {
c.drawColor(Ui.BG);
return;
}
p.setShader(glow);
p.setAlpha(art == null ? 150 : 65);
c.drawRect(0, 0, getWidth(), getHeight(), p);
p.setAlpha(255);
p.setShader(left);
c.drawRect(0, 0, getWidth(), getHeight(), p);
p.setShader(bottom);
c.drawRect(0, 0, getWidth(), getHeight(), p);
p.setShader(null);
paint.setAlpha(previousFrame == null ? 255 : (int) (mix * 255));
c.drawBitmap(frame, null, destination, paint);
paint.setAlpha(255);
}
private void drawArt(Canvas canvas, Bitmap art, int alpha) {
if (art == null || art.isRecycled() || alpha == 0) return;
float scale =
Math.max(getWidth() / (float) art.getWidth(), getHeight() / (float) art.getHeight());
float w = art.getWidth() * scale, h = art.getHeight() * scale;
destination.set(
(getWidth() - w) / 2, (getHeight() - h) / 2, (getWidth() + w) / 2, (getHeight() + h) / 2);
p.setAlpha(alpha);
canvas.drawBitmap(art, null, destination, p);
p.setAlpha(255);
@Override
public boolean isOpaque() {
return true;
}
void stopAnimations() {
if (animator != null) animator.cancel();
if (imageAnimator != null) imageAnimator.cancel();
previousArt = null;
artMix = 1;
previousFrame = null;
mix = 1;
}
void pause() {
active = false;
generation++;
renderer.getQueue().clear();
stopAnimations();
}
void resume() {
active = true;
if (dirty || frame == null) requestRender();
}
void release() {
stopAnimations();
pause();
wallpaper = null;
mediaArt = null;
frame = null;
dirty = true;
invalidate();
}
void shutdown() {
release();
renderer.shutdownNow();
}
}