123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include "movingphoto_layout_algorithm.h"
2023b3eb3cSopenharmony_ci#include "movingphoto_layout_property.h"
2123b3eb3cSopenharmony_ci#include "movingphoto_event_hub.h"
2223b3eb3cSopenharmony_ci#include "movingphoto_controller.h"
2323b3eb3cSopenharmony_ci#include "movingphoto_utils.h"
2423b3eb3cSopenharmony_ci
2523b3eb3cSopenharmony_ci#include "base/image/pixel_map.h"
2623b3eb3cSopenharmony_ci#include "base/memory/referenced.h"
2723b3eb3cSopenharmony_ci#include "core/common/ai/image_analyzer_manager.h"
2823b3eb3cSopenharmony_ci#include "core/common/container.h"
2923b3eb3cSopenharmony_ci#include "core/components_ng/event/event_hub.h"
3023b3eb3cSopenharmony_ci#include "core/components_ng/event/long_press_event.h"
3123b3eb3cSopenharmony_ci#include "core/components_ng/event/touch_event.h"
3223b3eb3cSopenharmony_ci#include "core/components_ng/pattern/pattern.h"
3323b3eb3cSopenharmony_ci#include "core/components_ng/render/media_player.h"
3423b3eb3cSopenharmony_ci#include "core/components_ng/render/render_surface.h"
3523b3eb3cSopenharmony_ci#include "core/components/video/video_utils.h"
3623b3eb3cSopenharmony_ci#include "core/components/image/image_event.h"
3723b3eb3cSopenharmony_ci#include "interfaces/inner_api/ace/ai/image_analyzer.h"
3823b3eb3cSopenharmony_ci
3923b3eb3cSopenharmony_cinamespace OHOS::Ace::NG {
4023b3eb3cSopenharmony_ciclass MovingPhotoPattern : public Pattern {
4123b3eb3cSopenharmony_ci    DECLARE_ACE_TYPE(MovingPhotoPattern, Pattern);
4223b3eb3cSopenharmony_ci
4323b3eb3cSopenharmony_cipublic:
4423b3eb3cSopenharmony_ci    MovingPhotoPattern() = delete;
4523b3eb3cSopenharmony_ci    explicit MovingPhotoPattern(const RefPtr<MovingPhotoController>& controller);
4623b3eb3cSopenharmony_ci    ~MovingPhotoPattern() override;
4723b3eb3cSopenharmony_ci
4823b3eb3cSopenharmony_ci    RefPtr<EventHub> CreateEventHub() override
4923b3eb3cSopenharmony_ci    {
5023b3eb3cSopenharmony_ci        return MakeRefPtr<MovingPhotoEventHub>();
5123b3eb3cSopenharmony_ci    }
5223b3eb3cSopenharmony_ci
5323b3eb3cSopenharmony_ci    RefPtr<LayoutProperty> CreateLayoutProperty() override
5423b3eb3cSopenharmony_ci    {
5523b3eb3cSopenharmony_ci        return MakeRefPtr<MovingPhotoLayoutProperty>();
5623b3eb3cSopenharmony_ci    }
5723b3eb3cSopenharmony_ci
5823b3eb3cSopenharmony_ci    RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
5923b3eb3cSopenharmony_ci    {
6023b3eb3cSopenharmony_ci        return MakeRefPtr<MovingPhotoLayoutAlgorithm>();
6123b3eb3cSopenharmony_ci    }
6223b3eb3cSopenharmony_ci
6323b3eb3cSopenharmony_ci    void UpdateMuted(bool isMuted)
6423b3eb3cSopenharmony_ci    {
6523b3eb3cSopenharmony_ci        isMuted_ = isMuted;
6623b3eb3cSopenharmony_ci    }
6723b3eb3cSopenharmony_ci
6823b3eb3cSopenharmony_ci    bool GetMuted()
6923b3eb3cSopenharmony_ci    {
7023b3eb3cSopenharmony_ci        return isMuted_;
7123b3eb3cSopenharmony_ci    }
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ci    bool GetPrepared()
7423b3eb3cSopenharmony_ci    {
7523b3eb3cSopenharmony_ci        return isPrepared_;
7623b3eb3cSopenharmony_ci    }
7723b3eb3cSopenharmony_ci
7823b3eb3cSopenharmony_ci    bool GetPlayByController()
7923b3eb3cSopenharmony_ci    {
8023b3eb3cSopenharmony_ci        return isPlayByController_;
8123b3eb3cSopenharmony_ci    }
8223b3eb3cSopenharmony_ci
8323b3eb3cSopenharmony_ci    void OnVisibleChange(bool isVisible) override;
8423b3eb3cSopenharmony_ci
8523b3eb3cSopenharmony_ci    void OnAreaChangedInner() override;
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_ci    void AutoPlayPeriod(int64_t startTime, int64_t endTime);
8823b3eb3cSopenharmony_ci
8923b3eb3cSopenharmony_ci    void AutoPlay(bool isAutoPlay);
9023b3eb3cSopenharmony_ci
9123b3eb3cSopenharmony_ci    void RepeatPlay(bool isRepeatPlay);
9223b3eb3cSopenharmony_ci
9323b3eb3cSopenharmony_ci    FocusPattern GetFocusPattern() const override
9423b3eb3cSopenharmony_ci    {
9523b3eb3cSopenharmony_ci        return { FocusType::NODE, false };
9623b3eb3cSopenharmony_ci    }
9723b3eb3cSopenharmony_ci
9823b3eb3cSopenharmony_ci    void UpdateCurrentDateModified(int64_t currentDateModified)
9923b3eb3cSopenharmony_ci    {
10023b3eb3cSopenharmony_ci        currentDateModified_ = currentDateModified;
10123b3eb3cSopenharmony_ci    }
10223b3eb3cSopenharmony_ci
10323b3eb3cSopenharmony_ci    int64_t GetCurrentDateModified()
10423b3eb3cSopenharmony_ci    {
10523b3eb3cSopenharmony_ci        return currentDateModified_;
10623b3eb3cSopenharmony_ci    }
10723b3eb3cSopenharmony_ci
10823b3eb3cSopenharmony_ci    void EnableAnalyzer(bool enabled);
10923b3eb3cSopenharmony_ci
11023b3eb3cSopenharmony_ci    void SetImageAIOptions(void* options);
11123b3eb3cSopenharmony_ci
11223b3eb3cSopenharmony_ci    bool GetAnalyzerState();
11323b3eb3cSopenharmony_ci
11423b3eb3cSopenharmony_ciprotected:
11523b3eb3cSopenharmony_ci    int32_t instanceId_;
11623b3eb3cSopenharmony_ci
11723b3eb3cSopenharmony_ci    RefPtr<MediaPlayer> mediaPlayer_ = MediaPlayer::Create();
11823b3eb3cSopenharmony_ci    RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create();
11923b3eb3cSopenharmony_ci    RefPtr<RenderContext> renderContextForMediaPlayer_ = RenderContext::Create();
12023b3eb3cSopenharmony_ci
12123b3eb3cSopenharmony_ciprivate:
12223b3eb3cSopenharmony_ci    void OnModifyDone() override;
12323b3eb3cSopenharmony_ci    void OnAttachToFrameNode() override;
12423b3eb3cSopenharmony_ci    void OnDetachFromFrameNode(FrameNode* frameNode) override;
12523b3eb3cSopenharmony_ci    void OnDetachFromMainTree() override;
12623b3eb3cSopenharmony_ci    void OnRebuildFrame() override;
12723b3eb3cSopenharmony_ci    bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
12823b3eb3cSopenharmony_ci    void OnWindowHide() override;
12923b3eb3cSopenharmony_ci    void OnWindowShow() override;
13023b3eb3cSopenharmony_ci
13123b3eb3cSopenharmony_ci    void RegisterVisibleAreaChange();
13223b3eb3cSopenharmony_ci    void VisibleAreaCallback(bool visible);
13323b3eb3cSopenharmony_ci
13423b3eb3cSopenharmony_ci    void InitEvent();
13523b3eb3cSopenharmony_ci    void LongPressEventModify(bool status);
13623b3eb3cSopenharmony_ci    void HandleLongPress(GestureEvent& info);
13723b3eb3cSopenharmony_ci    void HandleTouchEvent(TouchEventInfo& info);
13823b3eb3cSopenharmony_ci
13923b3eb3cSopenharmony_ci    void UpdateImageNode();
14023b3eb3cSopenharmony_ci    void UpdateVideoNode();
14123b3eb3cSopenharmony_ci    void UpdatePlayMode();
14223b3eb3cSopenharmony_ci    void HandleImageAnalyzerMode();
14323b3eb3cSopenharmony_ci    SizeF CalculateFitContain(const SizeF& rawSize, const SizeF& layoutSize);
14423b3eb3cSopenharmony_ci    SizeF CalculateFitFill(const SizeF& layoutSize);
14523b3eb3cSopenharmony_ci    SizeF CalculateFitCover(const SizeF& rawSize, const SizeF& layoutSize);
14623b3eb3cSopenharmony_ci    SizeF CalculateFitNone(const SizeF& rawSize);
14723b3eb3cSopenharmony_ci    SizeF CalculateFitScaleDown(const SizeF& rawSize, const SizeF& layoutSize);
14823b3eb3cSopenharmony_ci    SizeF CalculateFitAuto(const SizeF& rawSize, const SizeF& layoutSize);
14923b3eb3cSopenharmony_ci    SizeF MeasureContentLayout(const SizeF& layoutSize, const RefPtr<MovingPhotoLayoutProperty>& layoutProperty);
15023b3eb3cSopenharmony_ci    SizeF GetRawImageSize();
15123b3eb3cSopenharmony_ci
15223b3eb3cSopenharmony_ci    void PrepareMediaPlayer();
15323b3eb3cSopenharmony_ci    void ResetMediaPlayer();
15423b3eb3cSopenharmony_ci    void PrepareSurface();
15523b3eb3cSopenharmony_ci    void RegisterMediaPlayerEvent();
15623b3eb3cSopenharmony_ci    void PrintMediaPlayerStatus(PlaybackStatus status);
15723b3eb3cSopenharmony_ci    void RegisterImageEvent();
15823b3eb3cSopenharmony_ci    void HandleImageCompleteEvent(const LoadImageSuccessEvent& info);
15923b3eb3cSopenharmony_ci    void MediaResetToPlay();
16023b3eb3cSopenharmony_ci
16123b3eb3cSopenharmony_ci    void FireMediaPlayerImageComplete();
16223b3eb3cSopenharmony_ci    void OnMediaPlayerStatusChanged(PlaybackStatus status);
16323b3eb3cSopenharmony_ci    void OnMediaPlayerInitialized();
16423b3eb3cSopenharmony_ci    void OnMediaPlayerPrepared();
16523b3eb3cSopenharmony_ci    void OnMediaPlayerStoped();
16623b3eb3cSopenharmony_ci    void OnMediaPlayerCompletion();
16723b3eb3cSopenharmony_ci    void OnPlayPositionChanged(uint32_t pos) {};
16823b3eb3cSopenharmony_ci    void FireMediaPlayerStart();
16923b3eb3cSopenharmony_ci    void FireMediaPlayerStop();
17023b3eb3cSopenharmony_ci    void FireMediaPlayerPause();
17123b3eb3cSopenharmony_ci    void FireMediaPlayerFinish();
17223b3eb3cSopenharmony_ci    void FireMediaPlayerError();
17323b3eb3cSopenharmony_ci    void OnResolutionChange();
17423b3eb3cSopenharmony_ci    void OnStartRenderFrame();
17523b3eb3cSopenharmony_ci    void OnStartedStatusCallback();
17623b3eb3cSopenharmony_ci
17723b3eb3cSopenharmony_ci    void Start();
17823b3eb3cSopenharmony_ci    void Pause();
17923b3eb3cSopenharmony_ci    void Stop();
18023b3eb3cSopenharmony_ci    void Seek(int32_t position);
18123b3eb3cSopenharmony_ci
18223b3eb3cSopenharmony_ci    void VisiblePlayback();
18323b3eb3cSopenharmony_ci    void SelectPlaybackMode(PlaybackMode mode);
18423b3eb3cSopenharmony_ci    void StartPlayback();
18523b3eb3cSopenharmony_ci    void StartAnimation();
18623b3eb3cSopenharmony_ci    void StopPlayback();
18723b3eb3cSopenharmony_ci    void PausePlayback();
18823b3eb3cSopenharmony_ci    void StopAnimation();
18923b3eb3cSopenharmony_ci    void StopAnimationCallback();
19023b3eb3cSopenharmony_ci    void StartAutoPlay();
19123b3eb3cSopenharmony_ci    void StartRepeatPlay();
19223b3eb3cSopenharmony_ci    void SetAutoPlayPeriod(int64_t startTime, int64_t endTime);
19323b3eb3cSopenharmony_ci    void HandleImageAnalyzerPlayCallBack();
19423b3eb3cSopenharmony_ci
19523b3eb3cSopenharmony_ci    void UpdateMediaPlayerSpeed();
19623b3eb3cSopenharmony_ci    void UpdateMediaPlayerMuted();
19723b3eb3cSopenharmony_ci
19823b3eb3cSopenharmony_ci    void HideImageNode();
19923b3eb3cSopenharmony_ci
20023b3eb3cSopenharmony_ci    bool IsSupportImageAnalyzer();
20123b3eb3cSopenharmony_ci    bool ShouldUpdateImageAnalyzer();
20223b3eb3cSopenharmony_ci    void StartImageAnalyzer();
20323b3eb3cSopenharmony_ci    void StartUpdateImageAnalyzer();
20423b3eb3cSopenharmony_ci    void CreateAnalyzerOverlay();
20523b3eb3cSopenharmony_ci    void DestroyAnalyzerOverlay();
20623b3eb3cSopenharmony_ci    void UpdateAnalyzerOverlay();
20723b3eb3cSopenharmony_ci    void UpdateAnalyzerUIConfig(const RefPtr<NG::GeometryNode>& geometryNode);
20823b3eb3cSopenharmony_ci    void UpdateOverlayVisibility(VisibleType type);
20923b3eb3cSopenharmony_ci    void GetPixelMap();
21023b3eb3cSopenharmony_ci    int64_t GetUriCoverPosition();
21123b3eb3cSopenharmony_ci    void HandleAnalyzerPlayEvent(bool canPlay);
21223b3eb3cSopenharmony_ci
21323b3eb3cSopenharmony_ci    RefPtr<LongPressEvent> longPressEvent_;
21423b3eb3cSopenharmony_ci    RefPtr<TouchEventImpl> touchEvent_;
21523b3eb3cSopenharmony_ci    RefPtr<MovingPhotoController> controller_;
21623b3eb3cSopenharmony_ci    RefPtr<PixelMap> pixelMap_;
21723b3eb3cSopenharmony_ci
21823b3eb3cSopenharmony_ci    int32_t fd_ = -1;
21923b3eb3cSopenharmony_ci    int64_t autoPlayPeriodStartTime_ = -1;
22023b3eb3cSopenharmony_ci    int64_t autoPlayPeriodEndTime_ = -1;
22123b3eb3cSopenharmony_ci    std::string uri_ = "";
22223b3eb3cSopenharmony_ci    bool startAnimationFlag_ = false;
22323b3eb3cSopenharmony_ci    bool isPrepared_ = false;
22423b3eb3cSopenharmony_ci    bool isMuted_ = false;
22523b3eb3cSopenharmony_ci    bool isPlayByController_ = false;
22623b3eb3cSopenharmony_ci    bool isFastKeyUp_ = false;
22723b3eb3cSopenharmony_ci    bool hasVisibleChangeRegistered_ = false;
22823b3eb3cSopenharmony_ci    bool isFirstRepeatPlay_ = true;
22923b3eb3cSopenharmony_ci    bool isSetAutoPlayPeriod_ = false;
23023b3eb3cSopenharmony_ci    bool isVisible_ = false;
23123b3eb3cSopenharmony_ci    bool isChangePlayMode_ = false;
23223b3eb3cSopenharmony_ci    bool needUpdateImageNode_ = false;
23323b3eb3cSopenharmony_ci    PlaybackStatus currentPlayStatus_ = PlaybackStatus::NONE;
23423b3eb3cSopenharmony_ci    PlaybackMode autoAndRepeatLevel_ = PlaybackMode::NONE;
23523b3eb3cSopenharmony_ci    PlaybackMode historyAutoAndRepeatLevel_ = PlaybackMode::NONE;
23623b3eb3cSopenharmony_ci    int64_t currentDateModified_ = -2;
23723b3eb3cSopenharmony_ci
23823b3eb3cSopenharmony_ci    bool isEnableAnalyzer_ = false;
23923b3eb3cSopenharmony_ci    bool isContentSizeChanged_ = false;
24023b3eb3cSopenharmony_ci    bool isAnalyzerCreated_ = false;
24123b3eb3cSopenharmony_ci    bool isPixelMapChanged_ = false;
24223b3eb3cSopenharmony_ci    bool isAnalyzerPlaying_ = false;
24323b3eb3cSopenharmony_ci
24423b3eb3cSopenharmony_ci    Rect lastBoundsRect_;
24523b3eb3cSopenharmony_ci    Rect contentRect_;
24623b3eb3cSopenharmony_ci
24723b3eb3cSopenharmony_ci    std::shared_ptr<ImageAnalyzerManager> imageAnalyzerManager_;
24823b3eb3cSopenharmony_ci
24923b3eb3cSopenharmony_ci    ACE_DISALLOW_COPY_AND_MOVE(MovingPhotoPattern);
25023b3eb3cSopenharmony_ci};
25123b3eb3cSopenharmony_ci} // namespace OHOS::Ace::NG
25223b3eb3cSopenharmony_ci
25323b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MOVING_PHOTO_MOVING_PHOTO_PATTERN_H
254