• 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/rs_extended_modifier.h"
23 
24 #include "core/components_ng/render/adapter/rosen_modifier_adapter.h"
25 
26 namespace OHOS::Ace::NG {
27 class BackgroundModifier : public Rosen::RSBackgroundStyleModifier {
28 public:
29     BackgroundModifier() = default;
30     ~BackgroundModifier() override = default;
31 
Draw(Rosen::RSDrawingContext & context)32     void Draw(Rosen::RSDrawingContext& context) const override
33     {
34 #ifndef USE_ROSEN_DRAWING
35 #ifdef NEW_SKIA
36         CHECK_NULL_VOID(pixelMap_);
37         std::shared_ptr<Media::PixelMap> mediaPixelMap = pixelMap_->GetPixelMapSharedPtr();
38         std::shared_ptr<SkCanvas> skCanvas { context.canvas, [](SkCanvas* /* unused */) {} };
39         auto* recordingCanvas = static_cast<Rosen::RSRecordingCanvas*>(skCanvas.get());
40         SkSamplingOptions samplingOptions;
41         SkPaint paint;
42 
43         SizeF desSize(initialNodeWidth_, initialNodeHeight_);
44         SizeF srcSize(mediaPixelMap->GetWidth(), mediaPixelMap->GetHeight());
45         NG::OffsetF offset1 = Alignment::GetAlignPosition(srcSize, desSize, align_);
46         NG::OffsetF offset2 = Alignment::GetAlignPosition(desSize, srcSize, align_);
47         SkRect srcSKRect = SkRect::MakeXYWH(offset1.GetX(), offset1.GetY(), srcSize.Width(), srcSize.Height());
48         SkRect desSKRect = SkRect::MakeXYWH(offset2.GetX() * context.width / initialNodeWidth_,
49             offset2.GetY() * context.height / initialNodeHeight_, srcSize.Width() * context.width / initialNodeWidth_,
50             srcSize.Height() * context.height / initialNodeHeight_);
51         if (srcSize.Width() > desSize.Width()) {
52             srcSKRect.fRight = offset1.GetX() + desSize.Width();
53             desSKRect.fRight = context.width;
54         }
55         if (srcSize.Height() > desSize.Height()) {
56             srcSKRect.fBottom = offset1.GetY() + desSize.Height();
57             desSKRect.fBottom = context.height;
58         }
59         recordingCanvas->DrawPixelMapRect(mediaPixelMap, srcSKRect, desSKRect, samplingOptions, &paint);
60 #endif
61 #endif
62     }
63 
SetPixelMap(const RefPtr<PixelMap> & pixelMap)64     void SetPixelMap(const RefPtr<PixelMap>& pixelMap)
65     {
66         CHECK_NULL_VOID(pixelMap);
67         pixelMap_ = pixelMap;
68     }
69 
SetAlign(const Alignment & align)70     void SetAlign(const Alignment& align)
71     {
72         align_ = align;
73     }
74 
SetInitialNodeSize(const float width,const float height)75     void SetInitialNodeSize(const float width, const float height)
76     {
77         initialNodeWidth_ = width;
78         initialNodeHeight_ = height;
79     }
80 
Modify()81     void Modify()
82     {
83         if (!flag_) {
84             flag_ = std::make_shared<Rosen::RSProperty<bool>>(0);
85             AttachProperty(flag_);
86         } else {
87             flag_->Set(!flag_->Get());
88         }
89     }
90 
91 private:
92     RefPtr<PixelMap> pixelMap_;
93     Alignment align_;
94     float initialNodeWidth_ = 1.0f;
95     float initialNodeHeight_ = 1.0f;
96     std::shared_ptr<Rosen::RSProperty<bool>> flag_;
97 };
98 } // namespace OHOS::Ace::NG
99 
100 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_BACKGROUND_MODIFIER_H
101