1 /*
2 * Copyright (c) 2022-2023 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 #include "drawing/engine_adapter/skia_adapter/skia_data.h"
19
20 namespace OHOS::Ace {
21 class AnimatedImageObject : public ImageObject {
22 DECLARE_ACE_TYPE(AnimatedImageObject, ImageObject);
23 public:
AnimatedImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const std::shared_ptr<RSData> & data)24 AnimatedImageObject(
25 ImageSourceInfo source,
26 const Size& imageSize,
27 int32_t frameCount,
28 const std::shared_ptr<RSData>& data)
29 : ImageObject(source, imageSize, frameCount), drawingData_(data)
30 {}
31
32 ~AnimatedImageObject() override = default;
33
34 void UploadToGpuForRender(
35 const WeakPtr<PipelineBase>& context,
36 const UploadSuccessCallback& successCallback,
37 const FailedCallback& failedCallback,
38 const Size& imageSize,
39 bool forceResize,
40 bool syncMode = false) override;
41
Pause()42 void Pause() override
43 {
44 if (animatedPlayer_) {
45 LOGI("animatied image Paused");
46 animatedPlayer_->Pause();
47 }
48 }
49
Resume()50 void Resume() override
51 {
52 if (animatedPlayer_) {
53 LOGI("animatied image Resume");
54 animatedPlayer_->Resume();
55 }
56 }
57
ClearData()58 void ClearData() override
59 {
60 drawingData_ = nullptr;
61 }
62
Clone()63 RefPtr<ImageObject> Clone() override
64 {
65 return MakeRefPtr<AnimatedImageObject>(imageSource_, imageSize_, frameCount_, drawingData_);
66 }
67
68 private:
69 std::shared_ptr<RSData> drawingData_;
70 RefPtr<AnimatedImagePlayer> animatedPlayer_;
71 };
72
UploadToGpuForRender(const WeakPtr<PipelineBase> & context,const UploadSuccessCallback & successCallback,const FailedCallback & failedCallback,const Size & imageSize,bool forceResize,bool syncMode)73 void AnimatedImageObject::UploadToGpuForRender(
74 const WeakPtr<PipelineBase>& context,
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_ && drawingData_) {
83 RSDataWrapper* wrapper = new RSDataWrapper{drawingData_};
84 auto skData =
85 SkData::MakeWithProc(drawingData_->GetData(), drawingData_->GetSize(), RSDataWrapperReleaseProc, wrapper);
86 auto codec = SkCodec::MakeFromData(skData);
87 int32_t dstWidth = -1;
88 int32_t dstHeight = -1;
89 if (forceResize) {
90 dstWidth = static_cast<int32_t>(imageSize.Width() + SizeOffset);
91 dstHeight = static_cast<int32_t>(imageSize.Height() + SizeOffset);
92 }
93 animatedPlayer_ = MakeRefPtr<AnimatedImagePlayer>(
94 imageSource_,
95 successCallback,
96 context,
97 std::move(codec),
98 dstWidth,
99 dstHeight);
100 ClearData();
101 } else if (animatedPlayer_ && forceResize && imageSize.IsValid()) {
102 LOGI("animated player has been constructed, forceResize: %{private}s", imageSize.ToString().c_str());
103 int32_t dstWidth = static_cast<int32_t>(imageSize.Width() + SizeOffset);
104 int32_t dstHeight = static_cast<int32_t>(imageSize.Height() + SizeOffset);
105 animatedPlayer_->SetTargetSize(dstWidth, dstHeight);
106 } else if (!animatedPlayer_ && !drawingData_) {
107 LOGE("animated player is not constructed and image data is null, can not construct animated player!");
108 } else if (animatedPlayer_ && !forceResize) {
109 LOGI("animated player has been constructed, do nothing!");
110 }
111 }
112
CreateAnimatedImageObject(ImageSourceInfo source,const Size & imageSize,int32_t frameCount,const std::shared_ptr<RSData> & data)113 RefPtr<ImageObject> CreateAnimatedImageObject(ImageSourceInfo source, const Size& imageSize, int32_t frameCount,
114 const std::shared_ptr<RSData>& data)
115 {
116 return Referenced::MakeRefPtr<AnimatedImageObject>(source, imageSize, frameCount, data);
117 }
118 }