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