Tweak the logic about generating 'RasterCacheKey' of the type 'kLayerChildren' (flutter/engine#32676)

This commit is contained in:
ColdPaleLight
2022-07-18 12:24:05 +08:00
committed by GitHub
parent 4dd89afeeb
commit 676e99c1f4
5 changed files with 118 additions and 49 deletions

View File

@@ -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<DisplayListRasterCacheItem> display_list_raster_cache_item_;

View File

@@ -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;

View File

@@ -5,22 +5,24 @@
#include "flutter/flow/raster_cache_key.h"
#include <optional>
#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<std::vector<uint64_t>> RasterCacheKeyID::LayerChildrenIds(
Layer* layer) {
std::optional<std::vector<RasterCacheKeyID>> 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<uint64_t> ids;
std::transform(children_layers.begin(), children_layers.end(),
std::back_inserter(ids),
[](auto& layer) -> uint64_t { return layer->unique_id(); });
std::vector<RasterCacheKeyID> ids;
std::transform(
children_layers.begin(), children_layers.end(), std::back_inserter(ids),
[](auto& layer) -> RasterCacheKeyID { return layer->caching_key_id(); });
return ids;
}

View File

@@ -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<uint64_t> ids, RasterCacheKeyType type)
: ids_(ids), type_(type) {}
RasterCacheKeyID(uint64_t unique_id, RasterCacheKeyType type)
: unique_id_(unique_id), type_(type) {}
const std::vector<uint64_t>& ids() const { return ids_; }
RasterCacheKeyID(std::vector<RasterCacheKeyID> 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<std::vector<uint64_t>> LayerChildrenIds(Layer* layer);
const std::vector<RasterCacheKeyID>& child_ids() const { return child_ids_; }
static std::optional<std::vector<RasterCacheKeyID>> 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<uint64_t> ids_;
const uint64_t unique_id_;
const RasterCacheKeyType type_;
const std::vector<RasterCacheKeyID> child_ids_;
mutable std::optional<std::size_t> 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 {

View File

@@ -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<uint64_t> 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<ContainerLayer>();
const SkPath child_path = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f));
auto mock_layer = std::make_shared<MockLayer>(child_path);
layer->Add(mock_layer);
auto display_list = GetSampleDisplayList();
auto display_list_layer = std::make_shared<DisplayListLayer>(
SkPoint::Make(0.0f, 0.0f),
SkiaGPUObject<DisplayList>(display_list, unref_queue()), false, false);
layer->Add(display_list_layer);
auto ids = RasterCacheKeyID::LayerChildrenIds(layer.get()).value();
std::vector<RasterCacheKeyID> 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