From 676e99c1f40308520ddfc171a1dd8e0be699b605 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Mon, 18 Jul 2022 12:24:05 +0800 Subject: [PATCH] Tweak the logic about generating 'RasterCacheKey' of the type 'kLayerChildren' (flutter/engine#32676) --- .../flutter/flow/layers/display_list_layer.h | 5 + engine/src/flutter/flow/layers/layer.h | 3 + engine/src/flutter/flow/raster_cache_key.cc | 14 +-- engine/src/flutter/flow/raster_cache_key.h | 48 ++++++--- .../flutter/flow/raster_cache_unittests.cc | 97 +++++++++++++------ 5 files changed, 118 insertions(+), 49 deletions(-) diff --git a/engine/src/flutter/flow/layers/display_list_layer.h b/engine/src/flutter/flow/layers/display_list_layer.h index 3eaef523e7..c8066d0d43 100644 --- a/engine/src/flutter/flow/layers/display_list_layer.h +++ b/engine/src/flutter/flow/layers/display_list_layer.h @@ -44,6 +44,11 @@ class DisplayListLayer : public Layer { return display_list_raster_cache_item_.get(); } + RasterCacheKeyID caching_key_id() const override { + return RasterCacheKeyID(display_list()->unique_id(), + RasterCacheKeyType::kDisplayList); + } + private: std::unique_ptr display_list_raster_cache_item_; diff --git a/engine/src/flutter/flow/layers/layer.h b/engine/src/flutter/flow/layers/layer.h index dbc08d4998..63d30c3bb5 100644 --- a/engine/src/flutter/flow/layers/layer.h +++ b/engine/src/flutter/flow/layers/layer.h @@ -375,6 +375,9 @@ class Layer { uint64_t unique_id() const { return unique_id_; } + virtual RasterCacheKeyID caching_key_id() const { + return RasterCacheKeyID(unique_id_, RasterCacheKeyType::kLayer); + } virtual const ContainerLayer* as_container_layer() const { return nullptr; } virtual const DisplayListLayer* as_display_list_layer() const { return nullptr; diff --git a/engine/src/flutter/flow/raster_cache_key.cc b/engine/src/flutter/flow/raster_cache_key.cc index c8eedf9b64..ce091ea7a8 100644 --- a/engine/src/flutter/flow/raster_cache_key.cc +++ b/engine/src/flutter/flow/raster_cache_key.cc @@ -5,22 +5,24 @@ #include "flutter/flow/raster_cache_key.h" #include #include "flutter/flow/layers/container_layer.h" +#include "flutter/flow/layers/display_list_layer.h" #include "flutter/flow/layers/layer.h" namespace flutter { // -std::optional> RasterCacheKeyID::LayerChildrenIds( - Layer* layer) { +std::optional> RasterCacheKeyID::LayerChildrenIds( + const Layer* layer) { + FML_DCHECK(layer->as_container_layer()); auto& children_layers = layer->as_container_layer()->layers(); auto children_count = children_layers.size(); if (children_count == 0) { return std::nullopt; } - std::vector ids; - std::transform(children_layers.begin(), children_layers.end(), - std::back_inserter(ids), - [](auto& layer) -> uint64_t { return layer->unique_id(); }); + std::vector ids; + std::transform( + children_layers.begin(), children_layers.end(), std::back_inserter(ids), + [](auto& layer) -> RasterCacheKeyID { return layer->caching_key_id(); }); return ids; } diff --git a/engine/src/flutter/flow/raster_cache_key.h b/engine/src/flutter/flow/raster_cache_key.h index 2f61f8f6d8..64292b6e89 100644 --- a/engine/src/flutter/flow/raster_cache_key.h +++ b/engine/src/flutter/flow/raster_cache_key.h @@ -22,28 +22,43 @@ enum class RasterCacheKeyType { kLayer, kDisplayList, kLayerChildren }; class RasterCacheKeyID { public: - RasterCacheKeyID(uint16_t id, RasterCacheKeyType type) - : ids_({id}), type_(type) {} + static constexpr uint64_t kDefaultUniqueID = 0; - RasterCacheKeyID(const std::vector ids, RasterCacheKeyType type) - : ids_(ids), type_(type) {} + RasterCacheKeyID(uint64_t unique_id, RasterCacheKeyType type) + : unique_id_(unique_id), type_(type) {} - const std::vector& ids() const { return ids_; } + RasterCacheKeyID(std::vector child_ids, + RasterCacheKeyType type) + : unique_id_(kDefaultUniqueID), + type_(type), + child_ids_(std::move(child_ids)) {} + + uint64_t unique_id() const { return unique_id_; } RasterCacheKeyType type() const { return type_; } - static std::optional> LayerChildrenIds(Layer* layer); + const std::vector& child_ids() const { return child_ids_; } + + static std::optional> LayerChildrenIds( + const Layer* layer); std::size_t GetHash() const { - std::size_t seed = fml::HashCombine(); - for (auto id : ids_) { - fml::HashCombineSeed(seed, id); + if (cached_hash_) { + return cached_hash_.value(); } - return fml::HashCombine(seed, type_); + std::size_t seed = fml::HashCombine(); + fml::HashCombineSeed(seed, unique_id_); + fml::HashCombineSeed(seed, type_); + for (auto& child_id : child_ids_) { + fml::HashCombineSeed(seed, child_id.GetHash()); + } + cached_hash_ = seed; + return seed; } bool operator==(const RasterCacheKeyID& other) const { - return type_ == other.type_ && ids_ == other.ids_; + return unique_id_ == other.unique_id_ && type_ == other.type_ && + GetHash() == other.GetHash() && child_ids_ == other.child_ids_; } bool operator!=(const RasterCacheKeyID& other) const { @@ -51,16 +66,20 @@ class RasterCacheKeyID { } private: - const std::vector ids_; + const uint64_t unique_id_; const RasterCacheKeyType type_; + const std::vector child_ids_; + mutable std::optional cached_hash_; }; enum class RasterCacheKeyKind { kLayerMetrics, kDisplayListMetrics }; class RasterCacheKey { public: - RasterCacheKey(uint64_t id, RasterCacheKeyType type, const SkMatrix& ctm) - : RasterCacheKey(RasterCacheKeyID(id, type), ctm) {} + RasterCacheKey(uint64_t unique_id, + RasterCacheKeyType type, + const SkMatrix& ctm) + : RasterCacheKey(RasterCacheKeyID(unique_id, type), ctm) {} RasterCacheKey(RasterCacheKeyID id, const SkMatrix& ctm) : id_(std::move(id)), matrix_(ctm) { @@ -69,7 +88,6 @@ class RasterCacheKey { } const RasterCacheKeyID& id() const { return id_; } - RasterCacheKeyType type() const { return id_.type(); } const SkMatrix& matrix() const { return matrix_; } RasterCacheKeyKind kind() const { diff --git a/engine/src/flutter/flow/raster_cache_unittests.cc b/engine/src/flutter/flow/raster_cache_unittests.cc index 55146fa7c7..80b2022381 100644 --- a/engine/src/flutter/flow/raster_cache_unittests.cc +++ b/engine/src/flutter/flow/raster_cache_unittests.cc @@ -5,9 +5,12 @@ #include "flutter/display_list/display_list.h" #include "flutter/display_list/display_list_builder.h" #include "flutter/display_list/display_list_test_utils.h" +#include "flutter/flow/layers/container_layer.h" +#include "flutter/flow/layers/display_list_layer.h" #include "flutter/flow/raster_cache.h" #include "flutter/flow/raster_cache_item.h" #include "flutter/flow/testing/mock_raster_cache.h" +#include "flutter/flow/testing/skia_gpu_object_layer_test.h" #include "gtest/gtest.h" #include "include/core/SkMatrix.h" #include "include/core/SkPoint.h" @@ -638,12 +641,15 @@ TEST(RasterCache, RasterCacheKeySameType) { ASSERT_EQ(map[display_list_third_key], 450); type = RasterCacheKeyType::kLayerChildren; - RasterCacheKey layer_children_first_key(RasterCacheKeyID({1, 2, 3}, type), - matrix); - RasterCacheKey layer_children_second_key(RasterCacheKeyID({2, 3, 1}, type), - matrix); - RasterCacheKey layer_children_third_key(RasterCacheKeyID({3, 2, 1}, type), - matrix); + RasterCacheKeyID foo = RasterCacheKeyID(10, RasterCacheKeyType::kLayer); + RasterCacheKeyID bar = RasterCacheKeyID(20, RasterCacheKeyType::kLayer); + RasterCacheKeyID baz = RasterCacheKeyID(30, RasterCacheKeyType::kLayer); + RasterCacheKey layer_children_first_key( + RasterCacheKeyID({foo, bar, baz}, type), matrix); + RasterCacheKey layer_children_second_key( + RasterCacheKeyID({foo, baz, bar}, type), matrix); + RasterCacheKey layer_children_third_key( + RasterCacheKeyID({baz, bar, foo}, type), matrix); map[layer_children_first_key] = 100; map[layer_children_second_key] = 200; map[layer_children_third_key] = 300; @@ -654,45 +660,80 @@ TEST(RasterCache, RasterCacheKeySameType) { TEST(RasterCache, RasterCacheKeyID_Equal) { RasterCacheKeyID first = RasterCacheKeyID(1, RasterCacheKeyType::kLayer); - RasterCacheKeyID second = + RasterCacheKeyID second = RasterCacheKeyID(2, RasterCacheKeyType::kLayer); + RasterCacheKeyID third = RasterCacheKeyID(1, RasterCacheKeyType::kLayerChildren); - RasterCacheKeyID third = RasterCacheKeyID(2, RasterCacheKeyType::kLayer); + ASSERT_NE(first, second); ASSERT_NE(first, third); ASSERT_NE(second, third); RasterCacheKeyID fourth = - RasterCacheKeyID({1, 2}, RasterCacheKeyType::kLayer); + RasterCacheKeyID({first, second}, RasterCacheKeyType::kLayer); RasterCacheKeyID fifth = - RasterCacheKeyID({1, 2}, RasterCacheKeyType::kLayerChildren); + RasterCacheKeyID({first, second}, RasterCacheKeyType::kLayerChildren); RasterCacheKeyID sixth = - RasterCacheKeyID({2, 1}, RasterCacheKeyType::kLayerChildren); + RasterCacheKeyID({second, first}, RasterCacheKeyType::kLayerChildren); ASSERT_NE(fourth, fifth); ASSERT_NE(fifth, sixth); } -size_t HashIds(std::vector ids, RasterCacheKeyType type) { - std::size_t seed = fml::HashCombine(); - for (auto id : ids) { - fml::HashCombineSeed(seed, id); - } - return fml::HashCombine(seed, type); -} - TEST(RasterCache, RasterCacheKeyID_HashCode) { uint64_t foo = 1; uint64_t bar = 2; RasterCacheKeyID first = RasterCacheKeyID(foo, RasterCacheKeyType::kLayer); - RasterCacheKeyID second = - RasterCacheKeyID({foo, bar}, RasterCacheKeyType::kLayerChildren); - RasterCacheKeyID third = - RasterCacheKeyID({bar, foo}, RasterCacheKeyType::kLayerChildren); + RasterCacheKeyID second = RasterCacheKeyID(bar, RasterCacheKeyType::kLayer); + std::size_t first_hash = first.GetHash(); + std::size_t second_hash = second.GetHash(); - ASSERT_EQ(first.GetHash(), HashIds({foo}, RasterCacheKeyType::kLayer)); - ASSERT_EQ(second.GetHash(), - HashIds({foo, bar}, RasterCacheKeyType::kLayerChildren)); - ASSERT_EQ(third.GetHash(), - HashIds({bar, foo}, RasterCacheKeyType::kLayerChildren)); + ASSERT_EQ(first_hash, fml::HashCombine(foo, RasterCacheKeyType::kLayer)); + ASSERT_EQ(second_hash, fml::HashCombine(bar, RasterCacheKeyType::kLayer)); + + RasterCacheKeyID third = + RasterCacheKeyID({first, second}, RasterCacheKeyType::kLayerChildren); + RasterCacheKeyID fourth = + RasterCacheKeyID({second, first}, RasterCacheKeyType::kLayerChildren); + std::size_t third_hash = third.GetHash(); + std::size_t fourth_hash = fourth.GetHash(); + + ASSERT_EQ(third_hash, fml::HashCombine(RasterCacheKeyID::kDefaultUniqueID, + RasterCacheKeyType::kLayerChildren, + first.GetHash(), second.GetHash())); + ASSERT_EQ(fourth_hash, fml::HashCombine(RasterCacheKeyID::kDefaultUniqueID, + RasterCacheKeyType::kLayerChildren, + second.GetHash(), first.GetHash())); + + // Verify that the cached hash code is correct. + ASSERT_EQ(first_hash, first.GetHash()); + ASSERT_EQ(second_hash, second.GetHash()); + ASSERT_EQ(third_hash, third.GetHash()); + ASSERT_EQ(fourth_hash, fourth.GetHash()); +} + +using RasterCacheTest = SkiaGPUObjectLayerTest; + +TEST_F(RasterCacheTest, RasterCacheKeyID_LayerChildrenIds) { + auto layer = std::make_shared(); + + const SkPath child_path = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f)); + auto mock_layer = std::make_shared(child_path); + layer->Add(mock_layer); + + auto display_list = GetSampleDisplayList(); + auto display_list_layer = std::make_shared( + SkPoint::Make(0.0f, 0.0f), + SkiaGPUObject(display_list, unref_queue()), false, false); + layer->Add(display_list_layer); + + auto ids = RasterCacheKeyID::LayerChildrenIds(layer.get()).value(); + std::vector expected_ids; + expected_ids.emplace_back( + RasterCacheKeyID(mock_layer->unique_id(), RasterCacheKeyType::kLayer)); + expected_ids.emplace_back(RasterCacheKeyID(display_list->unique_id(), + RasterCacheKeyType::kDisplayList)); + ASSERT_EQ(expected_ids[0], mock_layer->caching_key_id()); + ASSERT_EQ(expected_ids[1], display_list_layer->caching_key_id()); + ASSERT_EQ(ids, expected_ids); } } // namespace testing