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 #include <unordered_map> 9 #include "flutter/flow/matrix_decomposition.h" 10 #include "flutter/fml/logging.h" 11 12 namespace flutter { 13 14 template <typename ID> 15 class RasterCacheKey { 16 public: RasterCacheKey(ID id,const SkMatrix & ctm)17 RasterCacheKey(ID id, const SkMatrix& ctm) : id_(id), matrix_(ctm) { 18 matrix_[SkMatrix::kMTransX] = SkScalarFraction(ctm.getTranslateX()); 19 matrix_[SkMatrix::kMTransY] = SkScalarFraction(ctm.getTranslateY()); 20 #ifndef SUPPORT_FRACTIONAL_TRANSLATION 21 FML_DCHECK(matrix_.getTranslateX() == 0 && matrix_.getTranslateY() == 0); 22 #endif 23 } 24 id()25 ID id() const { return id_; } matrix()26 const SkMatrix& matrix() const { return matrix_; } 27 28 struct Hash { operatorHash29 uint32_t operator()(RasterCacheKey const& key) const { 30 return std::hash<ID>()(key.id_); 31 } 32 }; 33 34 struct Equal { operatorEqual35 constexpr bool operator()(const RasterCacheKey& lhs, 36 const RasterCacheKey& rhs) const { 37 return lhs.id_ == rhs.id_ && lhs.matrix_ == rhs.matrix_; 38 } 39 }; 40 41 template <class Value> 42 using Map = std::unordered_map<RasterCacheKey, Value, Hash, Equal>; 43 44 private: 45 ID id_; 46 47 // ctm where only fractional (0-1) translations are preserved: 48 // matrix_ = ctm; 49 // matrix_[SkMatrix::kMTransX] = SkScalarFraction(ctm.getTranslateX()); 50 // matrix_[SkMatrix::kMTransY] = SkScalarFraction(ctm.getTranslateY()); 51 SkMatrix matrix_; 52 }; 53 54 // The ID is the uint32_t picture uniqueID 55 using PictureRasterCacheKey = RasterCacheKey<uint32_t>; 56 57 class Layer; 58 59 // The ID is the uint64_t layer unique_id 60 using LayerRasterCacheKey = RasterCacheKey<uint64_t>; 61 62 } // namespace flutter 63 64 #endif // FLUTTER_FLOW_RASTER_CACHE_KEY_H_ 65