• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "MorpherJS.h"
17 
18 #include <meta/api/make_callback.h>
19 #include <meta/interface/intf_task_queue.h>
20 #include <meta/interface/intf_task_queue_registry.h>
21 #include <meta/interface/property/property_events.h>
22 #include <scene/interface/intf_mesh.h>
23 #include <scene/interface/intf_node.h>
24 #include <scene/interface/intf_scene.h>
25 #include <scene/ext/util.h>
26 
27 #include <render/intf_render_context.h>
28 
29 #include "SceneJS.h"
30 
31 using namespace SCENE_NS;
32 
Init(napi_env env,napi_value exports)33 void MorpherJS::Init(napi_env env, napi_value exports)
34 {
35     using namespace NapiApi;
36 
37     BASE_NS::vector<napi_property_descriptor> props;
38 
39     napi_value func;
40     auto status = napi_define_class(env, "Morpher", NAPI_AUTO_LENGTH, BaseObject::ctor<MorpherJS>(),
41         nullptr, props.size(), props.data(), &func);
42 
43     NapiApi::MyInstanceState* mis;
44     NapiApi::MyInstanceState::GetInstance(env, reinterpret_cast<void**>(&mis));
45     if (mis) {
46         mis->StoreCtor("Morpher", func);
47     }
48 }
49 
Dispose(NapiApi::FunctionContext<> & ctx)50 napi_value MorpherJS::Dispose(NapiApi::FunctionContext<>& ctx)
51 {
52     LOG_V("MorpherJS::Dispose");
53     DisposeNative(nullptr);
54     return {};
55 }
DisposeNative(void * scene)56 void MorpherJS::DisposeNative(void* scene)
57 {
58     if (!disposed_) {
59         disposed_ = true;
60         LOG_V("MorpherJS::DisposeNative");
61 
62         /// destroy the target object.
63         if (!targets_.IsEmpty()) {
64             NapiApi::Object target = targets_.GetObject();
65             while (!proxies_.empty()) {
66                 auto it = proxies_.begin();
67                 // removes hooks for meta property & jsproperty.
68                 target.DeleteProperty(it->first);
69                 // destroy the proxy.
70                 proxies_.erase(it);
71             }
72         }
73         targets_.Reset();
74         UnsetNativeObject();
75 
76         if (auto* sceneJS = static_cast<SceneJS*>(scene)) {
77             sceneJS->ReleaseDispose(reinterpret_cast<uintptr_t>(&scene_));
78         }
79         scene_.Reset();
80     }
81 }
82 
GetInstanceImpl(uint32_t id)83 void* MorpherJS::GetInstanceImpl(uint32_t id)
84 {
85     if (id == MorpherJS::ID) {
86         return this;
87     }
88     return nullptr;
89 }
Finalize(napi_env env)90 void MorpherJS::Finalize(napi_env env)
91 {
92     DisposeNative(scene_.GetObject().GetJsWrapper<SceneJS>());
93     BaseObject::Finalize(env);
94 }
MorpherJS(napi_env e,napi_callback_info i)95 MorpherJS::MorpherJS(napi_env e, napi_callback_info i) : BaseObject(e, i)
96 {
97     LOG_V("MorpherJS ++");
98     NapiApi::FunctionContext<NapiApi::Object, NapiApi::Object> fromJs(e, i);
99     if (!fromJs) {
100         return;
101     }
102     scene_ = { NapiApi::Object(fromJs.Arg<0>()) };
103     NapiApi::Object node = fromJs.Arg<1>();
104     const auto native = GetNativeObject();
105     if (!native) {
106         return;
107     }
108 
109     NapiApi::Object meJs(fromJs.This());
110 
111     BASE_NS::string name = native->GetName();
112     meJs.Set("name", name);
113     AddProperties(meJs, native);
114 
115     SetNativeObject(native, PtrType::WEAK);
116 }
~MorpherJS()117 MorpherJS::~MorpherJS()
118 {
119     LOG_V("MorpherJS --");
120     DisposeNative(nullptr);
121 }
122 
AddProperties(NapiApi::Object meJs,const META_NS::IObject::Ptr & obj)123 void MorpherJS::AddProperties(NapiApi::Object meJs, const META_NS::IObject::Ptr& obj)
124 {
125     auto morph = interface_cast<SCENE_NS::IMorpher>(obj);
126     if (!morph) {
127         return;
128     }
129 
130     NapiApi::Object targets(meJs.GetEnv());
131     napi_env e = targets.GetEnv();
132 
133     BASE_NS::vector<napi_property_descriptor> targetProps;
134 
135     auto morphNames = morph->MorphNames();
136     META_NS::ArrayProperty<float> morphWeights = morph->PropertyMorphWeights();
137 
138     for (META_NS::IArrayAny::IndexType i = 0; i < morphNames->GetSize(); i++) {
139         auto name = morphNames->GetValueAt(i);
140         if (name.empty()) {
141             name = "key_" + BASE_NS::to_string(i);
142         }
143         auto proxt = PropertyToArrayProxy(scene_.GetObject(), targets, morphWeights, i);
144         if (proxt) {
145             auto res = proxies_.insert_or_assign(name, proxt);
146             targetProps.push_back(CreateArrayProxyDesc(res.first->first.c_str(), BASE_NS::move(proxt)));
147         }
148     }
149 
150     if (!targetProps.empty()) {
151         napi_define_properties(targets.GetEnv(), targets.ToNapiValue(), targetProps.size(), targetProps.data());
152     }
153 
154     targets_ = NapiApi::StrongRef(targets);
155     meJs.Set("targets", targets_.GetValue());
156 }
157