• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #ifndef RENDERCONTEXTJS_H
17 #define RENDERCONTEXTJS_H
18 
19 #include <base/containers/unordered_map.h>
20 
21 #include <meta/api/threading/mutex.h>
22 
23 #include <scene/interface/resource/intf_render_resource_manager.h>
24 
25 #ifdef __SCENE_ADAPTER__
26 #include "scene_adapter/intf_scene_adapter.h"
27 #endif
28 
29 #include "BaseObjectJS.h"
30 #include "DisposeContainer.h"
31 
32 struct GlobalResources;
33 
34 class RenderResources {
35 public:
36     RenderResources(napi_env env);
37     ~RenderResources();
38 
39     void DisposeHook(uintptr_t token, NapiApi::Object);
40     void ReleaseDispose(uintptr_t token);
41 
42     void StrongDisposeHook(uintptr_t token, NapiApi::Object);
43     void ReleaseStrongDispose(uintptr_t token);
44 
45     void StoreBitmap(BASE_NS::string_view uri, SCENE_NS::IBitmap::Ptr bitmap);
46     SCENE_NS::IBitmap::Ptr FetchBitmap(BASE_NS::string_view uri);
47 
48 private:
49     CORE_NS::Mutex mutex_;
50     napi_env env_;
51     BASE_NS::unordered_map<BASE_NS::string, SCENE_NS::IBitmap::Ptr> bitmaps_;
52     DisposeContainer disposeContainer_;
53 };
54 
55 class RenderContextJS : public BaseObject {
56 public:
57     static constexpr uint32_t ID = 201;
58     static void Init(napi_env env, napi_value exports);
59     static void RegisterEnums(NapiApi::Object exports);
60 
61     RenderContextJS(napi_env, napi_callback_info);
62     ~RenderContextJS() override;
63     void* GetInstanceImpl(uint32_t) override;
64 
65     BASE_NS::shared_ptr<RenderResources> GetResources() const;
66 
67     static napi_value GetDefaultContext(napi_env env);
68 
69 private:
70     napi_value Dispose(NapiApi::FunctionContext<>& ctx);
71     void DisposeNative(void* id) override;
72     void Finalize(napi_env env) override;
73 
74 public:
75     napi_value GetResourceFactory(NapiApi::FunctionContext<>& ctx);
76     napi_value LoadPlugin(NapiApi::FunctionContext<BASE_NS::string>& ctx);
77 
78     // create shader
79     napi_value CreateShader(NapiApi::FunctionContext<NapiApi::Object>& ctx);
80     // create image
81     napi_value CreateImage(NapiApi::FunctionContext<NapiApi::Object>& ctx);
82     // create mesh
83     napi_value CreateMeshResource(NapiApi::FunctionContext<NapiApi::Object, NapiApi::Object>& ctx);
84     // create sampler
85     // create scene
86     // Register shader Path
87     napi_value RegisterResourcePath(NapiApi::FunctionContext<BASE_NS::string, BASE_NS::string>& ctx);
88 
89 private:
90     CORE_NS::Mutex mutex_;
91     napi_env env_;
92     SCENE_NS::IRenderResourceManager::Ptr renderManager_;
93     BASE_NS::shared_ptr<GlobalResources> globalResources_;
94     mutable BASE_NS::weak_ptr<RenderResources> resources_;
95 };
96 
97 #endif // RENDERCONTEXTJS_H
98