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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_BORDER_IMAGE_MODIFIER_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_BORDER_IMAGE_MODIFIER_H
17 
18 #include <functional>
19 #include <memory>
20 
21 #include "render_service_client/core/modifier/rs_extended_modifier.h"
22 
23 #include "core/components_ng/property/gradient_property.h"
24 #include "core/components_ng/render/adapter/rosen_modifier_adapter.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 class BorderImageModifier : public Rosen::RSForegroundStyleModifier {
29 public:
30     BorderImageModifier() = default;
31     ~BorderImageModifier() override = default;
32 
Draw(RSDrawingContext & context)33     void Draw(RSDrawingContext& context) const override
34     {
35 #ifndef USE_ROSEN_DRAWING
36         std::shared_ptr<SkCanvas> skCanvas { context.canvas, [](SkCanvas* /*unused*/) {} };
37         RSCanvas rsCanvas(&skCanvas);
38         CHECK_NULL_VOID(paintTask_);
39         paintTask_(rsCanvas);
40 #else
41         CHECK_NULL_VOID(context.canvas);
42         CHECK_NULL_VOID(paintTask_);
43         paintTask_(*context.canvas);
44 #endif
45     }
46 
SetPaintTask(std::function<void (RSCanvas &)> && paintTask)47     void SetPaintTask(std::function<void(RSCanvas&)>&& paintTask)
48     {
49         paintTask_ = paintTask;
50     }
51 
Modify()52     void Modify()
53     {
54         if (!flag_) {
55             flag_ = std::make_shared<Rosen::RSProperty<bool>>(0);
56             AttachProperty(flag_);
57         } else {
58             flag_->Set(!flag_->Get());
59         }
60     }
61 
62 private:
63     std::shared_ptr<Rosen::RSProperty<bool>> flag_;
64     std::function<void(RSCanvas&)> paintTask_;
65 };
66 
67 } // namespace OHOS::Ace::NG
68 
69 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_BORDER_IMAGE_MODIFIER_H
70