package tv.luma.launcher.tests; import android.app.*; import android.content.*; import android.graphics.*; import android.os.*; import android.view.*; import android.widget.*; import java.lang.reflect.*; import java.util.concurrent.*; /** Compare actual GPU app-tile interiors with the previous clip-and-bitmap implementation. */ public final class TileRenderingInstrumentation extends Instrumentation { private Object get(Object o, String name) throws Exception { Field f = o.getClass().getDeclaredField(name); f.setAccessible(true); return f.get(o); } private void set(Object o, String name, Object v) throws Exception { Field f = o.getClass().getDeclaredField(name); f.setAccessible(true); f.set(o, v); } private int dp(Context c, float n) { return Math.round(n * c.getResources().getDisplayMetrics().density); } private Bitmap capture(Activity a) throws Exception { CountDownLatch done = new CountDownLatch(1); Bitmap[] image = {null}; int[] code = {-1}; runOnMainSync( () -> { View v = a.getWindow().getDecorView(); image[0] = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); PixelCopy.request( a.getWindow(), image[0], r -> { code[0] = r; done.countDown(); }, new Handler(Looper.getMainLooper())); }); if (!done.await(4, TimeUnit.SECONDS) || code[0] != 0) throw new AssertionError("PixelCopy " + code[0]); return image[0]; } private final class Reference extends View { final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); final RectF r = new RectF(); final Path clip = new Path(); Bitmap bitmap; float progress; Reference(Context c) { super(c); } protected void onSizeChanged(int w, int h, int x, int y) { r.set( dp(getContext(), 2), dp(getContext(), 2), w - dp(getContext(), 2), h - dp(getContext(), 2)); clip.addRoundRect(r, dp(getContext(), 12), dp(getContext(), 12), Path.Direction.CW); } protected void onDraw(Canvas c) { if (bitmap == null) return; c.save(); c.clipPath(clip); p.setColor(0xFF1B263B); c.drawRect(r, p); float s = Math.max(r.width() / bitmap.getWidth(), r.height() / bitmap.getHeight()); float w = bitmap.getWidth() * s, h = bitmap.getHeight() * s; c.drawBitmap( bitmap, null, new RectF( r.centerX() - w / 2, r.centerY() - h / 2, r.centerX() + w / 2, r.centerY() + h / 2), p); if (progress > 0) { p.setColor(0xA0080D18); c.drawRect(r.left, r.bottom - dp(getContext(), 4), r.right, r.bottom, p); p.setColor(0xFFE8CBA6); c.drawRect( r.left, r.bottom - dp(getContext(), 4), r.left + r.width() * progress, r.bottom, p); } c.restore(); p.setStyle(Paint.Style.STROKE); p.setStrokeWidth(dp(getContext(), 1.5f)); p.setColor(0xDBF4E8D8); c.drawRoundRect(r, dp(getContext(), 12), dp(getContext(), 12), p); p.setStyle(Paint.Style.FILL); } } @Override public void onCreate(Bundle b) { super.onCreate(b); start(); } @Override public void onStart() { Bundle result = new Bundle(); StringBuilder report = new StringBuilder(); try { Activity a = startActivitySync( new Intent(Intent.ACTION_MAIN) .setComponent( new ComponentName("tv.luma.launcher", "tv.luma.launcher.MainActivity")) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); Thread.sleep(2500); Class type = Class.forName("tv.luma.launcher.AppTile"); Constructor constructor = type.getDeclaredConstructor(Context.class, String.class, int.class, boolean.class); constructor.setAccessible(true); Object[] tile = {null}; View[] art = {null}; Reference[] reference = {null}; FrameLayout[] overlay = {null}; runOnMainSync( () -> { try { tile[0] = constructor.newInstance(a, "Sample", 150, false); art[0] = (View) get(tile[0], "art"); ((LinearLayout) tile[0]).removeView(art[0]); set(art[0], "focus", true); overlay[0] = new FrameLayout(a); overlay[0].setBackgroundColor(0xFF080D18); reference[0] = new Reference(a); FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(512, 288); lp.leftMargin = 200; lp.topMargin = 200; overlay[0].addView(reference[0], lp); overlay[0].addView(art[0], new FrameLayout.LayoutParams(lp)); ((FrameLayout) get(a, "root")) .addView(overlay[0], new FrameLayout.LayoutParams(-1, -1)); } catch (Exception e) { throw new RuntimeException(e); } }); Method image = type.getDeclaredMethod("bitmap", Bitmap.class); image.setAccessible(true); for (int mode = 0; mode < 3; mode++) { int w = mode == 1 ? 100 : 360, h = mode == 1 ? 300 : 160; Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Paint p = new Paint(); p.setShader( new LinearGradient( 0, 0, w, h, mode == 2 ? 0x00000000 : Color.RED, Color.CYAN, Shader.TileMode.CLAMP)); new Canvas(b).drawRect(0, 0, w, h, p); if (mode != 2) b.setHasAlpha(false); final float progress = mode == 2 ? .65f : 0; runOnMainSync( () -> { try { image.invoke(tile[0], b); set(art[0], "progress", progress); reference[0].bitmap = b; reference[0].progress = progress; reference[0].setVisibility(View.VISIBLE); reference[0].invalidate(); art[0].setVisibility(View.INVISIBLE); } catch (Exception e) { throw new RuntimeException(e); } }); Thread.sleep(150); Bitmap before = capture(a); runOnMainSync( () -> { reference[0].setVisibility(View.INVISIBLE); art[0].setVisibility(View.VISIBLE); art[0].invalidate(); }); Thread.sleep(150); Bitmap after = capture(a); int worst = 0; for (int y = 206; y < 483; y += 3) for (int x = 230; x < 682; x += 3) { int aa = before.getPixel(x, y), bb = after.getPixel(x, y); for (int shift : new int[] {0, 8, 16}) worst = Math.max(worst, Math.abs(((aa >> shift) & 255) - ((bb >> shift) & 255))); } if (worst > 3) throw new AssertionError("tile crop/pixels changed by " + worst + " mode=" + mode); if (after.getPixel(202, 202) != 0xFF080D18) throw new AssertionError("bitmap leaks outside rounded corner"); report .append("PASS: tile crop/opacity/progress mode ") .append(mode) .append("; max pixel difference ") .append(worst) .append('\n'); before.recycle(); after.recycle(); } runOnMainSync( () -> { try { image.invoke(tile[0], new Object[] {null}); } catch (Exception e) { throw new RuntimeException(e); } }); if (get(art[0], "imageShader") != null || ((Paint) get(art[0], "imagePaint")).getShader() != null) throw new AssertionError("tile shader retains released image"); Bitmap shadow = (Bitmap) get(tile[0], "shadowMask"); if (shadow.getConfig() != Bitmap.Config.ALPHA_8 || shadow.getAllocationByteCount() > 24576) throw new AssertionError("shadow exceeds small alpha texture budget"); report .append("PASS: shader releases images; shared shadow uses ") .append(shadow.getAllocationByteCount()) .append(" bytes\n"); result.putString("stream", report.toString()); finish(Activity.RESULT_OK, result); } catch (Throwable t) { result.putString("stream", report + "FAIL: " + t + "\n"); finish(Activity.RESULT_CANCELED, result); } } }