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_MODIFIER_RS_EXTENDED_MODIFIER_H 17 #define RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_EXTENDED_MODIFIER_H 18 19 #include "command/rs_node_command.h" 20 #include "common/rs_common_def.h" 21 #include "common/rs_macros.h" 22 #include "modifier/rs_modifier.h" 23 #include "pipeline/rs_draw_cmd_list.h" 24 #include "transaction/rs_transaction_proxy.h" 25 #include "ui/rs_canvas_node.h" 26 27 28 namespace OHOS { 29 namespace Rosen { 30 struct RSDrawingContext { 31 Drawing::Canvas* canvas; 32 float width; 33 float height; 34 }; 35 36 class RSC_EXPORT RSExtendedModifierHelper { 37 public: 38 static RSDrawingContext CreateDrawingContext(NodeId nodeId); 39 static std::shared_ptr<RSRenderModifier> CreateRenderModifier( 40 RSDrawingContext& ctx, PropertyId id, RSModifierType type); 41 static std::shared_ptr<Drawing::DrawCmdList> FinishDrawing(RSDrawingContext& ctx); 42 }; 43 44 class RSC_EXPORT RSExtendedModifier : public RSModifier { 45 public: 46 RSExtendedModifier(const std::shared_ptr<RSPropertyBase>& property = {}) RSModifier(property,RSModifierType::EXTENDED)47 : RSModifier(property, RSModifierType::EXTENDED) 48 { 49 property_->SetIsCustom(true); 50 } 51 virtual ~RSExtendedModifier() = default; 52 GetModifierType()53 RSModifierType GetModifierType() const override 54 { 55 return RSModifierType::EXTENDED; 56 } 57 virtual void Draw(RSDrawingContext& context) const = 0; 58 59 protected: 60 explicit RSExtendedModifier(const RSModifierType type, const std::shared_ptr<RSPropertyBase>& property = {}) RSModifier(property,type)61 : RSModifier(property, type) 62 { 63 property_->SetIsCustom(true); 64 } 65 GetPropertyModifierType()66 RSPropertyModifierType GetPropertyModifierType() const override 67 { 68 return RSPropertyModifierType::CUSTOM; 69 } 70 CreateRenderModifier()71 std::shared_ptr<RSRenderModifier> CreateRenderModifier() const override 72 { 73 auto node = property_->target_.lock(); 74 if (node == nullptr) { 75 return nullptr; 76 } 77 RSDrawingContext ctx = RSExtendedModifierHelper::CreateDrawingContext(node->GetId()); 78 Draw(ctx); 79 return RSExtendedModifierHelper::CreateRenderModifier(ctx, property_->GetId(), GetModifierType()); 80 } 81 UpdateToRender()82 void UpdateToRender() override 83 { 84 auto node = property_->target_.lock(); 85 if (node == nullptr) { 86 return; 87 } 88 RSDrawingContext ctx = RSExtendedModifierHelper::CreateDrawingContext(node->GetId()); 89 Draw(ctx); 90 auto drawCmdList = RSExtendedModifierHelper::FinishDrawing(ctx); 91 bool isEmpty = drawCmdList == nullptr; 92 if (lastDrawCmdListEmpty_ && isEmpty) { 93 return; 94 } 95 lastDrawCmdListEmpty_ = isEmpty; 96 97 std::unique_ptr<RSCommand> command = std::make_unique<RSUpdatePropertyDrawCmdList>( 98 node->GetId(), drawCmdList, property_->id_, UPDATE_TYPE_OVERWRITE); 99 auto transactionProxy = RSTransactionProxy::GetInstance(); 100 if (transactionProxy != nullptr) { 101 transactionProxy->AddCommand(command, node->IsRenderServiceNode()); 102 if (node->NeedForcedSendToRemote()) { 103 std::unique_ptr<RSCommand> commandForRemote = std::make_unique<RSUpdatePropertyDrawCmdList>( 104 node->GetId(), drawCmdList, property_->id_, UPDATE_TYPE_OVERWRITE); 105 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); 106 } 107 } 108 } 109 private: 110 bool lastDrawCmdListEmpty_ = false; 111 }; 112 113 class RS_EXPORT RSGeometryTransModifier : public RSExtendedModifier { 114 public: RSGeometryTransModifier()115 RSGeometryTransModifier() : RSExtendedModifier(RSModifierType::GEOMETRYTRANS) 116 {} 117 virtual ~RSGeometryTransModifier() = default; 118 GetModifierType()119 RSModifierType GetModifierType() const override 120 { 121 return RSModifierType::GEOMETRYTRANS; 122 } 123 Draw(RSDrawingContext & context)124 void Draw(RSDrawingContext& context) const override {}; 125 126 virtual Drawing::Matrix GeometryEffect(float width, float height) const = 0; 127 128 protected: CreateRenderModifier()129 std::shared_ptr<RSRenderModifier> CreateRenderModifier() const override 130 { 131 auto node = property_->target_.lock(); 132 if (node == nullptr) { 133 return nullptr; 134 } 135 auto canvasnode = RSNodeMap::Instance().GetNode<RSCanvasNode>(node->GetId()); 136 if (canvasnode == nullptr) { 137 return nullptr; 138 } 139 auto renderProperty = std::make_shared<RSRenderProperty<Drawing::Matrix>>( 140 GeometryEffect(canvasnode->GetPaintWidth(), canvasnode->GetPaintHeight()), property_->GetId()); 141 auto renderModifier = std::make_shared<RSGeometryTransRenderModifier>(renderProperty); 142 return renderModifier; 143 } 144 UpdateToRender()145 void UpdateToRender() override 146 { 147 auto node = property_->target_.lock(); 148 if (node == nullptr) { 149 return; 150 } 151 auto canvasnode = RSNodeMap::Instance().GetNode<RSCanvasNode>(node->GetId()); 152 if (canvasnode == nullptr) { 153 return; 154 } 155 auto matrix = GeometryEffect(canvasnode->GetPaintWidth(), canvasnode->GetPaintHeight()); 156 std::unique_ptr<RSCommand> command = std::make_unique<RSUpdatePropertyDrawingMatrix>( 157 node->GetId(), matrix, property_->id_, UPDATE_TYPE_OVERWRITE); 158 auto transactionProxy = RSTransactionProxy::GetInstance(); 159 if (transactionProxy != nullptr) { 160 transactionProxy->AddCommand(command, node->IsRenderServiceNode()); 161 if (node->NeedForcedSendToRemote()) { 162 std::unique_ptr<RSCommand> commandForRemote = std::make_unique<RSUpdatePropertyDrawingMatrix>( 163 node->GetId(), matrix, property_->id_, UPDATE_TYPE_OVERWRITE); 164 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); 165 } 166 } 167 } 168 }; 169 170 class RSC_EXPORT RSTransitionModifier : public RSExtendedModifier { 171 public: RSTransitionModifier()172 RSTransitionModifier() : RSExtendedModifier(RSModifierType::TRANSITION) 173 {} 174 GetModifierType()175 RSModifierType GetModifierType() const override 176 { 177 return RSModifierType::TRANSITION; 178 } 179 180 virtual void Active() = 0; 181 182 virtual void Identity() = 0; 183 }; 184 185 class RSC_EXPORT RSBackgroundStyleModifier : public RSExtendedModifier { 186 public: RSBackgroundStyleModifier()187 RSBackgroundStyleModifier() : RSExtendedModifier(RSModifierType::BACKGROUND_STYLE) 188 {} 189 GetModifierType()190 RSModifierType GetModifierType() const override 191 { 192 return RSModifierType::BACKGROUND_STYLE; 193 } 194 }; 195 196 class RSC_EXPORT RSContentStyleModifier : public RSExtendedModifier { 197 public: RSContentStyleModifier()198 RSContentStyleModifier() : RSExtendedModifier(RSModifierType::CONTENT_STYLE) 199 {} 200 GetModifierType()201 RSModifierType GetModifierType() const override 202 { 203 return RSModifierType::CONTENT_STYLE; 204 } 205 }; 206 207 class RSC_EXPORT RSForegroundStyleModifier : public RSExtendedModifier { 208 public: RSForegroundStyleModifier()209 RSForegroundStyleModifier() : RSExtendedModifier(RSModifierType::FOREGROUND_STYLE) 210 {} 211 GetModifierType()212 RSModifierType GetModifierType() const override 213 { 214 return RSModifierType::FOREGROUND_STYLE; 215 } 216 }; 217 218 class RSC_EXPORT RSOverlayStyleModifier : public RSExtendedModifier { 219 public: RSOverlayStyleModifier()220 RSOverlayStyleModifier() : RSExtendedModifier(RSModifierType::OVERLAY_STYLE) 221 {} 222 GetModifierType()223 RSModifierType GetModifierType() const override 224 { 225 return RSModifierType::OVERLAY_STYLE; 226 } 227 }; 228 229 class RSC_EXPORT RSNodeModifier : public RSExtendedModifier { 230 public: RSNodeModifier()231 RSNodeModifier() : RSExtendedModifier(RSModifierType::NODE_MODIFIER) 232 {} 233 GetModifierType()234 RSModifierType GetModifierType() const override 235 { 236 return RSModifierType::NODE_MODIFIER; 237 } 238 239 virtual void Modify(RSNode& target) const = 0; 240 241 private: OnAttachToNode(const std::weak_ptr<RSNode> & target)242 void OnAttachToNode(const std::weak_ptr<RSNode>& target) override 243 { 244 target_ = target; 245 } 246 UpdateToRender()247 void UpdateToRender() override 248 { 249 auto node = target_.lock(); 250 if (node == nullptr) { 251 return; 252 } 253 Modify(*node); 254 } 255 Draw(RSDrawingContext & context)256 void Draw(RSDrawingContext& context) const override final {} 257 258 std::weak_ptr<RSNode> target_; 259 }; 260 } // namespace Rosen 261 } // namespace OHOS 262 263 #endif // RENDER_SERVICE_CLIENT_CORE_MODIFIER_RS_EXTENDED_MODIFIER_H 264