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 #include <napi_api.h>
16
17 #include <core/os/platform_create_info.h>
18
19 #include <meta/interface/intf_object_context.h>
20 #include <meta/interface/intf_object_registry.h>
21
22 #include <scene/interface/intf_scene.h>
23
24 #include "scene_adapter/scene_adapter.h"
25 #include "3d_widget_adapter_log.h"
26
27 void RegisterClasses(napi_env env, napi_value exports);
28
29 namespace OHOS::Render3D {
30 /*
31 * Function registering all props and functions of ohos.medialibrary module
32 */
Export(napi_env env,napi_value exports)33 static napi_value Export(napi_env env, napi_value exports)
34 {
35 auto sceneAdapter_ = std::make_shared<OHOS::Render3D::SceneAdapter>();
36 sceneAdapter_->LoadPluginsAndInit();
37
38 NapiApi::MyInstanceState *mis;
39 GetInstanceData(env, (void **)&mis);
40 if (mis) {
41 // should not happen?
42 WIDGET_LOGW("scene.napi reloaded!");
43 } else {
44 // Create storage (js object that will contain constructors for "native" objects)
45 napi_value Storage;
46 napi_create_object(env, &Storage);
47 mis = new NapiApi::MyInstanceState(env, Storage);
48 auto status = SetInstanceData(
49 env,
50 mis,
51 [](napi_env env, void *finalize_data, void *finalize_hint) {
52 WIDGET_LOGD("scene.napi finalizer begin");
53 auto d = static_cast<NapiApi::MyInstanceState *>(finalize_data);
54 // Make sure that the render thread is gone.
55 OHOS::Render3D::SceneAdapter::DeinitRenderThread();
56 // check for leaked objects.
57 // note this might return false positives.
58 // (as it lists ALL object instances that exist, which might come from somewhere else. )
59 auto& obr = META_NS::GetObjectRegistry();
60 auto objs = obr.GetAllObjectInstances();
61 for (auto o : objs) {
62 if (o.get() != interface_cast<META_NS::IObject>(obr.GetDefaultObjectContext())) {
63 WIDGET_LOGE("leaking %s", BASE_NS::string(o->GetClassName()).c_str());
64 }
65 }
66 // release plugin registry
67 OHOS::Render3D::SceneAdapter::ShutdownPluginRegistry();
68 delete d;
69 },
70 nullptr);
71 if (status != napi_ok) {
72 WIDGET_LOGE("napi_set_instance_data api fail!");
73 delete mis;
74 }
75
76 RegisterClasses(env, exports);
77 }
78
79 return exports;
80 }
81
82 /*
83 * module define
84 */
85 static napi_module g_module = {
86 .nm_version = 1,
87 .nm_flags = 0,
88 .nm_filename = nullptr,
89 .nm_register_func = Export,
90 .nm_modname = "scene.napi",
91 .nm_priv = (reinterpret_cast<void *>(0)),
92 .reserved = {0}
93 };
94
95 /*
96 * module register
97 */
Graphics3dKitRegisterModule(void)98 extern "C" __attribute__((constructor)) void Graphics3dKitRegisterModule(void)
99 {
100 WIDGET_LOGD("graphics 3d register");
101 napi_module_register(&g_module);
102 }
103 } // namespace OHOS::Render3D
104