• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EXTENDED_MODIFIER_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_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 
26 class SkCanvas;
27 
28 namespace OHOS {
29 namespace Rosen {
30 class RSC_EXPORT RSExtendedModifierHelper {
31 public:
32     static RSDrawingContext CreateDrawingContext(NodeId nodeId);
33     static std::shared_ptr<RSRenderModifier> CreateRenderModifier(
34         RSDrawingContext& ctx, PropertyId id, RSModifierType type);
35     static std::shared_ptr<DrawCmdList> FinishDrawing(RSDrawingContext& ctx);
36 };
37 
38 class RSC_EXPORT RSExtendedModifier : public RSModifier {
39 public:
40     RSExtendedModifier(const std::shared_ptr<RSPropertyBase>& property = {})
RSModifier(property,RSModifierType::EXTENDED)41         : RSModifier(property, RSModifierType::EXTENDED)
42     {
43         property_->SetIsCustom(true);
44     }
GetModifierType()45     RSModifierType GetModifierType() const override
46     {
47         return RSModifierType::EXTENDED;
48     }
49     virtual ~RSExtendedModifier() = default;
50     virtual void Draw(RSDrawingContext& context) const = 0;
51 
52 protected:
53     explicit RSExtendedModifier(const RSModifierType type, const std::shared_ptr<RSPropertyBase>& property = {})
RSModifier(property,type)54         : RSModifier(property, type)
55     {
56         property_->SetIsCustom(true);
57     }
58 
CreateRenderModifier()59     std::shared_ptr<RSRenderModifier> CreateRenderModifier() const override
60     {
61         auto node = property_->target_.lock();
62         if (node == nullptr) {
63             return nullptr;
64         }
65         RSDrawingContext ctx = RSExtendedModifierHelper::CreateDrawingContext(node->GetId());
66         Draw(ctx);
67         return RSExtendedModifierHelper::CreateRenderModifier(ctx, property_->GetId(), GetModifierType());
68     }
69 
UpdateToRender()70     void UpdateToRender() override
71     {
72         auto node = property_->target_.lock();
73         if (node == nullptr) {
74             return;
75         }
76         RSDrawingContext ctx = RSExtendedModifierHelper::CreateDrawingContext(node->GetId());
77         Draw(ctx);
78         auto drawCmdList = RSExtendedModifierHelper::FinishDrawing(ctx);
79         std::unique_ptr<RSCommand> command = std::make_unique<RSUpdatePropertyDrawCmdList>(
80             node->GetId(), drawCmdList, property_->id_, false);
81         auto transactionProxy = RSTransactionProxy::GetInstance();
82         if (transactionProxy != nullptr) {
83             transactionProxy->AddCommand(command, node->IsRenderServiceNode());
84             if (node->NeedForcedSendToRemote()) {
85                 std::unique_ptr<RSCommand> commandForRemote = std::make_unique<RSUpdatePropertyDrawCmdList>(
86                     node->GetId(), drawCmdList, property_->id_, false);
87                 transactionProxy->AddCommand(commandForRemote, true, node->GetFollowType(), node->GetId());
88             }
89             if (node->NeedSendExtraCommand()) {
90                 std::unique_ptr<RSCommand> extraCommand = std::make_unique<RSUpdatePropertyDrawCmdList>(
91                     node->GetId(), drawCmdList, property_->id_, false);
92                 transactionProxy->AddCommand(extraCommand, !node->IsRenderServiceNode(), node->GetFollowType(),
93                     node->GetId());
94             }
95         }
96     }
97 };
98 
99 class RSC_EXPORT RSBackgroundStyleModifier : public RSExtendedModifier {
100 public:
RSBackgroundStyleModifier()101     RSBackgroundStyleModifier() : RSExtendedModifier(RSModifierType::BACKGROUND_STYLE)
102     {}
103 
GetModifierType()104     RSModifierType GetModifierType() const override
105     {
106         return RSModifierType::BACKGROUND_STYLE;
107     }
108 };
109 
110 class RSC_EXPORT RSContentStyleModifier : public RSExtendedModifier {
111 public:
RSContentStyleModifier()112     RSContentStyleModifier() : RSExtendedModifier(RSModifierType::CONTENT_STYLE)
113     {}
114 
GetModifierType()115     RSModifierType GetModifierType() const override
116     {
117         return RSModifierType::CONTENT_STYLE;
118     }
119 };
120 
121 class RSC_EXPORT RSForegroundStyleModifier : public RSExtendedModifier {
122 public:
RSForegroundStyleModifier()123     RSForegroundStyleModifier() : RSExtendedModifier(RSModifierType::FOREGROUND_STYLE)
124     {}
125 
GetModifierType()126     RSModifierType GetModifierType() const override
127     {
128         return RSModifierType::FOREGROUND_STYLE;
129     }
130 };
131 
132 class RSC_EXPORT RSOverlayStyleModifier : public RSExtendedModifier {
133 public:
RSOverlayStyleModifier()134     RSOverlayStyleModifier() : RSExtendedModifier(RSModifierType::OVERLAY_STYLE)
135     {}
136 
GetModifierType()137     RSModifierType GetModifierType() const override
138     {
139         return RSModifierType::OVERLAY_STYLE;
140     }
141 
SetOverlayBounds(std::shared_ptr<RectI> rect)142     void SetOverlayBounds(std::shared_ptr<RectI> rect)
143     {
144         overlayRect_ = rect;
145     }
146 
GetOverlayBounds()147     std::shared_ptr<RectI> GetOverlayBounds() const
148     {
149         return overlayRect_;
150     }
151 
CreateRenderModifier()152     std::shared_ptr<RSRenderModifier> CreateRenderModifier() const override
153     {
154         auto renderModifier = RSExtendedModifier::CreateRenderModifier();
155         auto drawCmdModifier = std::static_pointer_cast<RSDrawCmdListRenderModifier>(renderModifier);
156         if (drawCmdModifier != nullptr && drawCmdModifier->GetType() == RSModifierType::OVERLAY_STYLE) {
157             drawCmdModifier->SetOverlayBounds(overlayRect_);
158         }
159         return renderModifier;
160     }
161 
162 private:
163     std::shared_ptr<RectI> overlayRect_ = nullptr;
164 };
165 } // namespace Rosen
166 } // namespace OHOS
167 
168 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_EXTENDED_MODIFIER_H
169