From 189d1d13d8948b7d1f6836a9d9f47540b338e5c0 Mon Sep 17 00:00:00 2001 From: Liana Kazanova Date: Fri, 21 Apr 2023 23:41:43 +0000 Subject: [PATCH] Revert "Restrict maximum size of FontInterpolator font caches" Revert submission 22757815-cherrypicker-L01300000960051069:N96900001359941510 Reason for revert: b/279232694 Reverted changes: /q/submissionid:22757815-cherrypicker-L01300000960051069:N96900001359941510 Change-Id: I5f91ff07a33d4f09b9a12ffd78a5de984865c68e --- .../systemui/animation/FontInterpolator.kt | 16 ++++-------- .../systemui/animation/TextAnimator.kt | 12 ++++----- .../animation/FontInterpolatorTest.kt | 25 ------------------- 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt b/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt index 83e44b69812b2..f0a82113c3a3b 100644 --- a/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt +++ b/packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt @@ -18,10 +18,8 @@ package com.android.systemui.animation import android.graphics.fonts.Font import android.graphics.fonts.FontVariationAxis -import android.util.LruCache import android.util.MathUtils import android.util.MathUtils.abs -import androidx.annotation.VisibleForTesting import java.lang.Float.max import java.lang.Float.min @@ -36,10 +34,6 @@ private const val FONT_ITALIC_MIN = 0f private const val FONT_ITALIC_ANIMATION_STEP = 0.1f private const val FONT_ITALIC_DEFAULT_VALUE = 0f -// Benchmarked via Perfetto, difference between 10 and 50 entries is about 0.3ms in -// frame draw time on a Pixel 6. -@VisibleForTesting const val FONT_CACHE_MAX_ENTRIES = 10 - /** Provide interpolation of two fonts by adjusting font variation settings. */ class FontInterpolator { @@ -87,8 +81,8 @@ class FontInterpolator { // Font interpolator has two level caches: one for input and one for font with different // variation settings. No synchronization is needed since FontInterpolator is not designed to be // thread-safe and can be used only on UI thread. - private val interpCache = LruCache(FONT_CACHE_MAX_ENTRIES) - private val verFontCache = LruCache(FONT_CACHE_MAX_ENTRIES) + private val interpCache = hashMapOf() + private val verFontCache = hashMapOf() // Mutable keys for recycling. private val tmpInterpKey = InterpKey(null, null, 0f) @@ -158,7 +152,7 @@ class FontInterpolator { tmpVarFontKey.set(start, newAxes) val axesCachedFont = verFontCache[tmpVarFontKey] if (axesCachedFont != null) { - interpCache.put(InterpKey(start, end, progress), axesCachedFont) + interpCache[InterpKey(start, end, progress)] = axesCachedFont return axesCachedFont } @@ -166,8 +160,8 @@ class FontInterpolator { // Font.Builder#build won't throw IOException since creating fonts from existing fonts will // not do any IO work. val newFont = Font.Builder(start).setFontVariationSettings(newAxes.toTypedArray()).build() - interpCache.put(InterpKey(start, end, progress), newFont) - verFontCache.put(VarFontKey(start, newAxes), newFont) + interpCache[InterpKey(start, end, progress)] = newFont + verFontCache[VarFontKey(start, newAxes)] = newFont return newFont } diff --git a/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt b/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt index 3ee97be360f0b..9e9929e79d47d 100644 --- a/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt +++ b/packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt @@ -24,10 +24,8 @@ import android.graphics.Canvas import android.graphics.Typeface import android.graphics.fonts.Font import android.text.Layout -import android.util.LruCache private const val DEFAULT_ANIMATION_DURATION: Long = 300 -private const val TYPEFACE_CACHE_MAX_ENTRIES = 5 typealias GlyphCallback = (TextAnimator.PositionedGlyph, Float) -> Unit /** @@ -116,7 +114,7 @@ class TextAnimator(layout: Layout, private val invalidateCallback: () -> Unit) { private val fontVariationUtils = FontVariationUtils() - private val typefaceCache = LruCache(TYPEFACE_CACHE_MAX_ENTRIES) + private val typefaceCache = HashMap() fun updateLayout(layout: Layout) { textInterpolator.layout = layout @@ -220,12 +218,12 @@ class TextAnimator(layout: Layout, private val invalidateCallback: () -> Unit) { } if (!fvar.isNullOrBlank()) { - textInterpolator.targetPaint.typeface = typefaceCache.get(fvar) ?: run { - textInterpolator.targetPaint.fontVariationSettings = fvar - textInterpolator.targetPaint.typeface?.also { + textInterpolator.targetPaint.typeface = + typefaceCache.getOrElse(fvar) { + textInterpolator.targetPaint.fontVariationSettings = fvar typefaceCache.put(fvar, textInterpolator.targetPaint.typeface) + textInterpolator.targetPaint.typeface } - } } if (color != null) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt index 57a355f4e1278..8a5c5b58d0580 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt @@ -106,29 +106,4 @@ class FontInterpolatorTest : SysuiTestCase() { val reversedFont = interp.lerp(endFont, startFont, 0.5f) assertThat(resultFont).isSameInstanceAs(reversedFont) } - - @Test - fun testCacheMaxSize() { - val interp = FontInterpolator() - - val startFont = Font.Builder(sFont) - .setFontVariationSettings("'wght' 100") - .build() - val endFont = Font.Builder(sFont) - .setFontVariationSettings("'wght' 1") - .build() - val resultFont = interp.lerp(startFont, endFont, 0.5f) - for (i in 0..FONT_CACHE_MAX_ENTRIES + 1) { - val f1 = Font.Builder(sFont) - .setFontVariationSettings("'wght' ${i * 100}") - .build() - val f2 = Font.Builder(sFont) - .setFontVariationSettings("'wght' $i") - .build() - interp.lerp(f1, f2, 0.5f) - } - - val cachedFont = interp.lerp(startFont, endFont, 0.5f) - assertThat(resultFont).isNotSameInstanceAs(cachedFont) - } }