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 <scene/interface/intf_camera.h>
17 #include <scene/interface/intf_material.h>
18 #include <scene/interface/resource/image_info.h>
19
20 #include <base/containers/unordered_map.h>
21
22 #include <meta/base/meta_types.h>
23 #include <meta/ext/any_builder.h>
24 #include <meta/ext/serialization/common_value_serializers.h>
25 #include <meta/interface/detail/any.h>
26
27 SCENE_BEGIN_NAMESPACE()
28
29 namespace Internal {
30
31 using IntPair = BASE_NS::pair<BASE_NS::string_view, int64_t>;
32
33 template<typename Type>
ExportIntPair(const BASE_NS::string & name,const IntPair & i1,const IntPair & i2)34 static auto ExportIntPair(const BASE_NS::string& name, const IntPair& i1, const IntPair& i2)
35 {
36 auto obj = META_NS::CreateObjectNode(META_NS::UidFromType<Type>(), name);
37 if (obj) {
38 auto& r = META_NS::GetObjectRegistry();
39 if (auto n = r.Create<META_NS::IMapNode>(META_NS::ClassId::MapNode)) {
40 if (auto in = r.Create<META_NS::IIntNode>(META_NS::ClassId::IntNode)) {
41 in->SetValue(i1.second);
42 n->AddNode(i1.first, in);
43 }
44 if (auto in = r.Create<META_NS::IIntNode>(META_NS::ClassId::IntNode)) {
45 in->SetValue(i2.second);
46 n->AddNode(i2.first, in);
47 }
48 obj->SetMembers(n);
49 }
50 }
51 return obj;
52 }
53
ImportIntPair(const META_NS::ISerNode::ConstPtr & node,IntPair & i1,IntPair & i2)54 static bool ImportIntPair(const META_NS::ISerNode::ConstPtr& node, IntPair& i1, IntPair& i2)
55 {
56 if (auto obj = interface_cast<META_NS::IObjectNode>(node)) {
57 if (auto m = interface_cast<META_NS::IMapNode>(obj->GetMembers())) {
58 auto rslNode = m->FindNode(i1.first);
59 auto rsloNode = m->FindNode(i2.first);
60 return META_NS::ExtractInteger(rslNode, i1.second) && META_NS::ExtractInteger(rsloNode, i2.second);
61 }
62 }
63 return false;
64 }
65
RegisterSerializers()66 void RegisterSerializers()
67 {
68 auto& data = META_NS::GetObjectRegistry().GetGlobalSerializationData();
69
70 META_NS::RegisterSerializer<RenderSort>(
71 [](auto&, const RenderSort& v) {
72 return ExportIntPair<RenderSort>("RenderSort", IntPair { "renderSortLayer", v.renderSortLayer },
73 IntPair { "renderSortLayerOrder", v.renderSortLayerOrder });
74 },
75 [](auto&, const META_NS::ISerNode::ConstPtr& node, RenderSort& out) {
76 IntPair i1 { "renderSortLayer", {} };
77 IntPair i2 { "renderSortLayerOrder", {} };
78 auto res = ImportIntPair(node, i1, i2);
79 if (res) {
80 out.renderSortLayer = static_cast<uint8_t>(i1.second);
81 out.renderSortLayerOrder = static_cast<uint8_t>(i2.second);
82 }
83 return res;
84 });
85
86 META_NS::RegisterSerializer<ColorFormat>(
87 [](auto&, const ColorFormat& v) {
88 return ExportIntPair<ColorFormat>(
89 "ColorFormat", IntPair { "format", v.format }, IntPair { "usageFlags", v.usageFlags });
90 },
91 [](auto&, const META_NS::ISerNode::ConstPtr& node, ColorFormat& out) {
92 IntPair i1 { "format", {} };
93 IntPair i2 { "usageFlags", {} };
94 auto res = ImportIntPair(node, i1, i2);
95 if (res) {
96 out.format = static_cast<BASE_NS::Format>(i1.second);
97 out.usageFlags = static_cast<uint32_t>(i2.second);
98 }
99 return res;
100 });
101
102 META_NS::RegisterSerializer<ImageLoadInfo>(
103 data,
104 [](auto&, const ImageLoadInfo& v) {
105 auto obj = META_NS::CreateObjectNode(META_NS::UidFromType<ImageLoadInfo>(), "ImageLoadInfo");
106 if (obj) {
107 auto& r = META_NS::GetObjectRegistry();
108 if (auto n = r.Create<META_NS::IMapNode>(META_NS::ClassId::MapNode)) {
109 if (auto in = r.Create<META_NS::IUIntNode>(META_NS::ClassId::UIntNode)) {
110 in->SetValue(static_cast<uint64_t>(v.loadFlags));
111 n->AddNode("LoadFlags", in);
112 }
113 if (auto in = r.Create<META_NS::IUIntNode>(META_NS::ClassId::UIntNode)) {
114 in->SetValue(static_cast<uint64_t>(v.info.usageFlags));
115 n->AddNode("UsageFlags", in);
116 }
117 if (auto in = r.Create<META_NS::IUIntNode>(META_NS::ClassId::UIntNode)) {
118 in->SetValue(static_cast<uint64_t>(v.info.memoryFlags));
119 n->AddNode("MemoryFlags", in);
120 }
121 if (auto in = r.Create<META_NS::IUIntNode>(META_NS::ClassId::UIntNode)) {
122 in->SetValue(static_cast<uint64_t>(v.info.creationFlags));
123 n->AddNode("CreationFlags", in);
124 }
125 obj->SetMembers(n);
126 }
127 }
128 return obj;
129 },
130 [](auto&, const META_NS::ISerNode::ConstPtr& node, ImageLoadInfo& out) {
131 if (auto obj = interface_cast<META_NS::IObjectNode>(node)) {
132 if (auto m = interface_cast<META_NS::IMapNode>(obj->GetMembers())) {
133 auto lf = m->FindNode("LoadFlags");
134 auto uf = m->FindNode("UsageFlags");
135 auto mf = m->FindNode("MemoryFlags");
136 auto cf = m->FindNode("CreationFlags");
137 return META_NS::ExtractInteger(lf, out.loadFlags) &&
138 META_NS::ExtractInteger(uf, out.info.usageFlags) &&
139 META_NS::ExtractInteger(mf, out.info.memoryFlags) &&
140 META_NS::ExtractInteger(cf, out.info.creationFlags);
141 }
142 }
143 return false;
144 });
145 }
146
UnRegisterSerializers()147 void UnRegisterSerializers()
148 {
149 auto& data = META_NS::GetObjectRegistry().GetGlobalSerializationData();
150 META_NS::UnregisterSerializer<RenderSort>(data);
151 META_NS::UnregisterSerializer<ColorFormat>(data);
152 }
153
154 } // namespace Internal
155 SCENE_END_NAMESPACE()
156