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 "render_node_manager.h"
17 
18 #include <render/namespace.h>
19 
20 #include "util/log.h"
21 
22 using namespace BASE_NS;
23 using namespace CORE_NS;
24 
RENDER_BEGIN_NAMESPACE()25 RENDER_BEGIN_NAMESPACE()
26 IRenderNode* RenderNodeManager::Create(char const* nodeType)
27 {
28     if (auto const pos = factories_.find(nodeType); pos != factories_.end()) {
29         return pos->second.createNode();
30     }
31     return nullptr;
32 }
33 
Destroy(char const * nodeType,IRenderNode * aInstance)34 void RenderNodeManager::Destroy(char const* nodeType, IRenderNode* aInstance)
35 {
36     if (auto const pos = factories_.find(nodeType); pos != factories_.end()) {
37         return pos->second.destroyNode(aInstance);
38     }
39 }
40 
CreateRenderNode(char const * nodeType)41 unique_ptr<IRenderNode, RenderNodeTypeInfo::DestroyRenderNodeFn> RenderNodeManager::CreateRenderNode(
42     char const* nodeType)
43 {
44     if (auto const pos = factories_.find(nodeType); pos != factories_.end()) {
45         return { pos->second.createNode(), pos->second.destroyNode };
46     }
47     return { nullptr, nullptr };
48 }
49 
GetRenderNodeTypeInfoFlags(char const * nodeType)50 RenderNodeManager::RenderNodeTypeInfoFlags RenderNodeManager::GetRenderNodeTypeInfoFlags(char const* nodeType)
51 {
52     if (auto const pos = factories_.find(nodeType); pos != factories_.end()) {
53         return { pos->second.renderNodeBackendFlags, pos->second.renderNodeClassType };
54     }
55     return {};
56 }
57 
AddRenderNodeFactory(const RenderNodeTypeInfo & typeInfo)58 void RenderNodeManager::AddRenderNodeFactory(const RenderNodeTypeInfo& typeInfo)
59 {
60     if (typeInfo.createNode && typeInfo.destroyNode) {
61         factories_.insert({ typeInfo.typeName, typeInfo });
62     } else {
63         PLUGIN_LOG_E("RenderNodeTypeInfo must provide non-null function pointers");
64         PLUGIN_ASSERT(typeInfo.createNode && "createNode cannot be null");
65         PLUGIN_ASSERT(typeInfo.destroyNode && "destroyNode cannot be null");
66     }
67 }
68 
RemoveRenderNodeFactory(const RenderNodeTypeInfo & typeInfo)69 void RenderNodeManager::RemoveRenderNodeFactory(const RenderNodeTypeInfo& typeInfo)
70 {
71     factories_.erase(string_view(typeInfo.typeName));
72 }
73 RENDER_END_NAMESPACE()
74