• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "image_object.h"
17 
18 namespace OHOS::Ace {
19 class AnimatedImageObject : public ImageObject {
20     DECLARE_ACE_TYPE(AnimatedImageObject, ImageObject);
21 public:
AnimatedImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const sk_sp<SkData> & data)22     AnimatedImageObject(
23         ImageSourceInfo source,
24         const Size& imageSize,
25         int32_t frameCount,
26         const sk_sp<SkData>& data)
27         : ImageObject(source, imageSize, frameCount), skData_(data)
28     {}
29 
30     ~AnimatedImageObject() override = default;
31 
32     void UploadToGpuForRender(
33         const WeakPtr<PipelineBase>& context,
34         const RefPtr<FlutterRenderTaskHolder>& renderTaskHolder,
35         const UploadSuccessCallback& successCallback,
36         const FailedCallback& failedCallback,
37         const Size& imageSize,
38         bool forceResize,
39         bool syncMode = false) override;
40 
Pause()41     void Pause() override
42     {
43         if (animatedPlayer_) {
44             LOGI("animatied image Paused");
45             animatedPlayer_->Pause();
46         }
47     }
48 
Resume()49     void Resume() override
50     {
51         if (animatedPlayer_) {
52             LOGI("animatied image Resume");
53             animatedPlayer_->Resume();
54         }
55     }
56 
ClearData()57     void ClearData() override
58     {
59         skData_ = nullptr;
60     }
61 
Clone()62     RefPtr<ImageObject> Clone() override
63     {
64         return MakeRefPtr<AnimatedImageObject>(imageSource_, imageSize_, frameCount_, skData_);
65     }
66 
67 private:
68     sk_sp<SkData> skData_;
69     RefPtr<AnimatedImagePlayer> animatedPlayer_;
70 };
71 
UploadToGpuForRender(const WeakPtr<PipelineBase> & context,const RefPtr<FlutterRenderTaskHolder> & renderTaskHolder,const UploadSuccessCallback & successCallback,const FailedCallback & failedCallback,const Size & imageSize,bool forceResize,bool syncMode)72 void AnimatedImageObject::UploadToGpuForRender(
73     const WeakPtr<PipelineBase>& context,
74     const RefPtr<FlutterRenderTaskHolder>& renderTaskHolder,
75     const UploadSuccessCallback& successCallback,
76     const FailedCallback& failedCallback,
77     const Size& imageSize,
78     bool forceResize,
79     bool syncMode)
80 {
81     constexpr float SizeOffset = 0.5f;
82     if (!animatedPlayer_ && skData_) {
83         auto codec = SkCodec::MakeFromData(skData_);
84         int32_t dstWidth = -1;
85         int32_t dstHeight = -1;
86         if (forceResize) {
87             dstWidth = static_cast<int32_t>(imageSize.Width() + SizeOffset);
88             dstHeight = static_cast<int32_t>(imageSize.Height() + SizeOffset);
89         }
90         animatedPlayer_ = MakeRefPtr<AnimatedImagePlayer>(
91             imageSource_,
92             successCallback,
93             context,
94             renderTaskHolder->ioManager,
95             renderTaskHolder->unrefQueue,
96             std::move(codec),
97             dstWidth,
98             dstHeight);
99         ClearData();
100     } else if (animatedPlayer_ && forceResize && imageSize.IsValid()) {
101         LOGI("animated player has been constructed, forceResize: %{public}s", imageSize.ToString().c_str());
102         int32_t dstWidth = static_cast<int32_t>(imageSize.Width() + SizeOffset);
103         int32_t dstHeight = static_cast<int32_t>(imageSize.Height() + SizeOffset);
104         animatedPlayer_->SetTargetSize(dstWidth, dstHeight);
105     } else if (!animatedPlayer_ && !skData_) {
106         LOGE("animated player is not constructed and image data is null, can not construct animated player!");
107     } else if (animatedPlayer_ && !forceResize) {
108         LOGI("animated player has been constructed, do nothing!");
109     }
110 }
111 
CreateAnimatedImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const sk_sp<SkData> & data)112 RefPtr<ImageObject> CreateAnimatedImageObject(ImageSourceInfo source, const Size& imageSize, int32_t frameCount,
113     const sk_sp<SkData>& data)
114 {
115     return Referenced::MakeRefPtr<AnimatedImageObject>(source, imageSize, frameCount, data);
116 }
117 }