• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_BACKGROUND_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_BACKGROUND_MODIFIER_H
18 
19 #include <functional>
20 #include <memory>
21 
22 #include "render_service_client/core/modifier_ng/custom/rs_background_style_modifier.h"
23 
24 #include "core/components_ng/render/adapter/rosen_modifier_adapter.h"
25 
26 namespace OHOS::Ace::NG {
27 using RSBackgroundStyleModifier = Rosen::ModifierNG::RSBackgroundStyleModifier;
28 using RSDrawingContext = Rosen::ModifierNG::RSDrawingContext;
29 
30 class BackgroundModifier : public RSBackgroundStyleModifier {
31 public:
32     BackgroundModifier() = default;
33     ~BackgroundModifier() override = default;
34 
Draw(RSDrawingContext & context)35     void Draw(RSDrawingContext& context) const override
36     {
37         auto host = host_.Upgrade();
38         CHECK_NULL_VOID(host);
39         auto curSize = host->GetGeometryNode()->GetFrameSize();
40         auto curWidth = curSize.Width();
41         auto curHeight = curSize.Height();
42         CHECK_NULL_VOID(pixelMap_);
43         std::shared_ptr<Media::PixelMap> mediaPixelMap = pixelMap_->GetPixelMapSharedPtr();
44         CHECK_NULL_VOID(mediaPixelMap);
45         CHECK_NULL_VOID(context.canvas);
46         auto& recordingCanvas = static_cast<Rosen::ExtendRecordingCanvas&>(*context.canvas);
47         RSSamplingOptions samplingOptions;
48         RSBrush brush;
49 
50         SizeF desSize(initialNodeWidth_, initialNodeHeight_);
51         SizeF srcSize(mediaPixelMap->GetWidth(), mediaPixelMap->GetHeight());
52         NG::OffsetF offset1 = Alignment::GetAlignPosition(srcSize, desSize, align_);
53         NG::OffsetF offset2 = Alignment::GetAlignPosition(desSize, srcSize, align_);
54         RSRect srcRSRect =
55             RSRect(offset1.GetX(), offset1.GetY(), srcSize.Width() + offset1.GetX(), srcSize.Height() + offset1.GetY());
56         RSRect desRSRect =
57             RSRect(offset2.GetX() * curWidth / initialNodeWidth_, offset2.GetY() * curHeight / initialNodeHeight_,
58                 srcSize.Width() * curWidth / initialNodeWidth_ + offset2.GetX() * curWidth / initialNodeWidth_,
59                 srcSize.Height() * curHeight / initialNodeHeight_ + offset2.GetY() * curHeight / initialNodeHeight_);
60         if (srcSize.Width() > desSize.Width()) {
61             srcRSRect.SetRight(offset1.GetX() + desSize.Width());
62             desRSRect.SetRight(curWidth);
63         }
64         if (srcSize.Height() > desSize.Height()) {
65             srcRSRect.SetBottom(offset1.GetY() + desSize.Height());
66             desRSRect.SetBottom(curHeight);
67         }
68         recordingCanvas.AttachBrush(brush);
69         recordingCanvas.DrawPixelMapRect(mediaPixelMap, srcRSRect, desRSRect, samplingOptions);
70         recordingCanvas.DetachBrush();
71     }
72 
SetPixelMap(const RefPtr<PixelMap> & pixelMap)73     void SetPixelMap(const RefPtr<PixelMap>& pixelMap)
74     {
75         CHECK_NULL_VOID(pixelMap);
76         pixelMap_ = pixelMap;
77     }
78 
SetAlign(const Alignment & align)79     void SetAlign(const Alignment& align)
80     {
81         align_ = align;
82     }
83 
SetInitialNodeSize(const float width,const float height)84     void SetInitialNodeSize(const float width, const float height)
85     {
86         initialNodeWidth_ = width;
87         initialNodeHeight_ = height;
88     }
89 
Modify()90     void Modify()
91     {
92         if (!flag_) {
93             flag_ = std::make_shared<Rosen::RSProperty<bool>>(0);
94             AttachProperty(flag_);
95         } else {
96             flag_->Set(!flag_->Get());
97         }
98     }
99 
SetHostNode(RefPtr<FrameNode> host)100     void SetHostNode(RefPtr<FrameNode> host)
101     {
102         host_ = host;
103     }
104 
105 private:
106     RefPtr<PixelMap> pixelMap_;
107     Alignment align_;
108     float initialNodeWidth_ = 1.0f;
109     float initialNodeHeight_ = 1.0f;
110     std::shared_ptr<Rosen::RSProperty<bool>> flag_;
111     WeakPtr<FrameNode> host_;
112 };
113 } // namespace OHOS::Ace::NG
114 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_BACKGROUND_MODIFIER_H
115