Skip to content

Commit 67d7e55

Browse files
authored
Fixes for texture-backed gradients (flutter#191925)
Two fixes for texture-backed gradients: 1. Fix incorrect ending coord check in IPSampleLinearWithTileMode for decal mode. This fixes an issue where decal gradients end a pixel early. 2. Make CreateGradientBuffer compatible with overlapping color stops Related: flutter#190401 This re-implements part of flutter#190874. That PR was reverted because it didn't handle wide gamut colors properly. I will re-implement a fixed version of it in multiple parts. In the original reviews for that PR, it was suggested that these self-contained gradient fixes could be put in their own separate PR. Now that the PR is reverted, we get our chance to put these fixes in their own PR. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent cbb8915 commit 67d7e55

4 files changed

Lines changed: 85 additions & 63 deletions

File tree

‎engine/src/flutter/impeller/compiler/shader_lib/impeller/texture.glsl‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ vec4 IPSampleLinearWithTileMode(sampler2D tex,
9898
float x_tile_mode,
9999
float y_tile_mode,
100100
vec4 decal_border_color) {
101-
if (x_tile_mode == kTileModeDecal && (coords.x < 0 || coords.x >= 1) ||
102-
y_tile_mode == kTileModeDecal && (coords.y < 0 || coords.y >= 1)) {
101+
// Unlike 2D image sampling which uses a half-open interval [0, 1) where 1.0
102+
// is outside the texture boundary, linear sampling uses a closed interval
103+
// [0, 1]. Therefore, decal mode checks the ending coordinate using > 1 rather
104+
// than >= 1.
105+
if (x_tile_mode == kTileModeDecal && (coords.x < 0.0 || coords.x > 1.0) ||
106+
y_tile_mode == kTileModeDecal && (coords.y < 0.0 || coords.y > 1.0)) {
103107
return decal_border_color;
104108
}
105109

‎engine/src/flutter/impeller/geometry/geometry_unittests.cc‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,13 +1834,28 @@ TEST(GeometryTest, Gradient) {
18341834
}
18351835

18361836
{
1837-
// Gradient with duplicate stops does not create an empty texture.
1837+
// Gradient with duplicate stops creates a 1024 texture.
18381838
std::vector<Color> colors = {Color::Red(), Color::Yellow(), Color::Black(),
18391839
Color::Blue()};
18401840
std::vector<Scalar> stops = {0.0, 0.25, 0.25, 1.0};
18411841

18421842
auto gradient = CreateGradientBuffer(colors, stops);
1843-
ASSERT_EQ(gradient.texture_size, 5u);
1843+
ASSERT_EQ(gradient.texture_size, 1024u);
1844+
1845+
std::vector<Color> expected_colors(1024);
1846+
for (size_t i = 0; i < 1024; i++) {
1847+
double t = i / 1023.0;
1848+
if (i < 256) {
1849+
// Interval 1: texels 0 to 255 (stop interval 0 to 0.25)
1850+
expected_colors[i] =
1851+
Color::Lerp(Color::Red(), Color::Yellow(), t / 0.25);
1852+
} else {
1853+
// Interval 2: texels 256 to 1023 (stop interval 0.25 to 1.0)
1854+
expected_colors[i] =
1855+
Color::Lerp(Color::Black(), Color::Blue(), (t - 0.25) / 0.75);
1856+
}
1857+
}
1858+
ASSERT_COLOR_BUFFER_NEAR(gradient.color_bytes, expected_colors);
18441859
}
18451860

18461861
{

‎engine/src/flutter/impeller/geometry/gradient.cc‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
namespace impeller {
1111

12+
// TODO(b-luk): this should use a platform specific max texture size.
13+
// https://github.com/flutter/flutter/issues/191820
14+
static constexpr uint32_t kMaxGradientTextureSize = 1024;
15+
1216
static void AppendColor(const Color& color, GradientData* data) {
1317
auto converted = color.ToR8G8B8A8();
1418
data->color_bytes.push_back(converted[0]);
@@ -26,30 +30,29 @@ GradientData CreateGradientBuffer(const std::vector<Color>& colors,
2630
texture_size = colors.size();
2731
} else {
2832
auto minimum_delta = 1.0;
33+
// Avoid creating textures that are absurdly large due to stops that are
34+
// very close together.
35+
auto minimum_allowable_delta = 1.0 / (kMaxGradientTextureSize - 1.0);
2936
for (size_t i = 1; i < stops.size(); i++) {
3037
auto value = stops[i] - stops[i - 1];
31-
// Smaller than kEhCloseEnough
32-
if (value < 0.0001) {
33-
continue;
38+
if (value <= minimum_allowable_delta) {
39+
minimum_delta = minimum_allowable_delta;
40+
break;
3441
}
3542
if (value < minimum_delta) {
3643
minimum_delta = value;
3744
}
3845
}
39-
// Avoid creating textures that are absurdly large due to stops that are
40-
// very close together.
41-
// TODO(jonahwilliams): this should use a platform specific max texture
42-
// size.
43-
texture_size = std::min(
44-
static_cast<uint32_t>(std::round(1.0 / minimum_delta)) + 1, 1024u);
46+
texture_size = static_cast<uint32_t>(std::round(1.0 / minimum_delta)) + 1;
4547
}
4648
GradientData data = {
4749
.color_bytes = {},
4850
.texture_size = texture_size,
4951
};
5052
data.color_bytes.reserve(texture_size * 4);
5153

52-
if (texture_size == colors.size() && colors.size() <= 1024) {
54+
if (texture_size == colors.size() &&
55+
colors.size() <= kMaxGradientTextureSize) {
5356
for (auto i = 0u; i < colors.size(); i++) {
5457
AppendColor(colors[i], &data);
5558
}
@@ -79,17 +82,14 @@ GradientData CreateGradientBuffer(const std::vector<Color>& colors,
7982

8083
AppendColor(mixed_color, &data);
8184
} else {
82-
// We've slightly overshot the previous stop.
85+
// We've passed the next stop. Advance to the next stop interval.
86+
// Decrement `i` to re-evaluate the current texel against the new
87+
// interval. When there are overlapping or zero-delta stops, this branch
88+
// evaluates repeatedly until the overlap is passed.
8389
previous_color = next_color;
8490
previous_stop = next_stop;
8591
previous_color_index += 1;
86-
next_color = colors[previous_color_index + 1];
87-
auto next_stop = stops[previous_color_index + 1];
88-
89-
auto t = (scaled_i - previous_stop) / (next_stop - previous_stop);
90-
auto mixed_color = Color::Lerp(previous_color, next_color, t);
91-
92-
AppendColor(mixed_color, &data);
92+
i--;
9393
}
9494
}
9595
// The last index is always equal to the last color, exactly.

‎engine/src/flutter/impeller/tools/malioc.json‎

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@
744744
],
745745
"longest_path_cycles": [
746746
0.675000011920929,
747-
0.46875,
747+
0.484375,
748748
0.675000011920929,
749749
0.1875,
750750
0.0,
@@ -778,9 +778,9 @@
778778
"arith_cvt"
779779
],
780780
"total_cycles": [
781-
0.862500011920929,
781+
0.84375,
782782
0.59375,
783-
0.862500011920929,
783+
0.84375,
784784
0.3125,
785785
0.0,
786786
0.25,
@@ -816,8 +816,8 @@
816816
"arith_fma"
817817
],
818818
"longest_path_cycles": [
819-
0.328125,
820-
0.328125,
819+
0.34375,
820+
0.34375,
821821
0.296875,
822822
0.125,
823823
0.0,
@@ -834,12 +834,14 @@
834834
"texture"
835835
],
836836
"shortest_path_bound_pipelines": [
837+
"arith_total",
838+
"arith_fma",
837839
"varying"
838840
],
839841
"shortest_path_cycles": [
840-
0.234375,
841-
0.234375,
842-
0.203125,
842+
0.25,
843+
0.25,
844+
0.1875,
843845
0.125,
844846
0.0,
845847
0.25,
@@ -850,9 +852,9 @@
850852
"arith_cvt"
851853
],
852854
"total_cycles": [
853-
0.421875,
855+
0.40625,
854856
0.359375,
855-
0.421875,
857+
0.40625,
856858
0.125,
857859
0.0,
858860
0.25,
@@ -889,7 +891,7 @@
889891
],
890892
"longest_path_cycles": [
891893
0.421875,
892-
0.328125,
894+
0.34375,
893895
0.421875,
894896
0.125,
895897
0.0,
@@ -922,9 +924,9 @@
922924
"arith_cvt"
923925
],
924926
"total_cycles": [
925-
0.546875,
927+
0.53125,
926928
0.359375,
927-
0.546875,
929+
0.53125,
928930
0.125,
929931
0.0,
930932
0.25,
@@ -9365,13 +9367,15 @@
93659367
"has_stack_spilling": false,
93669368
"performance": {
93679369
"longest_path_bound_pipelines": [
9370+
"arith_total",
9371+
"arith_fma",
93689372
"varying",
93699373
"texture"
93709374
],
93719375
"longest_path_cycles": [
9372-
0.234375,
9373-
0.234375,
9374-
0.234375,
9376+
0.25,
9377+
0.25,
9378+
0.21875,
93759379
0.0625,
93769380
0.0,
93779381
0.25,
@@ -9390,9 +9394,9 @@
93909394
"varying"
93919395
],
93929396
"shortest_path_cycles": [
9393-
0.171875,
9394-
0.140625,
9395-
0.171875,
9397+
0.15625,
9398+
0.15625,
9399+
0.15625,
93969400
0.0625,
93979401
0.0,
93989402
0.25,
@@ -9403,9 +9407,9 @@
94039407
"arith_cvt"
94049408
],
94059409
"total_cycles": [
9406-
0.328125,
9407-
0.265625,
9408-
0.328125,
9410+
0.3125,
9411+
0.28125,
9412+
0.3125,
94099413
0.0625,
94109414
0.0,
94119415
0.25,
@@ -9906,12 +9910,12 @@
99069910
"performance": {
99079911
"longest_path_bound_pipelines": [
99089912
"arith_total",
9909-
"arith_cvt"
9913+
"arith_fma"
99109914
],
99119915
"longest_path_cycles": [
99129916
0.265625,
9913-
0.25,
99149917
0.265625,
9918+
0.25,
99159919
0.125,
99169920
0.0,
99179921
0.25,
@@ -9930,9 +9934,9 @@
99309934
"varying"
99319935
],
99329936
"shortest_path_cycles": [
9933-
0.203125,
9934-
0.15625,
9935-
0.203125,
9937+
0.1875,
9938+
0.171875,
9939+
0.1875,
99369940
0.125,
99379941
0.0,
99389942
0.25,
@@ -9943,9 +9947,9 @@
99439947
"arith_cvt"
99449948
],
99459949
"total_cycles": [
9946-
0.359375,
9947-
0.28125,
9948-
0.359375,
9950+
0.34375,
9951+
0.296875,
9952+
0.34375,
99499953
0.125,
99509954
0.0,
99519955
0.25,
@@ -10884,9 +10888,9 @@
1088410888
"arith_fma"
1088510889
],
1088610890
"longest_path_cycles": [
10887-
0.40625,
10888-
0.40625,
10889-
0.296875,
10891+
0.421875,
10892+
0.421875,
10893+
0.28125,
1089010894
0.3125,
1089110895
0.0,
1089210896
0.25,
@@ -10903,13 +10907,12 @@
1090310907
],
1090410908
"shortest_path_bound_pipelines": [
1090510909
"arith_total",
10906-
"arith_fma",
10907-
"arith_sfu"
10910+
"arith_fma"
1090810911
],
1090910912
"shortest_path_cycles": [
10910-
0.3125,
10911-
0.3125,
10912-
0.234375,
10913+
0.328125,
10914+
0.328125,
10915+
0.21875,
1091310916
0.3125,
1091410917
0.0,
1091510918
0.25,
@@ -10920,9 +10923,9 @@
1092010923
"arith_fma"
1092110924
],
1092210925
"total_cycles": [
10923-
0.4375,
10924-
0.4375,
10925-
0.390625,
10926+
0.453125,
10927+
0.453125,
10928+
0.375,
1092610929
0.3125,
1092710930
0.0,
1092810931
0.25,

0 commit comments

Comments
 (0)