1 /*
2  * Copyright (c) 2022 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_IMAGE_APNG_APNG_IMAGE_ANIMATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_APNG_APNG_IMAGE_ANIMATION_H
18 
19 #include "core/animation/picture_animation.h"
20 
21 namespace OHOS::Ace {
22 
23 class APngImageAnimation : public Animation<int32_t> {
24     DECLARE_ACE_TYPE(APngImageAnimation, Animation);
25 public:
26     APngImageAnimation() = default;
27 
28     ~APngImageAnimation() override = default;
29 
30     // add picture. duration in normalized time.
31     bool AddPicture(float duration, const int32_t &pictureInfo);
32 
ClearPictures()33     void ClearPictures()
34     {
35         this->duration_ = 0.0f;
36         pictures_.clear();
37     }
38 
GetValue()39     const int32_t &GetValue() const override
40     {
41         return currentPicture_->GetPictureInfo();
42     }
43 
44     // if total normalized duration of all pictures not equals 1.0, scale it to 1.0
45     // usually call it after all pictures had been added.
AutoScale()46     void AutoScale()
47     {
48         if (NearZero(this->duration_)) {
49             return;
50         }
51         if (pictures_.empty()) {
52             return;
53         }
54 
55         // already equals to 1.0
56         if (NearEqual(this->duration_, NORMALIZED_DURATION_MAX)) {
57             return;
58         }
59 
60         for (const auto &picture : pictures_) {
61             picture->UpdateDurationWithScale(this->duration_);
62         }
63 
64         this->duration_ = NORMALIZED_DURATION_MAX;
65     }
66 
67     void UpdateAndNotifyPicture(const RefPtr<PictureFrame<int32_t>> &picture);
68 
69     /**
70      * Use timestamp in order get the true sequence
71      * @param normalized
72      * @param revserse
73      */
74     void OnNormalizedTimestampChanged(float normalized, bool revserse) override;
75 
76 private:
77     RefPtr<PictureFrame<int32_t>> currentPicture_ = AceType::MakeRefPtr<PictureFrame<int32_t>>();
78     std::vector<RefPtr<PictureFrame<int32_t>>> pictures_;
79     int32_t currentIndex_ = -1;
80     double totalTime_ = 0;
81     double lastFrameTime_ = 0;
82 };
83 
84 } // namespace OHOS::Ace
85 
86 
87 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_APNG_APNG_IMAGE_ANIMATION_H
88