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