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_LAYER_RASTER_CACHE_ITEM_H_
6#define FLUTTER_FLOW_LAYER_RASTER_CACHE_ITEM_H_
7
8#include <memory>
9#include <optional>
10
11#include "flutter/flow/raster_cache_item.h"
12
13namespace flutter {
14
15class LayerRasterCacheItem : public RasterCacheItem {
16 public:
17 explicit LayerRasterCacheItem(Layer* layer,
18 int layer_cached_threshold = 1,
19 bool can_cache_children = false);
20
21 /**
22 * @brief Create a LayerRasterCacheItem, connect a layer and manage the
23 * Layer's raster cache
24 *
25 * @param layer_cache_threshold after how many frames to start trying to
26 * cache the layer self
27 * @param can_cache_children the layer can do a cache for his children
28 */
29 static std::unique_ptr<LayerRasterCacheItem>
30 Make(Layer*, int layer_cache_threshold, bool can_cache_children = false);
31
32 std::optional<RasterCacheKeyID> GetId() const override;
33
34 void PrerollSetup(PrerollContext* context, const SkMatrix& matrix) override;
35
36 void PrerollFinalize(PrerollContext* context,
37 const SkMatrix& matrix) override;
38
39 bool Draw(const PaintContext& context, const DlPaint* paint) const override;
40
41 bool Draw(const PaintContext& context,
42 DlCanvas* canvas,
43 const DlPaint* paint) const override;
44
45 bool TryToPrepareRasterCache(const PaintContext& context,
46 bool parent_cached = false) const override;
47
48 void MarkCacheChildren() { can_cache_children_ = true; }
49
50 void MarkNotCacheChildren() { can_cache_children_ = false; }
51
52 bool IsCacheChildren() const { return cache_state_ == CacheState::kChildren; }
53
54 protected:
55 const SkRect* GetPaintBoundsFromLayer() const;
56
57 Layer* layer_;
58
59 // The id for cache the layer's children.
60 std::optional<RasterCacheKeyID> layer_children_id_;
61
62 int layer_cached_threshold_ = 1;
63
64 // if the layer's children can be directly cache, set the param is true;
65 bool can_cache_children_ = false;
66
67 mutable int num_cache_attempts_ = 1;
68};
69
70} // namespace flutter
71
72#endif // FLUTTER_FLOW_LAYER_RASTER_CACHE_ITEM_H_
73

source code of flutter_engine/flutter/flow/layers/layer_raster_cache_item.h