Skip to content

Commit b5f4dc4

Browse files
authored
Don't call skia_image() for raster-owned images in DlImage::Equals (flutter#196473)
Fixes flutter#195888 `DlImage::Equals` compared two Skia images by comparing their `skia_image()` results. Calling `skia_image()` on a `DlDeferredImageGPUSkia` `DlImage` is not allowed on the UI thread: https://github.com/flutter/flutter/blob/3b04b3b21d614748b0b1c18b22795b69dea4ddc5/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.h#L46-L51 `DlImage::Equals` could get called on the UI thread with a `DlDeferredImageGPUSkia`. On Skia, `Picture.toImageSync` and `Scene.toImageSync` return `DlDeferredImageGPUSkia` images. If this is used as a `Paint`'s `ColorSource`, `DisplayListBuilder`'s [`setColorSource`](https://github.com/flutter/flutter/blob/3b04b3b21d614748b0b1c18b22795b69dea4ddc5/engine/src/flutter/display_list/dl_builder.h#L332) calls `NotEquals` on it, which calls its `DlImage::Equals` method. This ends up causing crashes: flutter#195888. ### Fix Exits early without calling `skia_image()` for raster-owned Skia images, which is exactly `DlDeferredImageGPUSkia`. Exiting early with `false` is fine because In `DlImage::Equals`, the `skia_image()` equality comparison would always be false for this case: `skia_image()` for `DlDeferredImageGPUSkia` generates a new `SkImage` on each call, which will always compare false to other calls. So for `DlDeferredImageGPUSkia`, calling`skia_image()` is not needed and `DlImage::Equals` can rely on the existing pointer comparison to check for equality, just like what `DlImage::Equals` does for [Impeller images](https://github.com/flutter/flutter/blob/3b04b3b21d614748b0b1c18b22795b69dea4ddc5/engine/src/flutter/display_list/image/dl_image.cc#L35-L44). I manually tested with the code sample from the issue. I could reliably repro the crash without this fix, and the crash does not occur with this fix. ## 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 in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [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/CONTRIBUTING.ai.md [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 [flutter/website]: https://github.com/flutter/website [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 9d23f8b commit b5f4dc4

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ if (enable_unittests) {
185185
"geometry/dl_path_unittests.cc",
186186
"geometry/dl_region_unittests.cc",
187187
"geometry/dl_rtree_unittests.cc",
188+
"image/dl_image_unittests.cc",
188189
"skia/dl_sk_canvas_unittests.cc",
189190
"skia/dl_sk_conversions_unittests.cc",
190191
"skia/dl_sk_paint_dispatcher_unittests.cc",

‎engine/src/flutter/display_list/image/dl_image.cc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ bool DlImage::Equals(const DlImage* other) const {
4242
// Impeller images have pointer equality (handled by this == other)
4343
return false;
4444
}
45+
if (owning_context() == OwningContext::kRaster ||
46+
other->owning_context() == OwningContext::kRaster) {
47+
// Skia raster-owned images also have pointer equality (handled by
48+
// this == other).
49+
return false;
50+
}
51+
// For Skia non-raster-owned images, check whether they wrap the same
52+
// `SkImage`. Note that `skia_image()` must never be called on the UI thread
53+
// for a raster-owned image. `Equals` may be called from the UI thread, so
54+
// the `owning_context()` check above protects against this case.
4555
return skia_this->skia_image() == skia_other->skia_image();
4656
}
4757

