Skip to content

Commit 0f801f8

Browse files
[Impeller] In Paint::CreateContents, do not set a color source size if the size is empty (#163099)
The Canvas::DrawVertices lazy texture function will be unable to create a snapshot based on an empty color source size. See flutter/flutter#162969
1 parent 512d1d5 commit 0f801f8

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -832,15 +832,15 @@ void Canvas::DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
832832
contents->SetAlpha(paint.color.alpha);
833833
contents->SetGeometry(vertices);
834834
contents->SetLazyTextureCoverage(src_coverage);
835-
contents->SetLazyTexture(
836-
[src_contents, src_coverage](const ContentContext& renderer) {
837-
// Applying the src coverage as the coverage limit prevents the 1px
838-
// coverage pad from adding a border that is picked up by developer
839-
// specified UVs.
840-
return src_contents
841-
->RenderToSnapshot(renderer, {}, Rect::Round(src_coverage))
842-
->texture;
843-
});
835+
contents->SetLazyTexture([src_contents,
836+
src_coverage](const ContentContext& renderer) {
837+
// Applying the src coverage as the coverage limit prevents the 1px
838+
// coverage pad from adding a border that is picked up by developer
839+
// specified UVs.
840+
auto snapshot =
841+
src_contents->RenderToSnapshot(renderer, {}, Rect::Round(src_coverage));
842+
return snapshot.has_value() ? snapshot->texture : nullptr;
843+
});
844844
entity.SetContents(paint.WithFilters(std::move(contents)));
845845
AddRenderEntityToCurrentPass(entity);
846846
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "impeller/core/texture_descriptor.h"
1414
#include "impeller/display_list/aiks_unittests.h"
1515
#include "impeller/display_list/canvas.h"
16+
#include "impeller/display_list/dl_vertices_geometry.h"
1617
#include "impeller/geometry/geometry_asserts.h"
1718
#include "impeller/renderer/render_target.h"
1819

@@ -278,5 +279,48 @@ TEST_P(AiksTest, BackdropCountDownWithNestedSaveLayers) {
278279
EXPECT_TRUE(canvas->RequiresReadback());
279280
}
280281

282+
TEST_P(AiksTest, DrawVerticesLinearGradientWithEmptySize) {
283+
RenderCallback callback = [&](RenderTarget& render_target) {
284+
ContentContext context(GetContext(), nullptr);
285+
Canvas canvas(context, render_target, true, false);
286+
287+
std::vector<flutter::DlPoint> vertex_coordinates = {
288+
flutter::DlPoint(0, 0),
289+
flutter::DlPoint(600, 0),
290+
flutter::DlPoint(0, 600),
291+
};
292+
std::vector<flutter::DlPoint> texture_coordinates = {
293+
flutter::DlPoint(0, 0),
294+
flutter::DlPoint(500, 0),
295+
flutter::DlPoint(0, 500),
296+
};
297+
std::vector<uint16_t> indices = {0, 1, 2};
298+
flutter::DlVertices::Builder vertices_builder(
299+
flutter::DlVertexMode::kTriangleStrip, vertex_coordinates.size(),
300+
flutter::DlVertices::Builder::kHasTextureCoordinates, indices.size());
301+
vertices_builder.store_vertices(vertex_coordinates.data());
302+
vertices_builder.store_indices(indices.data());
303+
vertices_builder.store_texture_coordinates(texture_coordinates.data());
304+
auto vertices = vertices_builder.build();
305+
306+
std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
307+
flutter::DlColor::kRed()};
308+
std::vector<Scalar> stops = {0.0, 1.0};
309+
auto gradient = flutter::DlColorSource::MakeLinear(
310+
{0, 0}, {0, 600}, 2, colors.data(), stops.data(),
311+
flutter::DlTileMode::kClamp);
312+
313+
Paint paint;
314+
paint.color_source = gradient.get();
315+
canvas.DrawVertices(std::make_shared<DlVerticesGeometry>(vertices, context),
316+
BlendMode::kSourceOver, paint);
317+
318+
canvas.EndReplay();
319+
return true;
320+
};
321+
322+
ASSERT_TRUE(Playground::OpenPlaygroundHere(callback));
323+
}
324+
281325
} // namespace testing
282326
} // namespace impeller

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ std::shared_ptr<ColorSourceContents> Paint::CreateContents() const {
6565

6666
std::array<Point, 2> bounds{start_point, end_point};
6767
auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
68-
if (intrinsic_size.has_value()) {
68+
if (intrinsic_size.has_value() && !intrinsic_size->IsEmpty()) {
6969
contents->SetColorSourceSize(intrinsic_size->GetSize());
7070
}
7171
return contents;
@@ -95,7 +95,7 @@ std::shared_ptr<ColorSourceContents> Paint::CreateContents() const {
9595
auto radius_pt = Point(radius, radius);
9696
std::array<Point, 2> bounds{center + radius_pt, center - radius_pt};
9797
auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
98-
if (intrinsic_size.has_value()) {
98+
if (intrinsic_size.has_value() && !intrinsic_size->IsEmpty()) {
9999
contents->SetColorSourceSize(intrinsic_size->GetSize());
100100
}
101101
return contents;
@@ -129,7 +129,7 @@ std::shared_ptr<ColorSourceContents> Paint::CreateContents() const {
129129
auto radius_pt = Point(radius, radius);
130130
std::array<Point, 2> bounds{center + radius_pt, center - radius_pt};
131131
auto intrinsic_size = Rect::MakePointBounds(bounds.begin(), bounds.end());
132-
if (intrinsic_size.has_value()) {
132+
if (intrinsic_size.has_value() && !intrinsic_size->IsEmpty()) {
133133
contents->SetColorSourceSize(intrinsic_size->GetSize());
134134
}
135135
return contents;

0 commit comments

Comments
 (0)