1 /* 2 * Copyright (c) 2022-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_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H 17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H 18 19 #include "animation/rs_value_estimator.h" 20 #include "common/rs_common_def.h" 21 #include "common/rs_macros.h" 22 #include "common/rs_rect.h" 23 #include "modifier/rs_animatable_arithmetic.h" 24 #include "modifier/rs_modifier_type.h" 25 #include "transaction/rs_marshalling_helper.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 class RSRenderNode; 30 31 enum PropertyUpdateType : int8_t { 32 UPDATE_TYPE_OVERWRITE, // overwrite by given value 33 UPDATE_TYPE_INCREMENTAL, // incremental update by given value 34 UPDATE_TYPE_FORCE_OVERWRITE, // overwrite and cancel all previous animations 35 }; 36 37 class RSB_EXPORT RSRenderPropertyBase : public std::enable_shared_from_this<RSRenderPropertyBase> { 38 public: 39 RSRenderPropertyBase() = default; RSRenderPropertyBase(const PropertyId & id)40 RSRenderPropertyBase(const PropertyId& id) : id_(id) {} 41 RSRenderPropertyBase(const RSRenderPropertyBase&) = delete; 42 RSRenderPropertyBase(const RSRenderPropertyBase&&) = delete; 43 RSRenderPropertyBase& operator=(const RSRenderPropertyBase&) = delete; 44 RSRenderPropertyBase& operator=(const RSRenderPropertyBase&&) = delete; 45 virtual ~RSRenderPropertyBase() = default; 46 GetId()47 PropertyId GetId() const 48 { 49 return id_; 50 } 51 Attach(std::weak_ptr<RSRenderNode> node)52 void Attach(std::weak_ptr<RSRenderNode> node) 53 { 54 node_ = node; 55 OnChange(); 56 } 57 SetModifierType(RSModifierType type)58 void SetModifierType(RSModifierType type) 59 { 60 modifierType_ = type; 61 UpdatePropertyUnit(type); 62 } 63 64 static bool Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderPropertyBase>& val); 65 [[nodiscard]] static bool Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderPropertyBase>& val); 66 67 protected: 68 void OnChange() const; 69 70 void UpdatePropertyUnit(RSModifierType type); 71 Clone()72 virtual const std::shared_ptr<RSRenderPropertyBase> Clone() const 73 { 74 return nullptr; 75 } 76 SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)77 virtual void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) {} 78 SetPropertyType(const RSRenderPropertyType type)79 virtual void SetPropertyType(const RSRenderPropertyType type) {} 80 GetPropertyType()81 virtual RSRenderPropertyType GetPropertyType() const 82 { 83 return RSRenderPropertyType::INVALID; 84 } 85 SetPropertyUnit(RSPropertyUnit unit)86 virtual void SetPropertyUnit(RSPropertyUnit unit) {} 87 GetPropertyUnit()88 virtual RSPropertyUnit GetPropertyUnit() const 89 { 90 return RSPropertyUnit::UNKNOWN; 91 } 92 ToFloat()93 virtual float ToFloat() const 94 { 95 return 1.f; 96 } 97 CreateRSValueEstimator(const RSValueEstimatorType type)98 virtual std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type) 99 { 100 return nullptr; 101 } 102 IsNearEqual(const std::shared_ptr<RSRenderPropertyBase> & value,float zeroThreshold)103 virtual bool IsNearEqual(const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const 104 { 105 return true; 106 } 107 108 PropertyId id_; 109 std::weak_ptr<RSRenderNode> node_; 110 RSModifierType modifierType_ { RSModifierType::INVALID }; 111 112 private: Add(const std::shared_ptr<const RSRenderPropertyBase> & value)113 virtual std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value) 114 { 115 return shared_from_this(); 116 } 117 Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)118 virtual std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value) 119 { 120 return shared_from_this(); 121 } 122 Multiply(const float scale)123 virtual std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale) 124 { 125 return shared_from_this(); 126 } 127 IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)128 virtual bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const 129 { 130 return true; 131 } 132 133 friend std::shared_ptr<RSRenderPropertyBase> operator+=( 134 const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b); 135 friend std::shared_ptr<RSRenderPropertyBase> operator-=( 136 const std::shared_ptr<RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b); 137 friend std::shared_ptr<RSRenderPropertyBase> operator*=( 138 const std::shared_ptr<RSRenderPropertyBase>& value, const float scale); 139 friend std::shared_ptr<RSRenderPropertyBase> operator+( 140 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b); 141 friend std::shared_ptr<RSRenderPropertyBase> operator-( 142 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b); 143 friend std::shared_ptr<RSRenderPropertyBase> operator*( 144 const std::shared_ptr<const RSRenderPropertyBase>& value, const float scale); 145 friend bool operator==( 146 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b); 147 friend bool operator!=( 148 const std::shared_ptr<const RSRenderPropertyBase>& a, const std::shared_ptr<const RSRenderPropertyBase>& b); 149 friend class RSAnimationRateDecider; 150 friend class RSRenderPropertyAnimation; 151 friend class RSMarshallingHelper; 152 friend class RSValueEstimator; 153 friend class RSRenderPathAnimation; 154 friend class RSRenderSpringAnimation; 155 friend class RSRenderInterpolatingSpringAnimation; 156 friend class RSRenderCurveAnimation; 157 friend class RSRenderKeyframeAnimation; 158 template<typename T> 159 friend class RSSpringModel; 160 friend class RSTransitionCustom; 161 }; 162 163 template<typename T> 164 class RSB_EXPORT_TMP RSRenderProperty : public RSRenderPropertyBase { 165 public: RSRenderProperty()166 RSRenderProperty() : RSRenderPropertyBase(0) {} RSRenderProperty(const T & value,const PropertyId & id)167 RSRenderProperty(const T& value, const PropertyId& id) : RSRenderPropertyBase(id), stagingValue_(value) {} RSRenderProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type)168 RSRenderProperty(const T& value, const PropertyId& id, const RSRenderPropertyType type) 169 : RSRenderPropertyBase(id), stagingValue_(value) 170 {} 171 virtual ~RSRenderProperty() = default; 172 Set(const T & value)173 void Set(const T& value) 174 { 175 if (value == stagingValue_) { 176 return; 177 } 178 stagingValue_ = value; 179 OnChange(); 180 if (updateUIPropertyFunc_) { 181 updateUIPropertyFunc_(shared_from_this()); 182 } 183 } 184 Get()185 T Get() const 186 { 187 return stagingValue_; 188 } 189 GetRef()190 T& GetRef() 191 { 192 return stagingValue_; 193 } 194 SetUpdateUIPropertyFunc(const std::function<void (const std::shared_ptr<RSRenderPropertyBase> &)> & updateUIPropertyFunc)195 void SetUpdateUIPropertyFunc( 196 const std::function<void(const std::shared_ptr<RSRenderPropertyBase>&)>& updateUIPropertyFunc) 197 { 198 updateUIPropertyFunc_ = updateUIPropertyFunc; 199 } 200 201 protected: 202 T stagingValue_; 203 std::function<void(const std::shared_ptr<RSRenderPropertyBase>&)> updateUIPropertyFunc_; GetPropertyType()204 RSRenderPropertyType GetPropertyType() const 205 { 206 return RSRenderPropertyType::INVALID; 207 } 208 209 friend class RSMarshallingHelper; 210 }; 211 212 template<typename T> 213 class RSB_EXPORT_TMP RSRenderAnimatableProperty : public RSRenderProperty<T> { 214 public: RSRenderAnimatableProperty()215 RSRenderAnimatableProperty() : RSRenderProperty<T>() {} RSRenderAnimatableProperty(const T & value)216 RSRenderAnimatableProperty(const T& value) : RSRenderProperty<T>(value, 0) {} RSRenderAnimatableProperty(const T & value,const PropertyId & id)217 RSRenderAnimatableProperty(const T& value, const PropertyId& id) : RSRenderProperty<T>(value, id) {} RSRenderAnimatableProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type)218 RSRenderAnimatableProperty(const T& value, const PropertyId& id, const RSRenderPropertyType type) 219 : RSRenderProperty<T>(value, id), type_(type) 220 {} RSRenderAnimatableProperty(const T & value,const PropertyId & id,const RSRenderPropertyType type,const RSPropertyUnit unit)221 RSRenderAnimatableProperty(const T& value, const PropertyId& id, 222 const RSRenderPropertyType type, const RSPropertyUnit unit) 223 : RSRenderProperty<T>(value, id), type_(type), unit_(unit) 224 {} 225 virtual ~RSRenderAnimatableProperty() = default; 226 227 protected: Clone()228 const std::shared_ptr<RSRenderPropertyBase> Clone() const override 229 { 230 return std::make_shared<RSRenderAnimatableProperty<T>>( 231 RSRenderProperty<T>::stagingValue_, RSRenderProperty<T>::id_, type_, unit_); 232 } 233 SetValue(const std::shared_ptr<RSRenderPropertyBase> & value)234 void SetValue(const std::shared_ptr<RSRenderPropertyBase>& value) override 235 { 236 auto property = std::static_pointer_cast<RSRenderAnimatableProperty<T>>(value); 237 if (property != nullptr && property->GetPropertyType() == type_) { 238 RSRenderProperty<T>::Set(property->Get()); 239 } 240 } 241 SetPropertyType(const RSRenderPropertyType type)242 void SetPropertyType(const RSRenderPropertyType type) override 243 { 244 type_ = type; 245 } 246 GetPropertyType()247 virtual RSRenderPropertyType GetPropertyType() const override 248 { 249 return type_; 250 } 251 SetPropertyUnit(RSPropertyUnit unit)252 void SetPropertyUnit(RSPropertyUnit unit) override 253 { 254 unit_ = unit; 255 } 256 GetPropertyUnit()257 RSPropertyUnit GetPropertyUnit() const override 258 { 259 return unit_; 260 } 261 ToFloat()262 float ToFloat() const override 263 { 264 return 1.f; 265 } 266 IsNearEqual(const std::shared_ptr<RSRenderPropertyBase> & value,float zeroThreshold)267 bool IsNearEqual(const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const override 268 { 269 return IsEqual(value); 270 } 271 CreateRSValueEstimator(const RSValueEstimatorType type)272 std::shared_ptr<RSValueEstimator> CreateRSValueEstimator(const RSValueEstimatorType type) override 273 { 274 switch (type) { 275 case RSValueEstimatorType::CURVE_VALUE_ESTIMATOR: { 276 return std::make_shared<RSCurveValueEstimator<T>>(); 277 } 278 case RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR: { 279 return std::make_shared<RSKeyframeValueEstimator<T>>(); 280 } 281 default: { 282 return nullptr; 283 } 284 } 285 } 286 287 private: 288 RSRenderPropertyType type_ = RSRenderPropertyType::INVALID; 289 RSPropertyUnit unit_ = RSPropertyUnit::UNKNOWN; 290 Add(const std::shared_ptr<const RSRenderPropertyBase> & value)291 std::shared_ptr<RSRenderPropertyBase> Add(const std::shared_ptr<const RSRenderPropertyBase>& value) override 292 { 293 auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value); 294 if (animatableProperty != nullptr) { 295 RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ + animatableProperty->stagingValue_; 296 } 297 return RSRenderProperty<T>::shared_from_this(); 298 } 299 Minus(const std::shared_ptr<const RSRenderPropertyBase> & value)300 std::shared_ptr<RSRenderPropertyBase> Minus(const std::shared_ptr<const RSRenderPropertyBase>& value) override 301 { 302 auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value); 303 if (animatableProperty != nullptr) { 304 RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ - animatableProperty->stagingValue_; 305 } 306 return RSRenderProperty<T>::shared_from_this(); 307 } 308 Multiply(const float scale)309 std::shared_ptr<RSRenderPropertyBase> Multiply(const float scale) override 310 { 311 RSRenderProperty<T>::stagingValue_ = RSRenderProperty<T>::stagingValue_ * scale; 312 return RSRenderProperty<T>::shared_from_this(); 313 } 314 IsEqual(const std::shared_ptr<const RSRenderPropertyBase> & value)315 bool IsEqual(const std::shared_ptr<const RSRenderPropertyBase>& value) const override 316 { 317 auto animatableProperty = std::static_pointer_cast<const RSRenderAnimatableProperty<T>>(value); 318 if (animatableProperty != nullptr) { 319 return RSRenderProperty<T>::stagingValue_ == animatableProperty->stagingValue_; 320 } 321 return true; 322 } 323 324 friend class RSMarshallingHelper; 325 friend class RSRenderPathAnimation; 326 friend class RSRenderPropertyBase; 327 }; 328 329 template<> 330 RSB_EXPORT float RSRenderAnimatableProperty<float>::ToFloat() const; 331 template<> 332 RSB_EXPORT float RSRenderAnimatableProperty<Vector4f>::ToFloat() const; 333 template<> 334 RSB_EXPORT float RSRenderAnimatableProperty<Quaternion>::ToFloat() const; 335 template<> 336 RSB_EXPORT float RSRenderAnimatableProperty<Vector2f>::ToFloat() const; 337 338 template<> 339 RSB_EXPORT bool RSRenderAnimatableProperty<float>::IsNearEqual( 340 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 341 template<> 342 RSB_EXPORT bool RSRenderAnimatableProperty<Vector4f>::IsNearEqual( 343 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 344 template<> 345 RSB_EXPORT bool RSRenderAnimatableProperty<Quaternion>::IsNearEqual( 346 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 347 template<> 348 RSB_EXPORT bool RSRenderAnimatableProperty<Vector2f>::IsNearEqual( 349 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 350 template<> 351 RSB_EXPORT bool RSRenderAnimatableProperty<Matrix3f>::IsNearEqual( 352 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 353 template<> 354 RSB_EXPORT bool RSRenderAnimatableProperty<Color>::IsNearEqual( 355 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 356 template<> 357 RSB_EXPORT bool RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>::IsNearEqual( 358 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 359 template<> 360 RSB_EXPORT bool RSRenderAnimatableProperty<Vector4<Color>>::IsNearEqual( 361 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 362 template<> 363 RSB_EXPORT bool RSRenderAnimatableProperty<RRect>::IsNearEqual( 364 const std::shared_ptr<RSRenderPropertyBase>& value, float zeroThreshold) const; 365 366 #if defined(_WIN32) 367 extern template class RSRenderAnimatableProperty<float>; 368 extern template class RSRenderAnimatableProperty<Vector4f>; 369 extern template class RSRenderAnimatableProperty<Quaternion>; 370 extern template class RSRenderAnimatableProperty<Vector2f>; 371 extern template class RSRenderAnimatableProperty<RRect>; 372 extern template class RSRenderAnimatableProperty<Matrix3f>; 373 extern template class RSRenderAnimatableProperty<Color>; 374 extern template class RSRenderAnimatableProperty<std::shared_ptr<RSFilter>>; 375 extern template class RSRenderAnimatableProperty<Vector4<Color>>; 376 #endif 377 } // namespace Rosen 378 } // namespace OHOS 379 380 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PROP_H 381