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_CUSTOM_RS_CUSTOM_MODIFIER_H 17 #define RENDER_SERVICE_CLIENT_CORE_MODIFIER_NG_CUSTOM_RS_CUSTOM_MODIFIER_H 18 19 #include "command/rs_node_command.h" 20 #include "modifier_ng/rs_modifier_ng.h" 21 #include "pipeline/rs_draw_cmd_list.h" 22 23 namespace OHOS::Rosen { 24 class RSNode; 25 namespace ModifierNG { 26 struct RSDrawingContext { 27 Drawing::Canvas* canvas; 28 float width; 29 float height; 30 }; 31 32 class RSC_EXPORT RSCustomModifierHelper { 33 public: 34 static RSDrawingContext CreateDrawingContext(std::shared_ptr<RSNode> node); 35 static std::shared_ptr<Drawing::DrawCmdList> FinishDrawing(RSDrawingContext& ctx); 36 }; 37 38 class RSC_EXPORT RSCustomModifier : public RSModifier { 39 public: 40 RSCustomModifier() = default; 41 virtual ~RSCustomModifier() = default; 42 43 virtual void Draw(RSDrawingContext& context) const = 0; 44 SetNoNeedUICaptured(bool noNeedUICaptured)45 void SetNoNeedUICaptured(bool noNeedUICaptured) 46 { 47 noNeedUICaptured_ = noNeedUICaptured; 48 } 49 IsCustom()50 bool IsCustom() const override 51 { 52 return true; 53 } 54 GetIndex()55 int16_t GetIndex() const 56 { 57 return Getter(RSPropertyType::CUSTOM_INDEX, 0); 58 } 59 SetIndex(int16_t index)60 void SetIndex(int16_t index) 61 { 62 Setter<RSProperty, int16_t>(RSPropertyType::CUSTOM_INDEX, index); 63 } 64 65 protected: CreateRenderModifier()66 std::shared_ptr<RSRenderModifier> CreateRenderModifier() override 67 { 68 auto node = node_.lock(); 69 if (node == nullptr) { 70 return nullptr; 71 } 72 RSDrawingContext ctx = RSCustomModifierHelper::CreateDrawingContext(node); 73 Draw(ctx); 74 auto drawCmdList = RSCustomModifierHelper::FinishDrawing(ctx); 75 Setter<RSProperty, std::shared_ptr<Drawing::DrawCmdList>>(GetInnerPropertyType(), drawCmdList); 76 return RSModifier::CreateRenderModifier(); 77 } 78 GetInnerPropertyType()79 virtual RSPropertyType GetInnerPropertyType() const 80 { 81 return RSPropertyType::CUSTOM; 82 } 83 UpdateToRender()84 void UpdateToRender() override 85 { 86 auto node = node_.lock(); 87 if (node == nullptr) { 88 return; 89 } 90 RSDrawingContext ctx = RSCustomModifierHelper::CreateDrawingContext(node); 91 Draw(ctx); 92 auto drawCmdList = RSCustomModifierHelper::FinishDrawing(ctx); 93 bool isEmpty = drawCmdList == nullptr; 94 if (lastDrawCmdListEmpty_ && isEmpty) { 95 return; 96 } 97 if (drawCmdList) { 98 drawCmdList->SetNoNeedUICaptured(noNeedUICaptured_); 99 drawCmdList->SetIsNeedUnmarshalOnDestruct(!node->IsRenderServiceNode()); 100 } 101 lastDrawCmdListEmpty_ = isEmpty; 102 auto it = properties_.find(GetInnerPropertyType()); 103 if (it == properties_.end()) { 104 return; 105 } 106 if (it->second == nullptr) { 107 return; 108 } 109 std::unique_ptr<RSCommand> command = 110 std::make_unique<RSUpdatePropertyDrawCmdListNG>(node->GetId(), drawCmdList, it->second->GetId()); 111 node->AddCommand(command, node->IsRenderServiceNode()); 112 if (node->NeedForcedSendToRemote()) { 113 std::unique_ptr<RSCommand> commandForRemote = 114 std::make_unique<RSUpdatePropertyDrawCmdListNG>(node->GetId(), drawCmdList, it->second->GetId()); 115 node->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId()); 116 } 117 } 118 119 private: 120 bool lastDrawCmdListEmpty_ = false; 121 bool noNeedUICaptured_ = false; 122 123 friend class OHOS::Rosen::RSNode; 124 }; 125 } // namespace ModifierNG 126 } // namespace OHOS::Rosen 127 #endif // RENDER_SERVICE_CLIENT_CORE_MODIFIER_NG_CUSTOM_RS_CUSTOM_MODIFIER_H 128