• 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 
Process(RSContext & context)60     void Process(RSContext& context) override
61     {
62         // expand the tuple to function parameters
63         std::apply([&context](auto&... args) { return (*processFunc)(context, args...); }, params_);
64     }
65 
Marshalling(Parcel & parcel)66     bool Marshalling(Parcel& parcel) const override
67     {
68         return RSMarshallingHelper::Marshalling(parcel, commandType) &&
69                RSMarshallingHelper::Marshalling(parcel, commandSubType) &&
70                std::apply([&parcel](const auto&... args) { return RSMarshallingHelper::Marshalling(parcel, args...); },
71                    params_);
72     };
73 
Unmarshalling(Parcel & parcel)74     static RSCommand* Unmarshalling(Parcel& parcel)
75     {
76         std::tuple<Params...> params;
77         if (!std::apply(
78             [&parcel](auto&... args) { return RSMarshallingHelper::Unmarshalling(parcel, args...); }, params)) {
79             return nullptr;
80         }
81         return new RSCommandTemplate(std::move(params));
82     }
83 
84     static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry;
85 
86 private:
87     std::tuple<Params...> params_;
88 };
89 } // namespace Rosen
90 } // namespace OHOS
91 
92 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_TEMPLATES_H
93