1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "scene.h"
17
18 #include <scene/ext/util.h>
19
20 #include <render/intf_render_context.h>
21
22 #include "component_factory.h"
23 #include "core/internal_scene.h"
24 #include "core/log.h"
25 #include "render_configuration.h"
26
SCENE_BEGIN_NAMESPACE()27 SCENE_BEGIN_NAMESPACE()
28
29 SceneObject::~SceneObject()
30 {
31 if (internal_) {
32 if (updateTask_) {
33 internal_->GetEngineTaskQueue()->CancelTask(updateTask_);
34 }
35 internal_->AddTask([&] { return internal_->Uninitialize(); }).Wait();
36 }
37 }
38
Build(const META_NS::IMetadata::Ptr & d)39 bool SceneObject::Build(const META_NS::IMetadata::Ptr& d)
40 {
41 bool res = Super::Build(d);
42 if (res) {
43 BASE_NS::shared_ptr<RENDER_NS::IRenderContext> rc =
44 GetInterfaceBuildArg<RENDER_NS::IRenderContext>(d, "RenderContext");
45 META_NS::ITaskQueue::Ptr engine = GetInterfaceBuildArg<META_NS::ITaskQueue>(d, "EngineQueue");
46 META_NS::ITaskQueue::Ptr app = GetInterfaceBuildArg<META_NS::ITaskQueue>(d, "AppQueue");
47 if (!engine || !app || !rc) {
48 CORE_LOG_E("Invalid parameters to construct Scene");
49 return false;
50 }
51 auto in = CreateShared<InternalScene>(
52 GetSelf<IScene>(), BASE_NS::move(engine), BASE_NS::move(app), BASE_NS::move(rc));
53 internal_ = in;
54 in->SetSelf(internal_);
55 AddBuiltinComponentFactories(internal_);
56 auto customSystemGraphUri = d->GetProperty<BASE_NS::string>("customSystemGraphUri");
57 if (customSystemGraphUri) {
58 internal_->SetSystemGraphUri(customSystemGraphUri->GetValue());
59 CORE_LOG_E("customSystemGraphUri %s", customSystemGraphUri->GetValue().c_str());
60 }
61 res = internal_->Initialize();
62 }
63 return res;
64 }
65
InitDynamicProperty(const META_NS::IProperty::Ptr & p,BASE_NS::string_view path)66 bool SceneObject::InitDynamicProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path)
67 {
68 if (p->GetName() == "RenderConfiguration") {
69 auto obj = CreateObject<IRenderConfiguration>(ClassId::RenderConfiguration).GetResult();
70 if (META_NS::Property<IRenderConfiguration::Ptr> t { p }) {
71 return t->SetValue(obj);
72 }
73 }
74 return false;
75 }
76
AttachEngineProperty(const META_NS::IProperty::Ptr & p,BASE_NS::string_view path)77 bool SceneObject::AttachEngineProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path)
78 {
79 return false;
80 }
81
StartAutoUpdate(META_NS::TimeSpan interval)82 void SceneObject::StartAutoUpdate(META_NS::TimeSpan interval)
83 {
84 if (updateTask_) {
85 internal_->GetEngineTaskQueue()->CancelTask(updateTask_);
86 }
87 updateTask_ = internal_->GetEngineTaskQueue()->AddTask(META_NS::MakeCallback<META_NS::ITaskQueueTask>([&] {
88 internal_->Update();
89 return true;
90 }),
91 interval);
92 }
93
GetRootNode() const94 Future<INode::Ptr> SceneObject::GetRootNode() const
95 {
96 return internal_->AddTask([=] { return internal_->FindNode("", {}); });
97 }
98
CreateNode(const BASE_NS::string_view uri,META_NS::ObjectId id)99 Future<INode::Ptr> SceneObject::CreateNode(const BASE_NS::string_view uri, META_NS::ObjectId id)
100 {
101 return internal_->AddTask([=, path = BASE_NS::string(uri)] { return internal_->CreateNode(path, id); });
102 }
103
FindNode(BASE_NS::string_view uri,META_NS::ObjectId id) const104 Future<INode::Ptr> SceneObject::FindNode(BASE_NS::string_view uri, META_NS::ObjectId id) const
105 {
106 return internal_->AddTask([=, path = BASE_NS::string(uri)] { return internal_->FindNode(path, id); });
107 }
108
CreateObject(META_NS::ObjectId id)109 Future<META_NS::IObject::Ptr> SceneObject::CreateObject(META_NS::ObjectId id)
110 {
111 return internal_->AddTask([=] { return internal_->CreateObject(id); });
112 }
113
ReleaseNode(INode::Ptr && node,bool recursive)114 Future<bool> SceneObject::ReleaseNode(INode::Ptr&& node, bool recursive)
115 {
116 return internal_->AddTask(
117 [=, n = BASE_NS::move(node)]() mutable { return internal_->ReleaseNode(BASE_NS::move(n), recursive); });
118 }
RemoveNode(const INode::Ptr & node)119 Future<bool> SceneObject::RemoveNode(const INode::Ptr& node)
120 {
121 return internal_->AddTask([=] { return internal_->RemoveNode(node); });
122 }
123
GetCameras() const124 Future<BASE_NS::vector<ICamera::Ptr>> SceneObject::GetCameras() const
125 {
126 return internal_->AddTask([=] { return internal_->GetCameras(); });
127 }
128
GetAnimations() const129 Future<BASE_NS::vector<META_NS::IAnimation::Ptr>> SceneObject::GetAnimations() const
130 {
131 return internal_->AddTask([=] { return internal_->GetAnimations(); });
132 }
133
GetInternalScene() const134 IInternalScene::Ptr SceneObject::GetInternalScene() const
135 {
136 return internal_;
137 }
138
SetRenderMode(RenderMode mode)139 Future<bool> SceneObject::SetRenderMode(RenderMode mode)
140 {
141 return internal_->AddTask([=] { return internal_->SetRenderMode(mode); });
142 }
GetRenderMode() const143 Future<RenderMode> SceneObject::GetRenderMode() const
144 {
145 return internal_->AddTask([=] { return internal_->GetRenderMode(); });
146 }
CastRay(const BASE_NS::Math::Vec3 & pos,const BASE_NS::Math::Vec3 & dir,const RayCastOptions & options) const147 Future<NodeHits> SceneObject::CastRay(
148 const BASE_NS::Math::Vec3& pos, const BASE_NS::Math::Vec3& dir, const RayCastOptions& options) const
149 {
150 return internal_->AddTask([=] {
151 RayCastOptions ops;
152 if (ops.layerMask == NONE_LAYER_MASK) {
153 ops.layerMask = ALL_LAYER_MASK;
154 }
155 NodeHits result;
156 if (auto ir = interface_cast<IInternalRayCast>(internal_)) {
157 result = ir->CastRay(pos, dir, ops);
158 }
159 return result;
160 });
161 }
162 SCENE_END_NAMESPACE()