1 /*
2  * Copyright (c) 2024 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_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
18 
19 #include "movingphoto_layout_algorithm.h"
20 #include "movingphoto_layout_property.h"
21 #include "movingphoto_event_hub.h"
22 #include "movingphoto_controller.h"
23 #include "movingphoto_utils.h"
24 
25 #include "base/image/pixel_map.h"
26 #include "base/memory/referenced.h"
27 #include "core/common/ai/image_analyzer_manager.h"
28 #include "core/common/container.h"
29 #include "core/components_ng/event/event_hub.h"
30 #include "core/components_ng/event/long_press_event.h"
31 #include "core/components_ng/event/touch_event.h"
32 #include "core/components_ng/pattern/pattern.h"
33 #include "core/components_ng/render/media_player.h"
34 #include "core/components_ng/render/render_surface.h"
35 #include "core/components/video/video_utils.h"
36 #include "core/components/image/image_event.h"
37 #include "interfaces/inner_api/ace/ai/image_analyzer.h"
38 
39 namespace OHOS::Ace::NG {
40 class MovingPhotoPattern : public Pattern {
41     DECLARE_ACE_TYPE(MovingPhotoPattern, Pattern);
42 
43 public:
44     MovingPhotoPattern() = delete;
45     explicit MovingPhotoPattern(const RefPtr<MovingPhotoController>& controller);
46     ~MovingPhotoPattern() override;
47 
48     RefPtr<EventHub> CreateEventHub() override
49     {
50         return MakeRefPtr<MovingPhotoEventHub>();
51     }
52 
53     RefPtr<LayoutProperty> CreateLayoutProperty() override
54     {
55         return MakeRefPtr<MovingPhotoLayoutProperty>();
56     }
57 
58     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
59     {
60         return MakeRefPtr<MovingPhotoLayoutAlgorithm>();
61     }
62 
UpdateMuted(bool isMuted)63     void UpdateMuted(bool isMuted)
64     {
65         isMuted_ = isMuted;
66     }
67 
GetMuted()68     bool GetMuted()
69     {
70         return isMuted_;
71     }
72 
GetPrepared()73     bool GetPrepared()
74     {
75         return isPrepared_;
76     }
77 
GetPlayByController()78     bool GetPlayByController()
79     {
80         return isPlayByController_;
81     }
82 
83     void OnVisibleChange(bool isVisible) override;
84 
85     void OnAreaChangedInner() override;
86 
87     void AutoPlayPeriod(int64_t startTime, int64_t endTime);
88 
89     void AutoPlay(bool isAutoPlay);
90 
91     void RepeatPlay(bool isRepeatPlay);
92 
93     FocusPattern GetFocusPattern() const override
94     {
95         return { FocusType::NODE, false };
96     }
97 
UpdateCurrentDateModified(int64_t currentDateModified)98     void UpdateCurrentDateModified(int64_t currentDateModified)
99     {
100         currentDateModified_ = currentDateModified;
101     }
102 
GetCurrentDateModified()103     int64_t GetCurrentDateModified()
104     {
105         return currentDateModified_;
106     }
107 
108     void EnableAnalyzer(bool enabled);
109 
110     void SetImageAIOptions(void* options);
111 
112     bool GetAnalyzerState();
113 
114 protected:
115     int32_t instanceId_;
116 
117     RefPtr<MediaPlayer> mediaPlayer_ = MediaPlayer::Create();
118     RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create();
119     RefPtr<RenderContext> renderContextForMediaPlayer_ = RenderContext::Create();
120 
121 private:
122     void OnModifyDone() override;
123     void OnAttachToFrameNode() override;
124     void OnDetachFromFrameNode(FrameNode* frameNode) override;
125     void OnDetachFromMainTree() override;
126     void OnRebuildFrame() override;
127     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
128     void OnWindowHide() override;
129     void OnWindowShow() override;
130 
131     void RegisterVisibleAreaChange();
132     void VisibleAreaCallback(bool visible);
133 
134     void InitEvent();
135     void LongPressEventModify(bool status);
136     void HandleLongPress(GestureEvent& info);
137     void HandleTouchEvent(TouchEventInfo& info);
138 
139     void UpdateImageNode();
140     void UpdateVideoNode();
141     void UpdatePlayMode();
142     void HandleImageAnalyzerMode();
143     SizeF CalculateFitContain(const SizeF& rawSize, const SizeF& layoutSize);
144     SizeF CalculateFitFill(const SizeF& layoutSize);
145     SizeF CalculateFitCover(const SizeF& rawSize, const SizeF& layoutSize);
146     SizeF CalculateFitNone(const SizeF& rawSize);
147     SizeF CalculateFitScaleDown(const SizeF& rawSize, const SizeF& layoutSize);
148     SizeF CalculateFitAuto(const SizeF& rawSize, const SizeF& layoutSize);
149     SizeF MeasureContentLayout(const SizeF& layoutSize, const RefPtr<MovingPhotoLayoutProperty>& layoutProperty);
150     SizeF GetRawImageSize();
151 
152     void PrepareMediaPlayer();
153     void ResetMediaPlayer();
154     void PrepareSurface();
155     void RegisterMediaPlayerEvent();
156     void PrintMediaPlayerStatus(PlaybackStatus status);
157     void RegisterImageEvent();
158     void HandleImageCompleteEvent(const LoadImageSuccessEvent& info);
159     void MediaResetToPlay();
160 
161     void FireMediaPlayerImageComplete();
162     void OnMediaPlayerStatusChanged(PlaybackStatus status);
163     void OnMediaPlayerInitialized();
164     void OnMediaPlayerPrepared();
165     void OnMediaPlayerStoped();
166     void OnMediaPlayerCompletion();
OnPlayPositionChanged(uint32_t pos)167     void OnPlayPositionChanged(uint32_t pos) {};
168     void FireMediaPlayerStart();
169     void FireMediaPlayerStop();
170     void FireMediaPlayerPause();
171     void FireMediaPlayerFinish();
172     void FireMediaPlayerError();
173     void OnResolutionChange();
174     void OnStartRenderFrame();
175     void OnStartedStatusCallback();
176 
177     void Start();
178     void Pause();
179     void Stop();
180     void Seek(int32_t position);
181 
182     void VisiblePlayback();
183     void SelectPlaybackMode(PlaybackMode mode);
184     void StartPlayback();
185     void StartAnimation();
186     void StopPlayback();
187     void PausePlayback();
188     void StopAnimation();
189     void StopAnimationCallback();
190     void StartAutoPlay();
191     void StartRepeatPlay();
192     void SetAutoPlayPeriod(int64_t startTime, int64_t endTime);
193     void HandleImageAnalyzerPlayCallBack();
194 
195     void UpdateMediaPlayerSpeed();
196     void UpdateMediaPlayerMuted();
197 
198     void HideImageNode();
199 
200     bool IsSupportImageAnalyzer();
201     bool ShouldUpdateImageAnalyzer();
202     void StartImageAnalyzer();
203     void StartUpdateImageAnalyzer();
204     void CreateAnalyzerOverlay();
205     void DestroyAnalyzerOverlay();
206     void UpdateAnalyzerOverlay();
207     void UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode);
208     void UpdateOverlayVisibility(VisibleType type);
209     void GetPixelMap();
210     int64_t GetUriCoverPosition();
211     void HandleAnalyzerPlayEvent(bool canPlay);
212 
213     RefPtr<LongPressEvent> longPressEvent_;
214     RefPtr<TouchEventImpl> touchEvent_;
215     RefPtr<MovingPhotoController> controller_;
216     RefPtr<PixelMap> pixelMap_;
217 
218     int32_t fd_ = -1;
219     int64_t autoPlayPeriodStartTime_ = -1;
220     int64_t autoPlayPeriodEndTime_ = -1;
221     std::string uri_ = "";
222     bool startAnimationFlag_ = false;
223     bool isPrepared_ = false;
224     bool isMuted_ = false;
225     bool isPlayByController_ = false;
226     bool isFastKeyUp_ = false;
227     bool hasVisibleChangeRegistered_ = false;
228     bool isFirstRepeatPlay_ = true;
229     bool isSetAutoPlayPeriod_ = false;
230     bool isVisible_ = false;
231     bool isChangePlayMode_ = false;
232     bool needUpdateImageNode_ = false;
233     PlaybackStatus currentPlayStatus_ = PlaybackStatus::NONE;
234     PlaybackMode autoAndRepeatLevel_ = PlaybackMode::NONE;
235     PlaybackMode historyAutoAndRepeatLevel_ = PlaybackMode::NONE;
236     int64_t currentDateModified_ = -2;
237 
238     bool isEnableAnalyzer_ = false;
239     bool isContentSizeChanged_ = false;
240     bool isAnalyzerCreated_ = false;
241     bool isPixelMapChanged_ = false;
242     bool isAnalyzerPlaying_ = false;
243 
244     Rect lastBoundsRect_;
245     Rect contentRect_;
246 
247     std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_;
248 
249     ACE_DISALLOW_COPY_AND_MOVE(MovingPhotoPattern);
250 };
251 } // namespace OHOS::Ace::NG
252 
253 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
254