Jim Graham 8cbaba6a03 Lazily allocate RasterCacheItems only when caching is enabled (flutter/engine#45211)
Fixes https://github.com/flutter/flutter/issues/133377

The default allocation of RasterCacheItems in the layer tree was showing up in profiles of apps running on Impeller which don't actually use the raster cache.

In order to eliminate the overhead of those allocations, RasterCacheItems are now lazily allocated only when the layers encounter an actual raster_cache during the Preroll phase.
2023-08-31 20:32:06 +00:00

45 lines
1.3 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/flow/layers/cacheable_layer.h"
namespace flutter {
AutoCache::AutoCache(CacheableLayer& cacheable_layer,
PrerollContext* context,
bool caching_enabled) {
if (context->raster_cache && caching_enabled) {
raster_cache_item_ = cacheable_layer.realize_raster_cache_item();
if (raster_cache_item_) {
context_ = context;
matrix_ = context->state_stack.transform_3x3();
raster_cache_item_->PrerollSetup(context_, matrix_);
}
} else {
cacheable_layer.disable_raster_cache_item();
}
}
AutoCache::~AutoCache() {
if (raster_cache_item_) {
raster_cache_item_->PrerollFinalize(context_, matrix_);
}
}
RasterCacheItem* CacheableContainerLayer::realize_raster_cache_item() {
if (!layer_raster_cache_item_) {
layer_raster_cache_item_ = LayerRasterCacheItem::Make(
this, layer_cache_threshold_, can_cache_children_);
}
return layer_raster_cache_item_.get();
}
void CacheableContainerLayer::disable_raster_cache_item() {
if (layer_raster_cache_item_) {
layer_raster_cache_item_->reset_cache_state();
}
}
} // namespace flutter