Skip to content

Commit fc93b1e

Browse files
author
Android Build Coastguard Worker
committed
Merge cherrypicks of ['googleplex-android-review.googlesource.com/22338474', 'googleplex-android-review.googlesource.com/22757815', 'googleplex-android-review.googlesource.com/22775991'] into tm-qpr3-release.
Change-Id: Icd14eaa30d62f3e86455adeb13a2cf933a69a150
2 parents be1c38a + dfd7561 commit fc93b1e

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

packages/SystemUI/animation/src/com/android/systemui/animation/FontInterpolator.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package com.android.systemui.animation
1818

1919
import android.graphics.fonts.Font
2020
import android.graphics.fonts.FontVariationAxis
21+
import android.util.LruCache
2122
import android.util.MathUtils
2223
import android.util.MathUtils.abs
24+
import androidx.annotation.VisibleForTesting
2325
import java.lang.Float.max
2426
import java.lang.Float.min
2527

@@ -34,6 +36,10 @@ private const val FONT_ITALIC_MIN = 0f
3436
private const val FONT_ITALIC_ANIMATION_STEP = 0.1f
3537
private const val FONT_ITALIC_DEFAULT_VALUE = 0f
3638

39+
// Benchmarked via Perfetto, difference between 10 and 50 entries is about 0.3ms in
40+
// frame draw time on a Pixel 6.
41+
@VisibleForTesting const val FONT_CACHE_MAX_ENTRIES = 10
42+
3743
/** Provide interpolation of two fonts by adjusting font variation settings. */
3844
class FontInterpolator {
3945

@@ -81,8 +87,8 @@ class FontInterpolator {
8187
// Font interpolator has two level caches: one for input and one for font with different
8288
// variation settings. No synchronization is needed since FontInterpolator is not designed to be
8389
// thread-safe and can be used only on UI thread.
84-
private val interpCache = hashMapOf<InterpKey, Font>()
85-
private val verFontCache = hashMapOf<VarFontKey, Font>()
90+
private val interpCache = LruCache<InterpKey, Font>(FONT_CACHE_MAX_ENTRIES)
91+
private val verFontCache = LruCache<VarFontKey, Font>(FONT_CACHE_MAX_ENTRIES)
8692

8793
// Mutable keys for recycling.
8894
private val tmpInterpKey = InterpKey(null, null, 0f)
@@ -152,16 +158,16 @@ class FontInterpolator {
152158
tmpVarFontKey.set(start, newAxes)
153159
val axesCachedFont = verFontCache[tmpVarFontKey]
154160
if (axesCachedFont != null) {
155-
interpCache[InterpKey(start, end, progress)] = axesCachedFont
161+
interpCache.put(InterpKey(start, end, progress), axesCachedFont)
156162
return axesCachedFont
157163
}
158164

159165
// This is the first time to make the font for the axes. Build and store it to the cache.
160166
// Font.Builder#build won't throw IOException since creating fonts from existing fonts will
161167
// not do any IO work.
162168
val newFont = Font.Builder(start).setFontVariationSettings(newAxes.toTypedArray()).build()
163-
interpCache[InterpKey(start, end, progress)] = newFont
164-
verFontCache[VarFontKey(start, newAxes)] = newFont
169+
interpCache.put(InterpKey(start, end, progress), newFont)
170+
verFontCache.put(VarFontKey(start, newAxes), newFont)
165171
return newFont
166172
}
167173

packages/SystemUI/animation/src/com/android/systemui/animation/TextAnimator.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import android.graphics.Canvas
2424
import android.graphics.Typeface
2525
import android.graphics.fonts.Font
2626
import android.text.Layout
27+
import android.util.LruCache
2728

2829
private const val DEFAULT_ANIMATION_DURATION: Long = 300
30+
private const val TYPEFACE_CACHE_MAX_ENTRIES = 5
2931

3032
typealias GlyphCallback = (TextAnimator.PositionedGlyph, Float) -> Unit
3133
/**
@@ -114,7 +116,7 @@ class TextAnimator(layout: Layout, private val invalidateCallback: () -> Unit) {
114116

115117
private val fontVariationUtils = FontVariationUtils()
116118

117-
private val typefaceCache = HashMap<String, Typeface?>()
119+
private val typefaceCache = LruCache<String, Typeface>(TYPEFACE_CACHE_MAX_ENTRIES)
118120

119121
fun updateLayout(layout: Layout) {
120122
textInterpolator.layout = layout
@@ -218,12 +220,12 @@ class TextAnimator(layout: Layout, private val invalidateCallback: () -> Unit) {
218220
}
219221

220222
if (!fvar.isNullOrBlank()) {
221-
textInterpolator.targetPaint.typeface =
222-
typefaceCache.getOrElse(fvar) {
223-
textInterpolator.targetPaint.fontVariationSettings = fvar
223+
textInterpolator.targetPaint.typeface = typefaceCache.get(fvar) ?: run {
224+
textInterpolator.targetPaint.fontVariationSettings = fvar
225+
textInterpolator.targetPaint.typeface?.also {
224226
typefaceCache.put(fvar, textInterpolator.targetPaint.typeface)
225-
textInterpolator.targetPaint.typeface
226227
}
228+
}
227229
}
228230

229231
if (color != null) {

packages/SystemUI/res/values/dimens.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@
396396
<dimen name="navigation_key_width">70dp</dimen>
397397

398398
<!-- The width/height of the icon of a navigation button -->
399-
<dimen name="navigation_icon_size">32dp</dimen>
399+
<dimen name="navigation_icon_size">24dp</dimen>
400400

401401
<!-- The padding on the side of the navigation bar. Must be greater than or equal to
402402
navigation_extra_key_width -->

packages/SystemUI/tests/src/com/android/keyguard/KeyguardSecurityContainerControllerTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,19 @@ public class KeyguardSecurityContainerControllerTest extends SysuiTestCase {
163163
@Captor
164164
private ArgumentCaptor<KeyguardSecurityContainer.SwipeListener> mSwipeListenerArgumentCaptor;
165165

166-
private Configuration mConfiguration;
167-
168166
private KeyguardSecurityContainerController mKeyguardSecurityContainerController;
169167
private KeyguardPasswordViewController mKeyguardPasswordViewController;
170168
private KeyguardPasswordView mKeyguardPasswordView;
171169
private TestableResources mTestableResources;
172170

173171
@Before
174172
public void setup() {
175-
mConfiguration = new Configuration();
176-
mConfiguration.setToDefaults(); // Defaults to ORIENTATION_UNDEFINED.
177173
mTestableResources = mContext.getOrCreateTestableResources();
174+
mTestableResources.getResources().getConfiguration().orientation =
175+
Configuration.ORIENTATION_UNDEFINED;
178176

179177
when(mView.getContext()).thenReturn(mContext);
180-
when(mView.getResources()).thenReturn(mContext.getResources());
178+
when(mView.getResources()).thenReturn(mTestableResources.getResources());
181179
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* width= */ 0, /* height= */
182180
0);
183181
lp.gravity = 0;
@@ -254,6 +252,8 @@ public void startDisappearAnimation_animatesKeyboard() {
254252

255253
@Test
256254
public void onResourcesUpdate_callsThroughOnRotationChange() {
255+
clearInvocations(mView);
256+
257257
// Rotation is the same, shouldn't cause an update
258258
mKeyguardSecurityContainerController.updateResources();
259259
verify(mView, never()).initMode(eq(MODE_DEFAULT), eq(mGlobalSettings), eq(mFalsingManager),
@@ -579,22 +579,20 @@ public void testGravityReappliedOnConfigurationChange() {
579579
// Set initial gravity
580580
mTestableResources.addOverride(R.integer.keyguard_host_view_gravity,
581581
Gravity.CENTER);
582+
mTestableResources.addOverride(
583+
R.bool.can_use_one_handed_bouncer, false);
582584

583585
// Kick off the initial pass...
584586
mKeyguardSecurityContainerController.onInit();
585-
verify(mView).setLayoutParams(argThat(
586-
(ArgumentMatcher<FrameLayout.LayoutParams>) argument ->
587-
argument.gravity == Gravity.CENTER));
587+
verify(mView).setLayoutParams(any());
588588
clearInvocations(mView);
589589

590590
// Now simulate a config change
591591
mTestableResources.addOverride(R.integer.keyguard_host_view_gravity,
592592
Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
593593

594594
mKeyguardSecurityContainerController.updateResources();
595-
verify(mView).setLayoutParams(argThat(
596-
(ArgumentMatcher<FrameLayout.LayoutParams>) argument ->
597-
argument.gravity == (Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)));
595+
verify(mView).setLayoutParams(any());
598596
}
599597

600598
@Test

packages/SystemUI/tests/src/com/android/systemui/animation/FontInterpolatorTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,29 @@ class FontInterpolatorTest : SysuiTestCase() {
106106
val reversedFont = interp.lerp(endFont, startFont, 0.5f)
107107
assertThat(resultFont).isSameInstanceAs(reversedFont)
108108
}
109+
110+
@Test
111+
fun testCacheMaxSize() {
112+
val interp = FontInterpolator()
113+
114+
val startFont = Font.Builder(sFont)
115+
.setFontVariationSettings("'wght' 100")
116+
.build()
117+
val endFont = Font.Builder(sFont)
118+
.setFontVariationSettings("'wght' 1")
119+
.build()
120+
val resultFont = interp.lerp(startFont, endFont, 0.5f)
121+
for (i in 0..FONT_CACHE_MAX_ENTRIES + 1) {
122+
val f1 = Font.Builder(sFont)
123+
.setFontVariationSettings("'wght' ${i * 100}")
124+
.build()
125+
val f2 = Font.Builder(sFont)
126+
.setFontVariationSettings("'wght' $i")
127+
.build()
128+
interp.lerp(f1, f2, 0.5f)
129+
}
130+
131+
val cachedFont = interp.lerp(startFont, endFont, 0.5f)
132+
assertThat(resultFont).isNotSameInstanceAs(cachedFont)
133+
}
109134
}

0 commit comments

Comments
 (0)