Flutter Engine
The Flutter Engine
raster_cache_key.h
Go to the documentation of this file.
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#ifndef FLUTTER_FLOW_RASTER_CACHE_KEY_H_
6#define FLUTTER_FLOW_RASTER_CACHE_KEY_H_
7
8#if !SLIMPELLER
9
10#include <optional>
11#include <unordered_map>
12#include <utility>
13#include <vector>
14
15#include "flutter/fml/hash_combine.h"
16#include "flutter/fml/logging.h"
18
19namespace flutter {
20
21class Layer;
22
24
26 public:
27 static constexpr uint64_t kDefaultUniqueID = 0;
28
30 : unique_id_(unique_id), type_(type) {}
31
32 RasterCacheKeyID(std::vector<RasterCacheKeyID> child_ids,
34 : unique_id_(kDefaultUniqueID),
35 type_(type),
36 child_ids_(std::move(child_ids)) {}
37
38 uint64_t unique_id() const { return unique_id_; }
39
40 RasterCacheKeyType type() const { return type_; }
41
42 const std::vector<RasterCacheKeyID>& child_ids() const { return child_ids_; }
43
44 static std::optional<std::vector<RasterCacheKeyID>> LayerChildrenIds(
45 const Layer* layer);
46
47 std::size_t GetHash() const {
48 if (cached_hash_) {
49 return cached_hash_.value();
50 }
51 std::size_t seed = fml::HashCombine();
52 fml::HashCombineSeed(seed, unique_id_);
53 fml::HashCombineSeed(seed, type_);
54 for (auto& child_id : child_ids_) {
55 fml::HashCombineSeed(seed, child_id.GetHash());
56 }
57 cached_hash_ = seed;
58 return seed;
59 }
60
61 bool operator==(const RasterCacheKeyID& other) const {
62 return unique_id_ == other.unique_id_ && type_ == other.type_ &&
63 GetHash() == other.GetHash() && child_ids_ == other.child_ids_;
64 }
65
66 bool operator!=(const RasterCacheKeyID& other) const {
67 return !operator==(other);
68 }
69
70 private:
71 const uint64_t unique_id_;
72 const RasterCacheKeyType type_;
73 const std::vector<RasterCacheKeyID> child_ids_;
74 mutable std::optional<std::size_t> cached_hash_;
75};
76
78
80 public:
81 RasterCacheKey(uint64_t unique_id,
83 const SkMatrix& ctm)
84 : RasterCacheKey(RasterCacheKeyID(unique_id, type), ctm) {}
85
87 : id_(std::move(id)), matrix_(ctm) {
88 matrix_[SkMatrix::kMTransX] = 0;
89 matrix_[SkMatrix::kMTransY] = 0;
90 }
91
92 const RasterCacheKeyID& id() const { return id_; }
93 const SkMatrix& matrix() const { return matrix_; }
94
96 switch (id_.type()) {
102 }
103 }
104
105 struct Hash {
106 std::size_t operator()(RasterCacheKey const& key) const {
107 return key.id_.GetHash();
108 }
109 };
110
111 struct Equal {
112 constexpr bool operator()(const RasterCacheKey& lhs,
113 const RasterCacheKey& rhs) const {
114 return lhs.id_ == rhs.id_ && lhs.matrix_ == rhs.matrix_;
115 }
116 };
117
118 template <class Value>
119 using Map = std::unordered_map<RasterCacheKey, Value, Hash, Equal>;
120
121 private:
123
124 // ctm where only fractional (0-1) translations are preserved:
125 // matrix_ = ctm;
126 // matrix_[SkMatrix::kMTransX] = SkScalarFraction(ctm.getTranslateX());
127 // matrix_[SkMatrix::kMTransY] = SkScalarFraction(ctm.getTranslateY());
128 SkMatrix matrix_;
129};
130
131} // namespace flutter
132
133#endif // !SLIMPELLER
134
135#endif // FLUTTER_FLOW_RASTER_CACHE_KEY_H_
GLenum type
static constexpr int kMTransY
vertical translation
Definition: SkMatrix.h:358
static constexpr int kMTransX
horizontal translation
Definition: SkMatrix.h:355
static constexpr uint64_t kDefaultUniqueID
bool operator==(const RasterCacheKeyID &other) const
RasterCacheKeyID(std::vector< RasterCacheKeyID > child_ids, RasterCacheKeyType type)
const std::vector< RasterCacheKeyID > & child_ids() const
RasterCacheKeyType type() const
RasterCacheKeyID(uint64_t unique_id, RasterCacheKeyType type)
bool operator!=(const RasterCacheKeyID &other) const
std::size_t GetHash() const
uint64_t unique_id() const
static std::optional< std::vector< RasterCacheKeyID > > LayerChildrenIds(const Layer *layer)
const RasterCacheKeyID & id() const
const SkMatrix & matrix() const
std::unordered_map< RasterCacheKey, Value, Hash, Equal > Map
RasterCacheKey(RasterCacheKeyID id, const SkMatrix &ctm)
RasterCacheKeyKind kind() const
RasterCacheKey(uint64_t unique_id, RasterCacheKeyType type, const SkMatrix &ctm)
constexpr std::size_t HashCombine()
Definition: hash_combine.h:25
constexpr void HashCombineSeed(std::size_t &seed, Type arg)
Definition: hash_combine.h:13
Definition: ref_ptr.h:256
constexpr bool operator()(const RasterCacheKey &lhs, const RasterCacheKey &rhs) const
std::size_t operator()(RasterCacheKey const &key) const