• 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/geometry/offset.h"
22 #include "base/memory/referenced.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/event/click_event.h"
25 #include "core/components_ng/manager/select_overlay/selection_host.h"
26 #include "core/components_ng/pattern/image/image_event_hub.h"
27 #include "core/components_ng/pattern/image/image_layout_algorithm.h"
28 #include "core/components_ng/pattern/image/image_layout_property.h"
29 #include "core/components_ng/pattern/image/image_render_property.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/components_ng/render/canvas_image.h"
32 #include "core/image/image_source_info.h"
33 #include "interfaces/inner_api/ace/ai/image_analyzer.h"
34 
35 namespace OHOS::Ace::NG {
36 
37 class ACE_EXPORT ImagePattern : public Pattern, public SelectionHost {
38     DECLARE_ACE_TYPE(ImagePattern, Pattern, SelectionHost);
39 
40 public:
ImagePattern()41     ImagePattern()
42     {
43         InitDefaultValue();
44     }
45     ~ImagePattern() override = default;
46 
GetContextParam()47     std::optional<RenderContext::ContextParam> GetContextParam() const override
48     {
49         return RenderContext::ContextParam { RenderContext::ContextType::CANVAS };
50     }
51 
52     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
53 
CreateLayoutProperty()54     RefPtr<LayoutProperty> CreateLayoutProperty() override
55     {
56         return MakeRefPtr<ImageLayoutProperty>();
57     }
58 
CreatePaintProperty()59     RefPtr<PaintProperty> CreatePaintProperty() override
60     {
61         return MakeRefPtr<ImageRenderProperty>();
62     }
63 
CreateLayoutAlgorithm()64     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
65     {
66         return MakeRefPtr<ImageLayoutAlgorithm>(loadingCtx_, altLoadingCtx_, autoResizeDefault_);
67     }
68 
CreateEventHub()69     RefPtr<EventHub> CreateEventHub() override
70     {
71         return MakeRefPtr<ImageEventHub>();
72     }
73 
74     // Called on main thread to check if need rerender of the content.
75     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
76 
GetFocusPattern()77     FocusPattern GetFocusPattern() const override
78     {
79         return { FocusType::NODE, false };
80     }
81 
GetCanvasImage()82     const RefPtr<CanvasImage>& GetCanvasImage()
83     {
84         return image_;
85     }
86 
87     void CreateObscuredImage();
88     void LoadImageDataIfNeed();
89     void OnNotifyMemoryLevel(int32_t level) override;
90     void OnWindowHide() override;
91     void OnWindowShow() override;
92     void OnVisibleChange(bool isVisible) override;
93     void OnRecycle() override;
94     void OnReuse() override;
95 
96     void EnableDrag();
97     bool BetweenSelectedPosition(const Offset& globalOffset) override;
98 
DefaultSupportDrag()99     bool DefaultSupportDrag() override
100     {
101         return true;
102     }
103 
SetCopyOption(CopyOptions value)104     void SetCopyOption(CopyOptions value)
105     {
106         copyOption_ = value;
107     }
108 
SetImageInterpolation(ImageInterpolation value)109     void SetImageInterpolation(ImageInterpolation value)
110     {
111         interpolation_ = value;
112     }
113 
GetImageInterpolation()114     std::string GetImageInterpolation()
115     {
116         switch (interpolation_) {
117             case ImageInterpolation::LOW:
118                 return "LOW";
119             case ImageInterpolation::MEDIUM:
120                 return "MEDIUM";
121             case ImageInterpolation::HIGH:
122                 return "HIGH";
123             default:
124                 return "NONE";
125         }
126     }
127 
SetSyncLoad(bool value)128     void SetSyncLoad(bool value)
129     {
130         syncLoad_ = value;
131     }
132 
EnableAnalyzer(bool value)133     void EnableAnalyzer(bool value)
134     {
135         isEnableAnalyzer_ = value;
136     }
137 
138     void SetImageAnalyzerConfig(const ImageAnalyzerConfig& config);
139 
140     void BeforeCreatePaintWrapper() override;
141     void DumpInfo() override;
142     void DumpAdvanceInfo() override;
143 
GetImageLoadingContext()144     WeakPtr<ImageLoadingContext> GetImageLoadingContext()
145     {
146         return WeakClaim(AceType::RawPtr(loadingCtx_));
147     }
148 
GetAltImageLoadingContext()149     WeakPtr<ImageLoadingContext> GetAltImageLoadingContext()
150     {
151         return WeakClaim(AceType::RawPtr(altLoadingCtx_));
152     }
153 
154 protected:
155     void RegisterWindowStateChangedCallback();
156     void UnregisterWindowStateChangedCallback();
157     bool isShow_ = true;
158 
159 private:
160     class ObscuredImage : public CanvasImage {
DrawToRSCanvas(RSCanvas & canvas,const RSRect & srcRect,const RSRect & dstRect,const BorderRadiusArray & radiusXY)161         void DrawToRSCanvas(
162             RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect, const BorderRadiusArray& radiusXY) override
163         {}
GetWidth()164         int32_t GetWidth() const override
165         {
166             return 0;
167         }
GetHeight()168         int32_t GetHeight() const override
169         {
170             return 0;
171         }
172     };
173 
174     void OnAttachToFrameNode() override;
175     void OnDetachFromFrameNode(FrameNode* frameNode) override;
176 
177     void OnModifyDone() override;
178     void UpdateGestureAndDragWhenModify();
179 
IsNeedInitClickEventRecorder()180     bool IsNeedInitClickEventRecorder() const override
181     {
182         return true;
183     }
184 
185     void OnLanguageConfigurationUpdate() override;
186 
187     void OnImageDataReady();
188     void OnImageLoadFail(const std::string& errorMsg);
189     void OnImageLoadSuccess();
190     void SetImagePaintConfig(const RefPtr<CanvasImage>& canvasImage, const RectF& srcRect, const RectF& dstRect,
191         const ImageSourceInfo& sourceInfo, int32_t frameCount = 1);
192     void UpdateInternalResource(ImageSourceInfo& sourceInfo);
193 
194     void PrepareAnimation(const RefPtr<CanvasImage>& image);
195     void SetRedrawCallback(const RefPtr<CanvasImage>& image);
196     void RegisterVisibleAreaChange();
197 
198     void InitCopy();
199     void HandleCopy();
200     void OpenSelectOverlay();
201     void CloseSelectOverlay();
202 
203     void UpdateFillColorIfForegroundColor();
204     void UpdateDragEvent(const RefPtr<OHOS::Ace::DragEvent>& event);
205 
206     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override;
207 
208     RectF CalcImageContentPaintSize(const RefPtr<GeometryNode>& geometryNode);
209 
210     DataReadyNotifyTask CreateDataReadyCallback();
211     LoadSuccessNotifyTask CreateLoadSuccessCallback();
212     LoadFailNotifyTask CreateLoadFailCallback();
213 
214     DataReadyNotifyTask CreateDataReadyCallbackForAlt();
215     LoadSuccessNotifyTask CreateLoadSuccessCallbackForAlt();
216     LoadFailNotifyTask CreateLoadFailCallbackForAlt();
217 
218     void OnColorConfigurationUpdate() override;
219     void OnIconConfigurationUpdate() override;
220     void OnConfigurationUpdate();
221     void LoadImage(const ImageSourceInfo& src);
222     void LoadAltImage(const ImageSourceInfo& altImageSourceInfo);
223 
224     void UpdateAnalyzerUIConfig(const RefPtr<GeometryNode>& geometryNode);
225     void CreateAnalyzerOverlay();
226     void UpdateAnalyzerOverlay();
227     void DeleteAnalyzerOverlay();
228     bool IsSupportImageAnalyzerFeature();
229     void UpdateAnalyzerOverlayLayout();
230     void InitDefaultValue();
231 
232     CopyOptions copyOption_ = CopyOptions::None;
233     ImageInterpolation interpolation_ = ImageInterpolation::NONE;
234 
235     RefPtr<ImageLoadingContext> loadingCtx_;
236     RefPtr<CanvasImage> image_;
237     RectF dstRect_;
238     RectF srcRect_;
239 
240     RefPtr<CanvasImage> obscuredImage_;
241 
242     // clear alt data after [OnImageLoadSuccess] being called
243     RefPtr<ImageLoadingContext> altLoadingCtx_;
244     RefPtr<CanvasImage> altImage_;
245     std::unique_ptr<RectF> altDstRect_;
246     std::unique_ptr<RectF> altSrcRect_;
247 
248     RefPtr<LongPressEvent> longPressEvent_;
249     RefPtr<ClickEvent> clickEvent_;
250     RefPtr<InputEvent> mouseEvent_;
251     RefPtr<Clipboard> clipboard_;
252     RefPtr<SelectOverlayProxy> selectOverlay_;
253     ImageAnalyzerConfig analyzerConfig_;
254     ImageAnalyzerInnerConfig analyzerUIConfig_;
255 
256     void* aiNapiValue_ = nullptr;
257     void* overlayData_ = nullptr;
258 
259     bool syncLoad_ = false;
260     bool isEnableAnalyzer_ = false;
261     bool isAnalyzerOverlayBuild_ = false;
262     bool autoResizeDefault_ = true;
263     ImageInterpolation interpolationDefault_ = ImageInterpolation::NONE;
264 
265     ACE_DISALLOW_COPY_AND_MOVE(ImagePattern);
266 };
267 
268 } // namespace OHOS::Ace::NG
269 
270 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_PATTERN_H
271