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 RENDER_SERVICE_BASE_TRANSACTION_RS_MARSHALLING_HELPER_H 17 #define RENDER_SERVICE_BASE_TRANSACTION_RS_MARSHALLING_HELPER_H 18 19 #include <memory> 20 #include <optional> 21 #include "common/rs_macros.h" 22 #ifdef USE_ROSEN_DRAWING 23 #include "image/image.h" 24 #endif 25 26 #include <parcel.h> 27 28 #include "common/rs_common_def.h" 29 30 #ifndef USE_ROSEN_DRAWING 31 template<typename T> 32 class sk_sp; 33 class SkData; 34 class SkDrawable; 35 class SkFlattenable; 36 class SkImage; 37 class SkImageFilter; 38 class SkPaint; 39 class SkPath; 40 class SkPicture; 41 struct SkRect; 42 class SkRegion; 43 class SkTextBlob; 44 class SkVertices; 45 class SkTypeface; 46 class SkBitmap; 47 48 #ifdef NEW_SKIA 49 struct SkSamplingOptions; 50 #endif 51 #endif 52 53 #if defined (ENABLE_DDGR_OPTIMIZE) 54 struct SkSerialProcs; 55 struct SkDeserialProcs; 56 #endif 57 58 namespace OHOS { 59 namespace Media { 60 class PixelMap; 61 } 62 namespace Rosen { 63 #ifndef USE_ROSEN_DRAWING 64 class DrawCmdList; 65 class OpItem; 66 #else 67 namespace Drawing { 68 class DrawCmdList; 69 class Data; 70 class Image; 71 } 72 #endif 73 class RSFilter; 74 class RSImage; 75 class RSImageBase; 76 class RSMask; 77 class RSPath; 78 class RSLinearGradientBlurPara; 79 template<typename T> 80 class RenderParticleParaType; 81 class EmitterConfig; 82 class ParticleVelocity; 83 class RenderParticleColorParaType; 84 class ParticleRenderParams; 85 class RSRenderCurveAnimation; 86 class RSRenderParticleAnimation; 87 class RSRenderInterpolatingSpringAnimation; 88 class RSRenderKeyframeAnimation; 89 class RSRenderPathAnimation; 90 class RSRenderSpringAnimation; 91 class RSRenderTransition; 92 class RSRenderTransitionEffect; 93 class RSRenderModifier; 94 template<typename T> 95 class RSRenderProperty; 96 template<typename T> 97 class RSRenderAnimatableProperty; 98 class RSShader; 99 template<typename T> 100 class RectT; 101 template<typename T> 102 class RRectT; 103 104 class RSB_EXPORT RSMarshallingHelper { 105 public: 106 // default marshalling and unmarshalling method for POD types 107 // [PLANNING]: implement marshalling & unmarshalling methods for other types (e.g. RSImage, drawCMDList) 108 template<typename T> Marshalling(Parcel & parcel,const T & val)109 static bool Marshalling(Parcel& parcel, const T& val) 110 { 111 return parcel.WriteUnpadBuffer(&val, sizeof(T)); 112 } 113 template<typename T> Unmarshalling(Parcel & parcel,T & val)114 static bool Unmarshalling(Parcel& parcel, T& val) 115 { 116 if (const uint8_t* buff = parcel.ReadUnpadBuffer(sizeof(T))) { 117 if (buff == nullptr) { 118 return false; 119 } 120 val = *(reinterpret_cast<const T*>(buff)); 121 return true; 122 } 123 return false; 124 } 125 126 template<typename T> MarshallingArray(Parcel & parcel,const T * val,int count)127 static bool MarshallingArray(Parcel& parcel, const T* val, int count) 128 { 129 if (count <= 0) { 130 return true; 131 } 132 return parcel.WriteUnpadBuffer(val, count * sizeof(T)); 133 } 134 template<typename T> UnmarshallingArray(Parcel & parcel,T * & val,int count)135 static bool UnmarshallingArray(Parcel& parcel, T*& val, int count) 136 { 137 if (count <= 0) { 138 return false; 139 } 140 if (const uint8_t* buff = parcel.ReadUnpadBuffer(count * sizeof(T))) { 141 if (buff == nullptr) { 142 return false; 143 } 144 val = reinterpret_cast<const T*>(buff); 145 return true; 146 } 147 return false; 148 } 149 150 #ifndef USE_ROSEN_DRAWING 151 static RSB_EXPORT bool Marshalling(Parcel& parcel, const sk_sp<SkImage>& val); 152 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, sk_sp<SkImage>& val); 153 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, sk_sp<SkImage>& val, void*& imagepixelAddr); 154 #else 155 static RSB_EXPORT bool Marshalling(Parcel& parcel, const std::shared_ptr<Drawing::Image>& val); 156 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Image>& val); 157 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Image>& val, void*& imagepixelAddr); 158 #endif 159 160 // reloaded marshalling & unmarshalling function for types 161 #define DECLARE_FUNCTION_OVERLOAD(TYPE) \ 162 static RSB_EXPORT bool Marshalling(Parcel& parcel, const TYPE& val); \ 163 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, TYPE& val); 164 165 // basic types 166 DECLARE_FUNCTION_OVERLOAD(bool) 167 DECLARE_FUNCTION_OVERLOAD(int8_t) 168 DECLARE_FUNCTION_OVERLOAD(uint8_t) 169 DECLARE_FUNCTION_OVERLOAD(int16_t) 170 DECLARE_FUNCTION_OVERLOAD(uint16_t) 171 DECLARE_FUNCTION_OVERLOAD(int32_t) 172 DECLARE_FUNCTION_OVERLOAD(uint32_t) 173 DECLARE_FUNCTION_OVERLOAD(int64_t) 174 DECLARE_FUNCTION_OVERLOAD(uint64_t) 175 DECLARE_FUNCTION_OVERLOAD(float) 176 DECLARE_FUNCTION_OVERLOAD(double) 177 // skia types 178 #ifndef USE_ROSEN_DRAWING 179 DECLARE_FUNCTION_OVERLOAD(SkPath) 180 DECLARE_FUNCTION_OVERLOAD(SkPaint) 181 DECLARE_FUNCTION_OVERLOAD(SkRect) 182 DECLARE_FUNCTION_OVERLOAD(SkRegion) 183 DECLARE_FUNCTION_OVERLOAD(SkBitmap) 184 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkFlattenable>) 185 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkTextBlob>) 186 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkPicture>) 187 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkDrawable>) 188 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkImageFilter>) 189 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkVertices>) 190 static bool SkipSkData(Parcel& parcel); 191 static bool SkipSkImage(Parcel& parcel); 192 #else 193 DECLARE_FUNCTION_OVERLOAD(Drawing::Matrix) 194 static bool SkipData(Parcel& parcel); 195 static bool SkipImage(Parcel& parcel); 196 #endif 197 // RS types 198 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSShader>) 199 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSPath>) 200 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSLinearGradientBlurPara>) 201 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSFilter>) 202 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSMask>) 203 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSImage>) 204 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSImageBase>) 205 DECLARE_FUNCTION_OVERLOAD(EmitterConfig) 206 DECLARE_FUNCTION_OVERLOAD(ParticleVelocity) 207 DECLARE_FUNCTION_OVERLOAD(RenderParticleParaType<float>) 208 DECLARE_FUNCTION_OVERLOAD(RenderParticleColorParaType) 209 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<ParticleRenderParams>) 210 DECLARE_FUNCTION_OVERLOAD(std::vector<std::shared_ptr<ParticleRenderParams>>) 211 #ifndef USE_ROSEN_DRAWING 212 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<DrawCmdList>) 213 #else 214 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<Drawing::DrawCmdList>) 215 #endif 216 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<Media::PixelMap>) 217 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RectT<float>>) 218 DECLARE_FUNCTION_OVERLOAD(RRectT<float>) 219 static bool SkipPixelMap(Parcel& parcel); 220 // animation 221 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSRenderTransition>) DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSRenderTransitionEffect>)222 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSRenderTransitionEffect>) 223 224 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSRenderModifier>) 225 #ifndef USE_ROSEN_DRAWING 226 DECLARE_FUNCTION_OVERLOAD(std::unique_ptr<OpItem>) 227 #ifdef NEW_SKIA 228 DECLARE_FUNCTION_OVERLOAD(SkSamplingOptions) 229 #endif 230 #endif 231 #undef DECLARE_FUNCTION_OVERLOAD 232 233 // reloaded marshalling & unmarshalling function for animation 234 #define DECLARE_ANIMATION_OVERLOAD(TEMPLATE) \ 235 static bool Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE>& val); \ 236 static bool Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE>& val); 237 238 DECLARE_ANIMATION_OVERLOAD(RSRenderCurveAnimation) 239 DECLARE_ANIMATION_OVERLOAD(RSRenderParticleAnimation) 240 DECLARE_ANIMATION_OVERLOAD(RSRenderInterpolatingSpringAnimation) 241 DECLARE_ANIMATION_OVERLOAD(RSRenderKeyframeAnimation) 242 DECLARE_ANIMATION_OVERLOAD(RSRenderSpringAnimation) 243 DECLARE_ANIMATION_OVERLOAD(RSRenderPathAnimation) 244 #undef DECLARE_ANIMATION_OVERLOAD 245 246 #define DECLARE_TEMPLATE_OVERLOAD(TEMPLATE) \ 247 template<typename T> \ 248 static RSB_EXPORT bool Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<T>>& val); \ 249 template<typename T> \ 250 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<T>>& val); 251 252 DECLARE_TEMPLATE_OVERLOAD(RSRenderProperty) 253 DECLARE_TEMPLATE_OVERLOAD(RSRenderAnimatableProperty) 254 #undef DECLARE_TEMPLATE_OVERLOAD 255 256 // reloaded marshalling & unmarshalling function for std::vector 257 template<typename T> 258 static bool Marshalling(Parcel& parcel, const std::vector<T>& val) 259 { 260 bool success = parcel.WriteUint32(val.size()); 261 for (const auto& item : val) { 262 success = success && Marshalling(parcel, item); 263 } 264 return success; 265 } 266 template<typename T> Unmarshalling(Parcel & parcel,std::vector<T> & val)267 static bool Unmarshalling(Parcel& parcel, std::vector<T>& val) 268 { 269 uint32_t size = 0; 270 if (!Unmarshalling(parcel, size)) { 271 return false; 272 } 273 val.resize(size); 274 for (uint32_t i = 0; i < size; ++i) { 275 // in-place unmarshalling 276 if (!Unmarshalling(parcel, val[i])) { 277 return false; 278 } 279 } 280 return true; 281 } 282 283 // reloaded marshalling & unmarshalling function for std::optional 284 template<typename T> Marshalling(Parcel & parcel,const std::optional<T> & val)285 static bool Marshalling(Parcel& parcel, const std::optional<T>& val) 286 { 287 if (!val.has_value()) { 288 parcel.WriteBool(false); 289 return true; 290 } 291 parcel.WriteBool(true); 292 return Marshalling(parcel, val.value()); 293 } 294 template<typename T> Unmarshalling(Parcel & parcel,std::optional<T> & val)295 static bool Unmarshalling(Parcel& parcel, std::optional<T>& val) 296 { 297 if (!parcel.ReadBool()) { 298 val.reset(); 299 return true; 300 } 301 T value; 302 if (!Unmarshalling(parcel, value)) { 303 return false; 304 } 305 val = value; 306 return true; 307 } 308 309 template<class T1, class T2> Marshalling(Parcel & parcel,const std::pair<T1,T2> & val)310 static bool Marshalling(Parcel& parcel, const std::pair<T1, T2>& val) 311 { 312 return Marshalling(parcel, val.first) && Marshalling(parcel, val.second); 313 } 314 template<class T1, class T2> Unmarshalling(Parcel & parcel,std::pair<T1,T2> & val)315 static bool Unmarshalling(Parcel& parcel, std::pair<T1, T2>& val) 316 { 317 return Unmarshalling(parcel, val.first) && Unmarshalling(parcel, val.second); 318 } 319 320 template<typename T, typename... Args> Marshalling(Parcel & parcel,const T & first,const Args &...args)321 static bool Marshalling(Parcel& parcel, const T& first, const Args&... args) 322 { 323 return Marshalling(parcel, first) && Marshalling(parcel, args...); 324 } 325 template<typename T, typename... Args> Unmarshalling(Parcel & parcel,T & first,Args &...args)326 static bool Unmarshalling(Parcel& parcel, T& first, Args&... args) 327 { 328 return Unmarshalling(parcel, first) && Unmarshalling(parcel, args...); 329 } 330 331 #ifndef USE_ROSEN_DRAWING 332 static bool Marshalling(Parcel& parcel, sk_sp<SkData> val); 333 static bool Unmarshalling(Parcel& parcel, sk_sp<SkData>& val); 334 static bool UnmarshallingWithCopy(Parcel& parcel, sk_sp<SkData>& val); 335 #else 336 static bool Marshalling(Parcel& parcel, std::shared_ptr<Drawing::Data> val); 337 static bool Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Data>& val); 338 static bool UnmarshallingWithCopy(Parcel& parcel, std::shared_ptr<Drawing::Data>& val); 339 #endif 340 341 #if defined (ENABLE_DDGR_OPTIMIZE) 342 static int IntegrateReadDescriptor(Parcel& pacel); 343 static bool IntegrateWriteDescriptor(Parcel& parcel, int fId); 344 static bool SerializeInternal(Parcel& parcel, const sk_sp<SkTextBlob>& val, 345 const SkSerialProcs& procs); 346 static bool DserializeInternal(Parcel& parcel, sk_sp<SkTextBlob>& val, 347 const SkDeserialProcs& procs, sk_sp<SkData>& data); 348 #endif 349 350 private: 351 static bool WriteToParcel(Parcel& parcel, const void* data, size_t size); 352 static const void* ReadFromParcel(Parcel& parcel, size_t size); 353 static bool SkipFromParcel(Parcel& parcel, size_t size); 354 #ifndef USE_ROSEN_DRAWING 355 static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx); 356 static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx); 357 #endif 358 359 static constexpr size_t MAX_DATA_SIZE = 128 * 1024 * 1024; // 128M 360 static constexpr size_t MIN_DATA_SIZE = 8 * 1024; // 8k 361 }; 362 363 } // namespace Rosen 364 } // namespace OHOS 365 366 #endif // RENDER_SERVICE_BASE_TRANSACTION_RS_MARSHALLING_HELPER_H 367