| Flutter Impeller |

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 "impeller/scene/node.h"
6
7 #include <inttypes.h>
8 #include <atomic>
9 #include <memory>
10 #include <vector>
11
12 #include "flutter/fml/logging.h"
13 #include "impeller/base/strings.h"
14 #include "impeller/base/thread.h"
15 #include "impeller/base/validation.h"
16 #include "impeller/geometry/matrix.h"
17 #include "impeller/scene/animation/animation_player.h"
18 #include "impeller/scene/importer/conversions.h"
19 #include "impeller/scene/importer/scene_flatbuffers.h"
20 #include "impeller/scene/mesh.h"
21 #include "impeller/scene/node.h"
22 #include "impeller/scene/scene_encoder.h"
23
24 namespace impeller {
25 namespace scene {
26
27 static std::atomic_uint64_t kNextNodeID = 0;
28
29 void Node::MutationLog::Append(const Entry& entry) {
30 WriterLock lock(write_mutex_);
31 dirty_ = true;
32 entries_.push_back(entry);
33 }
34
35 std::optional<std::vector<Node::MutationLog::Entry>>
36 Node::MutationLog::Flush() {
37 WriterLock lock(write_mutex_);
38 if (!dirty_) {
39 return std::nullopt;
40 }
41 dirty_ = false;
42 auto result = entries_;
43 entries_ = {};
44 return result;
45 }
46
47 std::shared_ptr<Node> Node::MakeFromFlatbuffer(
48 const fml::Mapping& ipscene_mapping,
49 Allocator& allocator) {
50 flatbuffers::Verifier verifier(ipscene_mapping.GetMapping(),
51 ipscene_mapping.GetSize());
52 if (!fb::VerifySceneBuffer(verifier)) {
53 VALIDATION_LOG << "Failed to unpack scene: Scene flatbuffer is invalid.";
54 return nullptr;
55 }
56
57 return Node::MakeFromFlatbuffer(*fb::GetScene(ipscene_mapping.GetMapping()),
58 allocator);
59 }
60
61 static std::shared_ptr<Texture> UnpackTextureFromFlatbuffer(
62 const fb::Texture* iptexture,
63 Allocator& allocator) {
64 if (iptexture == nullptr || iptexture->embedded_image() == nullptr ||
65 iptexture->embedded_image()->bytes() == nullptr) {
66 return nullptr;
67 }
68
69 auto embedded = iptexture->embedded_image();
70
71 uint8_t bytes_per_component = 0;
72 switch (embedded->component_type()) {
73 case fb::ComponentType::k8Bit:
74 bytes_per_component = 1;
75 break;
76 case fb::ComponentType::k16Bit:
77 // bytes_per_component = 2;
78 FML_LOG(WARNING) << "16 bit textures not yet supported.";
79 return nullptr;
80 }
81
82 DecompressedImage::Format format;
83 switch (embedded->component_count()) {
84 case 1:
85 format = DecompressedImage::Format::kGrey;
86 break;
87 case 3:
88 format = DecompressedImage::Format::kRGB;
89 break;
90 case 4:
91 format = DecompressedImage::Format::kRGBA;
92 break;
93 default:
94 FML_LOG(WARNING) << "Textures with " << embedded->component_count()
95 << " components are not supported." << std::endl;
96 return nullptr;
97 }
98 if (embedded->bytes()->size() != bytes_per_component *
99 embedded->component_count() *
100 embedded->width() * embedded->height()) {
101 FML_LOG(WARNING) << "Embedded texture has an unexpected size. Skipping."
102 << std::endl;
103 return nullptr;
104 }
105
106 auto image_mapping = std::make_shared<fml::NonOwnedMapping>(
107 embedded->bytes()->Data(), embedded->bytes()->size());
108 auto decompressed_image =
109 DecompressedImage(ISize(embedded->width(), embedded->height()), format,
110 image_mapping)
111 .ConvertToRGBA();
112
113 auto texture_descriptor = TextureDescriptor{};
114 texture_descriptor.storage_mode = StorageMode::kHostVisible;
115 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
116 texture_descriptor.size = decompressed_image.GetSize();
117 // TODO(bdero): Generate mipmaps for embedded textures.
118 texture_descriptor.mip_count = 1u;
119
120 auto texture = allocator.CreateTexture(texture_descriptor);
121 if (!texture) {
122 FML_LOG(ERROR) << "Could not allocate texture.";
123 return nullptr;
124 }
125
126 auto uploaded = texture->SetContents(decompressed_image.GetAllocation());
127 if (!uploaded) {
128 FML_LOG(ERROR) << "Could not upload texture to device memory.";
129 return nullptr;
130 }
131
132 return texture;
133 }
134
135 std::shared_ptr<Node> Node::MakeFromFlatbuffer(const fb::Scene& scene,
136 Allocator& allocator) {
137 // Unpack textures.
138 std::vector<std::shared_ptr<Texture>> textures;
139 if (scene.textures()) {
140 for (const auto iptexture : *scene.textures()) {
141 // The elements of the unpacked texture array must correspond exactly with
142 // the ipscene texture array. So if a texture is empty or invalid, a
143 // nullptr is inserted as a placeholder.
144 textures.push_back(UnpackTextureFromFlatbuffer(iptexture, allocator));
145 }
146 }
147
148 auto result = std::make_shared<Node>();
149 result->SetLocalTransform(importer::ToMatrix(*scene.transform()));
150
151 if (!scene.nodes() || !scene.children()) {
152 return result; // The scene is empty.
153 }
154
155 // Initialize nodes for unpacking the entire scene.
156 std::vector<std::shared_ptr<Node>> scene_nodes;
157 scene_nodes.reserve(scene.nodes()->size());
158 for (size_t node_i = 0; node_i < scene.nodes()->size(); node_i++) {
159 scene_nodes.push_back(std::make_shared<Node>());
160 }
161
162 // Connect children to the root node.
163 for (int child : *scene.children()) {
164 if (child < 0 || static_cast<size_t>(child) >= scene_nodes.size()) {
165 VALIDATION_LOG << "Scene child index out of range.";
166 continue;
167 }
168 result->AddChild(scene_nodes[child]);
169 }
170
171 // Unpack each node.
172 for (size_t node_i = 0; node_i < scene.nodes()->size(); node_i++) {
173 scene_nodes[node_i]->UnpackFromFlatbuffer(*scene.nodes()->Get(node_i),
174 scene_nodes, textures, allocator);
175 }
176
177 // Unpack animations.
178 if (scene.animations()) {
179 for (const auto animation : *scene.animations()) {
180 if (auto out_animation =
181 Animation::MakeFromFlatbuffer(*animation, scene_nodes)) {
182 result->animations_.push_back(out_animation);
183 }
184 }
185 }
186
187 return result;
188 }
189
190 void Node::UnpackFromFlatbuffer(
191 const fb::Node& source_node,
192 const std::vector<std::shared_ptr<Node>>& scene_nodes,
193 const std::vector<std::shared_ptr<Texture>>& textures,
194 Allocator& allocator) {
195 name_ = source_node.name()->str();
196 SetLocalTransform(importer::ToMatrix(*source_node.transform()));
197
198 /// Meshes.
199
200 if (source_node.mesh_primitives()) {
201 Mesh mesh;
202 for (const auto* primitives : *source_node.mesh_primitives()) {
203 auto geometry = Geometry::MakeFromFlatbuffer(*primitives, allocator);
204 auto material =
205 primitives->material()
206 ? Material::MakeFromFlatbuffer(*primitives->material(), textures)
207 : Material::MakeUnlit();
208 mesh.AddPrimitive({std::move(geometry), std::move(material)});
209 }
210 SetMesh(std::move(mesh));
211 }
212
213 /// Child nodes.
214
215 if (source_node.children()) {
216 // Wire up graph connections.
217 for (int child : *source_node.children()) {
218 if (child < 0 || static_cast<size_t>(child) >= scene_nodes.size()) {
219 VALIDATION_LOG << "Node child index out of range.";
220 continue;
221 }
222 AddChild(scene_nodes[child]);
223 }
224 }
225
226 /// Skin.
227
228 if (source_node.skin()) {
229 skin_ = Skin::MakeFromFlatbuffer(*source_node.skin(), scene_nodes);
230 }
231 }
232
233 Node::Node() : name_(SPrintF("__node%" PRIu64, kNextNodeID++)){};
234
235 Node::~Node() = default;
236
237 Mesh::Mesh(Mesh&& mesh) = default;
238
239 Mesh& Mesh::operator=(Mesh&& mesh) = default;
240
241 const std::string& Node::GetName() const {
242 return name_;
243 }
244
245 void Node::SetName(const std::string& new_name) {
246 name_ = new_name;
247 }
248
249 Node* Node::GetParent() const {
250 return parent_;
251 }
252
253 std::shared_ptr<Node> Node::FindChildByName(
254 const std::string& name,
255 bool exclude_animation_players) const {
256 for (auto& child : children_) {
257 if (exclude_animation_players && child->animation_player_.has_value()) {
258 continue;
259 }
260 if (child->GetName() == name) {
261 return child;
262 }
263 if (auto found = child->FindChildByName(name)) {
264 return found;
265 }
266 }
267 return nullptr;
268 }
269
270 std::shared_ptr<Animation> Node::FindAnimationByName(
271 const std::string& name) const {
272 for (const auto& animation : animations_) {
273 if (animation->GetName() == name) {
274 return animation;
275 }
276 }
277 return nullptr;
278 }
279
280 AnimationClip* Node::AddAnimation(const std::shared_ptr<Animation>& animation) {
281 if (!animation_player_.has_value()) {
282 animation_player_ = AnimationPlayer();
283 }
284 return animation_player_->AddAnimation(animation, this);
285 }
286
287 void Node::SetLocalTransform(Matrix transform) {
288 local_transform_ = transform;
289 }
290
291 Matrix Node::GetLocalTransform() const {
292 return local_transform_;
293 }
294
295 void Node::SetGlobalTransform(Matrix transform) {
296 Matrix inverse_global_transform =
297 parent_ ? parent_->GetGlobalTransform().Invert() : Matrix();
298
299 local_transform_ = inverse_global_transform * transform;
300 }
301
302 Matrix Node::GetGlobalTransform() const {
303 if (parent_) {
304 return parent_->GetGlobalTransform() * local_transform_;
305 }
306 return local_transform_;
307 }
308
309 bool Node::AddChild(std::shared_ptr<Node> node) {
310 if (!node) {
311 VALIDATION_LOG << "Cannot add null child to node.";
312 return false;
313 }
314
315 // TODO(bdero): Figure out a better paradigm/rules for nodes with multiple
316 // parents. We should probably disallow this, make deep
317 // copying of nodes cheap and easy, add mesh instancing, etc.
318 // Today, the parent link is only used for skin posing, and so
319 // it's reasonable to not have a check and allow multi-parenting.
320 // Even still, there should still be some kind of cycle
321 // prevention/detection, ideally at the protocol level.
322 //
323 // if (node->parent_ != nullptr) {
324 // VALIDATION_LOG
325 // << "Cannot add a node as a child which already has a parent.";
326 // return false;
327 // }
328 node->parent_ = this;
329 children_.push_back(std::move(node));
330
331 return true;
332 }
333
334 std::vector<std::shared_ptr<Node>>& Node::GetChildren() {
335 return children_;
336 }
337
338 void Node::SetMesh(Mesh mesh) {
339 mesh_ = std::move(mesh);
340 }
341
342 Mesh& Node::GetMesh() {
343 return mesh_;
344 }
345
346 void Node::SetIsJoint(bool is_joint) {
347 is_joint_ = is_joint;
348 }
349
350 bool Node::IsJoint() const {
351 return is_joint_;
352 }
353
354 bool Node::Render(SceneEncoder& encoder,
355 Allocator& allocator,
356 const Matrix& parent_transform) {
357 std::optional<std::vector<MutationLog::Entry>> log = mutation_log_.Flush();
358 if (log.has_value()) {
359 for (const auto& entry : log.value()) {
360 if (auto e = std::get_if<MutationLog::SetTransformEntry>(&entry)) {
361 local_transform_ = e->transform;
362 } else if (auto e =
363 std::get_if<MutationLog::SetAnimationStateEntry>(&entry)) {
364 AnimationClip* clip =
365 animation_player_.has_value()
366 ? animation_player_->GetClip(e->animation_name)
367 : nullptr;
368 if (!clip) {
369 auto animation = FindAnimationByName(e->animation_name);
370 if (!animation) {
371 continue;
372 }
373 clip = AddAnimation(animation);
374 if (!clip) {
375 continue;
376 }
377 }
378
379 clip->SetPlaying(e->playing);
380 clip->SetLoop(e->loop);
381 clip->SetWeight(e->weight);
382 clip->SetPlaybackTimeScale(e->time_scale);
383 } else if (auto e =
384 std::get_if<MutationLog::SeekAnimationEntry>(&entry)) {
385 AnimationClip* clip =
386 animation_player_.has_value()
387 ? animation_player_->GetClip(e->animation_name)
388 : nullptr;
389 if (!clip) {
390 auto animation = FindAnimationByName(e->animation_name);
391 if (!animation) {
392 continue;
393 }
394 clip = AddAnimation(animation);
395 if (!clip) {
396 continue;
397 }
398 }
399
400 clip->Seek(SecondsF(e->time));
401 }
402 }
403 }
404
405 if (animation_player_.has_value()) {
406 animation_player_->Update();
407 }
408
409 Matrix transform = parent_transform * local_transform_;
410 mesh_.Render(encoder, transform,
411 skin_ ? skin_->GetJointsTexture(allocator) : nullptr);
412
413 for (auto& child : children_) {
414 if (!child->Render(encoder, allocator, transform)) {
415 return false;
416 }
417 }
418 return true;
419 }
420
421 void Node::AddMutation(const MutationLog::Entry& entry) {
422 mutation_log_.Append(entry);
423 }
424
425 } // namespace scene
426 } // namespace impeller
impeller::DecompressedImage
Definition: decompressed_image.h:16
impeller::scene::Node::Node
Node()
Definition: node.cc:233
impeller::scene::Node::GetLocalTransform
Matrix GetLocalTransform() const
Definition: node.cc:291
impeller::scene::Node::GetParent
Node * GetParent() const
Definition: node.cc:249
impeller::DecompressedImage::Format::kRGBA
@ kRGBA
impeller::scene::Mesh
Definition: mesh.h:20
impeller::scene::Node::FindChildByName
std::shared_ptr< Node > FindChildByName(const std::string &name, bool exclude_animation_players=false) const
Definition: node.cc:253
impeller::scene::Node::~Node
~Node()
impeller::scene::AnimationClip::SetPlaybackTimeScale
void SetPlaybackTimeScale(Scalar playback_speed)
Sets the animation playback speed. Negative values make the clip play in reverse.
Definition: animation_clip.cc:61
impeller::scene::Mesh::Mesh
Mesh()
impeller::scene::Node::SetIsJoint
void SetIsJoint(bool is_joint)
Definition: node.cc:346
impeller::DecompressedImage::Format::kGrey
@ kGrey
impeller::WriterLock
Definition: thread.h:83
animation_player.h
impeller::scene::AnimationClip::SetLoop
void SetLoop(bool looping)
Definition: animation_clip.cc:53
impeller::scene::Skin::MakeFromFlatbuffer
static std::unique_ptr< Skin > MakeFromFlatbuffer(const fb::Skin &skin, const std::vector< std::shared_ptr< Node >> &scene_nodes)
Definition: skin.cc:19
impeller::scene::Node::SetMesh
void SetMesh(Mesh mesh)
Definition: node.cc:338
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::Allocator::CreateTexture
std::shared_ptr< Texture > CreateTexture(const TextureDescriptor &desc)
Definition: allocator.cc:49
validation.h
impeller::scene::AnimationClip
Definition: animation_clip.h:21
conversions.h
impeller::scene::Animation::MakeFromFlatbuffer
static std::shared_ptr< Animation > MakeFromFlatbuffer(const fb::Animation &animation, const std::vector< std::shared_ptr< Node >> &scene_nodes)
Definition: animation.cc:19
impeller::scene::Node::GetName
const std::string & GetName() const
Definition: node.cc:241
scene_encoder.h
impeller::scene::Node::GetChildren
std::vector< std::shared_ptr< Node > > & GetChildren()
Definition: node.cc:334
impeller::scene::Node::SetName
void SetName(const std::string &new_name)
Definition: node.cc:245
impeller::scene::Mesh::Render
bool Render(SceneEncoder &encoder, const Matrix &transform, const std::shared_ptr< Texture > &joints) const
Definition: mesh.cc:36
matrix.h
impeller::DecompressedImage::ConvertToRGBA
DecompressedImage ConvertToRGBA() const
Definition: decompressed_image.cc:62
impeller::SecondsF
std::chrono::duration< float > SecondsF
Definition: timing.h:12
impeller::scene::Node::FindAnimationByName
std::shared_ptr< Animation > FindAnimationByName(const std::string &name) const
Definition: node.cc:270
node.h
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::scene::UnpackTextureFromFlatbuffer
static std::shared_ptr< Texture > UnpackTextureFromFlatbuffer(const fb::Texture *iptexture, Allocator &allocator)
Definition: node.cc:61
impeller::scene::Mesh::AddPrimitive
void AddPrimitive(Primitive mesh_)
Definition: mesh.cc:21
impeller::scene::kNextNodeID
static std::atomic_uint64_t kNextNodeID
Definition: node.cc:27
impeller::DecompressedImage::Format::kRGB
@ kRGB
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:25
impeller::scene::Node::GetMesh
Mesh & GetMesh()
Definition: node.cc:342
impeller::scene::AnimationClip::SetWeight
void SetWeight(Scalar weight)
Definition: animation_clip.cc:69
impeller::scene::Geometry::MakeFromFlatbuffer
static std::shared_ptr< Geometry > MakeFromFlatbuffer(const fb::MeshPrimitive &mesh, Allocator &allocator)
Definition: geometry.cc:51
mesh.h
impeller::scene::AnimationPlayer
Definition: animation_player.h:25
strings.h
impeller::scene::Material::MakeFromFlatbuffer
static std::unique_ptr< Material > MakeFromFlatbuffer(const fb::Material &material, const std::vector< std::shared_ptr< Texture >> &textures)
Definition: material.cc:27
impeller::Matrix::Invert
Matrix Invert() const
Definition: matrix.cc:97
impeller::scene::Node::SetGlobalTransform
void SetGlobalTransform(Matrix transform)
Definition: node.cc:295
impeller::scene::Node::AddChild
bool AddChild(std::shared_ptr< Node > child)
Definition: node.cc:309
impeller::ISize
TSize< int64_t > ISize
Definition: size.h:136
impeller::scene::Node::Render
bool Render(SceneEncoder &encoder, Allocator &allocator, const Matrix &parent_transform)
Definition: node.cc:354
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::scene::Node::MutationLog::Entry
std::variant< SetTransformEntry, SetAnimationStateEntry, SeekAnimationEntry > Entry
Definition: node.h:51
impeller::scene::Node::IsJoint
bool IsJoint() const
Definition: node.cc:350
impeller::scene::Node::AddAnimation
AnimationClip * AddAnimation(const std::shared_ptr< Animation > &animation)
Definition: node.cc:280
impeller::scene::Node
Definition: node.h:29
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:40
impeller::scene::Node::AddMutation
void AddMutation(const MutationLog::Entry &entry)
Definition: node.cc:421
impeller::DecompressedImage::Format
Format
Definition: decompressed_image.h:18
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:39
impeller::scene::SceneEncoder
Definition: scene_encoder.h:29
impeller::scene::Node::SetLocalTransform
void SetLocalTransform(Matrix transform)
Definition: node.cc:287
impeller::scene::AnimationClip::Seek
void Seek(SecondsF time)
Move the animation to the specified time. The given time is clamped to the animation's playback range...
Definition: animation_clip.cc:77
impeller::scene::Node::MakeFromFlatbuffer
static std::shared_ptr< Node > MakeFromFlatbuffer(const fml::Mapping &ipscene_mapping, Allocator &allocator)
Definition: node.cc:47
impeller::scene::AnimationClip::SetPlaying
void SetPlaying(bool playing)
Definition: animation_clip.cc:32
thread.h
impeller
Definition: aiks_context.cc:10
impeller::scene::Node::MutationLog::Append
void Append(const Entry &entry)
Definition: node.cc:29
impeller::scene::Mesh::operator=
Mesh & operator=(Mesh &&mesh)
impeller::scene::Material
Definition: material.h:26
impeller::scene::Node::GetGlobalTransform
Matrix GetGlobalTransform() const
Definition: node.cc:302
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::scene::importer::ToMatrix
Matrix ToMatrix(const std::vector< double > &m)
Definition: conversions.cc:15