From d4a56c721b372fc8accb30ee5dea1f70a45c79f1 Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Mon, 17 Apr 2023 16:47:20 -0700 Subject: [PATCH] focus SkiaGPUObject wrappers on only those objects that need the protection (flutter/engine#41237) With this PR we no longer need to hold DisplayLists in GPUObject wrappers and they can be disposed instantly instead of queueing on the Unref thread. This will definitely be a win for Impeller as none of the objects used in a frame now require queueing, but the performance impact on apps running on top of skia is less clear if they depend on a lot of images inside their DisplayLists that still need to be queued to be freed. After getting further in the work, it looks like only decoded images need to use the protected DlImage wrappers and most of those should survive many frames before they are disposed. That should hopefully leave very few unrefs happening per frame. ~There are 3 unit tests in `shell_unittests.cc` and `embedder_metal_unittests.mm` that are now GSKIP'd as they now invoke code that needs a fully initialized UIDartState in order to protect their images. I will look into fixing the tests and/or making the code they invoke provide protection without relying on UIDartState.~ (This looks to be fixed in the latest commit by simply not creating DlImageGPUs all over the source base and simply catching only those that end up in UI data structures. There is actually existing code in one of the modules that feeds ui.Image with an answer to wrap the image in a DlImageGPU if it has a skia image anyway, so most of these additional uses of DlImageGPU that were having trouble getting the Skia unref queue just didn't need it anyway.) --- .../src/flutter/display_list/display_list.cc | 5 +- .../src/flutter/display_list/display_list.h | 3 + engine/src/flutter/display_list/dl_builder.cc | 13 +++- engine/src/flutter/display_list/dl_builder.h | 2 + .../display_list/effects/dl_color_source.h | 35 +++++++++ .../src/flutter/display_list/image/dl_image.h | 10 +++ .../display_list/image/dl_image_skia.cc | 11 +++ .../display_list/image/dl_image_skia.h | 3 + engine/src/flutter/flow/BUILD.gn | 2 - .../flutter/flow/layers/display_list_layer.cc | 20 +++-- .../flutter/flow/layers/display_list_layer.h | 9 +-- .../layers/display_list_layer_unittests.cc | 50 ++++++------ .../layers/display_list_raster_cache_item.cc | 2 +- .../flutter/flow/raster_cache_unittests.cc | 7 +- .../flutter/flow/testing/diff_context_test.cc | 11 +-- .../flutter/flow/testing/diff_context_test.h | 11 +-- .../src/flutter/flow/testing/mock_texture.cc | 2 +- .../testing/skia_gpu_object_layer_test.cc | 18 ----- .../flow/testing/skia_gpu_object_layer_test.h | 30 -------- .../display_list_image_impeller.cc | 6 ++ .../display_list_image_impeller.h | 3 + .../lib/ui/compositing/scene_builder.cc | 5 +- ...isplay_list_deferred_image_gpu_impeller.cc | 5 ++ ...display_list_deferred_image_gpu_impeller.h | 3 + .../display_list_deferred_image_gpu_skia.cc | 5 ++ .../display_list_deferred_image_gpu_skia.h | 4 +- .../lib/ui/painting/display_list_image_gpu.cc | 5 ++ .../lib/ui/painting/display_list_image_gpu.h | 3 + .../lib/ui/painting/fragment_shader.cc | 15 +++- .../src/flutter/lib/ui/painting/gradient.cc | 8 ++ engine/src/flutter/lib/ui/painting/image.h | 7 +- .../ui/painting/image_encoding_unittests.cc | 1 + .../flutter/lib/ui/painting/image_shader.cc | 23 ++---- .../flutter/lib/ui/painting/image_shader.h | 2 +- .../lib/ui/painting/multi_frame_codec.cc | 1 + engine/src/flutter/lib/ui/painting/picture.cc | 27 +++---- engine/src/flutter/lib/ui/painting/picture.h | 14 ++-- .../lib/ui/painting/picture_recorder.cc | 3 +- .../lib/ui/painting/scene/scene_shader.cc | 11 ++- engine/src/flutter/lib/ui/snapshot_delegate.h | 1 - engine/src/flutter/lib/ui/ui_dart_state.h | 12 --- .../flutter/shell/common/shell_unittests.cc | 77 +++---------------- .../shell/common/snapshot_controller.h | 3 + .../shell/common/snapshot_controller_skia.cc | 2 + .../FlutterDarwinExternalTextureMetal.mm | 3 + .../embedder/embedder_external_texture_gl.cc | 1 + .../embedder_external_texture_metal.mm | 1 + 47 files changed, 244 insertions(+), 251 deletions(-) delete mode 100644 engine/src/flutter/flow/testing/skia_gpu_object_layer_test.cc delete mode 100644 engine/src/flutter/flow/testing/skia_gpu_object_layer_test.h diff --git a/engine/src/flutter/display_list/display_list.cc b/engine/src/flutter/display_list/display_list.cc index 146eef1296..2425f699a1 100644 --- a/engine/src/flutter/display_list/display_list.cc +++ b/engine/src/flutter/display_list/display_list.cc @@ -21,7 +21,8 @@ DisplayList::DisplayList() nested_op_count_(0), unique_id_(0), bounds_({0, 0, 0, 0}), - can_apply_group_opacity_(true) {} + can_apply_group_opacity_(true), + is_ui_thread_safe_(true) {} DisplayList::DisplayList(DisplayListStorage&& storage, size_t byte_count, @@ -30,6 +31,7 @@ DisplayList::DisplayList(DisplayListStorage&& storage, unsigned int nested_op_count, const SkRect& bounds, bool can_apply_group_opacity, + bool is_ui_thread_safe, sk_sp rtree) : storage_(std::move(storage)), byte_count_(byte_count), @@ -39,6 +41,7 @@ DisplayList::DisplayList(DisplayListStorage&& storage, unique_id_(next_unique_id()), bounds_(bounds), can_apply_group_opacity_(can_apply_group_opacity), + is_ui_thread_safe_(is_ui_thread_safe), rtree_(std::move(rtree)) {} DisplayList::~DisplayList() { diff --git a/engine/src/flutter/display_list/display_list.h b/engine/src/flutter/display_list/display_list.h index 42a7de0ee9..cbc9eb7925 100644 --- a/engine/src/flutter/display_list/display_list.h +++ b/engine/src/flutter/display_list/display_list.h @@ -262,6 +262,7 @@ class DisplayList : public SkRefCnt { } bool can_apply_group_opacity() const { return can_apply_group_opacity_; } + bool isUIThreadSafe() const { return is_ui_thread_safe_; } static void DisposeOps(uint8_t* ptr, uint8_t* end); @@ -273,6 +274,7 @@ class DisplayList : public SkRefCnt { unsigned int nested_op_count, const SkRect& bounds, bool can_apply_group_opacity, + bool is_ui_thread_safe, sk_sp rtree); static uint32_t next_unique_id(); @@ -288,6 +290,7 @@ class DisplayList : public SkRefCnt { const SkRect bounds_; const bool can_apply_group_opacity_; + const bool is_ui_thread_safe_; const sk_sp rtree_; void Dispatch(DlOpReceiver& ctx, diff --git a/engine/src/flutter/display_list/dl_builder.cc b/engine/src/flutter/display_list/dl_builder.cc index 68f790c5c9..d48659426a 100644 --- a/engine/src/flutter/display_list/dl_builder.cc +++ b/engine/src/flutter/display_list/dl_builder.cc @@ -73,9 +73,10 @@ sk_sp DisplayListBuilder::Build() { nested_bytes_ = nested_op_count_ = 0; storage_.realloc(bytes); bool compatible = layer_stack_.back().is_group_opacity_compatible(); - return sk_sp(new DisplayList(std::move(storage_), bytes, count, - nested_bytes, nested_count, - bounds(), compatible, rtree())); + bool is_safe = is_ui_thread_safe_; + return sk_sp( + new DisplayList(std::move(storage_), bytes, count, nested_bytes, + nested_count, bounds(), compatible, is_safe, rtree())); } DisplayListBuilder::DisplayListBuilder(const SkRect& cull_rect, @@ -156,6 +157,7 @@ void DisplayListBuilder::onSetColorSource(const DlColorSource* source) { Push(0, 0); } else { current_.setColorSource(source->shared()); + is_ui_thread_safe_ = is_ui_thread_safe_ && source->isUIThreadSafe(); switch (source->type()) { case DlColorSourceType::kColor: { const DlColorColorSource* color_source = source->asColor(); @@ -923,6 +925,7 @@ void DisplayListBuilder::drawImage(const sk_sp image, ? Push(0, 1, image, point, sampling) : Push(0, 1, image, point, sampling); CheckLayerOpacityCompatibility(render_with_attributes); + is_ui_thread_safe_ = is_ui_thread_safe_ && image->isUIThreadSafe(); SkRect bounds = SkRect::MakeXYWH(point.fX, point.fY, // image->width(), image->height()); DisplayListAttributeFlags flags = render_with_attributes // @@ -951,6 +954,7 @@ void DisplayListBuilder::drawImageRect(const sk_sp image, Push(0, 1, image, src, dst, sampling, render_with_attributes, constraint); CheckLayerOpacityCompatibility(render_with_attributes); + is_ui_thread_safe_ = is_ui_thread_safe_ && image->isUIThreadSafe(); DisplayListAttributeFlags flags = render_with_attributes ? kDrawImageRectWithPaintFlags : kDrawImageRectFlags; @@ -979,6 +983,7 @@ void DisplayListBuilder::drawImageNine(const sk_sp image, ? Push(0, 1, image, center, dst, filter) : Push(0, 1, image, center, dst, filter); CheckLayerOpacityCompatibility(render_with_attributes); + is_ui_thread_safe_ = is_ui_thread_safe_ && image->isUIThreadSafe(); DisplayListAttributeFlags flags = render_with_attributes ? kDrawImageNineWithPaintFlags : kDrawImageNineFlags; @@ -1034,6 +1039,7 @@ void DisplayListBuilder::drawAtlas(const sk_sp atlas, // on it to distribute the opacity without overlap without checking all // of the transforms and texture rectangles. UpdateLayerOpacityCompatibility(false); + is_ui_thread_safe_ = is_ui_thread_safe_ && atlas->isUIThreadSafe(); SkPoint quad[4]; RectBoundsAccumulator atlasBounds; @@ -1075,6 +1081,7 @@ void DisplayListBuilder::DrawDisplayList(const sk_sp display_list, SkScalar opacity) { DlPaint current_paint = current_; Push(0, 1, display_list, opacity); + is_ui_thread_safe_ = is_ui_thread_safe_ && display_list->isUIThreadSafe(); // Not really necessary if the developer is interacting with us via // our attribute-state-less DlCanvas methods, but this avoids surprises // for those who may have been using the stateful Dispatcher methods. diff --git a/engine/src/flutter/display_list/dl_builder.h b/engine/src/flutter/display_list/dl_builder.h index 4917783e26..e35e160e70 100644 --- a/engine/src/flutter/display_list/dl_builder.h +++ b/engine/src/flutter/display_list/dl_builder.h @@ -480,6 +480,8 @@ class DisplayListBuilder final : public virtual DlCanvas, size_t nested_bytes_ = 0; int nested_op_count_ = 0; + bool is_ui_thread_safe_ = true; + template void* Push(size_t extra, int op_inc, Args&&... args); diff --git a/engine/src/flutter/display_list/effects/dl_color_source.h b/engine/src/flutter/display_list/effects/dl_color_source.h index b34e8eed87..394fe092af 100644 --- a/engine/src/flutter/display_list/effects/dl_color_source.h +++ b/engine/src/flutter/display_list/effects/dl_color_source.h @@ -109,6 +109,16 @@ class DlColorSource : public DlAttribute { virtual bool is_opaque() const = 0; + //---------------------------------------------------------------------------- + /// @brief If the underlying platform data held by this object is + /// held in a way that it can be stored and potentially + /// released from the UI thread, this method returns true. + /// + /// @return True if the class has no GPU related resources or if any + /// that it holds are held in a thread-safe manner. + /// + virtual bool isUIThreadSafe() const = 0; + // Return a DlColorColorSource pointer to this object iff it is an Color // type of ColorSource, otherwise return nullptr. virtual const DlColorColorSource* asColor() const { return nullptr; } @@ -160,6 +170,8 @@ class DlColorColorSource final : public DlColorSource { public: DlColorColorSource(DlColor color) : color_(color) {} + bool isUIThreadSafe() const override { return true; } + std::shared_ptr shared() const override { return std::make_shared(color_); } @@ -215,6 +227,10 @@ class DlImageColorSource final : public SkRefCnt, vertical_tile_mode_(vertical_tile_mode), sampling_(sampling) {} + bool isUIThreadSafe() const override { + return image_ ? image_->isUIThreadSafe() : true; + } + const DlImageColorSource* asImage() const override { return this; } std::shared_ptr shared() const override { @@ -338,6 +354,8 @@ class DlLinearGradientColorSource final : public DlGradientColorSourceBase { return this; } + bool isUIThreadSafe() const override { return true; } + DlColorSourceType type() const override { return DlColorSourceType::kLinearGradient; } @@ -399,6 +417,8 @@ class DlRadialGradientColorSource final : public DlGradientColorSourceBase { return this; } + bool isUIThreadSafe() const override { return true; } + std::shared_ptr shared() const override { return MakeRadial(center_, radius_, stop_count(), colors(), stops(), tile_mode(), matrix_ptr()); @@ -460,6 +480,8 @@ class DlConicalGradientColorSource final : public DlGradientColorSourceBase { return this; } + bool isUIThreadSafe() const override { return true; } + std::shared_ptr shared() const override { return MakeConical(start_center_, start_radius_, end_center_, end_radius_, stop_count(), colors(), stops(), tile_mode(), @@ -534,6 +556,8 @@ class DlSweepGradientColorSource final : public DlGradientColorSourceBase { return this; } + bool isUIThreadSafe() const override { return true; } + std::shared_ptr shared() const override { return MakeSweep(center_, start_, end_, stop_count(), colors(), stops(), tile_mode(), matrix_ptr()); @@ -604,6 +628,15 @@ class DlRuntimeEffectColorSource final : public DlColorSource { samplers_(std::move(samplers)), uniform_data_(std::move(uniform_data)) {} + bool isUIThreadSafe() const override { + for (auto sampler : samplers_) { + if (!sampler->isUIThreadSafe()) { + return false; + } + } + return true; + } + const DlRuntimeEffectColorSource* asRuntimeEffect() const override { return this; } @@ -666,6 +699,8 @@ class DlSceneColorSource final : public DlColorSource { impeller::Matrix camera_matrix) : node_(std::move(node)), camera_matrix_(camera_matrix) {} + bool isUIThreadSafe() const override { return true; } + const DlSceneColorSource* asScene() const override { return this; } std::shared_ptr shared() const override { diff --git a/engine/src/flutter/display_list/image/dl_image.h b/engine/src/flutter/display_list/image/dl_image.h index 19ae1d935b..0b78b55a4c 100644 --- a/engine/src/flutter/display_list/image/dl_image.h +++ b/engine/src/flutter/display_list/image/dl_image.h @@ -67,6 +67,16 @@ class DlImage : public SkRefCnt { virtual bool isTextureBacked() const = 0; + //---------------------------------------------------------------------------- + /// @brief If the underlying platform image held by this object has no + /// threading requirements for the release of that image (or if + /// arrangements have already been made to forward that image to + /// the correct thread upon deletion), this method returns true. + /// + /// @return True if the underlying image is held in a thread-safe manner. + /// + virtual bool isUIThreadSafe() const = 0; + //---------------------------------------------------------------------------- /// @return The dimensions of the pixel grid. /// diff --git a/engine/src/flutter/display_list/image/dl_image_skia.cc b/engine/src/flutter/display_list/image/dl_image_skia.cc index da6398ebd3..8c896e5c6f 100644 --- a/engine/src/flutter/display_list/image/dl_image_skia.cc +++ b/engine/src/flutter/display_list/image/dl_image_skia.cc @@ -31,6 +31,17 @@ bool DlImageSkia::isTextureBacked() const { return image_ ? image_->isTextureBacked() : false; } +// |DlImage| +bool DlImageSkia::isUIThreadSafe() const { + // Technically if the image is null then we are thread-safe, and possibly + // if the image is constructed from a heap raster as well, but there + // should never be a leak of an instance of this class into any data that + // is shared with the UI thread, regardless of value. + // All images intended to be shared with the UI thread should be constructed + // via one of the DlImage subclasses designed for that purpose. + return false; +} + // |DlImage| SkISize DlImageSkia::dimensions() const { return image_ ? image_->dimensions() : SkISize::MakeEmpty(); diff --git a/engine/src/flutter/display_list/image/dl_image_skia.h b/engine/src/flutter/display_list/image/dl_image_skia.h index c277cb2d04..a29985d095 100644 --- a/engine/src/flutter/display_list/image/dl_image_skia.h +++ b/engine/src/flutter/display_list/image/dl_image_skia.h @@ -29,6 +29,9 @@ class DlImageSkia final : public DlImage { // |DlImage| bool isTextureBacked() const override; + // |DlImage| + bool isUIThreadSafe() const override; + // |DlImage| SkISize dimensions() const override; diff --git a/engine/src/flutter/flow/BUILD.gn b/engine/src/flutter/flow/BUILD.gn index 1adceefde4..f996f0707e 100644 --- a/engine/src/flutter/flow/BUILD.gn +++ b/engine/src/flutter/flow/BUILD.gn @@ -123,8 +123,6 @@ if (enable_unittests) { "testing/mock_raster_cache.h", "testing/mock_texture.cc", "testing/mock_texture.h", - "testing/skia_gpu_object_layer_test.cc", - "testing/skia_gpu_object_layer_test.h", ] public_deps = [ diff --git a/engine/src/flutter/flow/layers/display_list_layer.cc b/engine/src/flutter/flow/layers/display_list_layer.cc index 7a36358cc2..543af526be 100644 --- a/engine/src/flutter/flow/layers/display_list_layer.cc +++ b/engine/src/flutter/flow/layers/display_list_layer.cc @@ -16,15 +16,14 @@ namespace flutter { DisplayListLayer::DisplayListLayer(const SkPoint& offset, - SkiaGPUObject display_list, + sk_sp display_list, bool is_complex, bool will_change) : offset_(offset), display_list_(std::move(display_list)) { - if (display_list_.skia_object() != nullptr) { - bounds_ = display_list_.skia_object()->bounds().makeOffset(offset_.x(), - offset_.y()); + if (display_list_) { + bounds_ = display_list_->bounds().makeOffset(offset_.x(), offset_.y()); display_list_raster_cache_item_ = DisplayListRasterCacheItem::Make( - display_list_.skia_object(), offset_, is_complex, will_change); + display_list_, offset_, is_complex, will_change); } } @@ -61,8 +60,8 @@ void DisplayListLayer::Diff(DiffContext* context, const Layer* old_layer) { bool DisplayListLayer::Compare(DiffContext::Statistics& statistics, const DisplayListLayer* l1, const DisplayListLayer* l2) { - const auto& dl1 = l1->display_list_.skia_object(); - const auto& dl2 = l2->display_list_.skia_object(); + const auto& dl1 = l1->display_list_; + const auto& dl2 = l2->display_list_; if (dl1.get() == dl2.get()) { statistics.AddSameInstancePicture(); return true; @@ -105,7 +104,7 @@ void DisplayListLayer::Preroll(PrerollContext* context) { } void DisplayListLayer::Paint(PaintContext& context) const { - FML_DCHECK(display_list_.skia_object()); + FML_DCHECK(display_list_); FML_DCHECK(needs_painting(context)); auto mutator = context.state_stack.save(); @@ -143,7 +142,7 @@ void DisplayListLayer::Paint(PaintContext& context) const { DlAutoCanvasRestore save(canvas, true); canvas->Clear(DlColor::kTransparent()); canvas->SetTransform(ctm); - canvas->DrawDisplayList(display_list_.skia_object(), opacity); + canvas->DrawDisplayList(display_list_, opacity); } canvas->Flush(); } @@ -158,8 +157,7 @@ void DisplayListLayer::Paint(PaintContext& context) const { context.layer_snapshot_store->Add(snapshot_data); } - auto display_list = display_list_.skia_object(); - context.canvas->DrawDisplayList(display_list, opacity); + context.canvas->DrawDisplayList(display_list_, opacity); } } // namespace flutter diff --git a/engine/src/flutter/flow/layers/display_list_layer.h b/engine/src/flutter/flow/layers/display_list_layer.h index fd981e167a..d7efe4e8ee 100644 --- a/engine/src/flutter/flow/layers/display_list_layer.h +++ b/engine/src/flutter/flow/layers/display_list_layer.h @@ -11,7 +11,6 @@ #include "flutter/flow/layers/display_list_raster_cache_item.h" #include "flutter/flow/layers/layer.h" #include "flutter/flow/raster_cache_item.h" -#include "flutter/flow/skia_gpu_object.h" namespace flutter { @@ -20,13 +19,11 @@ class DisplayListLayer : public Layer { static constexpr size_t kMaxBytesToCompare = 10000; DisplayListLayer(const SkPoint& offset, - SkiaGPUObject display_list, + sk_sp display_list, bool is_complex, bool will_change); - DisplayList* display_list() const { - return display_list_.skia_object().get(); - } + DisplayList* display_list() const { return display_list_.get(); } bool IsReplacing(DiffContext* context, const Layer* layer) const override; @@ -55,7 +52,7 @@ class DisplayListLayer : public Layer { SkPoint offset_; SkRect bounds_; - flutter::SkiaGPUObject display_list_; + sk_sp display_list_; static bool Compare(DiffContext::Statistics& statistics, const DisplayListLayer* l1, diff --git a/engine/src/flutter/flow/layers/display_list_layer_unittests.cc b/engine/src/flutter/flow/layers/display_list_layer_unittests.cc index 1f0ef185a7..eba4be82fb 100644 --- a/engine/src/flutter/flow/layers/display_list_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/display_list_layer_unittests.cc @@ -9,23 +9,21 @@ #include "flutter/display_list/dl_builder.h" #include "flutter/flow/layers/layer_tree.h" #include "flutter/flow/testing/diff_context_test.h" -#include "flutter/flow/testing/skia_gpu_object_layer_test.h" #include "flutter/fml/macros.h" #include "flutter/testing/mock_canvas.h" namespace flutter { namespace testing { -using DisplayListLayerTest = SkiaGPUObjectLayerTest; +using DisplayListLayerTest = LayerTest; #ifndef NDEBUG TEST_F(DisplayListLayerTest, PaintBeforePrerollInvalidDisplayListDies) { const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f); auto layer = std::make_shared( - layer_offset, SkiaGPUObject(), false, false); + layer_offset, sk_sp(), false, false); - EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()), - "display_list_\\.skia_object\\(\\)"); + EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()), "display_list_"); } TEST_F(DisplayListLayerTest, PaintBeforePrerollDies) { @@ -34,9 +32,8 @@ TEST_F(DisplayListLayerTest, PaintBeforePrerollDies) { DisplayListBuilder builder; builder.DrawRect(picture_bounds, DlPaint()); auto display_list = builder.Build(); - auto layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), - false, false); + auto layer = std::make_shared(layer_offset, display_list, + false, false); EXPECT_EQ(layer->paint_bounds(), SkRect::MakeEmpty()); EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()), @@ -49,9 +46,8 @@ TEST_F(DisplayListLayerTest, PaintingEmptyLayerDies) { DisplayListBuilder builder; builder.DrawRect(picture_bounds, DlPaint()); auto display_list = builder.Build(); - auto layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), - false, false); + auto layer = std::make_shared(layer_offset, display_list, + false, false); layer->Preroll(preroll_context()); EXPECT_EQ(layer->paint_bounds(), SkRect::MakeEmpty()); @@ -64,7 +60,7 @@ TEST_F(DisplayListLayerTest, PaintingEmptyLayerDies) { TEST_F(DisplayListLayerTest, InvalidDisplayListDies) { const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f); auto layer = std::make_shared( - layer_offset, SkiaGPUObject(), false, false); + layer_offset, sk_sp(), false, false); // Crashes reading a nullptr. EXPECT_DEATH_IF_SUPPORTED(layer->Preroll(preroll_context()), ""); @@ -79,8 +75,8 @@ TEST_F(DisplayListLayerTest, SimpleDisplayList) { DisplayListBuilder builder; builder.DrawRect(picture_bounds, DlPaint()); auto display_list = builder.Build(); - auto layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + auto layer = std::make_shared(layer_offset, display_list, + false, false); layer->Preroll(preroll_context()); EXPECT_EQ(layer->paint_bounds(), @@ -104,8 +100,8 @@ TEST_F(DisplayListLayerTest, CachingDoesNotChangeCullRect) { DisplayListBuilder builder; builder.DrawRect({10, 10, 20, 20}, DlPaint()); auto display_list = builder.Build(); - auto layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), true, false); + auto layer = std::make_shared(layer_offset, display_list, + true, false); SkRect original_cull_rect = preroll_context()->state_stack.device_cull_rect(); use_mock_raster_cache(); @@ -121,7 +117,7 @@ TEST_F(DisplayListLayerTest, SimpleDisplayListOpacityInheritance) { builder.DrawRect(picture_bounds, DlPaint()); auto display_list = builder.Build(); auto display_list_layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + layer_offset, display_list, false, false); EXPECT_TRUE(display_list->can_apply_group_opacity()); auto context = preroll_context(); @@ -173,7 +169,7 @@ TEST_F(DisplayListLayerTest, IncompatibleDisplayListOpacityInheritance) { builder.DrawRect(picture2_bounds, DlPaint()); auto display_list = builder.Build(); auto display_list_layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + layer_offset, display_list, false, false); EXPECT_FALSE(display_list->can_apply_group_opacity()); auto context = preroll_context(); @@ -233,7 +229,7 @@ TEST_F(DisplayListLayerTest, CachedIncompatibleDisplayListOpacityInheritance) { builder.DrawRect(picture2_bounds, DlPaint()); auto display_list = builder.Build(); auto display_list_layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), true, false); + layer_offset, display_list, true, false); EXPECT_FALSE(display_list->can_apply_group_opacity()); use_skia_raster_cache(); @@ -383,8 +379,8 @@ TEST_F(DisplayListLayerTest, LayerTreeSnapshotsWhenEnabled) { DisplayListBuilder builder; builder.DrawRect(picture_bounds, DlPaint()); auto display_list = builder.Build(); - auto layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + auto layer = std::make_shared(layer_offset, display_list, + false, false); layer->Preroll(preroll_context()); @@ -402,8 +398,8 @@ TEST_F(DisplayListLayerTest, NoLayerTreeSnapshotsWhenDisabledByDefault) { DisplayListBuilder builder; builder.DrawRect(picture_bounds, DlPaint()); auto display_list = builder.Build(); - auto layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + auto layer = std::make_shared(layer_offset, display_list, + false, false); layer->Preroll(preroll_context()); layer->Paint(paint_context()); @@ -420,8 +416,8 @@ TEST_F(DisplayListLayerTest, DisplayListAccessCountDependsOnVisibility) { DisplayListBuilder builder; builder.DrawRect(picture_bounds, DlPaint()); auto display_list = builder.Build(); - auto layer = std::make_shared( - layer_offset, SkiaGPUObject(display_list, unref_queue()), true, false); + auto layer = std::make_shared(layer_offset, display_list, + true, false); auto raster_cache_item = layer->raster_cache_item(); use_mock_raster_cache(); @@ -525,8 +521,8 @@ TEST_F(DisplayListLayerTest, OverflowCachedDisplayListOpacityInheritance) { ASSERT_FALSE(display_list->can_apply_group_opacity()); SkPoint offset = {i * 200.0f, 0}; - layers[i] = std::make_shared( - offset, SkiaGPUObject(display_list, unref_queue()), true, false); + layers[i] = + std::make_shared(offset, display_list, true, false); opacity_layer->Add(layers[i]); } for (size_t j = 0; j < context->raster_cache->access_threshold(); j++) { diff --git a/engine/src/flutter/flow/layers/display_list_raster_cache_item.cc b/engine/src/flutter/flow/layers/display_list_raster_cache_item.cc index 7c6e95af2e..b295c85ea2 100644 --- a/engine/src/flutter/flow/layers/display_list_raster_cache_item.cc +++ b/engine/src/flutter/flow/layers/display_list_raster_cache_item.cc @@ -14,7 +14,7 @@ #include "flutter/flow/raster_cache_item.h" #include "flutter/flow/raster_cache_key.h" #include "flutter/flow/raster_cache_util.h" -#include "flutter/flow/skia_gpu_object.h" +#include "third_party/skia/include/gpu/GrDirectContext.h" namespace flutter { diff --git a/engine/src/flutter/flow/raster_cache_unittests.cc b/engine/src/flutter/flow/raster_cache_unittests.cc index 1484c58f68..00b99d05fd 100644 --- a/engine/src/flutter/flow/raster_cache_unittests.cc +++ b/engine/src/flutter/flow/raster_cache_unittests.cc @@ -13,8 +13,8 @@ #include "flutter/flow/layers/transform_layer.h" #include "flutter/flow/raster_cache.h" #include "flutter/flow/raster_cache_item.h" +#include "flutter/flow/testing/layer_test.h" #include "flutter/flow/testing/mock_raster_cache.h" -#include "flutter/flow/testing/skia_gpu_object_layer_test.h" #include "flutter/testing/assertions_skia.h" #include "gtest/gtest.h" #include "include/core/SkMatrix.h" @@ -858,7 +858,7 @@ TEST(RasterCache, RasterCacheKeyIDHashCode) { ASSERT_EQ(fourth_hash, fourth.GetHash()); } -using RasterCacheTest = SkiaGPUObjectLayerTest; +using RasterCacheTest = LayerTest; TEST_F(RasterCacheTest, RasterCacheKeyIDLayerChildrenIds) { auto layer = std::make_shared(); @@ -869,8 +869,7 @@ TEST_F(RasterCacheTest, RasterCacheKeyIDLayerChildrenIds) { auto display_list = GetSampleDisplayList(); auto display_list_layer = std::make_shared( - SkPoint::Make(0.0f, 0.0f), - SkiaGPUObject(display_list, unref_queue()), false, false); + SkPoint::Make(0.0f, 0.0f), display_list, false, false); layer->Add(display_list_layer); auto ids = RasterCacheKeyID::LayerChildrenIds(layer.get()).value(); diff --git a/engine/src/flutter/flow/testing/diff_context_test.cc b/engine/src/flutter/flow/testing/diff_context_test.cc index 6bf66b2125..9860c76688 100644 --- a/engine/src/flutter/flow/testing/diff_context_test.cc +++ b/engine/src/flutter/flow/testing/diff_context_test.cc @@ -10,10 +10,7 @@ namespace flutter { namespace testing { -DiffContextTest::DiffContextTest() - : unref_queue_(fml::MakeRefCounted( - GetCurrentTaskRunner(), - fml::TimeDelta::FromSeconds(0))) {} +DiffContextTest::DiffContextTest() {} Damage DiffContextTest::DiffLayerTree(MockLayerTree& layer_tree, const MockLayerTree& old_layer_tree, @@ -40,11 +37,9 @@ sk_sp DiffContextTest::CreateDisplayList(const SkRect& bounds, } std::shared_ptr DiffContextTest::CreateDisplayListLayer( - sk_sp display_list, + const sk_sp& display_list, const SkPoint& offset) { - return std::make_shared( - offset, SkiaGPUObject(std::move(display_list), unref_queue()), false, - false); + return std::make_shared(offset, display_list, false, false); } std::shared_ptr DiffContextTest::CreateContainerLayer( diff --git a/engine/src/flutter/flow/testing/diff_context_test.h b/engine/src/flutter/flow/testing/diff_context_test.h index 0401b32320..f606e42bca 100644 --- a/engine/src/flutter/flow/testing/diff_context_test.h +++ b/engine/src/flutter/flow/testing/diff_context_test.h @@ -5,7 +5,7 @@ #include "flutter/flow/layers/container_layer.h" #include "flutter/flow/layers/display_list_layer.h" #include "flutter/flow/layers/opacity_layer.h" -#include "flutter/flow/testing/skia_gpu_object_layer_test.h" +#include "flutter/flow/testing/layer_test.h" #include "third_party/skia/include/core/SkPicture.h" #include "third_party/skia/include/core/SkPictureRecorder.h" @@ -31,7 +31,7 @@ class MockLayerTree { SkISize size_; }; -class DiffContextTest : public ThreadTest { +class DiffContextTest : public LayerTest { public: DiffContextTest(); @@ -47,7 +47,7 @@ class DiffContextTest : public ThreadTest { sk_sp CreateDisplayList(const SkRect& bounds, uint32_t color); std::shared_ptr CreateDisplayListLayer( - sk_sp display_list, + const sk_sp& display_list, const SkPoint& offset = SkPoint::Make(0, 0)); std::shared_ptr CreateContainerLayer( @@ -62,11 +62,6 @@ class DiffContextTest : public ThreadTest { std::initializer_list> layers, SkAlpha alpha, const SkPoint& offset = SkPoint::Make(0, 0)); - - fml::RefPtr unref_queue() { return unref_queue_; } - - private: - fml::RefPtr unref_queue_; }; } // namespace testing diff --git a/engine/src/flutter/flow/testing/mock_texture.cc b/engine/src/flutter/flow/testing/mock_texture.cc index ceb8e88fd6..7414c660b8 100644 --- a/engine/src/flutter/flow/testing/mock_texture.cc +++ b/engine/src/flutter/flow/testing/mock_texture.cc @@ -4,7 +4,7 @@ #include "flutter/flow/testing/mock_texture.h" #include "flutter/flow/layers/layer.h" -#include "flutter/flow/testing/skia_gpu_object_layer_test.h" +#include "flutter/testing/display_list_testing.h" namespace flutter { namespace testing { diff --git a/engine/src/flutter/flow/testing/skia_gpu_object_layer_test.cc b/engine/src/flutter/flow/testing/skia_gpu_object_layer_test.cc deleted file mode 100644 index 1fca8ec8f3..0000000000 --- a/engine/src/flutter/flow/testing/skia_gpu_object_layer_test.cc +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/flow/testing/skia_gpu_object_layer_test.h" - -#include "flutter/fml/time/time_delta.h" - -namespace flutter { -namespace testing { - -SkiaGPUObjectLayerTest::SkiaGPUObjectLayerTest() - : unref_queue_(fml::MakeRefCounted( - GetCurrentTaskRunner(), - fml::TimeDelta::FromSeconds(0))) {} - -} // namespace testing -} // namespace flutter diff --git a/engine/src/flutter/flow/testing/skia_gpu_object_layer_test.h b/engine/src/flutter/flow/testing/skia_gpu_object_layer_test.h deleted file mode 100644 index d573ac0b41..0000000000 --- a/engine/src/flutter/flow/testing/skia_gpu_object_layer_test.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLOW_TESTING_SKIA_GPU_OBJECT_LAYER_TEST_H_ -#define FLOW_TESTING_SKIA_GPU_OBJECT_LAYER_TEST_H_ - -#include "flutter/flow/skia_gpu_object.h" -#include "flutter/flow/testing/layer_test.h" -#include "flutter/testing/thread_test.h" - -namespace flutter { -namespace testing { - -// This fixture allows generating tests that create |SkiaGPUObject|'s which -// are destroyed on a |SkiaUnrefQueue|. -class SkiaGPUObjectLayerTest : public LayerTestBase { - public: - SkiaGPUObjectLayerTest(); - - fml::RefPtr unref_queue() { return unref_queue_; } - - private: - fml::RefPtr unref_queue_; -}; - -} // namespace testing -} // namespace flutter - -#endif // FLOW_TESTING_SKIA_GPU_OBJECT_LAYER_TEST_H_ diff --git a/engine/src/flutter/impeller/display_list/display_list_image_impeller.cc b/engine/src/flutter/impeller/display_list/display_list_image_impeller.cc index 0133908864..8a9d77b000 100644 --- a/engine/src/flutter/impeller/display_list/display_list_image_impeller.cc +++ b/engine/src/flutter/impeller/display_list/display_list_image_impeller.cc @@ -64,6 +64,12 @@ bool DlImageImpeller::isTextureBacked() const { return true; } +// |DlImage| +bool DlImageImpeller::isUIThreadSafe() const { + // Impeller textures are always thread-safe + return true; +} + // |DlImage| SkISize DlImageImpeller::dimensions() const { const auto size = texture_ ? texture_->GetSize() : ISize{}; diff --git a/engine/src/flutter/impeller/display_list/display_list_image_impeller.h b/engine/src/flutter/impeller/display_list/display_list_image_impeller.h index d76aaba5d3..72e3b68171 100644 --- a/engine/src/flutter/impeller/display_list/display_list_image_impeller.h +++ b/engine/src/flutter/impeller/display_list/display_list_image_impeller.h @@ -39,6 +39,9 @@ class DlImageImpeller final : public flutter::DlImage { // |DlImage| bool isTextureBacked() const override; + // |DlImage| + bool isUIThreadSafe() const override; + // |DlImage| SkISize dimensions() const override; diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.cc b/engine/src/flutter/lib/ui/compositing/scene_builder.cc index 6c3e83ba76..63ac03c92c 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.cc +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.cc @@ -243,9 +243,8 @@ void SceneBuilder::addPicture(double dx, // been disposed but not collected yet, but the display list is null. if (picture->display_list()) { auto layer = std::make_unique( - SkPoint::Make(SafeNarrow(dx), SafeNarrow(dy)), - UIDartState::CreateGPUObject(picture->display_list()), !!(hints & 1), - !!(hints & 2)); + SkPoint::Make(SafeNarrow(dx), SafeNarrow(dy)), picture->display_list(), + !!(hints & 1), !!(hints & 2)); AddLayer(std::move(layer)); } } diff --git a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.cc b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.cc index 49fb3e214d..40d09b8ff0 100644 --- a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.cc +++ b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.cc @@ -63,6 +63,11 @@ bool DlDeferredImageGPUImpeller::isTextureBacked() const { return wrapper_ && wrapper_->isTextureBacked(); } +// |DlImage| +bool DlDeferredImageGPUImpeller::isUIThreadSafe() const { + return true; +} + // |DlImage| SkISize DlDeferredImageGPUImpeller::dimensions() const { if (!wrapper_) { diff --git a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.h b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.h index 08692ff2b4..664818da8a 100644 --- a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.h +++ b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.h @@ -44,6 +44,9 @@ class DlDeferredImageGPUImpeller final : public DlImage { // |DlImage| bool isTextureBacked() const override; + // |DlImage| + bool isUIThreadSafe() const override; + // |DlImage| SkISize dimensions() const override; diff --git a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.cc b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.cc index fa4441fb86..a2bb4c0b94 100644 --- a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.cc +++ b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.cc @@ -75,6 +75,11 @@ bool DlDeferredImageGPUSkia::isTextureBacked() const { return image_wrapper_ ? image_wrapper_->isTextureBacked() : false; } +// |DlImage| +bool DlDeferredImageGPUSkia::isUIThreadSafe() const { + return true; +} + // |DlImage| SkISize DlDeferredImageGPUSkia::dimensions() const { return image_wrapper_ ? image_wrapper_->image_info().dimensions() diff --git a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.h b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.h index 6abbaeacbe..2d6c9b53c5 100644 --- a/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.h +++ b/engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.h @@ -12,7 +12,6 @@ #include "flutter/display_list/display_list.h" #include "flutter/display_list/image/dl_image.h" #include "flutter/flow/layers/layer_tree.h" -#include "flutter/flow/skia_gpu_object.h" #include "flutter/fml/macros.h" #include "flutter/fml/memory/weak_ptr.h" #include "flutter/lib/ui/io_manager.h" @@ -55,6 +54,9 @@ class DlDeferredImageGPUSkia final : public DlImage { // |DlImage| bool isTextureBacked() const override; + // |DlImage| + bool isUIThreadSafe() const override; + // |DlImage| SkISize dimensions() const override; diff --git a/engine/src/flutter/lib/ui/painting/display_list_image_gpu.cc b/engine/src/flutter/lib/ui/painting/display_list_image_gpu.cc index ce69f06a15..b4e8709053 100644 --- a/engine/src/flutter/lib/ui/painting/display_list_image_gpu.cc +++ b/engine/src/flutter/lib/ui/painting/display_list_image_gpu.cc @@ -45,6 +45,11 @@ bool DlImageGPU::isTextureBacked() const { return false; } +// |DlImage| +bool DlImageGPU::isUIThreadSafe() const { + return true; +} + // |DlImage| SkISize DlImageGPU::dimensions() const { const auto image = skia_image(); diff --git a/engine/src/flutter/lib/ui/painting/display_list_image_gpu.h b/engine/src/flutter/lib/ui/painting/display_list_image_gpu.h index 9122ae6306..4290c724c8 100644 --- a/engine/src/flutter/lib/ui/painting/display_list_image_gpu.h +++ b/engine/src/flutter/lib/ui/painting/display_list_image_gpu.h @@ -30,6 +30,9 @@ class DlImageGPU final : public DlImage { // |DlImage| bool isTextureBacked() const override; + // |DlImage| + bool isUIThreadSafe() const override; + // |DlImage| SkISize dimensions() const override; diff --git a/engine/src/flutter/lib/ui/painting/fragment_shader.cc b/engine/src/flutter/lib/ui/painting/fragment_shader.cc index 5a90c585e1..491d312e7e 100644 --- a/engine/src/flutter/lib/ui/painting/fragment_shader.cc +++ b/engine/src/flutter/lib/ui/painting/fragment_shader.cc @@ -59,6 +59,9 @@ bool ReusableFragmentShader::ValidateSamplers() { if (samplers_[i] == nullptr) { return false; } + // The samplers should have been checked as they were added, this + // is a double-sanity-check. + FML_DCHECK(samplers_[i]->isUIThreadSafe()); } return true; } @@ -71,12 +74,18 @@ void ReusableFragmentShader::SetImageSampler(Dart_Handle index_handle, if (index >= samplers_.size()) { Dart_ThrowException(tonic::ToDart("Sampler index out of bounds")); } + if (!image->image()->isUIThreadSafe()) { + Dart_ThrowException(tonic::ToDart("Image is not thread-safe")); + } // TODO(115794): Once the DlImageSampling enum is replaced, expose the // sampling options as a new default parameter for users. samplers_[index] = std::make_shared( image->image(), DlTileMode::kClamp, DlTileMode::kClamp, DlImageSampling::kNearestNeighbor, nullptr); + // This should be true since we already checked the image above, but + // we check again for sanity. + FML_DCHECK(samplers_[index]->isUIThreadSafe()); auto* uniform_floats = reinterpret_cast(uniform_data_->writable_data()); @@ -95,7 +104,11 @@ std::shared_ptr ReusableFragmentShader::shader( uniform_data->resize(uniform_data_->size()); memcpy(uniform_data->data(), uniform_data_->bytes(), uniform_data->size()); - return program_->MakeDlColorSource(std::move(uniform_data), samplers_); + auto source = program_->MakeDlColorSource(std::move(uniform_data), samplers_); + // The samplers should have been checked as they were added, this + // is a double-sanity-check. + FML_DCHECK(source->isUIThreadSafe()); + return source; } void ReusableFragmentShader::Dispose() { diff --git a/engine/src/flutter/lib/ui/painting/gradient.cc b/engine/src/flutter/lib/ui/painting/gradient.cc index 6f692add01..e54f39f41f 100644 --- a/engine/src/flutter/lib/ui/painting/gradient.cc +++ b/engine/src/flutter/lib/ui/painting/gradient.cc @@ -50,6 +50,8 @@ void CanvasGradient::initLinear(const tonic::Float32List& end_points, dl_shader_ = DlColorSource::MakeLinear( p0, p1, colors.num_elements(), colors_array, color_stops.data(), tile_mode, has_matrix ? &sk_matrix : nullptr); + // Just a sanity check, all gradient shaders should be thread-safe + FML_DCHECK(dl_shader_->isUIThreadSafe()); } void CanvasGradient::initRadial(double center_x, @@ -77,6 +79,8 @@ void CanvasGradient::initRadial(double center_x, SkPoint::Make(SafeNarrow(center_x), SafeNarrow(center_y)), SafeNarrow(radius), colors.num_elements(), colors_array, color_stops.data(), tile_mode, has_matrix ? &sk_matrix : nullptr); + // Just a sanity check, all gradient shaders should be thread-safe + FML_DCHECK(dl_shader_->isUIThreadSafe()); } void CanvasGradient::initSweep(double center_x, @@ -107,6 +111,8 @@ void CanvasGradient::initSweep(double center_x, SafeNarrow(end_angle) * 180.0f / static_cast(M_PI), colors.num_elements(), colors_array, color_stops.data(), tile_mode, has_matrix ? &sk_matrix : nullptr); + // Just a sanity check, all gradient shaders should be thread-safe + FML_DCHECK(dl_shader_->isUIThreadSafe()); } void CanvasGradient::initTwoPointConical(double start_x, @@ -139,6 +145,8 @@ void CanvasGradient::initTwoPointConical(double start_x, SkPoint::Make(SafeNarrow(end_x), SafeNarrow(end_y)), SafeNarrow(end_radius), colors.num_elements(), colors_array, color_stops.data(), tile_mode, has_matrix ? &sk_matrix : nullptr); + // Just a sanity check, all gradient shaders should be thread-safe + FML_DCHECK(dl_shader_->isUIThreadSafe()); } CanvasGradient::CanvasGradient() = default; diff --git a/engine/src/flutter/lib/ui/painting/image.h b/engine/src/flutter/lib/ui/painting/image.h index 0ffd4ec4f5..18a7200739 100644 --- a/engine/src/flutter/lib/ui/painting/image.h +++ b/engine/src/flutter/lib/ui/painting/image.h @@ -6,9 +6,7 @@ #define FLUTTER_LIB_UI_PAINTING_IMAGE_H_ #include "flutter/display_list/image/dl_image.h" -#include "flutter/flow/skia_gpu_object.h" #include "flutter/lib/ui/dart_wrapper.h" -#include "flutter/lib/ui/painting/display_list_image_gpu.h" #include "flutter/lib/ui/ui_dart_state.h" #include "third_party/skia/include/core/SkImage.h" @@ -41,7 +39,10 @@ class CanvasImage final : public RefCountedDartWrappable { sk_sp image() const { return image_; } - void set_image(sk_sp image) { image_ = image; } + void set_image(sk_sp image) { + FML_DCHECK(image->isUIThreadSafe()); + image_ = image; + } int colorSpace(); diff --git a/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc b/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc index 40e587c523..8a111167fc 100644 --- a/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc +++ b/engine/src/flutter/lib/ui/painting/image_encoding_unittests.cc @@ -35,6 +35,7 @@ class MockDlImage : public DlImage { MOCK_CONST_METHOD0(impeller_texture, std::shared_ptr()); MOCK_CONST_METHOD0(isOpaque, bool()); MOCK_CONST_METHOD0(isTextureBacked, bool()); + MOCK_CONST_METHOD0(isUIThreadSafe, bool()); MOCK_CONST_METHOD0(dimensions, SkISize()); MOCK_CONST_METHOD0(GetApproximateByteSize, size_t()); }; diff --git a/engine/src/flutter/lib/ui/painting/image_shader.cc b/engine/src/flutter/lib/ui/painting/image_shader.cc index e2cdae7dfb..5093f51433 100644 --- a/engine/src/flutter/lib/ui/painting/image_shader.cc +++ b/engine/src/flutter/lib/ui/painting/image_shader.cc @@ -28,7 +28,8 @@ Dart_Handle ImageShader::initWithImage(CanvasImage* image, DlTileMode tmy, int filter_quality_index, Dart_Handle matrix_handle) { - if (!image) { + // CanvasImage should have already checked for a UI thread safe image. + if (!image || !image->image()->isUIThreadSafe()) { return ToDart("ImageShader constructor called with non-genuine Image."); } @@ -40,25 +41,17 @@ Dart_Handle ImageShader::initWithImage(CanvasImage* image, DlImageSampling sampling = sampling_is_locked_ ? ImageFilter::SamplingFromIndex(filter_quality_index) : DlImageSampling::kLinear; - cached_shader_ = UIDartState::CreateGPUObject(sk_make_sp( - image_, tmx, tmy, sampling, &local_matrix)); + cached_shader_ = std::make_shared( + image_, tmx, tmy, sampling, &local_matrix); + FML_DCHECK(cached_shader_->isUIThreadSafe()); return Dart_Null(); } std::shared_ptr ImageShader::shader(DlImageSampling sampling) { - if (sampling_is_locked_) { - return cached_shader_.skia_object()->with_sampling( - cached_shader_.skia_object()->sampling()); + if (sampling_is_locked_ || sampling == cached_shader_->sampling()) { + return cached_shader_; } - // It might seem that if the sampling is locked we can just return the - // cached version, but since we need to hold the cached shader in a - // Skia GPU wrapper, and that wrapper requires an sk_sp<>, we are holding - // an sk_sp<> version of the shared object and we need a shared_ptr version. - // So, either way, we need the with_sampling() method to shared_ptr'ify - // our copy. - // If we can get rid of the need for the GPU unref queue, then this can all - // be simplified down to just a shared_ptr. - return cached_shader_.skia_object()->with_sampling(sampling); + return cached_shader_->with_sampling(sampling); } int ImageShader::width() { diff --git a/engine/src/flutter/lib/ui/painting/image_shader.h b/engine/src/flutter/lib/ui/painting/image_shader.h index 7c69d1389b..61b4a851eb 100644 --- a/engine/src/flutter/lib/ui/painting/image_shader.h +++ b/engine/src/flutter/lib/ui/painting/image_shader.h @@ -43,7 +43,7 @@ class ImageShader : public Shader { sk_sp image_; bool sampling_is_locked_; - flutter::SkiaGPUObject cached_shader_; + std::shared_ptr cached_shader_; }; } // namespace flutter diff --git a/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc b/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc index 796ea4645f..c52b4a72db 100644 --- a/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc +++ b/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc @@ -7,6 +7,7 @@ #include #include "flutter/fml/make_copyable.h" +#include "flutter/lib/ui/painting/display_list_image_gpu.h" #include "flutter/lib/ui/painting/image.h" #if IMPELLER_SUPPORTS_RENDERING #include "flutter/lib/ui/painting/image_decoder_impeller.h" diff --git a/engine/src/flutter/lib/ui/painting/picture.cc b/engine/src/flutter/lib/ui/painting/picture.cc index 0121c85c7c..53a9371277 100644 --- a/engine/src/flutter/lib/ui/painting/picture.cc +++ b/engine/src/flutter/lib/ui/painting/picture.cc @@ -14,6 +14,7 @@ #if IMPELLER_SUPPORTS_RENDERING #include "flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.h" #endif // IMPELLER_SUPPORTS_RENDERING +#include "flutter/lib/ui/painting/display_list_image_gpu.h" #include "third_party/tonic/converter/dart_converter.h" #include "third_party/tonic/dart_args.h" #include "third_party/tonic/dart_binding_macros.h" @@ -25,16 +26,16 @@ namespace flutter { IMPLEMENT_WRAPPERTYPEINFO(ui, Picture); -fml::RefPtr Picture::Create( - Dart_Handle dart_handle, - flutter::SkiaGPUObject display_list) { +fml::RefPtr Picture::Create(Dart_Handle dart_handle, + sk_sp display_list) { + FML_DCHECK(display_list->isUIThreadSafe()); auto canvas_picture = fml::MakeRefCounted(std::move(display_list)); canvas_picture->AssociateWithDartWrapper(dart_handle); return canvas_picture; } -Picture::Picture(flutter::SkiaGPUObject display_list) +Picture::Picture(sk_sp display_list) : display_list_(std::move(display_list)) {} Picture::~Picture() = default; @@ -42,19 +43,17 @@ Picture::~Picture() = default; Dart_Handle Picture::toImage(uint32_t width, uint32_t height, Dart_Handle raw_image_callback) { - if (!display_list_.skia_object()) { + if (!display_list_) { return tonic::ToDart("Picture is null"); } - return RasterizeToImage(display_list_.skia_object(), width, height, - raw_image_callback); + return RasterizeToImage(display_list_, width, height, raw_image_callback); } void Picture::toImageSync(uint32_t width, uint32_t height, Dart_Handle raw_image_handle) { - FML_DCHECK(display_list_.skia_object()); - RasterizeToImageSync(display_list_.skia_object(), width, height, - raw_image_handle); + FML_DCHECK(display_list_); + RasterizeToImageSync(display_list_, width, height, raw_image_handle); } static sk_sp CreateDeferredImage( @@ -108,8 +107,8 @@ void Picture::dispose() { } size_t Picture::GetAllocationSize() const { - if (auto display_list = display_list_.skia_object()) { - return display_list->bytes() + sizeof(Picture); + if (display_list_) { + return display_list_->bytes() + sizeof(Picture); } else { return sizeof(Picture); } @@ -177,7 +176,9 @@ Dart_Handle Picture::RasterizeToImage(const sk_sp& display_list, return; } - if (image->skia_image()) { + if (!image->isUIThreadSafe()) { + // All images with impeller textures should already be safe. + FML_DCHECK(image->impeller_texture() == nullptr); image = DlImageGPU::Make({image->skia_image(), std::move(unref_queue)}); } diff --git a/engine/src/flutter/lib/ui/painting/picture.h b/engine/src/flutter/lib/ui/painting/picture.h index 33bee1994f..d0c2a0123f 100644 --- a/engine/src/flutter/lib/ui/painting/picture.h +++ b/engine/src/flutter/lib/ui/painting/picture.h @@ -7,7 +7,6 @@ #include "flutter/display_list/display_list.h" #include "flutter/flow/layers/layer_tree.h" -#include "flutter/flow/skia_gpu_object.h" #include "flutter/lib/ui/dart_wrapper.h" #include "flutter/lib/ui/painting/image.h" #include "flutter/lib/ui/ui_dart_state.h" @@ -22,13 +21,10 @@ class Picture : public RefCountedDartWrappable { public: ~Picture() override; - static fml::RefPtr Create( - Dart_Handle dart_handle, - flutter::SkiaGPUObject display_list); + static fml::RefPtr Create(Dart_Handle dart_handle, + sk_sp display_list); - sk_sp display_list() const { - return display_list_.skia_object(); - } + sk_sp display_list() const { return display_list_; } Dart_Handle toImage(uint32_t width, uint32_t height, @@ -68,9 +64,9 @@ class Picture : public RefCountedDartWrappable { Dart_Handle raw_image_callback); private: - explicit Picture(flutter::SkiaGPUObject display_list); + explicit Picture(sk_sp display_list); - flutter::SkiaGPUObject display_list_; + sk_sp display_list_; }; } // namespace flutter diff --git a/engine/src/flutter/lib/ui/painting/picture_recorder.cc b/engine/src/flutter/lib/ui/painting/picture_recorder.cc index effbb196dc..a99b5e5855 100644 --- a/engine/src/flutter/lib/ui/painting/picture_recorder.cc +++ b/engine/src/flutter/lib/ui/painting/picture_recorder.cc @@ -38,8 +38,7 @@ fml::RefPtr PictureRecorder::endRecording(Dart_Handle dart_picture) { fml::RefPtr picture; - picture = Picture::Create(dart_picture, UIDartState::CreateGPUObject( - display_list_builder_->Build())); + picture = Picture::Create(dart_picture, display_list_builder_->Build()); display_list_builder_ = nullptr; canvas_->Invalidate(); diff --git a/engine/src/flutter/lib/ui/painting/scene/scene_shader.cc b/engine/src/flutter/lib/ui/painting/scene/scene_shader.cc index 05382d628f..fdfc0a1763 100644 --- a/engine/src/flutter/lib/ui/painting/scene/scene_shader.cc +++ b/engine/src/flutter/lib/ui/painting/scene/scene_shader.cc @@ -78,10 +78,13 @@ std::shared_ptr SceneShader::shader(DlImageSampling sampling) { // TODO(bdero): Gather the mutation log and include it in the scene color // source. - return std::make_shared(scene_node_->node_, - camera_transform_.IsIdentity() - ? DefaultCameraTransform() - : camera_transform_); + auto source = std::make_shared( + scene_node_->node_, camera_transform_.IsIdentity() + ? DefaultCameraTransform() + : camera_transform_); + // Just a sanity check, all scene shaders should be thread-safe + FML_DCHECK(source->isUIThreadSafe()); + return source; } void SceneShader::Dispose() { diff --git a/engine/src/flutter/lib/ui/snapshot_delegate.h b/engine/src/flutter/lib/ui/snapshot_delegate.h index 6f9fc113a3..0640fe0dd9 100644 --- a/engine/src/flutter/lib/ui/snapshot_delegate.h +++ b/engine/src/flutter/lib/ui/snapshot_delegate.h @@ -9,7 +9,6 @@ #include "flutter/common/graphics/texture.h" #include "flutter/display_list/display_list.h" -#include "flutter/flow/skia_gpu_object.h" #include "third_party/skia/include/core/SkImage.h" #include "third_party/skia/include/core/SkPicture.h" #include "third_party/skia/include/core/SkPromiseImageTexture.h" diff --git a/engine/src/flutter/lib/ui/ui_dart_state.h b/engine/src/flutter/lib/ui/ui_dart_state.h index c384cd420f..63824a552e 100644 --- a/engine/src/flutter/lib/ui/ui_dart_state.h +++ b/engine/src/flutter/lib/ui/ui_dart_state.h @@ -11,7 +11,6 @@ #include "flutter/common/settings.h" #include "flutter/common/task_runners.h" -#include "flutter/flow/skia_gpu_object.h" #include "flutter/fml/build_config.h" #include "flutter/fml/memory/weak_ptr.h" #include "flutter/fml/synchronization/waitable_event.h" @@ -153,17 +152,6 @@ class UIDartState : public tonic::DartState { // @param[in] message The message to be logged. void LogMessage(const std::string& tag, const std::string& message) const; - template - static flutter::SkiaGPUObject CreateGPUObject(sk_sp object) { - if (!object) { - return {}; - } - auto* state = UIDartState::Current(); - FML_DCHECK(state); - auto queue = state->GetSkiaUnrefQueue(); - return {std::move(object), std::move(queue)}; - }; - UnhandledExceptionCallback unhandled_exception_callback() const { return unhandled_exception_callback_; } diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 82dc5b3682..d85750e70d 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -804,13 +804,8 @@ TEST_F(ShellTest, ExternalEmbedderNoThreadMerger) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; @@ -935,13 +930,8 @@ TEST_F(ShellTest, RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; @@ -981,13 +971,8 @@ TEST_F(ShellTest, OnPlatformViewDestroyDisablesThreadMerger) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; @@ -1048,13 +1033,8 @@ TEST_F(ShellTest, OnPlatformViewDestroyAfterMergingThreads) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; @@ -1117,13 +1097,8 @@ TEST_F(ShellTest, OnPlatformViewDestroyWhenThreadsAreMerging) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; @@ -1185,13 +1160,8 @@ TEST_F(ShellTest, RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; PumpOneFrame(shell.get(), 100, 100, builder); @@ -1229,13 +1199,8 @@ TEST_F(ShellTest, OnPlatformViewDestroyWithoutRasterThreadMerger) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; PumpOneFrame(shell.get(), 100, 100, builder); @@ -1295,13 +1260,8 @@ TEST_F(ShellTest, OnPlatformViewDestroyWithStaticThreadMerging) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; PumpOneFrame(shell.get(), 100, 100, builder); @@ -1341,13 +1301,8 @@ TEST_F(ShellTest, GetUsedThisFrameShouldBeSetBeforeEndFrame) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; PumpOneFrame(shell.get(), 100, 100, builder); @@ -2083,13 +2038,8 @@ TEST_F(ShellTest, Screenshot) { RunEngine(shell.get(), std::move(configuration)); LayerTreeBuilder builder = [&](const std::shared_ptr& root) { - fml::RefPtr queue = fml::MakeRefCounted( - this->GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(10, 10), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(80, 80), queue}), - false, false); + SkPoint::Make(10, 10), MakeSizedDisplayList(80, 80), false, false); root->Add(display_list_layer); }; @@ -2441,13 +2391,8 @@ TEST_F(ShellTest, OnServiceProtocolEstimateRasterCacheMemoryWorks) { // 1. Construct a picture and a picture layer to be raster cached. sk_sp display_list = MakeSizedDisplayList(10, 10); - fml::RefPtr queue = fml::MakeRefCounted( - GetCurrentTaskRunner(), fml::TimeDelta::Zero()); auto display_list_layer = std::make_shared( - SkPoint::Make(0, 0), - flutter::SkiaGPUObject( - {MakeSizedDisplayList(100, 100), queue}), - false, false); + SkPoint::Make(0, 0), MakeSizedDisplayList(100, 100), false, false); display_list_layer->set_paint_bounds(SkRect::MakeWH(100, 100)); // 2. Rasterize the picture and the picture layer in the raster cache. diff --git a/engine/src/flutter/shell/common/snapshot_controller.h b/engine/src/flutter/shell/common/snapshot_controller.h index 54ad5e0b06..c402c3ec70 100644 --- a/engine/src/flutter/shell/common/snapshot_controller.h +++ b/engine/src/flutter/shell/common/snapshot_controller.h @@ -36,6 +36,9 @@ class SnapshotController { virtual ~SnapshotController() = default; + // Note that this image is not guaranteed to be UIThreadSafe and must + // be converted to a DlImageGPU if it is to be handed back to the UI + // thread. virtual sk_sp MakeRasterSnapshot(sk_sp display_list, SkISize size) = 0; diff --git a/engine/src/flutter/shell/common/snapshot_controller_skia.cc b/engine/src/flutter/shell/common/snapshot_controller_skia.cc index e5f0c49a00..e9fc66cde4 100644 --- a/engine/src/flutter/shell/common/snapshot_controller_skia.cc +++ b/engine/src/flutter/shell/common/snapshot_controller_skia.cc @@ -120,6 +120,8 @@ sk_sp SnapshotControllerSkia::DoMakeRasterSnapshot( })); } + // It is up to the caller to create a DlImageGPU version of this image + // if the result will interact with the UI thread. return DlImage::Make(result); } diff --git a/engine/src/flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm b/engine/src/flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm index 01f64b351a..3a8a5030b7 100644 --- a/engine/src/flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm +++ b/engine/src/flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm @@ -227,6 +227,7 @@ FLUTTER_ASSERT_ARC return nullptr; } + // This image should not escape local use by this flutter::Texture implementation return flutter::DlImage::Make(skImage); } @@ -272,6 +273,8 @@ FLUTTER_ASSERT_ARC if (!skImage) { return nullptr; } + + // This image should not escape local use by this flutter::Texture implementation return flutter::DlImage::Make(skImage); } diff --git a/engine/src/flutter/shell/platform/embedder/embedder_external_texture_gl.cc b/engine/src/flutter/shell/platform/embedder/embedder_external_texture_gl.cc index 04b069c5fa..53bcc3e92e 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_external_texture_gl.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder_external_texture_gl.cc @@ -101,6 +101,7 @@ sk_sp EmbedderExternalTextureGL::ResolveTexture( return nullptr; } + // This image should not escape local use by EmbedderExternalTextureGL return DlImage::Make(std::move(image)); } diff --git a/engine/src/flutter/shell/platform/embedder/embedder_external_texture_metal.mm b/engine/src/flutter/shell/platform/embedder/embedder_external_texture_metal.mm index ac759d5211..4e825e8cac 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_external_texture_metal.mm +++ b/engine/src/flutter/shell/platform/embedder/embedder_external_texture_metal.mm @@ -100,6 +100,7 @@ sk_sp EmbedderExternalTextureMetal::ResolveTexture(int64_t texture_id, FML_LOG(ERROR) << "Could not create external texture: " << texture_id; } + // This image should not escape local use by EmbedderExternalTextureMetal return DlImage::Make(std::move(image)); }