Skip to content

Commit 75004a6

Browse files
[Impeller] Add the paint color to the key of the text shadow cache (flutter#177140)
Fixes flutter#176146
1 parent daa3113 commit 75004a6

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

engine/src/flutter/impeller/display_list/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ template("display_list_unittests_component") {
114114
deps += [
115115
":display_list",
116116
"../playground:playground_test",
117+
"//flutter/display_list/testing:display_list_testing",
117118
"//flutter/impeller/golden_tests:screenshot",
118119
"//flutter/txt",
119120
]

engine/src/flutter/impeller/display_list/aiks_dl_text_unittests.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "flutter/display_list/effects/dl_color_source.h"
1212
#include "flutter/display_list/effects/dl_mask_filter.h"
1313
#include "flutter/display_list/geometry/dl_path_builder.h"
14+
#include "flutter/display_list/testing/dl_test_snippets.h"
1415
#include "flutter/fml/build_config.h"
1516
#include "flutter/impeller/display_list/aiks_unittests.h"
1617
#include "flutter/testing/testing.h"
@@ -834,6 +835,44 @@ TEST_P(AiksTest, MultipleTextWithShadowCache) {
834835
5u);
835836
}
836837

838+
TEST_P(AiksTest, MultipleColorWithShadowCache) {
839+
DisplayListBuilder builder;
840+
builder.Scale(GetContentScale().x, GetContentScale().y);
841+
DlPaint paint;
842+
paint.setColor(DlColor::kWhite());
843+
builder.DrawPaint(paint);
844+
845+
AiksContext aiks_context(GetContext(),
846+
std::make_shared<TypographerContextSkia>());
847+
// Cache empty
848+
EXPECT_EQ(aiks_context.GetContentContext()
849+
.GetTextShadowCache()
850+
.GetCacheSizeForTesting(),
851+
0u);
852+
853+
SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
854+
855+
std::array<DlColor, 4> colors{DlColor::kRed(), DlColor::kGreen(),
856+
DlColor::kBlue(), DlColor::kRed()};
857+
for (const auto& color : colors) {
858+
ASSERT_TRUE(RenderTextInCanvasSkia(
859+
GetContext(), builder, "A", kFontFixture,
860+
TextRenderOptions{
861+
.color = color,
862+
.filter = DlBlurMaskFilter::Make(DlBlurStyle::kNormal, 4)},
863+
sk_font));
864+
}
865+
866+
DisplayListToTexture(builder.Build(), {400, 400}, aiks_context);
867+
868+
// The count of cache entries should match the number of distinct colors
869+
// in the list. Repeated usage of a color should not add to the cache.
870+
EXPECT_EQ(aiks_context.GetContentContext()
871+
.GetTextShadowCache()
872+
.GetCacheSizeForTesting(),
873+
3u);
874+
}
875+
837876
TEST_P(AiksTest, SingleIconShadowTest) {
838877
DisplayListBuilder builder;
839878
builder.Scale(GetContentScale().x, GetContentScale().y);

engine/src/flutter/impeller/display_list/canvas.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,8 @@ bool Canvas::AttemptBlurredTextOptimization(
16491649
/*p_identifier=*/identifier,
16501650
/*p_is_single_glyph=*/maybe_glyph.has_value(),
16511651
/*p_font=*/text_frame->GetFont(),
1652-
/*p_sigma=*/paint.mask_blur_descriptor->sigma);
1652+
/*p_sigma=*/paint.mask_blur_descriptor->sigma,
1653+
/*p_color=*/paint.color);
16531654

16541655
std::optional<Entity> result = renderer_.GetTextShadowCache().Lookup(
16551656
renderer_, entity, filter, cache_key);

engine/src/flutter/impeller/entity/contents/text_shadow_cache.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ TextShadowCache::TextShadowCacheKey::TextShadowCacheKey(Scalar p_max_basis,
1919
int64_t p_identifier,
2020
bool p_is_single_glyph,
2121
const Font& p_font,
22-
Sigma p_sigma)
22+
Sigma p_sigma,
23+
Color p_color)
2324
: max_basis(p_max_basis),
2425
identifier(p_identifier),
2526
is_single_glyph(p_is_single_glyph),
2627
font(p_font),
2728
rounded_sigma(Rational(std::round(p_sigma.sigma * kMaxSigmaDenominator),
28-
kMaxSigmaDenominator)) {}
29+
kMaxSigmaDenominator)),
30+
color(p_color) {}
2931

3032
void TextShadowCache::MarkFrameStart() {
3133
for (auto& entry : entries_) {

engine/src/flutter/impeller/entity/contents/text_shadow_cache.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@ class TextShadowCache {
4141
bool is_single_glyph;
4242
Font font;
4343
Rational rounded_sigma;
44+
Color color;
4445

4546
TextShadowCacheKey(Scalar p_max_basis,
4647
int64_t p_identifier,
4748
bool p_is_single_glyph,
4849
const Font& p_font,
49-
Sigma p_sigma);
50+
Sigma p_sigma,
51+
Color p_color);
5052

5153
struct Hash {
5254
std::size_t operator()(const TextShadowCacheKey& key) const {
5355
return fml::HashCombine(key.max_basis, key.identifier,
5456
key.is_single_glyph, key.font.GetHash(),
55-
key.rounded_sigma.GetHash());
57+
key.rounded_sigma.GetHash(),
58+
key.color.ToARGB());
5659
}
5760
};
5861

@@ -63,7 +66,7 @@ class TextShadowCache {
6366
lhs.identifier == rhs.identifier &&
6467
lhs.is_single_glyph == rhs.is_single_glyph &&
6568
lhs.font.IsEqual(rhs.font) &&
66-
lhs.rounded_sigma == rhs.rounded_sigma;
69+
lhs.rounded_sigma == rhs.rounded_sigma && lhs.color == rhs.color;
6770
}
6871
};
6972
};

0 commit comments

Comments
 (0)