Skip to content

Commit

Permalink
Add CacheableLayer, Prepare RasterCache use local PrerollContext
Browse files Browse the repository at this point in the history
  • Loading branch information
JsouLiang committed Mar 16, 2022
1 parent 5c59e73 commit 1658c1a
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 63 deletions.
2 changes: 2 additions & 0 deletions flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ source_set("flow") {
"instrumentation.h",
"layers/backdrop_filter_layer.cc",
"layers/backdrop_filter_layer.h",
"layers/cacheable_layer.cc",
"layers/cacheable_layer.h",
"layers/clip_path_layer.cc",
"layers/clip_path_layer.h",
"layers/clip_rect_layer.cc",
Expand Down
5 changes: 5 additions & 0 deletions flow/layers/cacheable_layer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 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.

namespace flutter {} // namespace flutter
34 changes: 34 additions & 0 deletions flow/layers/cacheable_layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.

#ifndef FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_
#define FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_

#include <memory>
#include "flutter/flow/embedded_views.h"
#include "flutter/flow/layers/layer.h"
#include "flutter/flow/raster_cache.h"
#include "include/core/SkColor.h"
#include "include/core/SkMatrix.h"

namespace flutter {

class CacheableLayer : public Layer {
public:
enum class CacheType { kNone, kCurrent, kChildren };

bool IsCacheable() { return true; }

virtual void TryToPrepareRasterCache(PrerollContext* context,
const SkMatrix& matrix) {}

virtual CacheType NeedCaching(PrerollContext* context,
const SkMatrix& ctm) = 0;

virtual ~CacheableLayer() = default;
};

} // namespace flutter

#endif // FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_
26 changes: 12 additions & 14 deletions flow/layers/container_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,27 +189,21 @@ void ContainerLayer::PaintChildren(PaintContext& context) const {
}

void ContainerLayer::TryToPrepareRasterCache(PrerollContext* context,
const SkMatrix& ctm) {
if (!context->has_platform_view && !context->has_texture_layer &&
context->raster_cache &&
SkRect::Intersects(context->cull_rect, this->paint_bounds())) {
context->raster_cache->Prepare(context, this, ctm);
} else if (context->raster_cache) {
// Don't evict raster cache entry during partial repaint
context->raster_cache->Touch(this, ctm);
}
const SkMatrix& matrix) {
TryToPrepareRasterCache(context, this, matrix);
}

// TODO(JsouLiang): will remove this method
void ContainerLayer::TryToPrepareRasterCache(PrerollContext* context,
Layer* layer,
const SkMatrix& matrix) {
const SkMatrix& ctm) {
if (!context->has_platform_view && !context->has_texture_layer &&
context->raster_cache &&
SkRect::Intersects(context->cull_rect, layer->paint_bounds())) {
context->raster_cache->Prepare(context, layer, matrix);
context->raster_cache->Prepare(context, layer, ctm);
} else if (context->raster_cache) {
// Don't evict raster cache entry during partial repaint
context->raster_cache->Touch(layer, matrix);
context->raster_cache->Touch(layer, ctm);
}
}

Expand Down Expand Up @@ -258,10 +252,14 @@ ContainerLayer* MergedContainerLayer::GetChildContainer() const {
return static_cast<ContainerLayer*>(layers()[0].get());
}

Layer* MergedContainerLayer::GetCacheableChild() const {
CacheableLayer* MergedContainerLayer::GetCacheableChild() const {
ContainerLayer* child_container = GetChildContainer();
if (child_container->layers().size() == 1) {
return child_container->layers()[0].get();
auto* child = child_container->layers()[0].get();
if (child->IsCacheable()) {
return static_cast<CacheableLayer*>(child);
}
return nullptr;
}

return child_container;
Expand Down
18 changes: 11 additions & 7 deletions flow/layers/container_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

#include <vector>

#include "flutter/flow/layers/layer.h"

#include "flutter/flow/layers/cacheable_layer.h"
namespace flutter {

class ContainerLayer : public Layer {
class ContainerLayer : public CacheableLayer {
public:
ContainerLayer();

Expand All @@ -20,9 +19,6 @@ class ContainerLayer : public Layer {

virtual void Add(std::shared_ptr<Layer> layer);

void TryToPrepareRasterCache(PrerollContext* context,
const SkMatrix& ctm) override;

void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext& context) const override;

Expand All @@ -31,6 +27,14 @@ class ContainerLayer : public Layer {
virtual void DiffChildren(DiffContext* context,
const ContainerLayer* old_layer);

CacheableLayer::CacheType NeedCaching(PrerollContext* context,
const SkMatrix& ctm) override {
return CacheableLayer::CacheType::kNone;
}

void TryToPrepareRasterCache(PrerollContext* context,
const SkMatrix& matrix) override;

protected:
void PrerollChildren(PrerollContext* context,
const SkMatrix& child_matrix,
Expand Down Expand Up @@ -129,7 +133,7 @@ class MergedContainerLayer : public ContainerLayer {
* @see GetChildContainer()
* @return the best candidate Layer for caching the children
*/
Layer* GetCacheableChild() const;
CacheableLayer* GetCacheableChild() const;

private:
FML_DISALLOW_COPY_AND_ASSIGN(MergedContainerLayer);
Expand Down
20 changes: 11 additions & 9 deletions flow/layers/display_list_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "flutter/flow/layers/display_list_layer.h"

#include "flutter/display_list/display_list_builder.h"
#include "flutter/flow/layers/layer.h"

namespace flutter {

Expand Down Expand Up @@ -92,28 +91,31 @@ bool DisplayListLayer::Compare(DiffContext::Statistics& statistics,
void DisplayListLayer::Preroll(PrerollContext* context,
const SkMatrix& matrix) {
TRACE_EVENT0("flutter", "DisplayListLayer::Preroll");
context->raster_cached_entries.emplace_back(RasterCacheEntry(this));
context->raster_cached_entries.emplace_back(
RasterCacheEntry(this, *context, matrix));

auto& cache_entry = context->raster_cached_entries.back();
// display layer is a leaf node
cache_entry.num_child_entries = 0;
cache_entry.need_caching = IsNeedCached(context, matrix);
auto cached_type = NeedCaching(context, matrix);
cache_entry.need_caching = cached_type == CacheableLayer::CacheType::kCurrent;
}

bool DisplayListLayer::IsNeedCached(PrerollContext* context,
const SkMatrix& ctm) {
CacheableLayer::CacheType DisplayListLayer::NeedCaching(PrerollContext* context,
const SkMatrix& ctm) {
DisplayList* disp_list = display_list();

SkRect bounds = disp_list->bounds().makeOffset(offset_.x(), offset_.y());

if (auto* cache = context->raster_cache) {
TRACE_EVENT0("flutter", "DisplayListLayer::RasterCache (Preroll)");
if (context->cull_rect.intersects(bounds)) {
return cache->ShouldBeCached(context, disp_list, is_complex_,
will_change_, ctm);
if (context->cull_rect.intersects(bounds) &&
cache->ShouldBeCached(context, disp_list, is_complex_, will_change_,
ctm)) {
return CacheableLayer::CacheType::kCurrent;
}
}
return false;
return CacheableLayer::CacheType::kNone;
}

void DisplayListLayer::TryToPrepareRasterCache(PrerollContext* context,
Expand Down
7 changes: 4 additions & 3 deletions flow/layers/display_list_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#define FLUTTER_FLOW_LAYERS_DISPLAY_LIST_LAYER_H_

#include "flutter/display_list/display_list.h"
#include "flutter/flow/layers/layer.h"
#include "flutter/flow/layers/cacheable_layer.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/skia_gpu_object.h"
#include "include/core/SkMatrix.h"

namespace flutter {

class DisplayListLayer : public Layer {
class DisplayListLayer : public CacheableLayer {
public:
static constexpr size_t kMaxBytesToCompare = 10000;

Expand All @@ -34,7 +34,8 @@ class DisplayListLayer : public Layer {
return this;
}

bool IsNeedCached(PrerollContext* context, const SkMatrix& ctm) override;
CacheableLayer::CacheType NeedCaching(PrerollContext* context,
const SkMatrix& ctm) override;

void TryToPrepareRasterCache(PrerollContext* context,
const SkMatrix& ctm) override;
Expand Down
24 changes: 17 additions & 7 deletions flow/layers/image_filter_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "flutter/flow/layers/image_filter_layer.h"
#include "flutter/flow/layers/layer.h"

namespace flutter {

Expand Down Expand Up @@ -43,7 +44,8 @@ void ImageFilterLayer::Preroll(PrerollContext* context,
Layer::AutoPrerollSaveLayerState save =
Layer::AutoPrerollSaveLayerState::Create(context);

context->raster_cached_entries.emplace_back(RasterCacheEntry(this));
context->raster_cached_entries.emplace_back(
RasterCacheEntry(this, *context, matrix));
auto current_index = context->raster_cached_entries.size();

auto& cache_entry = context->raster_cached_entries.back();
Expand All @@ -53,7 +55,15 @@ void ImageFilterLayer::Preroll(PrerollContext* context,

cache_entry.num_child_entries =
context->raster_cached_entries.size() - current_index;
cache_entry.need_caching = IsNeedCached(context, matrix);
auto cache_type = NeedCaching(context, matrix);

cache_entry.need_caching = false;
if (cache_type == CacheableLayer::CacheType::kCurrent) {
cache_entry.layer = this;
} else if (cache_type == CacheableLayer::CacheType::kChildren) {
// Replace Cacheable child
cache_entry.UpdateRasterCacheEntry(GetCacheableChild(), *context, matrix);
}

if (!filter_) {
set_paint_bounds(child_bounds);
Expand All @@ -68,10 +78,10 @@ void ImageFilterLayer::Preroll(PrerollContext* context,
set_paint_bounds(child_bounds);
}

bool ImageFilterLayer::IsNeedCached(PrerollContext* context,
const SkMatrix& ctm) {
CacheableLayer::CacheType ImageFilterLayer::NeedCaching(PrerollContext* context,
const SkMatrix& ctm) {
if (render_count_ >= kMinimumRendersBeforeCachingFilterLayer) {
return true;
return CacheableLayer::CacheType::kCurrent;
}
transformed_filter_ = nullptr;
// This ImageFilterLayer is not yet considered stable so we
Expand All @@ -93,9 +103,9 @@ bool ImageFilterLayer::IsNeedCached(PrerollContext* context,
// stable between frames and also avoiding a rendering surface
// switch during the Paint phase even if they are not stable.
// This benefit is seen most during animations.
return true;
return CacheableLayer::CacheType::kChildren;
}
return false;
return CacheableLayer::CacheType::kNone;
}

void ImageFilterLayer::Paint(PaintContext& context) const {
Expand Down
4 changes: 3 additions & 1 deletion flow/layers/image_filter_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef FLUTTER_FLOW_LAYERS_IMAGE_FILTER_LAYER_H_
#define FLUTTER_FLOW_LAYERS_IMAGE_FILTER_LAYER_H_

#include "flutter/flow/layers/cacheable_layer.h"
#include "flutter/flow/layers/container_layer.h"
#include "third_party/skia/include/core/SkImageFilter.h"

Expand All @@ -20,7 +21,8 @@ class ImageFilterLayer : public MergedContainerLayer {

void Paint(PaintContext& context) const override;

bool IsNeedCached(PrerollContext* context, const SkMatrix& ctm) override;
CacheableLayer::CacheType NeedCaching(PrerollContext* context,
const SkMatrix& ctm) override;

private:
// The ImageFilterLayer might cache the filtered output of this layer
Expand Down
4 changes: 1 addition & 3 deletions flow/layers/layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "flutter/flow/layers/layer.h"
#include <algorithm>

#include "flutter/flow/diff_context.h"
#include "flutter/flow/paint_utils.h"
#include "flutter/flow/raster_cache.h"
#include "include/core/SkMatrix.h"
Expand Down Expand Up @@ -32,9 +33,6 @@ uint64_t Layer::NextUniqueID() {

void Layer::Preroll(PrerollContext* context, const SkMatrix& matrix) {}

void Layer::TryToPrepareRasterCache(PrerollContext* context,
const SkMatrix& ctm) {}

Layer::AutoPrerollSaveLayerState::AutoPrerollSaveLayerState(
PrerollContext* preroll_context,
bool save_layer_is_active,
Expand Down
Loading

0 comments on commit 1658c1a

Please sign in to comment.