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