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 "common/rs_macros.h" 21 22 #include <parcel.h> 23 24 #include "common/rs_common_def.h" 25 26 template<typename T> 27 class sk_sp; 28 class SkData; 29 class SkDrawable; 30 class SkFlattenable; 31 class SkImage; 32 class SkImageFilter; 33 class SkPaint; 34 class SkPath; 35 class SkPicture; 36 struct SkRect; 37 class SkRegion; 38 class SkTextBlob; 39 class SkVertices; 40 class SkTypeface; 41 42 namespace OHOS { 43 namespace Media { 44 class PixelMap; 45 } 46 namespace Rosen { 47 class DrawCmdList; 48 class RSFilter; 49 class RSImage; 50 class RSMask; 51 class RSPath; 52 class RSShader; 53 class RSRenderCurveAnimation; 54 class RSRenderKeyframeAnimation; 55 class RSRenderSpringAnimation; 56 class RSRenderPathAnimation; 57 class RSRenderTransition; 58 class RSRenderTransitionEffect; 59 class RSRenderModifier; 60 template<typename T> 61 class RSRenderProperty; 62 template<typename T> 63 class RSRenderAnimatableProperty; 64 65 class RSB_EXPORT RSMarshallingHelper { 66 public: 67 // default marshalling and unmarshalling method for POD types 68 // [PLANNING]: implement marshalling & unmarshalling methods for other types (e.g. RSImage, drawCMDList) 69 template<typename T> Marshalling(Parcel & parcel,const T & val)70 static bool Marshalling(Parcel& parcel, const T& val) 71 { 72 return parcel.WriteUnpadBuffer(&val, sizeof(T)); 73 } 74 template<typename T> Unmarshalling(Parcel & parcel,T & val)75 static bool Unmarshalling(Parcel& parcel, T& val) 76 { 77 if (const uint8_t* buff = parcel.ReadUnpadBuffer(sizeof(T))) { 78 val = *(reinterpret_cast<const T*>(buff)); 79 return true; 80 } 81 return false; 82 } 83 84 template<typename T> MarshallingArray(Parcel & parcel,const T * val,int count)85 static bool MarshallingArray(Parcel& parcel, const T* val, int count) 86 { 87 if (count <= 0) { 88 return true; 89 } 90 return parcel.WriteUnpadBuffer(val, count * sizeof(T)); 91 } 92 template<typename T> UnmarshallingArray(Parcel & parcel,T * & val,int count)93 static bool UnmarshallingArray(Parcel& parcel, T*& val, int count) 94 { 95 if (count <= 0) { 96 return true; 97 } 98 if (const uint8_t* buff = parcel.ReadUnpadBuffer(count * sizeof(T))) { 99 val = reinterpret_cast<const T*>(buff); 100 return true; 101 } 102 return false; 103 } 104 105 // reloaded marshalling & unmarshalling function for types 106 #define DECLARE_FUNCTION_OVERLOAD(TYPE) \ 107 static RSB_EXPORT bool Marshalling(Parcel& parcel, const TYPE& val); \ 108 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, TYPE& val); 109 110 // basic types 111 DECLARE_FUNCTION_OVERLOAD(bool) 112 DECLARE_FUNCTION_OVERLOAD(int8_t) 113 DECLARE_FUNCTION_OVERLOAD(uint8_t) 114 DECLARE_FUNCTION_OVERLOAD(int16_t) 115 DECLARE_FUNCTION_OVERLOAD(uint16_t) 116 DECLARE_FUNCTION_OVERLOAD(int32_t) 117 DECLARE_FUNCTION_OVERLOAD(uint32_t) 118 DECLARE_FUNCTION_OVERLOAD(int64_t) 119 DECLARE_FUNCTION_OVERLOAD(uint64_t) 120 DECLARE_FUNCTION_OVERLOAD(float) 121 DECLARE_FUNCTION_OVERLOAD(double) 122 // skia types 123 DECLARE_FUNCTION_OVERLOAD(SkPath) 124 DECLARE_FUNCTION_OVERLOAD(SkPaint) 125 DECLARE_FUNCTION_OVERLOAD(SkRect) 126 DECLARE_FUNCTION_OVERLOAD(SkRegion) 127 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkFlattenable>) 128 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkTextBlob>) 129 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkPicture>) 130 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkDrawable>) 131 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkImageFilter>) 132 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkImage>) 133 DECLARE_FUNCTION_OVERLOAD(sk_sp<SkVertices>) 134 static bool SkipSkData(Parcel& parcel); 135 static bool SkipSkImage(Parcel& parcel); 136 // RS types 137 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSShader>) DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSPath>)138 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSPath>) 139 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSFilter>) 140 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSMask>) 141 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSImage>) 142 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<DrawCmdList>) 143 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<Media::PixelMap>) 144 // animation 145 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSRenderTransition>) 146 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSRenderTransitionEffect>) 147 148 DECLARE_FUNCTION_OVERLOAD(std::shared_ptr<RSRenderModifier>) 149 #undef DECLARE_FUNCTION_OVERLOAD 150 151 // reloaded marshalling & unmarshalling function for animation 152 #define DECLARE_ANIMATION_OVERLOAD(TEMPLATE) \ 153 static bool Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE>& val); \ 154 static bool Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE>& val); 155 156 DECLARE_ANIMATION_OVERLOAD(RSRenderCurveAnimation) 157 DECLARE_ANIMATION_OVERLOAD(RSRenderKeyframeAnimation) 158 DECLARE_ANIMATION_OVERLOAD(RSRenderSpringAnimation) 159 DECLARE_ANIMATION_OVERLOAD(RSRenderPathAnimation) 160 #undef DECLARE_ANIMATION_OVERLOAD 161 162 #define DECLARE_TEMPLATE_OVERLOAD(TEMPLATE) \ 163 template<typename T> \ 164 static RSB_EXPORT bool Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<T>>& val); \ 165 template<typename T> \ 166 static RSB_EXPORT bool Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<T>>& val); 167 168 DECLARE_TEMPLATE_OVERLOAD(RSRenderProperty) 169 DECLARE_TEMPLATE_OVERLOAD(RSRenderAnimatableProperty) 170 #undef DECLARE_TEMPLATE_OVERLOAD 171 172 // reloaded marshalling & unmarshalling function for std::vector 173 template<typename T> 174 static bool Marshalling(Parcel& parcel, const std::vector<T>& val) 175 { 176 bool success = parcel.WriteUint32(val.size()); 177 for (const auto& item : val) { 178 success = success && Marshalling(parcel, item); 179 } 180 return success; 181 } 182 template<typename T> Unmarshalling(Parcel & parcel,std::vector<T> & val)183 static bool Unmarshalling(Parcel& parcel, std::vector<T>& val) 184 { 185 uint32_t size = 0; 186 if (!Unmarshalling(parcel, size)) { 187 return false; 188 } 189 val.resize(size); 190 for (uint32_t i = 0; i < size; ++i) { 191 // in-place unmarshalling 192 if (!Unmarshalling(parcel, val[i])) { 193 return false; 194 } 195 } 196 return true; 197 } 198 199 template<typename T, typename... Args> Marshalling(Parcel & parcel,const T & first,const Args &...args)200 static bool Marshalling(Parcel& parcel, const T& first, const Args&... args) 201 { 202 return Marshalling(parcel, first) && Marshalling(parcel, args...); 203 } 204 template<typename T, typename... Args> Unmarshalling(Parcel & parcel,T & first,Args &...args)205 static bool Unmarshalling(Parcel& parcel, T& first, Args&... args) 206 { 207 return Unmarshalling(parcel, first) && Unmarshalling(parcel, args...); 208 } 209 210 static bool Marshalling(Parcel& parcel, sk_sp<SkData> val); 211 static bool Unmarshalling(Parcel& parcel, sk_sp<SkData>& val); 212 static bool UnmarshallingWithCopy(Parcel& parcel, sk_sp<SkData>& val); 213 214 private: 215 static bool WriteToParcel(Parcel& parcel, const void* data, size_t size); 216 static const void* ReadFromParcel(Parcel& parcel, size_t size); 217 static bool SkipFromParcel(Parcel& parcel, size_t size); 218 static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx); 219 static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx); 220 221 static constexpr size_t MAX_DATA_SIZE = 128 * 1024 * 1024; // 128M 222 static constexpr size_t MIN_DATA_SIZE = 8 * 1024; // 8k 223 }; 224 225 } // namespace Rosen 226 } // namespace OHOS 227 228 #endif // RENDER_SERVICE_BASE_TRANSACTION_RS_MARSHALLING_HELPER_H 229