• 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 "MeshResourceJS.h"
17 
18 #include <scene/interface/intf_create_mesh.h>
19 #include <scene/interface/intf_mesh_resource.h>
20 #include <scene/interface/intf_scene.h>
21 
22 #include "SceneJS.h"
23 
MeshResourceJS(napi_env e,napi_callback_info i)24 MeshResourceJS::MeshResourceJS(napi_env e, napi_callback_info i)
25     : BaseObject<MeshResourceJS>(e, i), SceneResourceImpl(SceneResourceType::MESH_RESOURCE)
26 {
27     NapiApi::FunctionContext<NapiApi::Object, NapiApi::Object> fromJs(e, i);
28     NapiApi::Object meJs(fromJs.This());
29 
30     if (!fromJs) {
31         // We except this was created internally and that things will be initialized later.
32         return;
33     }
34     scene_ = fromJs.Arg<0>().valueOrDefault();
35     // Add the dispose hook to scene so that the MeshResourceJS is disposed when scene is disposed.
36     if (auto sceneJS = GetJsWrapper<SceneJS>(scene_.GetObject())) {
37         sceneJS->DisposeHook(reinterpret_cast<uintptr_t>(&scene_), fromJs.This());
38     }
39 
40     if (!GetNativeMeta<SCENE_NS::IScene>(scene_.GetObject())) {
41         LOG_F("INVALID SCENE!");
42     }
43 
44     auto resourceParams = NapiApi::Object { fromJs.Arg<1>() };
45     auto nativeObj = GetNativeObjectParam<META_NS::IObject>(resourceParams);
46     if (!nativeObj) {
47         nativeObj = META_NS::GetObjectRegistry().Create<META_NS::IObject>(SCENE_NS::ClassId::MeshResource);
48     }
49 
50     // TODO: Name remains undefined. This has no effect. There are prop problems with other SceneResourceImpls as well.
51     auto name = BASE_NS::string {};
52     if (auto nameParam = resourceParams.Get<BASE_NS::string>("name"); nameParam.IsDefined()) {
53         name = nameParam;
54     } else if (nativeObj) {
55         name = nativeObj->GetName();
56     }
57     if (!name.empty()) {
58         meJs.Set("name", name);
59     }
60 
61     SetNativeObject(nativeObj, false);
62     StoreJsObj(nativeObj, meJs);
63 
64     GeometryDefinition::GeometryDefinition* geomDef {};
65     napi_get_value_external(e, resourceParams.Get("GeometryDefinition"), (void**)&geomDef);
66     geometryDefinition_.reset(geomDef);
67 }
68 
Init(napi_env env,napi_value exports)69 void MeshResourceJS::Init(napi_env env, napi_value exports)
70 {
71     BASE_NS::vector<napi_property_descriptor> node_props;
72     SceneResourceImpl::GetPropertyDescs(node_props);
73 
74 #define NAPI_API_JS_NAME MeshResource
75     DeclareClass();
76 #undef NAPI_API_JS_NAME
77 }
78 
GetInstanceImpl(uint32_t id)79 void* MeshResourceJS::GetInstanceImpl(uint32_t id)
80 {
81     return (id == MeshResourceJS::ID) ? this : SceneResourceImpl::GetInstanceImpl(id);
82 }
83 
CreateMesh()84 SCENE_NS::IMesh::Ptr MeshResourceJS::CreateMesh()
85 {
86     auto scene = GetNativeMeta<SCENE_NS::IScene>(scene_.GetObject());
87     if (!scene || !geometryDefinition_) {
88         return {};
89     }
90 
91     const auto meshCreator = scene->CreateObject<SCENE_NS::ICreateMesh>(SCENE_NS::ClassId::MeshCreator).GetResult();
92     // Name and material aren't set here. Name is set in the constructor. Material needs to be manually set later.
93     auto meshConfig = SCENE_NS::MeshConfig {};
94     return geometryDefinition_->CreateMesh(meshCreator, meshConfig);
95 }
96 
DisposeNative(void * scene)97 void MeshResourceJS::DisposeNative(void* scene)
98 {
99     if (disposed_) {
100         return;
101     }
102     disposed_ = true;
103     if (auto node = interface_pointer_cast<SCENE_NS::IMeshResource>(GetNativeObject())) {
104         // reset the native object refs
105         SetNativeObject(nullptr, false);
106         SetNativeObject(nullptr, true);
107     }
108     geometryDefinition_.reset();
109 
110     if (auto* sceneJS = static_cast<SceneJS*>(scene)) {
111         sceneJS->ReleaseDispose(reinterpret_cast<uintptr_t>(&scene_));
112     }
113 
114     scene_.Reset();
115 }
116 
Finalize(napi_env env)117 void MeshResourceJS::Finalize(napi_env env)
118 {
119     DisposeNative(GetJsWrapper<SceneJS>(scene_.GetObject()));
120     BaseObject::Finalize(env);
121 }