|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | | -#ifndef FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_ |
6 | | -#define FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_ |
| 5 | +#ifndef FLUTTER_FLOW_LAYERS_CACHEABLE_LAYER_H_ |
| 6 | +#define FLUTTER_FLOW_LAYERS_CACHEABLE_LAYER_H_ |
7 | 7 |
|
8 | 8 | #include <memory> |
9 | 9 | #include "flutter/flow/embedded_views.h" |
|
14 | 14 |
|
15 | 15 | namespace flutter { |
16 | 16 |
|
17 | | -class CacheableLayer : public Layer { |
| 17 | +class Cacheable { |
18 | 18 | public: |
| 19 | + Cacheable() = default; |
| 20 | + |
19 | 21 | enum class CacheType { kNone, kCurrent, kChildren }; |
20 | 22 |
|
21 | | - virtual void TryToPrepareRasterCache( |
| 23 | + class AutoCache { |
| 24 | + public: |
| 25 | + static AutoCache Create(Cacheable* cacheable, |
| 26 | + PrerollContext* context, |
| 27 | + const SkMatrix& matrix) { |
| 28 | + return AutoCache(cacheable, context, matrix); |
| 29 | + } |
| 30 | + |
| 31 | + ~AutoCache() { |
| 32 | + cacheable_entry_->num_child_entries = |
| 33 | + context_->raster_cached_entries.size() - current_index_; |
| 34 | + // Get current layer's cache type |
| 35 | + auto cache_type = layer_->NeedCaching(context_, matrix_); |
| 36 | + // we should modify some parmas of the entry, like need_cache or matrix |
| 37 | + layer_->ConfigCacheType(cacheable_entry_, cache_type); |
| 38 | + cacheable_entry_->has_platform_view = context_->has_platform_view; |
| 39 | + cacheable_entry_->has_texture_layer = context_->has_texture_layer; |
| 40 | + } |
| 41 | + |
| 42 | + private: |
| 43 | + AutoCache(Cacheable* cacheable, |
| 44 | + PrerollContext* context, |
| 45 | + const SkMatrix& matrix) |
| 46 | + : layer_(cacheable), context_(context), matrix_(matrix) { |
| 47 | + cacheable_entry_ = |
| 48 | + context_->raster_cached_entries |
| 49 | + .emplace_back(RasterCacheableEntry::MarkLayerCacheable( |
| 50 | + layer_, *context_, matrix_)) |
| 51 | + .get(); |
| 52 | + current_index_ = context_->raster_cached_entries.size(); |
| 53 | + } |
| 54 | + |
| 55 | + int current_index_; |
| 56 | + RasterCacheableEntry* cacheable_entry_ = nullptr; |
| 57 | + Cacheable* layer_ = nullptr; |
| 58 | + PrerollContext* context_ = nullptr; |
| 59 | + const SkMatrix& matrix_; |
| 60 | + }; |
| 61 | + |
| 62 | + void TryToPrepareRasterCache( |
| 63 | + Layer* layer, |
22 | 64 | PrerollContext* context, |
23 | 65 | const SkMatrix& matrix, |
24 | | - RasterCacheLayerStrategy strategy = RasterCacheLayerStrategy::kLayer) {} |
| 66 | + RasterCacheLayerStrategy strategy = RasterCacheLayerStrategy::kLayer) { |
| 67 | + if (context->raster_cache) { |
| 68 | + if (!context->has_platform_view && !context->has_texture_layer && |
| 69 | + SkRect::Intersects(context->cull_rect, layer->paint_bounds())) { |
| 70 | + context->raster_cache->Prepare(context, layer, matrix, strategy); |
| 71 | + } else { |
| 72 | + // Don't evict raster cache entry during partial repaint |
| 73 | + context->raster_cache->Touch(layer, matrix, strategy); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + virtual Layer* asLayer() = 0; |
25 | 79 |
|
26 | 80 | virtual CacheType NeedCaching(PrerollContext* context, |
27 | 81 | const SkMatrix& ctm) = 0; |
28 | 82 |
|
29 | | - virtual ~CacheableLayer() = default; |
| 83 | + // Usually, we have this case to do: |
| 84 | + // 1. CacheType::kNone, which mean we don't need to cache this layer, so we |
| 85 | + // set the entry's need_caching to false |
| 86 | + // 2. CacheType::kChildren, which mean we need to cache the layer's children, |
| 87 | + // so we mark children need cache |
| 88 | + virtual void ConfigCacheType(RasterCacheableEntry* entry, CacheType type) { |
| 89 | + if (type == Cacheable::CacheType::kNone) { |
| 90 | + entry->need_caching = false; |
| 91 | + } else if (type == Cacheable::CacheType::kChildren) { |
| 92 | + // Replace Cacheable child |
| 93 | + entry->MarkLayerChildrenNeedCached(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + virtual ~Cacheable() = default; |
30 | 98 | }; |
31 | 99 |
|
32 | 100 | } // namespace flutter |
33 | 101 |
|
34 | | -#endif // FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_ |
| 102 | +#endif // FLUTTER_FLOW_LAYERS_CACHEABLE_LAYER_H_ |
0 commit comments