Flutter Engine
The Flutter Engine
scene.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/lib/ui/compositing/scene.h"
6
7#include "flutter/fml/trace_event.h"
8#include "flutter/lib/ui/painting/display_list_deferred_image_gpu_skia.h"
9#include "flutter/lib/ui/painting/image.h"
10#include "flutter/lib/ui/painting/picture.h"
11#include "flutter/lib/ui/ui_dart_state.h"
12#include "flutter/lib/ui/window/platform_configuration.h"
13#if IMPELLER_SUPPORTS_RENDERING
14#include "flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.h"
15#endif // IMPELLER_SUPPORTS_RENDERING
22
23namespace flutter {
24
26
27void Scene::create(Dart_Handle scene_handle,
28 std::shared_ptr<flutter::Layer> rootLayer) {
29 auto scene = fml::MakeRefCounted<Scene>(std::move(rootLayer));
30 scene->AssociateWithDartWrapper(scene_handle);
31}
32
33Scene::Scene(std::shared_ptr<flutter::Layer> rootLayer) {
34 layer_tree_root_layer_ = std::move(rootLayer);
35}
36
38
39bool Scene::valid() {
40 return layer_tree_root_layer_ != nullptr;
41}
42
44 layer_tree_root_layer_.reset();
46}
47
49 uint32_t height,
50 Dart_Handle raw_image_handle) {
51 TRACE_EVENT0("flutter", "Scene::toImageSync");
52
53 if (!valid()) {
54 return tonic::ToDart("Scene has been disposed.");
55 }
56
57 Scene::RasterizeToImage(width, height, raw_image_handle);
58 return Dart_Null();
59}
60
62 uint32_t height,
63 Dart_Handle raw_image_callback) {
64 TRACE_EVENT0("flutter", "Scene::toImage");
65
66 if (!valid()) {
67 return tonic::ToDart("Scene has been disposed.");
68 }
69
70 return Picture::RasterizeLayerTreeToImage(BuildLayerTree(width, height),
71 raw_image_callback);
72}
73
75 bool impeller,
76 std::unique_ptr<LayerTree> layer_tree,
78 fml::RefPtr<fml::TaskRunner> raster_task_runner,
79 fml::RefPtr<SkiaUnrefQueue> unref_queue) {
80#if IMPELLER_SUPPORTS_RENDERING
81 if (impeller) {
82 return DlDeferredImageGPUImpeller::Make(std::move(layer_tree),
83 std::move(snapshot_delegate),
84 std::move(raster_task_runner));
85 }
86#endif // IMPELLER_SUPPORTS_RENDERING
87
88#if SLIMPELLER
89 FML_LOG(FATAL) << "Impeller opt-out unavailable.";
90 return nullptr;
91#else // SLIMPELLER
92 const auto& frame_size = layer_tree->frame_size();
93 const SkImageInfo image_info =
94 SkImageInfo::Make(frame_size.width(), frame_size.height(),
97 image_info, std::move(layer_tree), std::move(snapshot_delegate),
98 raster_task_runner, std::move(unref_queue));
99#endif // SLIMPELLER
100}
101
102void Scene::RasterizeToImage(uint32_t width,
103 uint32_t height,
104 Dart_Handle raw_image_handle) {
105 auto* dart_state = UIDartState::Current();
106 if (!dart_state) {
107 return;
108 }
109 auto unref_queue = dart_state->GetSkiaUnrefQueue();
110 auto snapshot_delegate = dart_state->GetSnapshotDelegate();
111 auto raster_task_runner = dart_state->GetTaskRunners().GetRasterTaskRunner();
112
113 auto image = CanvasImage::Create();
114 auto dl_image = CreateDeferredImage(
115 dart_state->IsImpellerEnabled(), BuildLayerTree(width, height),
116 std::move(snapshot_delegate), std::move(raster_task_runner),
117 std::move(unref_queue));
118 image->set_image(dl_image);
119 image->AssociateWithDartWrapper(raw_image_handle);
120}
121
122std::unique_ptr<flutter::LayerTree> Scene::takeLayerTree(uint64_t width,
123 uint64_t height) {
124 return BuildLayerTree(width, height);
125}
126
127std::unique_ptr<LayerTree> Scene::BuildLayerTree(uint32_t width,
128 uint32_t height) {
129 if (!valid()) {
130 return nullptr;
131 }
132 return std::make_unique<LayerTree>(layer_tree_root_layer_,
134}
135
136} // namespace flutter
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
static fml::RefPtr< CanvasImage > Create()
Definition: image.h:28
static sk_sp< DlDeferredImageGPUImpeller > Make(std::unique_ptr< LayerTree > layer_tree, fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate, fml::RefPtr< fml::TaskRunner > raster_task_runner)
static sk_sp< DlDeferredImageGPUSkia > MakeFromLayerTree(const SkImageInfo &image_info, std::unique_ptr< LayerTree > layer_tree, fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate, const fml::RefPtr< fml::TaskRunner > &raster_task_runner, fml::RefPtr< SkiaUnrefQueue > unref_queue)
static Dart_Handle RasterizeLayerTreeToImage(std::unique_ptr< LayerTree > layer_tree, Dart_Handle raw_image_callback)
Definition: picture.cc:129
std::unique_ptr< flutter::LayerTree > takeLayerTree(uint64_t width, uint64_t height)
Definition: scene.cc:122
Dart_Handle toImage(uint32_t width, uint32_t height, Dart_Handle raw_image_handle)
Definition: scene.cc:61
void dispose()
Definition: scene.cc:43
static void create(Dart_Handle scene_handle, std::shared_ptr< flutter::Layer > rootLayer)
Definition: scene.cc:27
~Scene() override
Definition: scene.cc:37
Dart_Handle toImageSync(uint32_t width, uint32_t height, Dart_Handle raw_image_handle)
Definition: scene.cc:48
static UIDartState * Current()
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_Null(void)
#define FATAL(error)
#define FML_LOG(severity)
Definition: logging.h:82
sk_sp< const SkImage > image
Definition: SkRecords.h:269
IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, FlutterGpuTestClass)
static sk_sp< DlImage > CreateDeferredImage(bool impeller, std::unique_ptr< LayerTree > layer_tree, fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate, fml::RefPtr< fml::TaskRunner > raster_task_runner, fml::RefPtr< SkiaUnrefQueue > unref_queue)
Definition: scene.cc:74
Dart_Handle ToDart(const T &object)
int32_t height
int32_t width
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131