Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions flow/compositor_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

namespace flutter {

CompositorContext::CompositorContext(Delegate& delegate)
: delegate_(delegate),
raster_time_(delegate.GetFrameBudget()),
ui_time_(delegate.GetFrameBudget()) {}
CompositorContext::CompositorContext(fml::Milliseconds frame_budget)
: raster_time_(frame_budget), ui_time_(frame_budget) {}

CompositorContext::~CompositorContext() = default;

Expand All @@ -25,11 +23,8 @@ void CompositorContext::BeginFrame(ScopedFrame& frame,
}

void CompositorContext::EndFrame(ScopedFrame& frame,
bool enable_instrumentation,
size_t freed_hint) {
freed_hint += raster_cache_.SweepAfterFrame();
delegate_.OnCompositorEndFrame(freed_hint);

bool enable_instrumentation) {
raster_cache_.SweepAfterFrame();
if (enable_instrumentation) {
raster_time_.Stop();
}
Expand Down Expand Up @@ -69,7 +64,7 @@ CompositorContext::ScopedFrame::ScopedFrame(
}

CompositorContext::ScopedFrame::~ScopedFrame() {
context_.EndFrame(*this, instrumentation_enabled_, uncached_external_size_);
context_.EndFrame(*this, instrumentation_enabled_);
}

RasterStatus CompositorContext::ScopedFrame::Raster(
Expand Down
22 changes: 2 additions & 20 deletions flow/compositor_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ enum class RasterStatus {

class CompositorContext {
public:
class Delegate {
public:
/// Called at the end of a frame with approximately how many bytes mightbe
/// freed if a GC ran now.
///
/// This method is called from the raster task runner.
virtual void OnCompositorEndFrame(size_t freed_hint) = 0;

/// Time limit for a smooth frame. See `Engine::GetDisplayRefreshRate`.
virtual fml::Milliseconds GetFrameBudget() = 0;
};

class ScopedFrame {
public:
ScopedFrame(CompositorContext& context,
Expand Down Expand Up @@ -79,8 +67,6 @@ class CompositorContext {
virtual RasterStatus Raster(LayerTree& layer_tree,
bool ignore_raster_cache);

void add_external_size(size_t size) { uncached_external_size_ += size; }

private:
CompositorContext& context_;
GrDirectContext* gr_context_;
Expand All @@ -90,12 +76,11 @@ class CompositorContext {
const bool instrumentation_enabled_;
const bool surface_supports_readback_;
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
size_t uncached_external_size_ = 0;

FML_DISALLOW_COPY_AND_ASSIGN(ScopedFrame);
};

explicit CompositorContext(Delegate& delegate);
CompositorContext(fml::Milliseconds frame_budget = fml::kDefaultFrameBudget);

virtual ~CompositorContext();

Expand Down Expand Up @@ -123,7 +108,6 @@ class CompositorContext {
Stopwatch& ui_time() { return ui_time_; }

private:
Delegate& delegate_;
RasterCache raster_cache_;
TextureRegistry texture_registry_;
Counter frame_count_;
Expand All @@ -132,9 +116,7 @@ class CompositorContext {

void BeginFrame(ScopedFrame& frame, bool enable_instrumentation);

void EndFrame(ScopedFrame& frame,
bool enable_instrumentation,
size_t freed_hint);
void EndFrame(ScopedFrame& frame, bool enable_instrumentation);

FML_DISALLOW_COPY_AND_ASSIGN(CompositorContext);
};
Expand Down
5 changes: 2 additions & 3 deletions flow/layers/layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

namespace flutter {

Layer::Layer(size_t external_size)
Layer::Layer()
: paint_bounds_(SkRect::MakeEmpty()),
unique_id_(NextUniqueID()),
needs_system_composite_(false),
external_size_(external_size) {}
needs_system_composite_(false) {}

Layer::~Layer() = default;

Expand Down
6 changes: 1 addition & 5 deletions flow/layers/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ struct PrerollContext {
// Informs whether a layer needs to be system composited.
bool child_scene_layer_exists_below = false;
#endif
size_t uncached_external_size = 0;
};

// Represents a single composited layer. Created on the UI thread but then
// subquently used on the Rasterizer thread.
class Layer {
public:
Layer(size_t external_size = 0);
Layer();
virtual ~Layer();

virtual void Preroll(PrerollContext* context, const SkMatrix& matrix);
Expand Down Expand Up @@ -179,8 +178,6 @@ class Layer {

uint64_t unique_id() const { return unique_id_; }

size_t external_size() const { return external_size_; }

protected:
#if defined(LEGACY_FUCHSIA_EMBEDDER)
bool child_layer_exists_below_ = false;
Expand All @@ -190,7 +187,6 @@ class Layer {
SkRect paint_bounds_;
uint64_t unique_id_;
bool needs_system_composite_;
size_t external_size_ = 0;

static uint64_t NextUniqueID();

Expand Down
1 change: 0 additions & 1 deletion flow/layers/layer_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ bool LayerTree::Preroll(CompositorContext::ScopedFrame& frame,
device_pixel_ratio_};

root_layer_->Preroll(&context, frame.root_surface_transformation());
frame.add_external_size(context.uncached_external_size);
return context.surface_needs_readback;
}

Expand Down
17 changes: 2 additions & 15 deletions flow/layers/layer_tree_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
namespace flutter {
namespace testing {

class LayerTreeTest : public CanvasTest, public CompositorContext::Delegate {
class LayerTreeTest : public CanvasTest {
public:
LayerTreeTest()
: layer_tree_(SkISize::Make(64, 64), 1.0f),
compositor_context_(*this),
compositor_context_(fml::kDefaultFrameBudget),
root_transform_(SkMatrix::Translate(1.0f, 1.0f)),
scoped_frame_(compositor_context_.AcquireFrame(nullptr,
&mock_canvas(),
Expand All @@ -33,24 +33,11 @@ class LayerTreeTest : public CanvasTest, public CompositorContext::Delegate {
CompositorContext::ScopedFrame& frame() { return *scoped_frame_.get(); }
const SkMatrix& root_transform() { return root_transform_; }

// |CompositorContext::Delegate|
void OnCompositorEndFrame(size_t freed_hint) override {
last_freed_hint_ = freed_hint;
}

// |CompositorContext::Delegate|
fml::Milliseconds GetFrameBudget() override {
return fml::kDefaultFrameBudget;
}

size_t last_freed_hint() { return last_freed_hint_; }

private:
LayerTree layer_tree_;
CompositorContext compositor_context_;
SkMatrix root_transform_;
std::unique_ptr<CompositorContext::ScopedFrame> scoped_frame_;
size_t last_freed_hint_ = 0;
};

TEST_F(LayerTreeTest, PaintingEmptyLayerDies) {
Expand Down
16 changes: 4 additions & 12 deletions flow/layers/picture_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ namespace flutter {
PictureLayer::PictureLayer(const SkPoint& offset,
SkiaGPUObject<SkPicture> picture,
bool is_complex,
bool will_change,
size_t external_size)
: Layer(external_size),
offset_(offset),
bool will_change)
: offset_(offset),
picture_(std::move(picture)),
is_complex_(is_complex),
will_change_(will_change) {}
Expand All @@ -28,7 +26,6 @@ void PictureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {

SkPicture* sk_picture = picture();

bool cached = false;
if (auto* cache = context->raster_cache) {
TRACE_EVENT0("flutter", "PictureLayer::RasterCache (Preroll)");

Expand All @@ -37,13 +34,8 @@ void PictureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
#ifndef SUPPORT_FRACTIONAL_TRANSLATION
ctm = RasterCache::GetIntegralTransCTM(ctm);
#endif
cached = cache->Prepare(context->gr_context, sk_picture, ctm,
context->dst_color_space, is_complex_, will_change_,
external_size());
}

if (!cached) {
context->uncached_external_size += external_size();
cache->Prepare(context->gr_context, sk_picture, ctm,
context->dst_color_space, is_complex_, will_change_);
}

SkRect bounds = sk_picture->cullRect().makeOffset(offset_.x(), offset_.y());
Expand Down
3 changes: 1 addition & 2 deletions flow/layers/picture_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class PictureLayer : public Layer {
PictureLayer(const SkPoint& offset,
SkiaGPUObject<SkPicture> picture,
bool is_complex,
bool will_change,
size_t external_size);
bool will_change);

SkPicture* picture() const { return picture_.get().get(); }

Expand Down
15 changes: 5 additions & 10 deletions flow/layers/picture_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using PictureLayerTest = SkiaGPUObjectLayerTest;
TEST_F(PictureLayerTest, PaintBeforePrerollInvalidPictureDies) {
const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f);
auto layer = std::make_shared<PictureLayer>(
layer_offset, SkiaGPUObject<SkPicture>(), false, false, 0);
layer_offset, SkiaGPUObject<SkPicture>(), false, false);

EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
"picture_\\.get\\(\\)");
Expand All @@ -35,8 +35,7 @@ TEST_F(PictureLayerTest, PaintBeforePreollDies) {
const SkRect picture_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f);
auto mock_picture = SkPicture::MakePlaceholder(picture_bounds);
auto layer = std::make_shared<PictureLayer>(
layer_offset, SkiaGPUObject(mock_picture, unref_queue()), false, false,
0);
layer_offset, SkiaGPUObject(mock_picture, unref_queue()), false, false);

EXPECT_EQ(layer->paint_bounds(), SkRect::MakeEmpty());
EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
Expand All @@ -48,8 +47,7 @@ TEST_F(PictureLayerTest, PaintingEmptyLayerDies) {
const SkRect picture_bounds = SkRect::MakeEmpty();
auto mock_picture = SkPicture::MakePlaceholder(picture_bounds);
auto layer = std::make_shared<PictureLayer>(
layer_offset, SkiaGPUObject(mock_picture, unref_queue()), false, false,
0);
layer_offset, SkiaGPUObject(mock_picture, unref_queue()), false, false);

layer->Preroll(preroll_context(), SkMatrix());
EXPECT_EQ(layer->paint_bounds(), SkRect::MakeEmpty());
Expand All @@ -64,7 +62,7 @@ TEST_F(PictureLayerTest, PaintingEmptyLayerDies) {
TEST_F(PictureLayerTest, InvalidPictureDies) {
const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f);
auto layer = std::make_shared<PictureLayer>(
layer_offset, SkiaGPUObject<SkPicture>(), false, false, 0);
layer_offset, SkiaGPUObject<SkPicture>(), false, false);

// Crashes reading a nullptr.
EXPECT_DEATH_IF_SUPPORTED(layer->Preroll(preroll_context(), SkMatrix()), "");
Expand All @@ -77,10 +75,7 @@ TEST_F(PictureLayerTest, SimplePicture) {
const SkRect picture_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f);
auto mock_picture = SkPicture::MakePlaceholder(picture_bounds);
auto layer = std::make_shared<PictureLayer>(
layer_offset, SkiaGPUObject(mock_picture, unref_queue()), false, false,
1000);

EXPECT_EQ(layer->external_size(), 1000ul);
layer_offset, SkiaGPUObject(mock_picture, unref_queue()), false, false);

layer->Preroll(preroll_context(), SkMatrix());
EXPECT_EQ(layer->paint_bounds(),
Expand Down
12 changes: 4 additions & 8 deletions flow/raster_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ void RasterCache::Prepare(PrerollContext* context,
Entry& entry = layer_cache_[cache_key];
entry.access_count++;
entry.used_this_frame = true;
entry.external_size = layer->external_size();
if (!entry.image) {
entry.image = RasterizeLayer(context, layer, ctm, checkerboard_images_);
}
Expand Down Expand Up @@ -182,8 +181,7 @@ bool RasterCache::Prepare(GrDirectContext* context,
const SkMatrix& transformation_matrix,
SkColorSpace* dst_color_space,
bool is_complex,
bool will_change,
size_t external_size) {
bool will_change) {
// Disabling caching when access_threshold is zero is historic behavior.
if (access_threshold_ == 0) {
return false;
Expand All @@ -209,7 +207,6 @@ bool RasterCache::Prepare(GrDirectContext* context,

// Creates an entry, if not present prior.
Entry& entry = picture_cache_[cache_key];
entry.external_size = external_size;
if (entry.access_count < access_threshold_) {
// Frame threshold has not yet been reached.
return false;
Expand Down Expand Up @@ -263,12 +260,11 @@ bool RasterCache::Draw(const Layer* layer,
return false;
}

size_t RasterCache::SweepAfterFrame() {
size_t removed_size = SweepOneCacheAfterFrame(picture_cache_);
removed_size += SweepOneCacheAfterFrame(layer_cache_);
void RasterCache::SweepAfterFrame() {
SweepOneCacheAfterFrame(picture_cache_);
SweepOneCacheAfterFrame(layer_cache_);
picture_cached_this_frame_ = 0;
TraceStatsToTimeline();
return removed_size;
}

void RasterCache::Clear() {
Expand Down
12 changes: 3 additions & 9 deletions flow/raster_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ class RasterCache {
const SkMatrix& transformation_matrix,
SkColorSpace* dst_color_space,
bool is_complex,
bool will_change,
size_t external_size = 0);
bool will_change);

void Prepare(PrerollContext* context, Layer* layer, const SkMatrix& ctm);

Expand All @@ -157,8 +156,7 @@ class RasterCache {
SkCanvas& canvas,
SkPaint* paint = nullptr) const;

/// Returns the amount of external bytes freed by the sweep.
size_t SweepAfterFrame();
void SweepAfterFrame();

void Clear();

Expand Down Expand Up @@ -194,28 +192,24 @@ class RasterCache {
struct Entry {
bool used_this_frame = false;
size_t access_count = 0;
size_t external_size = 0;
std::unique_ptr<RasterCacheResult> image;
};

template <class Cache>
static size_t SweepOneCacheAfterFrame(Cache& cache) {
static void SweepOneCacheAfterFrame(Cache& cache) {
std::vector<typename Cache::iterator> dead;
size_t removed_size = 0;

for (auto it = cache.begin(); it != cache.end(); ++it) {
Entry& entry = it->second;
if (!entry.used_this_frame) {
dead.push_back(it);
removed_size += entry.external_size;
}
entry.used_this_frame = false;
}

for (auto it : dead) {
cache.erase(it);
}
return removed_size;
}

const size_t access_threshold_;
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/compositing/scene_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void SceneBuilder::addPicture(double dx,
pictureRect.offset(offset.x(), offset.y());
auto layer = std::make_unique<flutter::PictureLayer>(
offset, UIDartState::CreateGPUObject(picture->picture()), !!(hints & 1),
!!(hints & 2), picture->GetAllocationSize());
!!(hints & 2));
AddLayer(std::move(layer));
}

Expand Down
Loading