1 /* 2 * Copyright (c) 2021 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 "transaction/rs_marshalling_helper.h" 22 23 namespace OHOS { 24 namespace Rosen { 25 class RSUIDirector; 26 27 // avoiding C++ macros spilting parameters 28 #ifndef ARG 29 #define ARG(...) __VA_ARGS__ 30 #endif 31 32 // Add new RSCommand as alias of template class 33 // Explicit instantiating templates will register the unmarshalling function into RSCommandFactory. 34 // To avoid redundant registary, make sure templates only instantiated once. 35 #ifdef ROSEN_INSTANTIATE_COMMAND_TEMPLATE 36 #define ADD_COMMAND(ALIAS, TYPE) \ 37 using ALIAS = RSCommandTemplate<TYPE>; \ 38 template class RSCommandTemplate<TYPE>; 39 #else 40 #define ADD_COMMAND(ALIAS, TYPE) using ALIAS = RSCommandTemplate<TYPE>; 41 #endif 42 43 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename... Ts> 44 class RSCommandTemplate; 45 46 template<uint16_t commandType, uint16_t commandSubType, auto processFunc> 47 class RSCommandTemplate<commandType, commandSubType, processFunc> : public RSCommand { 48 public: RSCommandTemplate()49 RSCommandTemplate() {} 50 virtual ~RSCommandTemplate() = default; 51 52 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)53 bool Marshalling(Parcel& parcel) const override 54 { 55 return RSMarshallingHelper::Marshalling(parcel, commandType) && 56 RSMarshallingHelper::Marshalling(parcel, commandSubType); 57 } 58 Unmarshalling(Parcel & parcel)59 static RSCommand* Unmarshalling(Parcel& parcel) 60 { 61 return new RSCommandTemplate(); 62 } 63 64 static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry; 65 #endif // ROSEN_OHOS 66 Process(RSContext & context)67 void Process(RSContext& context) override 68 { 69 (*processFunc)(context); 70 } 71 72 private: 73 }; 74 75 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename T1> 76 class RSCommandTemplate<commandType, commandSubType, processFunc, T1> : public RSCommand { 77 public: RSCommandTemplate(const T1 & p1)78 RSCommandTemplate(const T1& p1) : parameter1_(p1) {} 79 virtual ~RSCommandTemplate() = default; 80 81 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)82 bool Marshalling(Parcel& parcel) const override 83 { 84 return RSMarshallingHelper::Marshalling(parcel, commandType) && 85 RSMarshallingHelper::Marshalling(parcel, commandSubType) && 86 RSMarshallingHelper::Marshalling(parcel, parameter1_); 87 } 88 Unmarshalling(Parcel & parcel)89 static RSCommand* Unmarshalling(Parcel& parcel) 90 { 91 T1 parameter1; 92 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter1)) { 93 return nullptr; 94 } 95 return new RSCommandTemplate(parameter1); 96 } 97 98 static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry; 99 #endif // ROSEN_OHOS 100 Process(RSContext & context)101 void Process(RSContext& context) override 102 { 103 (*processFunc)(context, parameter1_); 104 } 105 106 private: 107 T1 parameter1_; 108 }; 109 110 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename T1, typename T2> 111 class RSCommandTemplate<commandType, commandSubType, processFunc, T1, T2> : public RSCommand { 112 public: RSCommandTemplate(const T1 & p1,const T2 & p2)113 RSCommandTemplate(const T1& p1, const T2& p2) : parameter1_(p1), parameter2_(p2) {} 114 virtual ~RSCommandTemplate() = default; 115 116 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)117 bool Marshalling(Parcel& parcel) const override 118 { 119 return RSMarshallingHelper::Marshalling(parcel, commandType) && 120 RSMarshallingHelper::Marshalling(parcel, commandSubType) && 121 RSMarshallingHelper::Marshalling(parcel, parameter1_) && 122 RSMarshallingHelper::Marshalling(parcel, parameter2_); 123 } 124 Unmarshalling(Parcel & parcel)125 static RSCommand* Unmarshalling(Parcel& parcel) 126 { 127 T1 parameter1; 128 T2 parameter2; 129 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter1)) { 130 return nullptr; 131 } 132 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter2)) { 133 return nullptr; 134 } 135 return new RSCommandTemplate(parameter1, parameter2); 136 } 137 138 static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry; 139 #endif // ROSEN_OHOS 140 Process(RSContext & context)141 void Process(RSContext& context) override 142 { 143 (*processFunc)(context, parameter1_, parameter2_); 144 } 145 146 private: 147 T1 parameter1_; 148 T2 parameter2_; 149 }; 150 151 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename T1, typename T2, typename T3> 152 class RSCommandTemplate<commandType, commandSubType, processFunc, T1, T2, T3> : public RSCommand { 153 public: RSCommandTemplate(const T1 & p1,const T2 & p2,const T3 & p3)154 RSCommandTemplate(const T1& p1, const T2& p2, const T3& p3) : parameter1_(p1), parameter2_(p2), parameter3_(p3) {} 155 virtual ~RSCommandTemplate() = default; 156 157 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)158 bool Marshalling(Parcel& parcel) const override 159 { 160 return RSMarshallingHelper::Marshalling(parcel, commandType) && 161 RSMarshallingHelper::Marshalling(parcel, commandSubType) && 162 RSMarshallingHelper::Marshalling(parcel, parameter1_) && 163 RSMarshallingHelper::Marshalling(parcel, parameter2_) && 164 RSMarshallingHelper::Marshalling(parcel, parameter3_); 165 } 166 Unmarshalling(Parcel & parcel)167 static RSCommand* Unmarshalling(Parcel& parcel) 168 { 169 T1 parameter1; 170 T2 parameter2; 171 T3 parameter3; 172 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter1)) { 173 return nullptr; 174 } 175 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter2)) { 176 return nullptr; 177 } 178 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter3)) { 179 return nullptr; 180 } 181 return new RSCommandTemplate(parameter1, parameter2, parameter3); 182 } 183 184 static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry; 185 #endif // ROSEN_OHOS 186 Process(RSContext & context)187 void Process(RSContext& context) override 188 { 189 (*processFunc)(context, parameter1_, parameter2_, parameter3_); 190 } 191 192 private: 193 T1 parameter1_; 194 T2 parameter2_; 195 T3 parameter3_; 196 }; 197 198 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename T1, typename T2, typename T3, 199 typename T4> 200 class RSCommandTemplate<commandType, commandSubType, processFunc, T1, T2, T3, T4> : public RSCommand { 201 public: RSCommandTemplate(const T1 & p1,const T2 & p2,const T3 & p3,const T4 & p4)202 RSCommandTemplate(const T1& p1, const T2& p2, const T3& p3, const T4& p4) 203 : parameter1_(p1), parameter2_(p2), parameter3_(p3), parameter4_(p4) 204 {} 205 virtual ~RSCommandTemplate() = default; 206 207 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)208 bool Marshalling(Parcel& parcel) const override 209 { 210 return RSMarshallingHelper::Marshalling(parcel, commandType) && 211 RSMarshallingHelper::Marshalling(parcel, commandSubType) && 212 RSMarshallingHelper::Marshalling(parcel, parameter1_) && 213 RSMarshallingHelper::Marshalling(parcel, parameter2_) && 214 RSMarshallingHelper::Marshalling(parcel, parameter3_) && 215 RSMarshallingHelper::Marshalling(parcel, parameter4_); 216 } 217 Unmarshalling(Parcel & parcel)218 static RSCommand* Unmarshalling(Parcel& parcel) 219 { 220 T1 parameter1; 221 T2 parameter2; 222 T3 parameter3; 223 T4 parameter4; 224 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter1)) { 225 return nullptr; 226 } 227 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter2)) { 228 return nullptr; 229 } 230 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter3)) { 231 return nullptr; 232 } 233 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter4)) { 234 return nullptr; 235 } 236 return new RSCommandTemplate(parameter1, parameter2, parameter3, parameter4); 237 } 238 239 static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry; 240 #endif // ROSEN_OHOS 241 Process(RSContext & context)242 void Process(RSContext& context) override 243 { 244 (*processFunc)(context, parameter1_, parameter2_, parameter3_, parameter4_); 245 } 246 247 private: 248 T1 parameter1_; 249 T2 parameter2_; 250 T3 parameter3_; 251 T4 parameter4_; 252 }; 253 254 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename T1, typename T2, typename T3, 255 typename T4, typename T5> 256 class RSCommandTemplate<commandType, commandSubType, processFunc, T1, T2, T3, T4, T5> : public RSCommand { 257 public: RSCommandTemplate(const T1 & p1,const T2 & p2,const T3 & p3,const T4 & p4,const T5 & p5)258 RSCommandTemplate(const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5) 259 : parameter1_(p1), parameter2_(p2), parameter3_(p3), parameter4_(p4), parameter5_(p5) 260 {} 261 virtual ~RSCommandTemplate() = default; 262 263 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)264 bool Marshalling(Parcel& parcel) const override 265 { 266 return RSMarshallingHelper::Marshalling(parcel, commandType) && 267 RSMarshallingHelper::Marshalling(parcel, commandSubType) && 268 RSMarshallingHelper::Marshalling(parcel, parameter1_) && 269 RSMarshallingHelper::Marshalling(parcel, parameter2_) && 270 RSMarshallingHelper::Marshalling(parcel, parameter3_) && 271 RSMarshallingHelper::Marshalling(parcel, parameter4_) && 272 RSMarshallingHelper::Marshalling(parcel, parameter5_); 273 } 274 Unmarshalling(Parcel & parcel)275 static RSCommand* Unmarshalling(Parcel& parcel) 276 { 277 T1 parameter1; 278 T2 parameter2; 279 T3 parameter3; 280 T4 parameter4; 281 T5 parameter5; 282 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter1)) { 283 return nullptr; 284 } 285 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter2)) { 286 return nullptr; 287 } 288 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter3)) { 289 return nullptr; 290 } 291 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter4)) { 292 return nullptr; 293 } 294 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter5)) { 295 return nullptr; 296 } 297 return new RSCommandTemplate(parameter1, parameter2, parameter3, parameter4, parameter5); 298 } 299 300 static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry; 301 #endif // ROSEN_OHOS 302 Process(RSContext & context)303 void Process(RSContext& context) override 304 { 305 (*processFunc)(context, parameter1_, parameter2_, parameter3_, parameter4_, parameter5_); 306 } 307 308 private: 309 T1 parameter1_; 310 T2 parameter2_; 311 T3 parameter3_; 312 T4 parameter4_; 313 T5 parameter5_; 314 }; 315 316 template<uint16_t commandType, uint16_t commandSubType, auto processFunc, typename T1, typename T2, typename T3, 317 typename T4, typename T5, typename T6> 318 class RSCommandTemplate<commandType, commandSubType, processFunc, T1, T2, T3, T4, T5, T6> : public RSCommand { 319 public: RSCommandTemplate(const T1 & p1,const T2 & p2,const T3 & p3,const T4 & p4,const T5 & p5,const T6 & p6)320 RSCommandTemplate(const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5, const T6& p6) 321 : parameter1_(p1), parameter2_(p2), parameter3_(p3), parameter4_(p4), parameter5_(p5), parameter6_(p6) 322 {} 323 virtual ~RSCommandTemplate() = default; 324 325 #ifdef ROSEN_OHOS Marshalling(Parcel & parcel)326 bool Marshalling(Parcel& parcel) const override 327 { 328 return RSMarshallingHelper::Marshalling(parcel, commandType) && 329 RSMarshallingHelper::Marshalling(parcel, commandSubType) && 330 RSMarshallingHelper::Marshalling(parcel, parameter1_) && 331 RSMarshallingHelper::Marshalling(parcel, parameter2_) && 332 RSMarshallingHelper::Marshalling(parcel, parameter3_) && 333 RSMarshallingHelper::Marshalling(parcel, parameter4_) && 334 RSMarshallingHelper::Marshalling(parcel, parameter5_) && 335 RSMarshallingHelper::Marshalling(parcel, parameter6_); 336 } 337 Unmarshalling(Parcel & parcel)338 static RSCommand* Unmarshalling(Parcel& parcel) 339 { 340 T1 parameter1; 341 T2 parameter2; 342 T3 parameter3; 343 T4 parameter4; 344 T5 parameter5; 345 T6 parameter6; 346 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter1)) { 347 return nullptr; 348 } 349 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter2)) { 350 return nullptr; 351 } 352 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter3)) { 353 return nullptr; 354 } 355 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter4)) { 356 return nullptr; 357 } 358 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter5)) { 359 return nullptr; 360 } 361 if (!RSMarshallingHelper::Unmarshalling(parcel, parameter6)) { 362 return nullptr; 363 } 364 return new RSCommandTemplate(parameter1, parameter2, parameter3, parameter4, parameter5, parameter6); 365 } 366 367 static inline RSCommandRegister<commandType, commandSubType, Unmarshalling> registry; 368 #endif // ROSEN_OHOS 369 Process(RSContext & context)370 void Process(RSContext& context) override 371 { 372 (*processFunc)(context, parameter1_, parameter2_, parameter3_, parameter4_, parameter5_, parameter6_); 373 } 374 375 private: 376 T1 parameter1_; 377 T2 parameter2_; 378 T3 parameter3_; 379 T4 parameter4_; 380 T5 parameter5_; 381 T6 parameter6_; 382 }; 383 384 } // namespace Rosen 385 } // namespace OHOS 386 387 #endif // ROSEN_RENDER_SERVICE_BASE_COMMAND_RS_COMMAND_TEMPLATES_H 388