• 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_PATTERN_H
18 
19 #include <memory>
20 
21 #include "base/memory/referenced.h"
22 #include "base/utils/utils.h"
23 #include "core/components_ng/pattern/image/image_event_hub.h"
24 #include "core/components_ng/pattern/image/image_layout_algorithm.h"
25 #include "core/components_ng/pattern/image/image_layout_property.h"
26 #include "core/components_ng/pattern/image/image_render_property.h"
27 #include "core/components_ng/pattern/pattern.h"
28 #include "core/components_ng/property/property.h"
29 #include "core/components_ng/render/canvas_image.h"
30 #include "core/pipeline_ng/pipeline_context.h"
31 
32 namespace OHOS::Ace::NG {
33 
34 class ACE_EXPORT ImagePattern : public Pattern {
35     DECLARE_ACE_TYPE(ImagePattern, Pattern);
36 
37 public:
38     ImagePattern() = default;
39     ~ImagePattern() override = default;
40 
41     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
42 
CreateLayoutProperty()43     RefPtr<LayoutProperty> CreateLayoutProperty() override
44     {
45         return MakeRefPtr<ImageLayoutProperty>();
46     }
47 
CreatePaintProperty()48     RefPtr<PaintProperty> CreatePaintProperty() override
49     {
50         return MakeRefPtr<ImageRenderProperty>();
51     }
52 
CreateLayoutAlgorithm()53     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
54     {
55         return MakeRefPtr<ImageLayoutAlgorithm>(loadingCtx_, altLoadingCtx_);
56     }
57 
CreateEventHub()58     RefPtr<EventHub> CreateEventHub() override
59     {
60         return MakeRefPtr<ImageEventHub>();
61     }
62 
63     // Called on main thread to check if need rerender of the content.
64     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
65 
GetFocusPattern()66     FocusPattern GetFocusPattern() const override
67     {
68         return { FocusType::NODE, false };
69     }
70 
71     void LoadImageDataIfNeed();
72     void OnNotifyMemoryLevel(int32_t level) override;
73     void OnWindowHide() override;
74     void OnWindowShow() override;
75     void OnVisibleChange(bool isVisible) override;
76 
77     void EnableDrag();
SetDraggable(bool draggable)78     void SetDraggable(bool draggable)
79     {
80         draggable_ = draggable;
81     }
82 
83 private:
84     void OnAttachToFrameNode() override;
85     void OnDetachFromFrameNode(FrameNode* frameNode) override;
86 
87     void OnModifyDone() override;
88 
89     void PaintImage(RenderContext* renderContext, const OffsetF& offset);
90 
91     void OnImageDataReady();
92     void OnImageLoadFail();
93     void OnImageLoadSuccess();
94     void SetImagePaintConfig(
95         const RefPtr<CanvasImage>& canvasImage, const RectF& srcRect, const RectF& dstRect, bool isSvg);
96     void UpdateInternalResource(ImageSourceInfo& sourceInfo);
97 
98     void PrepareAnimation();
99     void SetRedrawCallback();
100     void RegisterVisibleAreaChange();
101 
102     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override;
103 
104     DataReadyNotifyTask CreateDataReadyCallback();
105     LoadSuccessNotifyTask CreateLoadSuccessCallback();
106     LoadFailNotifyTask CreateLoadFailCallback();
107 
108     DataReadyNotifyTask CreateDataReadyCallbackForAlt();
109     LoadSuccessNotifyTask CreateLoadSuccessCallbackForAlt();
110     LoadFailNotifyTask CreateLoadFailCallbackForAlt();
111 
112     RefPtr<ImageLoadingContext> loadingCtx_;
113     RefPtr<CanvasImage> image_;
114     RectF dstRect_;
115     RectF srcRect_;
116 
117     bool draggable_ = false;
118     bool isShow_ = true; // TODO: remove it later when use [isActive_] to determine image data management
119 
120     // clear alt data after [OnImageLoadSuccess] being called
121     RefPtr<ImageLoadingContext> altLoadingCtx_;
122     RefPtr<CanvasImage> altImage_;
123     std::unique_ptr<RectF> altDstRect_;
124     std::unique_ptr<RectF> altSrcRect_;
125 
126     ACE_DISALLOW_COPY_AND_MOVE(ImagePattern);
127 };
128 
129 } // namespace OHOS::Ace::NG
130 
131 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_PATTERN_H
132