@@ -44,12 +44,14 @@ struct TextRenderOptions {
4444 bool is_subpixel = false ;
4545};
4646
47- bool RenderTextInCanvasSkia (const std::shared_ptr<Context>& context,
48- DisplayListBuilder& canvas,
49- const std::string& text,
50- const std::string_view& font_fixture,
51- const TextRenderOptions& options = {},
52- const std::optional<SkFont>& font = std::nullopt ) {
47+ bool RenderTextInCanvasSkia (
48+ const std::shared_ptr<Context>& context,
49+ DisplayListBuilder& canvas,
50+ const std::string& text,
51+ const std::string_view& font_fixture,
52+ const TextRenderOptions& options = {},
53+ const std::optional<SkFont>& font = std::nullopt ,
54+ std::map<std::string, sk_sp<SkTypeface>>* typeface_cache = nullptr ) {
5355 // Draw the baseline.
5456 DlPaint paint;
5557 paint.setColor (DlColor::kAqua ().withAlpha (255 * 0.25 ));
@@ -65,13 +67,29 @@ bool RenderTextInCanvasSkia(const std::shared_ptr<Context>& context,
6567 SkFont selected_font;
6668 if (!font.has_value ()) {
6769 auto c_font_fixture = std::string (font_fixture);
68- auto mapping =
69- flutter::testing::OpenFixtureAsSkData (c_font_fixture.c_str ());
70- if (!mapping) {
71- return false ;
70+ sk_sp<SkTypeface> typeface;
71+ if (typeface_cache) {
72+ auto it = typeface_cache->find (c_font_fixture);
73+ if (it != typeface_cache->end ()) {
74+ typeface = it->second ;
75+ }
76+ }
77+ if (!typeface) {
78+ auto mapping =
79+ flutter::testing::OpenFixtureAsSkData (c_font_fixture.c_str ());
80+ if (!mapping) {
81+ return false ;
82+ }
83+ sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager ();
84+ typeface = font_mgr->makeFromData (mapping);
85+ if (!typeface) {
86+ return false ;
87+ }
88+ if (typeface_cache) {
89+ (*typeface_cache)[c_font_fixture] = typeface;
90+ }
7291 }
73- sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager ();
74- selected_font = SkFont (font_mgr->makeFromData (mapping), options.font_size );
92+ selected_font = SkFont (typeface, options.font_size );
7593 if (options.is_subpixel ) {
7694 selected_font.setSubpixel (true );
7795 }
@@ -867,22 +885,155 @@ TEST_P(AiksTest, MultipleTextWithShadowCache) {
867885
868886 for (auto i = 0 ; i < 5 ; i++) {
869887 ASSERT_TRUE (RenderTextInCanvasSkia (
870- GetContext (), builder, " Hello World" , kFontFixture ,
888+ GetContext (), builder,
889+ (std::string (" Hello World " ) + std::to_string (i)).c_str (), kFontFixture ,
871890 TextRenderOptions{
872891 .color = DlColor::kBlue (),
873892 .filter = DlBlurMaskFilter::Make (DlBlurStyle::kNormal , 4 )}));
874893 }
875894
876895 DisplayListToTexture (builder.Build (), {400 , 400 }, aiks_context);
877896
878- // Text should be cached. Each text gets its own entry as we don't analyze the
879- // strings.
897+ // Each distinct text gets its own cache entry.
880898 EXPECT_EQ (aiks_context.GetContentContext ()
881899 .GetTextShadowCache ()
882900 .GetCacheSizeForTesting (),
883901 5u );
884902}
885903
904+ TEST_P (AiksTest, DuplicateTextWithShadowCache) {
905+ DisplayListBuilder builder;
906+ builder.Scale (GetContentScale ().x , GetContentScale ().y );
907+ DlPaint paint;
908+ paint.setColor (DlColor::ARGB (1 , 0.1 , 0.1 , 0.1 ));
909+ builder.DrawPaint (paint);
910+
911+ AiksContext aiks_context (GetContext (),
912+ std::make_shared<TypographerContextSkia>());
913+ // Cache empty
914+ EXPECT_EQ (aiks_context.GetContentContext ()
915+ .GetTextShadowCache ()
916+ .GetCacheSizeForTesting (),
917+ 0u );
918+
919+ std::map<std::string, sk_sp<SkTypeface>> typeface_cache;
920+ for (auto i = 0 ; i < 5 ; i++) {
921+ ASSERT_TRUE (RenderTextInCanvasSkia (
922+ GetContext (), builder, " Hello World" , kFontFixture ,
923+ TextRenderOptions{
924+ .color = DlColor::kBlue (),
925+ .filter = DlBlurMaskFilter::Make (DlBlurStyle::kNormal , 4 )},
926+ std::nullopt , &typeface_cache));
927+ }
928+
929+ DisplayListToTexture (builder.Build (), {400 , 400 }, aiks_context);
930+
931+ // Duplicate text frames with identical layout share the single cache entry.
932+ EXPECT_EQ (aiks_context.GetContentContext ()
933+ .GetTextShadowCache ()
934+ .GetCacheSizeForTesting (),
935+ 1u );
936+ }
937+
938+ TEST_P (AiksTest, TextShadowCacheKeyCollisionSafety) {
939+ SkFont font = flutter::testing::CreateTestFontOfSize (12 );
940+
941+ Font impeller_font (
942+ MakeTextFrameFromTextBlobSkia (SkTextBlob::MakeFromString (" Text A" , font))
943+ ->GetFont ());
944+
945+ TextFrameFingerprint fp1{
946+ .run_count = 1 ,
947+ .total_glyph_count = 6 ,
948+ .first_glyph_id = 10 ,
949+ .last_glyph_id = 20 ,
950+ .full_hash = 12345 ,
951+ };
952+
953+ TextFrameFingerprint fp2{
954+ .run_count = 1 ,
955+ .total_glyph_count = 6 ,
956+ .first_glyph_id = 10 ,
957+ .last_glyph_id = 21 , // Different last glyph ID
958+ .full_hash = 12345 , // Simulated hash collision on full_hash!
959+ };
960+
961+ // Construct two keys with the exact same identifier & full_hash (simulating a
962+ // hash collision) but with different fingerprints (e.g. different
963+ // last_glyph_id).
964+ TextShadowCache::TextShadowCacheKey key1 (
965+ /* p_max_basis=*/ 1 .0f ,
966+ /* p_is_single_glyph=*/ false ,
967+ /* p_font=*/ impeller_font,
968+ /* p_sigma=*/ Sigma{4 .0f },
969+ /* p_color=*/ Color::Blue (),
970+ /* p_fingerprint=*/ fp1);
971+
972+ TextShadowCache::TextShadowCacheKey key2 (
973+ /* p_max_basis=*/ 1 .0f ,
974+ /* p_is_single_glyph=*/ false ,
975+ /* p_font=*/ impeller_font,
976+ /* p_sigma=*/ Sigma{4 .0f },
977+ /* p_color=*/ Color::Blue (),
978+ /* p_fingerprint=*/ fp2);
979+
980+ TextShadowCache::TextShadowCacheKey::Equal equal;
981+ // Key comparison must fail because fingerprints differ despite identical
982+ // hash.
983+ EXPECT_FALSE (equal (key1, key2));
984+
985+ TextShadowCache::TextShadowCacheKey key3 (
986+ /* p_max_basis=*/ 1 .0f ,
987+ /* p_is_single_glyph=*/ false ,
988+ /* p_font=*/ impeller_font,
989+ /* p_sigma=*/ Sigma{4 .0f },
990+ /* p_color=*/ Color::Blue (),
991+ /* p_fingerprint=*/ fp1);
992+
993+ // Key comparison must succeed for identical fingerprints.
994+ EXPECT_TRUE (equal (key1, key3));
995+ }
996+
997+ TEST_P (AiksTest, SingleGlyphTextShadowCache) {
998+ DisplayListBuilder builder;
999+ builder.Scale (GetContentScale ().x , GetContentScale ().y );
1000+ DlPaint paint;
1001+ paint.setColor (DlColor::ARGB (1 , 0.1 , 0.1 , 0.1 ));
1002+ builder.DrawPaint (paint);
1003+
1004+ AiksContext aiks_context (GetContext (),
1005+ std::make_shared<TypographerContextSkia>());
1006+ EXPECT_EQ (aiks_context.GetContentContext ()
1007+ .GetTextShadowCache ()
1008+ .GetCacheSizeForTesting (),
1009+ 0u );
1010+
1011+ SkFont font = flutter::testing::CreateTestFontOfSize (12 );
1012+ TextRenderOptions options{
1013+ .color = DlColor::kBlue (),
1014+ .filter = DlBlurMaskFilter::Make (DlBlurStyle::kNormal , 4 )};
1015+
1016+ // Draw single glyph "A".
1017+ ASSERT_TRUE (RenderTextInCanvasSkia (GetContext (), builder, " A" , kFontFixture ,
1018+ options, font));
1019+
1020+ // Draw single glyph "B" (different glyph ID, identical font/color/sigma).
1021+ ASSERT_TRUE (RenderTextInCanvasSkia (GetContext (), builder, " B" , kFontFixture ,
1022+ options, font));
1023+
1024+ // Draw single glyph "A" again (duplicate glyph ID).
1025+ ASSERT_TRUE (RenderTextInCanvasSkia (GetContext (), builder, " A" , kFontFixture ,
1026+ options, font));
1027+
1028+ DisplayListToTexture (builder.Build (), {400 , 400 }, aiks_context);
1029+
1030+ // Single glyphs "A" and "B" get distinct entries; duplicate "A" reuses "A".
1031+ EXPECT_EQ (aiks_context.GetContentContext ()
1032+ .GetTextShadowCache ()
1033+ .GetCacheSizeForTesting (),
1034+ 2u );
1035+ }
1036+
8861037TEST_P (AiksTest, MultipleColorWithShadowCache) {
8871038 DisplayListBuilder builder;
8881039 builder.Scale (GetContentScale ().x , GetContentScale ().y );
0 commit comments