diff --git a/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt b/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt index b3b33d8da0..b5dbb54ef3 100644 --- a/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt +++ b/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt @@ -141,6 +141,8 @@ class DroidDeckHudView(context: Context) : View(context) { private var dDividerTop = -1f private var dFrame: Bitmap? = null private var dFrameKey = "" + private var sFrame: Bitmap? = null + private var sFrameKey = "" private val glowPaint = Paint(Paint.ANTI_ALIAS_FLAG) private val colGpu = 0xFF5EE08A.toInt() @@ -274,9 +276,17 @@ class DroidDeckHudView(context: Context) : View(context) { seenFpsUpdates = updates periodDone.trySend(Unit) } - if (graphRect != null) invalidate() + if (graphRect == null) return + if (stripRect != null) { + val now = SystemClock.uptimeMillis() + if (now - lastGraphDrawMs < STRIP_GRAPH_REDRAW_MS) return + lastGraphDrawMs = now + } + invalidate() } + private var lastGraphDrawMs = 0L + private fun cycleSize() { size = size.next() onSizeCycledListener?.accept(size.token) @@ -524,6 +534,9 @@ class DroidDeckHudView(context: Context) : View(context) { private val inkRect = android.graphics.Rect() private fun rebuild() { + val renderingLayer = if (size == DroidDeckHudSize.STRIP && dock == DroidDeckHudDock.NONE) + LAYER_TYPE_SOFTWARE else LAYER_TYPE_NONE + if (layerType != renderingLayer) setLayerType(renderingLayer, null) glyphs.clear(); tileRects.clear(); graphRect = null headerMark = null; headerWordmark = null; chipRect = null; dPanel = null; dDividerTop = -1f; tabRect = null; stripRect = null val s = snap @@ -1369,17 +1382,30 @@ class DroidDeckHudView(context: Context) : View(context) { } private fun drawStrip(canvas: Canvas, rect: RectF) { - headerPaint.shader = null - headerPaint.color = panelColor() - canvas.drawRect(rect, headerPaint) - val stroke = outlineWidth() - if (stroke <= 0f) return - val line = max(stroke / 2f, density) val p = parent as? View val atBottom = p != null && p.height > 0 && y + height / 2f > p.height / 2f - headerPaint.color = withAlpha(outlineColor(), (110 + 145 * outlineIntensity).roundToInt().coerceAtMost(255)) - if (atBottom) canvas.drawRect(rect.left, rect.top, rect.right, rect.top + line, headerPaint) - else canvas.drawRect(rect.left, rect.bottom - line, rect.right, rect.bottom, headerPaint) + stripFrame(rect, atBottom)?.let { canvas.drawBitmap(it, 0f, 0f, null) } + } + + private fun stripFrame(rect: RectF, atBottom: Boolean): Bitmap? { + val w = rect.width().roundToInt(); val h = rect.height().roundToInt() + if (w <= 0 || h <= 0) return null + val stroke = outlineWidth() + val key = "$w,$h,$stroke,$atBottom,$surfaceRgb,$bgOpacity,$outlineIntensity,${outlineColor()}" + if (key == sFrameKey) sFrame?.let { return it } + val bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888) + val c = Canvas(bmp) + headerPaint.shader = null + headerPaint.color = panelColor() + c.drawRect(rect, headerPaint) + if (stroke > 0f) { + val line = max(stroke / 2f, density) + headerPaint.color = withAlpha(outlineColor(), (110 + 145 * outlineIntensity).roundToInt().coerceAtMost(255)) + if (atBottom) c.drawRect(rect.left, rect.top, rect.right, rect.top + line, headerPaint) + else c.drawRect(rect.left, rect.bottom - line, rect.right, rect.bottom, headerPaint) + } + sFrame = bmp; sFrameKey = key + return bmp } private fun outlineWidth(): Float = if (outlineIntensity > 0f) outlineIntensity * sp(3.5f) else 0f @@ -1499,6 +1525,7 @@ class DroidDeckHudView(context: Context) : View(context) { const val TAB_PAD_X_SP = 7f const val TAB_PAD_Y_SP = 5.5f const val REFRESH_MS = 500L + const val STRIP_GRAPH_REDRAW_MS = 66L private const val HUE_IDLE_MS = 500L const val MIN_SCALE = 0.5f const val MAX_SCALE = 2f diff --git a/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt b/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt index 6217b6656c..05c6d2e679 100644 --- a/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt @@ -139,6 +139,36 @@ class DroidDeckHudPanelTest { assertFalse(line, "BAT" in line) } + @Test fun stripGraphClearsThePreviousTraceWhenFrameHistoryChanges() { + val app = RuntimeEnvironment.getApplication() + val parent = FrameLayout(app) + val view = DroidDeckHudView(app) + parent.addView(view) + parent.layout(0, 0, 2400, 1080) + val stats = FrameStats() + var now = 1_000_000_000L + repeat(300) { now += 25_000_000L; stats.add(25_000_000L, now) } + view.setFrameStats(stats) + view.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + + ("hudSize" to "strip") + ("showFPSGraph" to "1") + ("hudOpacity" to "100"))) + view.showForTest(SNAPSHOT, "DXVK", now) + val unspecified = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) + view.measure(unspecified, unspecified) + view.layout(0, 0, view.measuredWidth, view.measuredHeight) + fun render() = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888).also { + view.draw(Canvas(it)) + } + val graph = requireNotNull(view.graphForTest()) + val x = (graph.left + graph.width() * 0.75f).toInt() + val y = graph.centerY().toInt() + val before = render() + stats.reset() + val after = render() + assertTrue("the graph initially crosses its midpoint", before.getPixel(x, y) != after.getPixel(x, y)) + assertEquals("the previous trace is cleared to the strip background", + after.getPixel(x, graph.top.toInt() + 2), after.getPixel(x, y)) + } + @Test fun stripFollowsTheHudScaleInHeightAndKeepsTheFullWidth() { val app = RuntimeEnvironment.getApplication() val parent = FrameLayout(app)