1 /* 2 * Copyright (c) 2025 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_MODIFIER_NG_RS_MODIFIER_NG_H 17 #define RENDER_SERVICE_CLIENT_CORE_MODIFIER_NG_RS_MODIFIER_NG_H 18 19 #include <map> 20 21 #include "modifier/rs_property.h" 22 #include "modifier_ng/rs_modifier_ng_type.h" 23 24 namespace OHOS::Rosen { 25 class RSModifierExtractor; 26 class RSNode; 27 class RSPropertyBase; 28 29 namespace ModifierNG { 30 class RSRenderModifier; 31 32 class RSC_EXPORT RSModifier : public std::enable_shared_from_this<RSModifier> { 33 public: GetId()34 ModifierId GetId() const 35 { 36 return id_; 37 } 38 39 void OnAttach(RSNode& node); 40 void OnDetach(); 41 void AttachProperty(const std::shared_ptr<RSPropertyBase>& property); 42 void AttachProperty(RSPropertyType type, std::shared_ptr<RSPropertyBase> property); 43 void DetachProperty(RSPropertyType type); 44 45 void SetDirty(bool isDirty, const std::shared_ptr<RSModifierManager>& modifierManager = nullptr); 46 47 virtual RSModifierType GetType() const = 0; 48 GetProperty(RSPropertyType type)49 std::shared_ptr<RSPropertyBase> GetProperty(RSPropertyType type) 50 { 51 auto it = properties_.find(type); 52 if (it == properties_.end()) { 53 return nullptr; 54 } 55 return it->second; 56 } 57 HasProperty(RSPropertyType type)58 bool HasProperty(RSPropertyType type) const 59 { 60 return properties_.count(type); 61 } 62 IsCustom()63 virtual bool IsCustom() const 64 { 65 return false; 66 } 67 ResetRSNodeExtendModifierDirty()68 void ResetRSNodeExtendModifierDirty() 69 { 70 if (auto node = node_.lock()) { 71 node->ResetExtendModifierDirty(); 72 } 73 } 74 75 protected: RSModifier()76 RSModifier() : id_(GenerateModifierId()) {} 77 virtual ~RSModifier() = default; 78 79 // only accept properties on white list ? 80 std::map<RSPropertyType, std::shared_ptr<RSPropertyBase>> properties_; 81 ModifierId id_; 82 std::weak_ptr<RSNode> node_; 83 84 virtual std::shared_ptr<RSRenderModifier> CreateRenderModifier(); UpdateToRender()85 virtual void UpdateToRender() {} MarkNodeDirty()86 virtual void MarkNodeDirty() {} 87 88 template<typename T> Getter(RSPropertyType type,const T & defaultValue)89 inline T Getter(RSPropertyType type, const T& defaultValue) const 90 { 91 auto it = properties_.find(type); 92 if (it == properties_.end()) { 93 return defaultValue; 94 } 95 auto property = std::static_pointer_cast<RSProperty<T>>(it->second); 96 return property->Get(); 97 } 98 99 template<typename T> GetterWithoutCheck(const std::shared_ptr<RSPropertyBase> property)100 inline T GetterWithoutCheck(const std::shared_ptr<RSPropertyBase> property) const 101 { 102 return std::static_pointer_cast<RSProperty<T>>(property)->Get(); 103 } 104 105 template<template<typename> class PropertyType = RSAnimatableProperty, typename T> Setter(RSPropertyType type,const T & value)106 inline void Setter(RSPropertyType type, const T& value) 107 { 108 auto it = properties_.find(type); 109 if (it != properties_.end()) { 110 auto property = std::static_pointer_cast<PropertyType<T>>(it->second); 111 property->Set(value); 112 } else { 113 std::shared_ptr<RSPropertyBase> property = std::make_shared<PropertyType<T>>(value); 114 AttachProperty(type, property); 115 } 116 } 117 118 template<typename T> GetterOptional(RSPropertyType type)119 inline std::optional<T> GetterOptional(RSPropertyType type) const 120 { 121 auto it = properties_.find(type); 122 if (it == properties_.end()) { 123 return std::nullopt; 124 } 125 auto property = std::static_pointer_cast<RSProperty<T>>(it->second); 126 return property->Get(); 127 } 128 129 template<template<typename> class PropertyType = RSProperty, typename T> SetterOptional(RSPropertyType type,const std::optional<T> & value)130 inline void SetterOptional(RSPropertyType type, const std::optional<T>& value) 131 { 132 if (!value.has_value()) { 133 DetachProperty(type); 134 return; 135 } 136 auto it = properties_.find(type); 137 if (it != properties_.end()) { 138 auto property = std::static_pointer_cast<PropertyType<T>>(it->second); 139 property->Set(value.value()); 140 } else { 141 std::shared_ptr<RSPropertyBase> property = std::make_shared<PropertyType<T>>(value.value()); 142 AttachProperty(type, property); 143 } 144 } 145 146 private: 147 static ModifierId GenerateModifierId(); 148 void SetPropertyThresholdType(RSPropertyType type, std::shared_ptr<RSPropertyBase> property); 149 bool isDirty_ { false }; 150 151 friend class OHOS::Rosen::RSModifierExtractor; 152 friend class OHOS::Rosen::RSModifierManager; 153 friend class OHOS::Rosen::RSNode; 154 friend class OHOS::Rosen::RSPropertyBase; 155 }; 156 } // namespace ModifierNG 157 } // namespace OHOS::Rosen 158 #endif // RENDER_SERVICE_CLIENT_CORE_MODIFIER_NG_RS_MODIFIER_NG_H 159