Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a407ee86 authored by Tyler Freeman's avatar Tyler Freeman
Browse files

feat(non linear font scaling): optimization: cache any generated tables to...

feat(non linear font scaling): optimization: cache any generated tables to reduce duplicate calculations

Some clients (e.g. Compose) might call this every time they convert, in
which case we need to ensure it is fast and avoids unnecessary
allocations.

Bug: b/239736383
Flag: android.content.res.font_scale_converter_public
Test:  atest FrameworksCoreTests:android.content.res.FontScaleConverterFactoryTest

Change-Id: Ie73fa6d49e7605eea12a37930c82db7747dd8e2c
parent f23efb69
Loading
Loading
Loading
Loading
+21 −3
Original line number Diff line number Diff line
@@ -145,7 +145,17 @@ public class FontScaleConverterFactory {
            // them a straight linear table instead.
            // This works because when FontScaleConverter encounters a size beyond its bounds, it
            // calculates a linear fontScale factor using the ratio of the last element pair.
            return new FontScaleConverterImpl(new float[] {1f}, new float[] {fontScale});
            FontScaleConverterImpl converter = new FontScaleConverterImpl(
                    new float[]{1f},
                    new float[]{fontScale}
            );

            if (Flags.fontScaleConverterPublic()) {
                // Cache for next time.
                put(fontScale, converter);
            }

            return converter;
        } else {
            float startScale = getScaleFromKey(LOOKUP_TABLES.keyAt(lowerIndex));
            float endScale = getScaleFromKey(LOOKUP_TABLES.keyAt(higherIndex));
@@ -156,10 +166,18 @@ public class FontScaleConverterFactory {
                    endScale,
                    fontScale
            );
            return createInterpolatedTableBetween(
            FontScaleConverter converter = createInterpolatedTableBetween(
                    LOOKUP_TABLES.valueAt(lowerIndex),
                    LOOKUP_TABLES.valueAt(higherIndex),
                    interpolationPoint);
                    interpolationPoint
            );

            if (Flags.fontScaleConverterPublic()) {
                // Cache for next time.
                put(fontScale, converter);
            }

            return converter;
        }
    }

+29 −1
Original line number Diff line number Diff line
@@ -16,14 +16,17 @@

package android.content.res


import android.platform.test.annotations.Presubmit
import android.platform.test.annotations.RequiresFlagsEnabled
import android.platform.test.flag.junit.CheckFlagsRule
import android.platform.test.flag.junit.DeviceFlagsValueProvider
import androidx.core.util.forEach
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import org.junit.Rule
import kotlin.math.ceil
import kotlin.math.floor
import org.junit.Test
@@ -39,6 +42,9 @@ import kotlin.random.Random.Default.nextFloat
@RunWith(AndroidJUnit4::class)
class FontScaleConverterFactoryTest {

    @get:Rule
    val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()

    @Test
    fun scale200IsTwiceAtSmallSizes() {
        val table = FontScaleConverterFactory.forScale(2F)!!
@@ -88,6 +94,28 @@ class FontScaleConverterFactoryTest {
        assertThat(table.convertSpToDp(0F)).isWithin(CONVERSION_TOLERANCE).of(0f)
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_FONT_SCALE_CONVERTER_PUBLIC)
    fun missingLookupTable_cachesInterpolated() {
        val table = FontScaleConverterFactory.forScale(1.6F)!!

        assertThat(FontScaleConverterFactory.LOOKUP_TABLES.contains((1.6F * 100).toInt())).isTrue()
        // Double check known existing values
        assertThat(FontScaleConverterFactory.LOOKUP_TABLES.contains((1.5F * 100).toInt())).isTrue()
        assertThat(FontScaleConverterFactory.LOOKUP_TABLES.contains((1.7F * 100).toInt())).isFalse()
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_FONT_SCALE_CONVERTER_PUBLIC)
    fun missingLookupTablePastEnd_cachesLinear() {
        val table = FontScaleConverterFactory.forScale(3F)!!

        assertThat(FontScaleConverterFactory.LOOKUP_TABLES.contains((3F * 100).toInt())).isTrue()
        // Double check known existing values
        assertThat(FontScaleConverterFactory.LOOKUP_TABLES.contains((1.5F * 100).toInt())).isTrue()
        assertThat(FontScaleConverterFactory.LOOKUP_TABLES.contains((1.7F * 100).toInt())).isFalse()
    }

    @SmallTest
    @Test
    fun missingLookupTableNegativeReturnsNull() {