Flutter Engine
 
Loading...
Searching...
No Matches
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
17#include "flutter/fml/logging.h"
18#include "third_party/skia/include/core/SkMatrix.h"
19
20namespace flutter {
21
22class Layer;
23
25
27 public:
28 static constexpr uint64_t kDefaultUniqueID = 0;
29
31 : unique_id_(unique_id), type_(type) {}
32
33 RasterCacheKeyID(std::vector<RasterCacheKeyID> child_ids,
35 : unique_id_(kDefaultUniqueID),
36 type_(type),
37 child_ids_(std::move(child_ids)) {}
38
39 uint64_t unique_id() const { return unique_id_; }
40
41 RasterCacheKeyType type() const { return type_; }
42
43 const std::vector<RasterCacheKeyID>& child_ids() const { return child_ids_; }
44
45 static std::optional<std::vector<RasterCacheKeyID>> LayerChildrenIds(
46 const Layer* layer);
47
48 std::size_t GetHash() const {
49 if (cached_hash_) {
50 return cached_hash_.value();
51 }
52 std::size_t seed = fml::HashCombine();
53 fml::HashCombineSeed(seed, unique_id_);
54 fml::HashCombineSeed(seed, type_);
55 for (auto& child_id : child_ids_) {
56 fml::HashCombineSeed(seed, child_id.GetHash());
57 }
58 cached_hash_ = seed;
59 return seed;
60 }
61
62 bool operator==(const RasterCacheKeyID& other) const {
63 return unique_id_ == other.unique_id_ && type_ == other.type_ &&
64 GetHash() == other.GetHash() && child_ids_ == other.child_ids_;
65 }
66
67 private:
68 const uint64_t unique_id_;
69 const RasterCacheKeyType type_;
70 const std::vector<RasterCacheKeyID> child_ids_;
71 mutable std::optional<std::size_t> cached_hash_;
72};
73
75
77 public:
78 RasterCacheKey(uint64_t unique_id,
80 const SkMatrix& ctm)
81 : RasterCacheKey(RasterCacheKeyID(unique_id, type), ctm) {}
82
83 RasterCacheKey(RasterCacheKeyID id, const SkMatrix& ctm)
84 : id_(std::move(id)), matrix_(ctm) {
85 matrix_[SkMatrix::kMTransX] = 0;
86 matrix_[SkMatrix::kMTransY] = 0;
87 }
88
89 const RasterCacheKeyID& id() const { return id_; }
90 const SkMatrix& matrix() const { return matrix_; }
91
101
102 struct Hash {
103 std::size_t operator()(RasterCacheKey const& key) const {
104 return key.id_.GetHash();
105 }
106 };
107
108 struct Equal {
109 constexpr bool operator()(const RasterCacheKey& lhs,
110 const RasterCacheKey& rhs) const {
111 return lhs.id_ == rhs.id_ && lhs.matrix_ == rhs.matrix_;
112 }
113 };
114
115 template <class Value>
116 using Map = std::unordered_map<RasterCacheKey, Value, Hash, Equal>;
117
118 private:
120
121 // ctm where only fractional (0-1) translations are preserved:
122 // matrix_ = ctm;
123 // matrix_[SkMatrix::kMTransX] = SkScalarFraction(ctm.getTranslateX());
124 // matrix_[SkMatrix::kMTransY] = SkScalarFraction(ctm.getTranslateY());
125 SkMatrix matrix_;
126};
127
128} // namespace flutter
129
130#endif // !SLIMPELLER
131
132#endif // FLUTTER_FLOW_RASTER_CACHE_KEY_H_
GLenum type
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)
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)
std::size_t GetHash() const
static std::optional< std::vector< RasterCacheKeyID > > LayerChildrenIds(const Layer *layer)
constexpr std::size_t HashCombine()
constexpr void HashCombineSeed(std::size_t &seed, const Type &arg)
Definition ref_ptr.h:261
constexpr bool operator()(const RasterCacheKey &lhs, const RasterCacheKey &rhs) const
std::size_t operator()(RasterCacheKey const &key) const