• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetCurrentDateModified()100     int64_t GetCurrentDateModified()
101     {
102         return currentDateModified_;
103     }
104 
105 protected:
106     int32_t instanceId_;
107 
108     RefPtr<MediaPlayer> mediaPlayer_ = MediaPlayer::Create();
109     RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create();
110     RefPtr<RenderContext> renderContextForMediaPlayer_ = RenderContext::Create();
111 
112 private:
113     void OnModifyDone() override;
114     void OnAttachToFrameNode() override;
115     void OnDetachFromFrameNode(FrameNode* frameNode) override;
116     void OnDetachFromMainTree() override;
117     void OnRebuildFrame() override;
118     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
119     void OnWindowHide() override;
120     void OnWindowShow() override;
121 
122     void RegisterVisibleAreaChange();
123     void VisibleAreaCallback(bool visible);
124 
125     void InitEvent();
126     void HandleLongPress(GestureEvent& info);
127     void HandleTouchEvent(TouchEventInfo& info);
128 
129     void UpdateImageNode();
130     void UpdateVideoNode();
131     void UpdatePlayMode();
132     SizeF CalculateFitContain(const SizeF& rawSize, const SizeF& layoutSize);
133     SizeF CalculateFitFill(const SizeF& layoutSize);
134     SizeF CalculateFitCover(const SizeF& rawSize, const SizeF& layoutSize);
135     SizeF CalculateFitNone(const SizeF& rawSize);
136     SizeF CalculateFitScaleDown(const SizeF& rawSize, const SizeF& layoutSize);
137     SizeF CalculateFitAuto(const SizeF& rawSize, const SizeF& layoutSize);
138     SizeF MeasureContentLayout(const SizeF& layoutSize, const RefPtr<MovingPhotoLayoutProperty>& layoutProperty);
139     SizeF GetRawImageSize();
140 
141     void PrepareMediaPlayer();
142     void ResetMediaPlayer();
143     void PrepareSurface();
144     void RegisterMediaPlayerEvent();
145     void PrintMediaPlayerStatus(PlaybackStatus status);
146     void RegisterImageEvent();
147     void HandleImageCompleteEvent(const LoadImageSuccessEvent& info);
148     void MediaResetToPlay();
149 
150     void FireMediaPlayerImageComplete();
151     void OnMediaPlayerStatusChanged(PlaybackStatus status);
152     void OnMediaPlayerInitialized();
153     void OnMediaPlayerPrepared();
154     void OnMediaPlayerStoped();
155     void OnMediaPlayerCompletion();
OnPlayPositionChanged(uint32_t pos)156     void OnPlayPositionChanged(uint32_t pos) {};
157     void FireMediaPlayerStart();
158     void FireMediaPlayerStop();
159     void FireMediaPlayerPause();
160     void FireMediaPlayerFinish();
161     void FireMediaPlayerError();
162     void OnResolutionChange();
163     void OnStartRenderFrame();
164     void OnStartedStatusCallback();
165 
166     void Start();
167     void Pause();
168     void Stop();
169     void Seek(int32_t position);
170 
171     void VisiblePlayback();
172     void SelectPlaybackMode(PlaybackMode mode);
173     void StartPlayback();
174     void StartAnimation();
175     void CommonStartAnimation(const RefPtr<RenderContext>& image, const RefPtr<RenderContext>& video);
176     void RepeatStartAnimation(const RefPtr<RenderContext>& videoRsContext);
177     void StopPlayback();
178     void PausePlayback();
179     void StopAnimation();
180     void RepeatStopAnimation(const RefPtr<RenderContext>& videoRsContext);
181     void StopAnimationCallback();
182     void StartAutoPlay();
183     void StartRepeatPlay();
184     void SetAutoPlayPeriod(int64_t startTime, int64_t endTime);
185 
186     void UpdateMediaPlayerSpeed();
187     void UpdateMediaPlayerMuted();
188 
189     void HideImageNode();
190 
191     RefPtr<LongPressEvent> longPressEvent_;
192     RefPtr<TouchEventImpl> touchEvent_;
193     RefPtr<MovingPhotoController> controller_;
194 
195     int32_t fd_ = -1;
196     int64_t autoPlayPeriodStartTime_ = -1;
197     int64_t autoPlayPeriodEndTime_ = -1;
198     std::string uri_ = "";
199     bool startAnimationFlag_ = false;
200     bool isPrepared_ = false;
201     bool isMuted_ = false;
202     bool isPlayByController_ = false;
203     bool isFastKeyUp_ = false;
204     bool hasVisibleChangeRegistered_ = false;
205     bool isFirstRepeatPlay_ = true;
206     bool isSetAutoPlayPeriod_ = false;
207     bool isVisible_ = false;
208     bool isChangePlayMode_ = false;
209     bool needUpdateImageNode_ = false;
210     PlaybackStatus currentPlayStatus_ = PlaybackStatus::NONE;
211     PlaybackMode autoAndRepeatLevel_ = PlaybackMode::NONE;
212     PlaybackMode historyAutoAndRepeatLevel_ = PlaybackMode::NONE;
213     int64_t currentDateModified_ = -2;
214 
215     Rect lastBoundsRect_;
216 
217     ACE_DISALLOW_COPY_AND_MOVE(MovingPhotoPattern);
218 };
219 } // namespace OHOS::Ace::NG
220 
221 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
222