‎engine/src/flutter/display_list/image/dl_image.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ class DlImage : public SkRefCnt {
140140
/// image.
141141
virtual std::optional<std::string> get_error() const;
142142

143+
//----------------------------------------------------------------------------
144+
/// @brief Equality check for `DlImage` objects.
145+
///
146+
/// This is an identity comparison, not a comparison of pixels.
147+
/// Two images with identical contents may compare unequal.
148+
///
149+
/// - An image is always equal to itself.
150+
/// - Impeller images and Skia images owned by the raster context
151+
/// (i.e. DlDeferredImageGPUSkia) are only equal to themselves.
152+
/// - Skia images that are not owned by the raster context are
153+
/// equal if they wrap the same SkImage.
143154
bool Equals(const DlImage* other) const;
144155

145156
bool Equals(const DlImage& other) const { return Equals(&other); }
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/display_list/dl_builder.h"
6+
#include "flutter/display_list/dl_paint.h"
7+
#include "flutter/display_list/effects/dl_color_source.h"
8+
#include "flutter/display_list/geometry/dl_path.h"
9+
#include "flutter/display_list/image/dl_image.h"
10+
#include "flutter/display_list/image/dl_image_skia.h"
11+
#include "flutter/display_list/testing/dl_test_snippets.h"
12+
13+
#include "gmock/gmock.h"
14+
#include "gtest/gtest.h"
15+
16+
namespace flutter {
17+
namespace testing {
18+
19+
namespace {
20+
21+
using ::testing::NiceMock;
22+
using ::testing::Return;
23+
24+
class MockDlImageSkia : public DlImageSkia {
25+
public:
26+
MockDlImageSkia() : DlImageSkia(nullptr) {}
27+
28+
MOCK_METHOD(sk_sp<SkImage>, skia_image, (), (const, override));
29+
MOCK_METHOD(DlImage::OwningContext, owning_context, (), (const, override));
30+
};
31+
32+
sk_sp<NiceMock<MockDlImageSkia>> MakeRasterOwnedImage() {
33+
auto image = sk_make_sp<NiceMock<MockDlImageSkia>>();
34+
ON_CALL(*image, owning_context())
35+
.WillByDefault(Return(DlImage::OwningContext::kRaster));
36+
return image;
37+
}
38+
39+
} // namespace
40+
41+
TEST(DisplayListImage, EqualsComparesTheSkiaImagesOfImagesNotOwnedByRaster) {
42+
sk_sp<DlImage> image = MakeTestImage(10, 10, DlColor::kGreen());
43+
sk_sp<DlImage> same_skia_image =
44+
DlImageSkia::Make(image->asSkiaImage()->skia_image());
45+
sk_sp<DlImage> other_image = MakeTestImage(10, 10, DlColor::kGreen());
46+
47+
ASSERT_NE(image.get(), same_skia_image.get());
48+
EXPECT_TRUE(image->Equals(image));
49+
EXPECT_TRUE(image->Equals(same_skia_image));
50+
EXPECT_TRUE(same_skia_image->Equals(image));
51+
EXPECT_FALSE(image->Equals(other_image));
52+
EXPECT_FALSE(image->Equals(nullptr));
53+
}
54+
55+
// Regression test for https://github.com/flutter/flutter/issues/195888.
56+
TEST(DisplayListImage, EqualsDoesNotAccessTheSkiaImageOfRasterOwnedImages) {
57+
auto image1 = MakeRasterOwnedImage();
58+
auto image2 = MakeRasterOwnedImage();
59+
sk_sp<DlImage> plain_image = MakeTestImage(10, 10, DlColor::kGreen());
60+
EXPECT_CALL(*image1, skia_image()).Times(0);
61+
EXPECT_CALL(*image2, skia_image()).Times(0);
62+
63+
// Raster owned images have pointer equality.
64+
EXPECT_TRUE(image1->Equals(image1));
65+
EXPECT_FALSE(image1->Equals(image2));
66+
EXPECT_FALSE(image2->Equals(image1));
67+
68+
// The raster owned image may be on either side of the comparison.
69+
EXPECT_FALSE(image1->Equals(plain_image));
70+
EXPECT_FALSE(plain_image->Equals(image1));
71+
}
72+
73+
// Regression test for https://github.com/flutter/flutter/issues/195888.
74+
//
75+
// The builder compares the color source of each draw against the previous
76+
// one, on the UI thread. For image color sources that compares the images.
77+
// This test ensures that the builder does not access the SkImage of a raster
78+
// owned image.
79+
TEST(DisplayListImage, BuilderDoesNotAccessTheSkiaImageOfRasterOwnedImages) {
80+
auto image1 = MakeRasterOwnedImage();
81+
auto image2 = MakeRasterOwnedImage();
82+
EXPECT_CALL(*image1, skia_image()).Times(0);
83+
EXPECT_CALL(*image2, skia_image()).Times(0);
84+
DlPaint paint1;
85+
paint1.setColorSource(DlColorSource::MakeImage(image1, DlTileMode::kRepeat,
86+
DlTileMode::kRepeat));
87+
DlPaint paint2;
88+
paint2.setColorSource(DlColorSource::MakeImage(image2, DlTileMode::kRepeat,
89+
DlTileMode::kRepeat));
90+
DlPath path = DlPath::MakeOval(DlRect::MakeLTRB(0, 0, 90, 60));
91+
92+
DisplayListBuilder builder;
93+
builder.DrawPath(path, paint1);
94+
builder.DrawPath(path, paint2);
95+
builder.DrawPath(path, paint1);
96+
sk_sp<DisplayList> display_list = builder.Build();
97+
98+
EXPECT_NE(display_list, nullptr);
99+
}
100+
101+
} // namespace testing
102+
} // namespace flutter

0 commit comments

Comments
 (0)