Flutter Engine
The Flutter Engine
scene_node.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/painting/scene/scene_node.h"
6
7#include <memory>
8#include <sstream>
9
10#include "flutter/assets/asset_manager.h"
11#include "flutter/fml/make_copyable.h"
12#include "flutter/fml/trace_event.h"
13#include "flutter/lib/ui/dart_wrapper.h"
14#include "flutter/lib/ui/painting/scene/scene_shader.h"
15#include "flutter/lib/ui/ui_dart_state.h"
16#include "flutter/lib/ui/window/platform_configuration.h"
19
25
26namespace flutter {
27
29
31 auto res = fml::MakeRefCounted<SceneNode>();
32 res->AssociateWithDartWrapper(wrapper);
33}
34
35std::string SceneNode::initFromAsset(const std::string& asset_name,
36 Dart_Handle completion_callback_handle) {
37 FML_TRACE_EVENT("flutter", "SceneNode::initFromAsset", "asset", asset_name);
38
39 if (!Dart_IsClosure(completion_callback_handle)) {
40 return "Completion callback must be a function.";
41 }
42
43 auto dart_state = UIDartState::Current();
44 if (!dart_state->IsImpellerEnabled()) {
45 return "3D scenes require the Impeller rendering backend to be enabled.";
46 }
47
48 std::shared_ptr<AssetManager> asset_manager =
49 dart_state->platform_configuration()->client()->GetAssetManager();
50 std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(asset_name);
51 if (data == nullptr) {
52 return std::string("Asset '") + asset_name + std::string("' not found.");
53 }
54
55 auto& task_runners = dart_state->GetTaskRunners();
56
57 std::promise<std::shared_ptr<impeller::Context>> context_promise;
58 auto impeller_context_promise = context_promise.get_future();
59 task_runners.GetIOTaskRunner()->PostTask(
60 fml::MakeCopyable([promise = std::move(context_promise),
61 io_manager = dart_state->GetIOManager()]() mutable {
62 promise.set_value(io_manager ? io_manager->GetImpellerContext()
63 : nullptr);
64 }));
65
66 auto persistent_completion_callback =
67 std::make_unique<tonic::DartPersistentValue>(dart_state,
68 completion_callback_handle);
69
70 auto ui_task = fml::MakeCopyable(
71 [this, callback = std::move(persistent_completion_callback)](
72 std::shared_ptr<impeller::scene::Node> node) mutable {
73 auto dart_state = callback->dart_state().lock();
74 if (!dart_state) {
75 // The root isolate could have died in the meantime.
76 return;
77 }
78 tonic::DartState::Scope scope(dart_state);
79
80 node_ = std::move(node);
81 tonic::DartInvoke(callback->Get(), {Dart_TypeVoid()});
82
83 // callback is associated with the Dart isolate and must be
84 // deleted on the UI thread.
85 callback.reset();
86 });
87
88 task_runners.GetRasterTaskRunner()->PostTask(
89 fml::MakeCopyable([ui_task = std::move(ui_task), task_runners,
90 impeller_context = impeller_context_promise.get(),
91 data = std::move(data)]() {
92 auto node = impeller::scene::Node::MakeFromFlatbuffer(
93 *data, *impeller_context->GetResourceAllocator());
94
95 task_runners.GetUITaskRunner()->PostTask(
96 [ui_task, node = std::move(node)]() { ui_task(node); });
97 }));
98
99 return "";
100}
101
102static impeller::Matrix ToMatrix(const tonic::Float64List& matrix4) {
103 return impeller::Matrix(static_cast<impeller::Scalar>(matrix4[0]),
104 static_cast<impeller::Scalar>(matrix4[1]),
105 static_cast<impeller::Scalar>(matrix4[2]),
106 static_cast<impeller::Scalar>(matrix4[3]),
107 static_cast<impeller::Scalar>(matrix4[4]),
108 static_cast<impeller::Scalar>(matrix4[5]),
109 static_cast<impeller::Scalar>(matrix4[6]),
110 static_cast<impeller::Scalar>(matrix4[7]),
111 static_cast<impeller::Scalar>(matrix4[8]),
112 static_cast<impeller::Scalar>(matrix4[9]),
113 static_cast<impeller::Scalar>(matrix4[10]),
114 static_cast<impeller::Scalar>(matrix4[11]),
115 static_cast<impeller::Scalar>(matrix4[12]),
116 static_cast<impeller::Scalar>(matrix4[13]),
117 static_cast<impeller::Scalar>(matrix4[14]),
118 static_cast<impeller::Scalar>(matrix4[15]));
119}
120
121void SceneNode::initFromTransform(const tonic::Float64List& matrix4) {
122 node_ = std::make_shared<impeller::scene::Node>();
123 node_->SetLocalTransform(ToMatrix(matrix4));
124}
125
126void SceneNode::AddChild(Dart_Handle scene_node_handle) {
127 if (!node_) {
128 return;
129 }
130 auto* scene_node =
132 if (!scene_node) {
133 return;
134 }
135 node_->AddChild(scene_node->node_);
136 children_.push_back(fml::Ref(scene_node));
137}
138
139void SceneNode::SetTransform(const tonic::Float64List& matrix4) {
141 ToMatrix(matrix4)};
142 node_->AddMutation(entry);
143}
144
145void SceneNode::SetAnimationState(const std::string& animation_name,
146 bool playing,
147 bool loop,
148 double weight,
149 double time_scale) {
151 .animation_name = animation_name,
152 .playing = playing,
153 .loop = loop,
154 .weight = static_cast<float>(weight),
155 .time_scale = static_cast<float>(time_scale),
156 };
157 node_->AddMutation(entry);
158}
159
160void SceneNode::SeekAnimation(const std::string& animation_name, double time) {
162 .animation_name = animation_name,
163 .time = static_cast<float>(time),
164 };
165 node_->AddMutation(entry);
166}
167
168SceneNode::SceneNode() = default;
169
170SceneNode::~SceneNode() = default;
171
172} // namespace flutter
A scene node, which may be a deserialized ipscene asset. This node can be safely added as a child to ...
Definition: scene_node.h:30
static void Create(Dart_Handle wrapper)
Definition: scene_node.cc:30
fml::RefPtr< SceneNode > node(Dart_Handle shader)
std::string initFromAsset(const std::string &asset_name, Dart_Handle completion_callback_handle)
Definition: scene_node.cc:35
static UIDartState * Current()
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, FlutterGpuTestClass)
static impeller::Matrix ToMatrix(const tonic::Float64List &matrix4)
Definition: scene_node.cc:102
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
RefPtr< T > Ref(T *ptr)
Definition: ref_ptr.h:237
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
float Scalar
Definition: scalar.h:18
SK_API sk_sp< PrecompileColorFilter > Matrix()
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20
static double time(int loops, Benchmark *bench, Target *target)
Definition: nanobench.cpp:394
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
#define FML_TRACE_EVENT(category_group, name,...)
Definition: trace_event.h:121