• 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/image/pixel_map.h"
26 #include "base/image/image_source.h"
27 #include "base/memory/referenced.h"
28 #include "core/common/ai/image_analyzer_manager.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 
CreateEventHub()48     RefPtr<EventHub> CreateEventHub() override
49     {
50         return MakeRefPtr<MovingPhotoEventHub>();
51     }
52 
CreateLayoutProperty()53     RefPtr<LayoutProperty> CreateLayoutProperty() override
54     {
55         return MakeRefPtr<MovingPhotoLayoutProperty>();
56     }
57 
CreateLayoutAlgorithm()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 
GetHdrBrightness()83     float GetHdrBrightness()
84     {
85         return hdrBrightness_;
86     }
87 
GetCameraPostprocessingEnabled()88     bool GetCameraPostprocessingEnabled()
89     {
90         return cameraPostprocessingEnabled_;
91     }
92 
93     void SetHdrBrightness(float hdrBrightness);
94 
95     void OnVisibleChange(bool isVisible) override;
96 
97     void OnAreaChangedInner() override;
98 
99     void AutoPlayPeriod(int64_t startTime, int64_t endTime);
100 
101     void AutoPlay(bool isAutoPlay);
102 
103     void RepeatPlay(bool isRepeatPlay);
104 
GetFocusPattern()105     FocusPattern GetFocusPattern() const override
106     {
107         return { FocusType::NODE, false };
108     }
109 
UpdateCurrentDateModified(int64_t currentDateModified)110     void UpdateCurrentDateModified(int64_t currentDateModified)
111     {
112         currentDateModified_ = currentDateModified;
113     }
114 
SetMovingPhotoFormat(MovingPhotoFormat format)115     void SetMovingPhotoFormat(MovingPhotoFormat format)
116     {
117         movingPhotoFormat_ = format;
118     }
119 
SetDynamicRangeMode(DynamicRangeMode rangeMode)120     void SetDynamicRangeMode(DynamicRangeMode rangeMode)
121     {
122         dynamicRangeMode_ = rangeMode;
123     }
124 
SetWaterMask(bool enabled)125     void SetWaterMask(bool enabled)
126     {
127         isPlayWithMask_ = enabled;
128     }
129 
GetWaterMask()130     bool GetWaterMask()
131     {
132         return isPlayWithMask_;
133     }
134 
SetEnableCameraPostprocessing(bool isEnabled)135     void SetEnableCameraPostprocessing(bool isEnabled)
136     {
137         cameraPostprocessingEnabled_ = isEnabled;
138     }
139 
GetCurrentDateModified()140     int64_t GetCurrentDateModified()
141     {
142         return currentDateModified_;
143     }
144 
GetXmageModeStatus()145     bool GetXmageModeStatus()
146     {
147         return isXmageMode_;
148     }
149 
GetXmageModeValue()150     int32_t GetXmageModeValue()
151     {
152         return xmageModeValue_;
153     }
154 
155     void EnableAnalyzer(bool enabled);
156 
157     void SetImageAIOptions(void* options);
158 
159     bool GetAnalyzerState();
160 
161     float CalculateRatio(SizeF layoutSize);
162 
163     void SetXmagePosition();
164 
165     SizeF CalculateXmageOffsetRatio(SizeF layoutSize);
166 
167 protected:
168     int32_t instanceId_;
169 
170     RefPtr<MediaPlayer> mediaPlayer_ = MediaPlayer::Create();
171     RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create();
172     RefPtr<RenderContext> renderContextForMediaPlayer_ = RenderContext::Create();
173     RefPtr<RenderSurface> columnSurface_ = RenderSurface::Create();
174     RefPtr<RenderContext> columnRenderContext_ = RenderContext::Create();
175 
176 private:
177     void OnModifyDone() override;
178     void OnAttachToFrameNode() override;
179     void OnDetachFromFrameNode(FrameNode* frameNode) override;
180     void OnDetachFromMainTree() override;
181     void OnRebuildFrame() override;
182     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
183     void OnWindowHide() override;
184     void OnWindowShow() override;
185 
186     void AddWindowStateChangedCallback();
187     void RegisterVisibleAreaChange();
188     void VisibleAreaCallback(bool visible);
189 
190     void InitEvent();
191     void LongPressEventModify(bool status);
192     void HandleLongPress(GestureEvent& info);
193     void HandleTouchEvent(TouchEventInfo& info);
194 
195     void UpdateImageNode();
196     void UpdateTempImageNode(const ImageSourceInfo& imageSourceInfo);
197     void UpdateVideoNode();
198     void UpdatePlayMode();
199     void HandleImageAnalyzerMode();
200     void UpdateImageHdrMode(const RefPtr<FrameNode>& imageNode);
201     void MovingPhotoFormatConvert(MovingPhotoFormat format);
202     void DynamicRangeModeConvert(DynamicRangeMode rangeMode);
203     void UpdateXmageProperty(
204         RefPtr<ImageSource> imageSrc, SizeF& imageSize, float imageW, float imageL, RefPtr<FrameNode>& host);
205     void SetRenderContextBounds(const SizeF& movingPhotoNodeSize, const SizeF& VideoFrameSize);
206     void SetRenderContextBoundsInXmage(const SizeF& movingPhotoNodeSize, const SizeF& videoFrameSize);
207     SizeF CalculateFitContain(const SizeF& rawSize, const SizeF& layoutSize);
208     SizeF CalculateFitFill(const SizeF& layoutSize);
209     SizeF CalculateFitCover(const SizeF& rawSize, const SizeF& layoutSize);
210     SizeF CalculateFitNone(const SizeF& rawSize);
211     SizeF CalculateFitScaleDown(const SizeF& rawSize, const SizeF& layoutSize);
212     SizeF CalculateFitAuto(const SizeF& rawSize, const SizeF& layoutSize);
213     SizeF CalculateModeFitContain(const SizeF& rawSize, const SizeF& layoutSize);
214     SizeF CalculateModeFitFill(const SizeF& layoutSize);
215     SizeF CalculateModeFitCover(const SizeF& rawSize, const SizeF& layoutSize);
216     SizeF CalculateModeFitNone(const SizeF& rawSize);
217     SizeF CalculateModeFitScaleDown(const SizeF& rawSize, const SizeF& layoutSize);
218     SizeF CalculateModeFitAuto(const SizeF& rawSize, const SizeF& layoutSize);
219     SizeF MeasureContentLayout(const SizeF& layoutSize, const RefPtr<MovingPhotoLayoutProperty>& layoutProperty);
220     SizeF MeasureModeContentLayout(const SizeF& layoutSize, const RefPtr<MovingPhotoLayoutProperty>& layoutProperty);
221     SizeF GetRawImageSize();
222     int32_t GetImageFd() const;
223 
224     void PrepareMediaPlayer();
225     void ResetMediaPlayer();
226     void PrepareSurface();
227     void RegisterMediaPlayerEvent();
228     void PrintMediaPlayerStatus(PlaybackStatus status);
229     void RegisterImageEvent(const RefPtr<FrameNode>& imageNode);
230     void HandleImageCompleteEvent(const LoadImageSuccessEvent& info);
231     void MediaResetToPlay();
232 
233     void FireMediaPlayerImageComplete();
234     void OnMediaPlayerStatusChanged(PlaybackStatus status);
235     void OnMediaPlayerInitialized();
236     void OnMediaPlayerPrepared();
237     void OnMediaPlayerStoped();
238     void OnMediaPlayerCompletion();
OnPlayPositionChanged(uint32_t pos)239     void OnPlayPositionChanged(uint32_t pos) {};
240     void FireMediaPlayerStart();
241     void FireMediaPlayerStop();
242     void FireMediaPlayerPause();
243     void FireMediaPlayerFinish();
244     void FireMediaPlayerError();
245     void FireMediaPlayerPrepared();
246     void OnResolutionChange();
247     void OnStartRenderFrame();
248     void OnStartedStatusCallback();
249 
250     void Start();
251     void Pause();
252     void Stop();
253     void Seek(int32_t position);
254 
255     void PreparedToPlay();
256     void SelectPlaybackMode(PlaybackMode mode);
257     void StartPlayback();
258     void StartAnimation();
259     void RsContextUpdateTransformScale(const RefPtr<RenderContext>& imageRsContext,
260             const RefPtr<RenderContext>& videoRsContext, PlaybackMode playbackMode);
261     void StopPlayback();
262     void PausePlayback();
263     void RefreshMovingPhoto();
264     void RefreshMovingPhotoSceneManager();
265     void PauseVideo();
266     void ResetVideo();
267     void RestartVideo();
268     void SetEnableTransition(bool enabled);
269     bool GetEnableTransition();
270     bool SetPlaybackPeriod(int64_t startTime, int64_t endTime);
271     void EnableAutoPlay(bool enabled);
272     void SetStartPlaybackImpl(const SingleTaskExecutor& uiTaskExecutor);
273     void SetStopPlaybackImpl(const SingleTaskExecutor& uiTaskExecutor);
274     void SetRefreshMovingPhotoImpl(const SingleTaskExecutor& uiTaskExecutor);
275     void SetPauseImpl(const SingleTaskExecutor& uiTaskExecutor);
276     void SetResetImpl(const SingleTaskExecutor& uiTaskExecutor);
277     void SetRestartImpl(const SingleTaskExecutor& uiTaskExecutor);
278     void SetEnableTransitionImpl(const SingleTaskExecutor& uiTaskExecutor);
279     void SetPlaybackPeriodImpl(const SingleTaskExecutor& uiTaskExecutor);
280     void SetEnableAutoPlayImpl(const SingleTaskExecutor& uiTaskExecutor);
281     void SetNotifyTransitionImpl(const SingleTaskExecutor& uiTaskExecutor);
282     void NotifyTransition();
283     void EightyToHundredAnimation();
284     void AddTempNode(const RefPtr<FrameNode>& imageNode, const RefPtr<FrameNode>& movingPhotoNode);
285     void DetachFirstImageFromFrameNode();
286     RefPtr<FrameNode> GetTempNode();
287     void StopAnimation();
288     void StopAnimationCallback();
289     void StartAutoPlay();
290     void StartRepeatPlay();
291     void SetAutoPlayPeriod(int64_t startTime, int64_t endTime);
292     void HandleImageAnalyzerPlayCallBack();
293 
294     void UpdateMediaPlayerSpeed();
295     void UpdateMediaPlayerMuted();
296 
297     void HideImageNode();
298 
299     bool IsSupportImageAnalyzer();
300     bool ShouldUpdateImageAnalyzer();
301     bool IsAllZeroPositionInXmage(const RefPtr<ImageSource>& imageSrc);
302     void StartImageAnalyzer();
303     void StartUpdateImageAnalyzer();
304     void CreateAnalyzerOverlay();
305     void DestroyAnalyzerOverlay();
306     void UpdateAnalyzerOverlay();
307     void UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode);
308     void UpdateOverlayVisibility(VisibleType type);
309     void GetPixelMap();
310     int64_t GetUriCoverPosition();
311     void HandleAnalyzerPlayEvent(bool canPlay);
312     bool IsRefreshMovingPhotoReturn(bool status);
313 
314     RefPtr<LongPressEvent> longPressEvent_;
315     RefPtr<TouchEventImpl> touchEvent_;
316     RefPtr<MovingPhotoController> controller_;
317     RefPtr<PixelMap> pixelMap_;
318 
319     SharedFd fd_;
320     int64_t autoPlayPeriodStartTime_ = -1;
321     int64_t autoPlayPeriodEndTime_ = -1;
322     float hdrBrightness_ = 1.0f;
323     std::string uri_ = "";
324     int32_t xmageModeValue_ = 0;
325     bool isXmageMode_ = false;
326     bool startAnimationFlag_ = false;
327     bool isPrepared_ = false;
328     bool isMuted_ = false;
329     bool isPlayByController_ = false;
330     bool isFastKeyUp_ = false;
331     bool hasVisibleChangeRegistered_ = false;
332     bool isFirstRepeatPlay_ = true;
333     bool isSetAutoPlayPeriod_ = false;
334     bool isVisible_ = false;
335     bool isChangePlayMode_ = false;
336     bool isRepeatChangePlayMode_ = false;
337     bool isAutoChangePlayMode_ = false;
338     bool needUpdateImageNode_ = false;
339     bool isPlayWithMask_ = false;
340     bool isEnableTransition_ = true;
341     bool isStopAnimation_ = false;
342     bool notifyTransitionFlag_ = false;
343     bool cameraPostprocessingEnabled_ = false;
344     bool isGestureTriggeredLongPress_ = false;
345     PlaybackStatus currentPlayStatus_ = PlaybackStatus::NONE;
346     PlaybackMode autoAndRepeatLevel_ = PlaybackMode::NONE;
347     PlaybackMode historyAutoAndRepeatLevel_ = PlaybackMode::NONE;
348     int64_t currentDateModified_ = -2;
349     MovingPhotoFormat movingPhotoFormat_ = MovingPhotoFormat::UNKNOWN;
350     PixelFormat imageFormat_ = PixelFormat::UNKNOWN;
351     DynamicRangeMode dynamicRangeMode_ = DynamicRangeMode::HIGH;
352 
353     bool isEnableAnalyzer_ = false;
354     bool isContentSizeChanged_ = false;
355     bool isAnalyzerPlaying_ = false;
356     bool isRefreshMovingPhoto_ = false;
357     bool isRefreshMovingPhotoPlaying_ = false;
358     bool isUsedMediaPlayerStatusChanged_ = false;
359 
360     Rect lastBoundsRect_;
361     Rect contentRect_;
362 
363     std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_;
364 
365     ACE_DISALLOW_COPY_AND_MOVE(MovingPhotoPattern);
366 };
367 } // namespace OHOS::Ace::NG
368 
369 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
370