1 /*
2 * Copyright (c) 2021-2023 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 "command/rs_command_factory.h"
17
18 #include "platform/common/rs_log.h"
19
20 // manually instantiate all RScommands (this is when the registry happens)
21 #define ROSEN_INSTANTIATE_COMMAND_TEMPLATE
22 // node
23 #include "command/rs_base_node_command.h"
24 #include "command/rs_canvas_drawing_node_command.h"
25 #include "command/rs_canvas_node_command.h"
26 #include "command/rs_display_node_command.h"
27 #include "command/rs_effect_node_command.h"
28 #include "command/rs_node_command.h"
29 #include "command/rs_proxy_node_command.h"
30 #include "command/rs_root_node_command.h"
31 #include "command/rs_surface_node_command.h"
32 // animation
33 #include "command/rs_animation_command.h"
34 // read showing property commands
35 #include "command/rs_node_showing_command.h"
36
37 #undef ROSEN_INSTANTIATE_COMMAND_TEMPLATE
38
39 namespace OHOS {
40 namespace Rosen {
41
42 namespace {
MakeKey(uint16_t commandType,uint16_t commandSubType)43 inline uint32_t MakeKey(uint16_t commandType, uint16_t commandSubType)
44 {
45 // 16: concat two uint16 into uint32
46 return ((uint32_t)commandType << 16) | commandSubType;
47 }
48 } // namespace
49
Instance()50 RSCommandFactory& RSCommandFactory::Instance()
51 {
52 static RSCommandFactory instance;
53 return instance;
54 }
55
Register(uint16_t type,uint16_t subtype,UnmarshallingFunc func)56 void RSCommandFactory::Register(uint16_t type, uint16_t subtype, UnmarshallingFunc func)
57 {
58 auto result = unmarshallingFuncLUT_.try_emplace(MakeKey(type, subtype), func);
59 if (!result.second) {
60 ROSEN_LOGE("RSCommandFactory::Register, Duplicate command & sub_command detected! type: %d subtype: %d", type,
61 subtype);
62 }
63 }
64
GetUnmarshallingFunc(uint16_t type,uint16_t subtype)65 UnmarshallingFunc RSCommandFactory::GetUnmarshallingFunc(uint16_t type, uint16_t subtype)
66 {
67 auto it = unmarshallingFuncLUT_.find(MakeKey(type, subtype));
68 if (it == unmarshallingFuncLUT_.end()) {
69 ROSEN_LOGE("RSCommandFactory::GetUnmarshallingFunc, Func is not found, type=%d subtype=%d", type, subtype);
70 return nullptr;
71 }
72 return it->second;
73 }
74 } // namespace Rosen
75 } // namespace OHOS
76