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
14 changes: 5 additions & 9 deletions flow/layers/layer_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,33 +171,29 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame,
}
}

sk_sp<DisplayList> LayerTree::Flatten(
const SkRect& bounds,
std::shared_ptr<TextureRegistry> texture_registry,
GrDirectContext* gr_context) {
sk_sp<DisplayList> LayerTree::Flatten(const SkRect& bounds) {
TRACE_EVENT0("flutter", "LayerTree::Flatten");

DisplayListCanvasRecorder builder(bounds);

MutatorsStack unused_stack;
const FixedRefreshRateStopwatch unused_stopwatch;
SkMatrix root_surface_transformation;

// No root surface transformation. So assume identity.
root_surface_transformation.reset();

PrerollContext preroll_context{
// clang-format off
.raster_cache = nullptr,
.gr_context = gr_context,
.gr_context = nullptr,
.view_embedder = nullptr,
.mutators_stack = unused_stack,
.dst_color_space = nullptr,
.cull_rect = kGiantRect,
.surface_needs_readback = false,
.raster_time = unused_stopwatch,
.ui_time = unused_stopwatch,
.texture_registry = texture_registry,
.texture_registry = nullptr,
.checkerboard_offscreen_layers = false,
.frame_device_pixel_ratio = device_pixel_ratio_
// clang-format on
Expand All @@ -213,12 +209,12 @@ sk_sp<DisplayList> LayerTree::Flatten(
// clang-format off
.internal_nodes_canvas = &internal_nodes_canvas,
.leaf_nodes_canvas = &builder,
.gr_context = gr_context,
.gr_context = nullptr,
.dst_color_space = nullptr,
.view_embedder = nullptr,
.raster_time = unused_stopwatch,
.ui_time = unused_stopwatch,
.texture_registry = texture_registry,
.texture_registry = nullptr,
.raster_cache = nullptr,
.checkerboard_offscreen_layers = false,
.frame_device_pixel_ratio = device_pixel_ratio_,
Expand Down
6 changes: 1 addition & 5 deletions flow/layers/layer_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <cstdint>
#include <memory>

#include "flutter/common/graphics/texture.h"
#include "flutter/flow/compositor_context.h"
#include "flutter/flow/layers/layer.h"
#include "flutter/flow/raster_cache.h"
Expand Down Expand Up @@ -42,10 +41,7 @@ class LayerTree {
void Paint(CompositorContext::ScopedFrame& frame,
bool ignore_raster_cache = false) const;

sk_sp<DisplayList> Flatten(
const SkRect& bounds,
std::shared_ptr<TextureRegistry> texture_registry = nullptr,
GrDirectContext* gr_context = nullptr);
sk_sp<DisplayList> Flatten(const SkRect& bounds);

Layer* root_layer() const { return root_layer_.get(); }

Expand Down
1 change: 0 additions & 1 deletion lib/ui/compositing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Scene extends NativeFieldWrapperClass1 {
external String? _toImageSync(int width, int height, _Image outImage);

/// Creates a raster image representation of the current state of the scene.
///
/// This is a slow operation that is performed on a background thread.
///
/// Callers must dispose the [Image] when they are done with it. If the result
Expand Down
39 changes: 13 additions & 26 deletions lib/ui/compositing/scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "flutter/lib/ui/compositing/scene.h"

#include "flutter/fml/trace_event.h"
#include "flutter/lib/ui/painting/display_list_deferred_image_gpu.h"
#include "flutter/lib/ui/painting/image.h"
#include "flutter/lib/ui/painting/picture.h"
#include "flutter/lib/ui/ui_dart_state.h"
Expand Down Expand Up @@ -43,7 +42,7 @@ Scene::Scene(std::shared_ptr<flutter::Layer> rootLayer,
->get_window(0)
->viewport_metrics();

layer_tree_ = std::make_shared<LayerTree>(
layer_tree_ = std::make_unique<LayerTree>(
SkISize::Make(viewport_metrics.physical_width,
viewport_metrics.physical_height),
static_cast<float>(viewport_metrics.device_pixel_ratio));
Expand All @@ -70,7 +69,12 @@ Dart_Handle Scene::toImageSync(uint32_t width,
return tonic::ToDart("Scene did not contain a layer tree.");
}

Scene::RasterizeToImage(width, height, raw_image_handle);
auto picture = layer_tree_->Flatten(SkRect::MakeWH(width, height));
if (!picture) {
return tonic::ToDart("Could not flatten scene into a layer tree.");
}

Picture::RasterizeToImageSync(picture, width, height, raw_image_handle);
return Dart_Null();
}

Expand All @@ -83,32 +87,15 @@ Dart_Handle Scene::toImage(uint32_t width,
return tonic::ToDart("Scene did not contain a layer tree.");
}

return Picture::RasterizeLayerTreeToImage(std::move(layer_tree_), width,
height, raw_image_callback);
}

void Scene::RasterizeToImage(uint32_t width,
uint32_t height,
Dart_Handle raw_image_handle) {
auto* dart_state = UIDartState::Current();
if (!dart_state) {
return;
auto picture = layer_tree_->Flatten(SkRect::MakeWH(width, height));
if (!picture) {
return tonic::ToDart("Could not flatten scene into a layer tree.");
}
auto unref_queue = dart_state->GetSkiaUnrefQueue();
auto snapshot_delegate = dart_state->GetSnapshotDelegate();
auto raster_task_runner = dart_state->GetTaskRunners().GetRasterTaskRunner();

auto image = CanvasImage::Create();
const SkImageInfo image_info = SkImageInfo::Make(
width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
auto dl_image = DlDeferredImageGPU::MakeFromLayerTree(
image_info, std::move(layer_tree_), std::move(snapshot_delegate),
std::move(raster_task_runner), std::move(unref_queue));
image->set_image(dl_image);
image->AssociateWithDartWrapper(raw_image_handle);

return Picture::RasterizeToImage(picture, width, height, raw_image_callback);
}

std::shared_ptr<flutter::LayerTree> Scene::takeLayerTree() {
std::unique_ptr<flutter::LayerTree> Scene::takeLayerTree() {
return std::move(layer_tree_);
}

Expand Down
25 changes: 8 additions & 17 deletions lib/ui/compositing/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,25 @@ class Scene : public RefCountedDartWrappable<Scene> {
bool checkerboardRasterCacheImages,
bool checkerboardOffscreenLayers);

std::shared_ptr<flutter::LayerTree> takeLayerTree();
std::unique_ptr<flutter::LayerTree> takeLayerTree();

Dart_Handle toImageSync(uint32_t width,
uint32_t height,
Dart_Handle raw_image_handle);

Dart_Handle toImage(uint32_t width,
uint32_t height,
Dart_Handle raw_image_handle);
Dart_Handle image_callback);

void dispose();

private:
Scene(std::shared_ptr<flutter::Layer> rootLayer,
uint32_t rasterizerTracingThreshold,
bool checkerboardRasterCacheImages,
bool checkerboardOffscreenLayers);

void RasterizeToImage(uint32_t width,
uint32_t height,
Dart_Handle raw_image_handle);

// This is a shared_ptr to support flattening the layer tree from the UI
// thread onto the raster thread - allowing access to the texture registry
// required to render TextureLayers.
//
// No longer valid after calling `takeLayerTree`.
std::shared_ptr<flutter::LayerTree> layer_tree_;
explicit Scene(std::shared_ptr<flutter::Layer> rootLayer,
uint32_t rasterizerTracingThreshold,
bool checkerboardRasterCacheImages,
bool checkerboardOffscreenLayers);

std::unique_ptr<flutter::LayerTree> layer_tree_;
};

} // namespace flutter
Expand Down
41 changes: 2 additions & 39 deletions lib/ui/painting/display_list_deferred_image_gpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ sk_sp<DlDeferredImageGPU> DlDeferredImageGPU::Make(
raster_task_runner));
}

sk_sp<DlDeferredImageGPU> DlDeferredImageGPU::MakeFromLayerTree(
const SkImageInfo& image_info,
std::shared_ptr<LayerTree> layer_tree,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<SkiaUnrefQueue> unref_queue) {
return sk_sp<DlDeferredImageGPU>(new DlDeferredImageGPU(
ImageWrapper::MakeFromLayerTree(
image_info, std::move(layer_tree), std::move(snapshot_delegate),
raster_task_runner, std::move(unref_queue)),
raster_task_runner));
}

DlDeferredImageGPU::DlDeferredImageGPU(
std::shared_ptr<ImageWrapper> image_wrapper,
fml::RefPtr<fml::TaskRunner> raster_task_runner)
Expand Down Expand Up @@ -105,20 +92,6 @@ DlDeferredImageGPU::ImageWrapper::Make(
return wrapper;
}

std::shared_ptr<DlDeferredImageGPU::ImageWrapper>
DlDeferredImageGPU::ImageWrapper::MakeFromLayerTree(
const SkImageInfo& image_info,
std::shared_ptr<LayerTree> layer_tree,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<SkiaUnrefQueue> unref_queue) {
auto wrapper = std::shared_ptr<ImageWrapper>(
new ImageWrapper(image_info, nullptr, std::move(snapshot_delegate),
std::move(raster_task_runner), std::move(unref_queue)));
wrapper->SnapshotDisplayList(std::move(layer_tree));
return wrapper;
}

DlDeferredImageGPU::ImageWrapper::ImageWrapper(
const SkImageInfo& image_info,
sk_sp<DisplayList> display_list,
Expand Down Expand Up @@ -158,11 +131,9 @@ bool DlDeferredImageGPU::ImageWrapper::isTextureBacked() const {
return texture_.isValid();
}

void DlDeferredImageGPU::ImageWrapper::SnapshotDisplayList(
std::shared_ptr<LayerTree> layer_tree) {
void DlDeferredImageGPU::ImageWrapper::SnapshotDisplayList() {
fml::TaskRunner::RunNowOrPostTask(
raster_task_runner_,
[weak_this = weak_from_this(), layer_tree = std::move(layer_tree)]() {
raster_task_runner_, [weak_this = weak_from_this()]() {
auto wrapper = weak_this.lock();
if (!wrapper) {
return;
Expand All @@ -171,14 +142,6 @@ void DlDeferredImageGPU::ImageWrapper::SnapshotDisplayList(
if (!snapshot_delegate) {
return;
}
if (layer_tree) {
auto display_list =
layer_tree->Flatten(SkRect::MakeWH(wrapper->image_info_.width(),
wrapper->image_info_.height()),
snapshot_delegate.get()->GetTextureRegistry(),
snapshot_delegate.get()->GetGrContext());
wrapper->display_list_ = std::move(display_list);
}
auto result = snapshot_delegate->MakeGpuImage(wrapper->display_list_,
wrapper->image_info_);
if (result->texture.isValid()) {
Expand Down
21 changes: 1 addition & 20 deletions lib/ui/painting/display_list_deferred_image_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "flutter/common/graphics/texture.h"
#include "flutter/display_list/display_list.h"
#include "flutter/display_list/display_list_image.h"
#include "flutter/flow/layers/layer_tree.h"
#include "flutter/flow/skia_gpu_object.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"
Expand All @@ -29,13 +28,6 @@ class DlDeferredImageGPU final : public DlImage {
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<SkiaUnrefQueue> unref_queue);

static sk_sp<DlDeferredImageGPU> MakeFromLayerTree(
const SkImageInfo& image_info,
std::shared_ptr<LayerTree> layer_tree,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<SkiaUnrefQueue> unref_queue);

// |DlImage|
~DlDeferredImageGPU() override;

Expand Down Expand Up @@ -81,13 +73,6 @@ class DlDeferredImageGPU final : public DlImage {
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<SkiaUnrefQueue> unref_queue);

static std::shared_ptr<ImageWrapper> MakeFromLayerTree(
const SkImageInfo& image_info,
std::shared_ptr<LayerTree> layer_tree,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<SkiaUnrefQueue> unref_queue);

const SkImageInfo image_info() const { return image_info_; }
const GrBackendTexture& texture() const { return texture_; }
bool isTextureBacked() const;
Expand Down Expand Up @@ -118,11 +103,7 @@ class DlDeferredImageGPU final : public DlImage {
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<SkiaUnrefQueue> unref_queue);

// If a layer tree is provided, it will be flattened during the raster
// thread task spwaned by this method. After being flattened into a display
// list, the image wrapper will be updated to hold this display list and the
// layer tree can be dropped.
void SnapshotDisplayList(std::shared_ptr<LayerTree> layer_tree = nullptr);
void SnapshotDisplayList();

// |ContextListener|
void OnGrContextCreated() override;
Expand Down
35 changes: 5 additions & 30 deletions lib/ui/painting/picture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,11 @@ Dart_Handle Picture::RasterizeToImage(sk_sp<DisplayList> display_list,
Dart_Handle raw_image_callback) {
return RasterizeToImage(
[display_list](SkCanvas* canvas) { display_list->RenderTo(canvas); },
nullptr, width, height, raw_image_callback);
}

Dart_Handle Picture::RasterizeLayerTreeToImage(
std::shared_ptr<LayerTree> layer_tree,
uint32_t width,
uint32_t height,
Dart_Handle raw_image_callback) {
return RasterizeToImage(nullptr, std::move(layer_tree), width, height,
raw_image_callback);
width, height, raw_image_callback);
}

Dart_Handle Picture::RasterizeToImage(
std::function<void(SkCanvas*)> draw_callback,
std::shared_ptr<LayerTree> layer_tree,
uint32_t width,
uint32_t height,
Dart_Handle raw_image_callback) {
Expand Down Expand Up @@ -168,25 +158,10 @@ Dart_Handle Picture::RasterizeToImage(

// Kick things off on the raster rask runner.
fml::TaskRunner::RunNowOrPostTask(
raster_task_runner,
[ui_task_runner, snapshot_delegate, draw_callback, picture_bounds,
ui_task, layer_tree = std::move(layer_tree)] {
sk_sp<SkImage> raster_image;
if (layer_tree) {
auto display_list = layer_tree->Flatten(
SkRect::MakeWH(picture_bounds.width(), picture_bounds.height()),
snapshot_delegate.get()->GetTextureRegistry(),
snapshot_delegate.get()->GetGrContext());

raster_image = snapshot_delegate->MakeRasterSnapshot(
[display_list](SkCanvas* canvas) {
display_list->RenderTo(canvas);
},
picture_bounds);
} else {
raster_image = snapshot_delegate->MakeRasterSnapshot(draw_callback,
picture_bounds);
}
raster_task_runner, [ui_task_runner, snapshot_delegate, draw_callback,
picture_bounds, ui_task] {
sk_sp<SkImage> raster_image = snapshot_delegate->MakeRasterSnapshot(
draw_callback, picture_bounds);

fml::TaskRunner::RunNowOrPostTask(
ui_task_runner,
Expand Down
12 changes: 0 additions & 12 deletions lib/ui/painting/picture.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define FLUTTER_LIB_UI_PAINTING_PICTURE_H_

#include "flutter/display_list/display_list.h"
#include "flutter/flow/layers/layer_tree.h"
#include "flutter/flow/skia_gpu_object.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "flutter/lib/ui/painting/image.h"
Expand Down Expand Up @@ -52,19 +51,8 @@ class Picture : public RefCountedDartWrappable<Picture> {
uint32_t height,
Dart_Handle raw_image_callback);

static Dart_Handle RasterizeLayerTreeToImage(
std::shared_ptr<LayerTree> layer_tree,
uint32_t width,
uint32_t height,
Dart_Handle raw_image_callback);

// Callers may provide either a draw_callback (which should reference a
// display list) or a layer tree. If a layer tree is provided, it will be
// flattened on the raster thread. In this case the draw callback will be
// ignored.
static Dart_Handle RasterizeToImage(
std::function<void(SkCanvas*)> draw_callback,
std::shared_ptr<LayerTree> layer_tree,
uint32_t width,
uint32_t height,
Dart_Handle raw_image_callback);
Expand Down
Loading