• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_TEMPLATES_H
17 #define ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_TEMPLATES_H
18 
19 #include "command/rs_command.h"
20 #include "command/rs_command_factory.h"
21 #include "platform/common/rs_log.h"
22 #include "transaction/rs_marshalling_helper.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 class RSUIDirector;
27 
28 // avoiding C++ macros spliting parameters
29 #ifndef ARG
30 #define ARG(...) __VA_ARGS__
31 #endif
32 
33 // Add new RSCommand as alias of template class
34 // Explicit instantiating templates will register the unmarshalling function into RSCommandFactory.
35 // To avoid redundant registry, make sure templates only instantiated once.
36 #ifdef ROSEN_INSTANTIATE_COMMAND_TEMPLATE
37 #define ADD_COMMAND(ALIAS, TYPE)           \
38     using ALIAS = RSCommandTemplate<TYPE>; \
39     template class RSCommandTemplate<TYPE>;
40 #else
41 #define ADD_COMMAND(ALIAS, TYPE) using ALIAS = RSCommandTemplate<TYPE>;
42 #endif
43 
44 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename... Params>
45 class RSCommandTemplate : public RSCommand {
46 public:
RSCommandTemplate(const Params &...params)47     RSCommandTemplate(const Params&... params) : params_(params...) {}
RSCommandTemplate(std::tuple<Params...> && params)48     RSCommandTemplate(std::tuple<Params...>&& params) : params_(std::move(params)) {}
49     ~RSCommandTemplate() override = default;
50 
GetType()51     uint16_t GetType() const override
52     {
53         return commandType;
54     }
GetSubType()55     uint16_t GetSubType() const override
56     {
57         return commandSubType;
58     }
59 
GetNodeId()60     NodeId GetNodeId() const override
61     {
62         using idType = typename std::tuple_element<0, decltype(params_)>::type;
63         if (std::is_same<NodeId, idType>::value) {
64             return std::get<0>(params_);
65         }
66         return 0; // invalidId
67     }
68 
Process(RSContext & context)69     void Process(RSContext& context) override
70     {
71         // expand the tuple to function parameters
72         std::apply([&context](auto&... args) { return (*processFunc)(context, args...); }, params_);
73     }
74 
Marshalling(Parcel & parcel)75     bool Marshalling(Parcel& parcel) const override
76     {
77         return RSMarshallingHelper::Marshalling(parcel, commandType) &&
78                RSMarshallingHelper::Marshalling(parcel, commandSubType) &&
79                std::apply([&parcel](const auto&... args) { return RSMarshallingHelper::Marshalling(parcel, args...); },
80                    params_);
81     };
82 
Unmarshalling(Parcel & parcel)83     [[nodiscard]] static RSCommand* Unmarshalling(Parcel& parcel)
84     {
85         std::tuple<Params...> params;
86         if (!std::apply(
87             [&parcel](auto&... args) { return RSMarshallingHelper::Unmarshalling(parcel, args...); }, params)) {
88             return nullptr;
89         }
90         return new RSCommandTemplate(std::move(params));
91     }
92 
93     static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry;
94 
95 private:
96     std::tuple<Params...> params_;
97 };
98 } // namespace Rosen
99 } // namespace OHOS
100 
101 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_TEMPLATES_H
102