• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/components_ng/image_provider/image_loading_context.h"
17 
18 #include "base/network/download_manager.h"
19 #include "base/thread/background_task_executor.h"
20 #include "base/utils/utils.h"
21 #include "core/common/container.h"
22 #include "core/components_ng/image_provider/image_state_manager.h"
23 #include "core/components_ng/image_provider/image_utils.h"
24 #include "core/components_ng/image_provider/pixel_map_image_object.h"
25 #include "core/components_ng/image_provider/static_image_object.h"
26 #include "core/components_ng/render/image_painter.h"
27 #include "core/image/image_file_cache.h"
28 #include "core/image/image_loader.h"
29 #include "core/pipeline_ng/pipeline_context.h"
30 #ifdef USE_ROSEN_DRAWING
31 #include "core/components_ng/image_provider/adapter/rosen/drawing_image_data.h"
32 #endif
33 
34 namespace OHOS::Ace::NG {
35 
36 namespace {
QueryDataFromCache(const ImageSourceInfo & src,bool & dataHit)37 RefPtr<ImageData> QueryDataFromCache(const ImageSourceInfo& src, bool& dataHit)
38 {
39     ACE_FUNCTION_TRACE();
40 #ifndef USE_ROSEN_DRAWING
41     auto cachedData = ImageLoader::QueryImageDataFromImageCache(src);
42     if (cachedData) {
43         dataHit = true;
44         return NG::ImageData::MakeFromDataWrapper(&cachedData);
45     }
46     auto skData = ImageLoader::LoadDataFromCachedFile(src.GetSrc());
47     if (skData) {
48         sk_sp<SkData> data = SkData::MakeWithCopy(skData->data(), skData->size());
49 
50         return NG::ImageData::MakeFromDataWrapper(&data);
51     }
52 #else
53     std::shared_ptr<RSData> rsData = nullptr;
54     rsData = ImageLoader::QueryImageDataFromImageCache(src);
55     if (rsData) {
56         dataHit = true;
57         return AceType::MakeRefPtr<NG::DrawingImageData>(rsData);
58     }
59     auto drawingData = ImageLoader::LoadDataFromCachedFile(src.GetSrc());
60     if (drawingData) {
61         auto data = std::make_shared<RSData>();
62         data->BuildWithCopy(drawingData->GetData(), drawingData->GetSize());
63         return AceType::MakeRefPtr<NG::DrawingImageData>(data);
64     }
65 #endif
66     return nullptr;
67 }
68 } // namespace
69 
ImageLoadingContext(const ImageSourceInfo & src,LoadNotifier && loadNotifier,bool syncLoad)70 ImageLoadingContext::ImageLoadingContext(const ImageSourceInfo& src, LoadNotifier&& loadNotifier, bool syncLoad)
71     : src_(src), notifiers_(std::move(loadNotifier)), syncLoad_(syncLoad)
72 {
73     stateManager_ = MakeRefPtr<ImageStateManager>(WeakClaim(this));
74     // pixmap src is ready to draw
75     if (src_.GetSrcType() == SrcType::PIXMAP) {
76         syncLoad_ = true;
77     }
78 }
79 
~ImageLoadingContext()80 ImageLoadingContext::~ImageLoadingContext()
81 {
82     // cancel background task
83     if (!syncLoad_) {
84         auto state = stateManager_->GetCurrentState();
85         if (state == ImageLoadingState::DATA_LOADING) {
86             // cancel CreateImgObj task
87             ImageProvider::CancelTask(src_.GetKey(), WeakClaim(this));
88         } else if (state == ImageLoadingState::MAKE_CANVAS_IMAGE) {
89             // cancel MakeCanvasImage task
90             if (InstanceOf<StaticImageObject>(imageObj_)) {
91                 ImageProvider::CancelTask(canvasKey_, WeakClaim(this));
92             }
93         }
94     }
95 }
96 
CalculateTargetSize(const SizeF & srcSize,const SizeF & dstSize,const SizeF & rawImageSize)97 SizeF ImageLoadingContext::CalculateTargetSize(const SizeF& srcSize, const SizeF& dstSize, const SizeF& rawImageSize)
98 {
99     if (!srcSize.IsPositive()) {
100         return rawImageSize;
101     }
102 
103     SizeF targetSize = rawImageSize;
104     auto context = PipelineContext::GetCurrentContext();
105     auto viewScale = context ? context->GetViewScale() : 1.0;
106     double widthScale = dstSize.Width() / srcSize.Width() * viewScale;
107     double heightScale = dstSize.Height() / srcSize.Height() * viewScale;
108     if (widthScale < 1.0 && heightScale < 1.0) {
109         targetSize = SizeF(targetSize.Width() * widthScale, targetSize.Height() * heightScale);
110     }
111     return targetSize;
112 }
113 
OnUnloaded()114 void ImageLoadingContext::OnUnloaded()
115 {
116     imageObj_ = nullptr;
117     canvasImage_ = nullptr;
118     srcRect_ = RectF();
119     dstRect_ = RectF();
120     dstSize_ = SizeF();
121 }
122 
OnLoadSuccess()123 void ImageLoadingContext::OnLoadSuccess()
124 {
125     if (DynamicCast<StaticImageObject>(imageObj_)) {
126         imageObj_->ClearData();
127     }
128     if (notifiers_.onLoadSuccess_) {
129         notifiers_.onLoadSuccess_(src_);
130     }
131     ImageUtils::PostToUI(std::move(pendingMakeCanvasImageTask_));
132 }
133 
OnLoadFail()134 void ImageLoadingContext::OnLoadFail()
135 {
136     if (notifiers_.onLoadFail_) {
137         notifiers_.onLoadFail_(src_, errorMsg_);
138     }
139 }
140 
OnDataReady()141 void ImageLoadingContext::OnDataReady()
142 {
143     if (notifiers_.onDataReady_) {
144         notifiers_.onDataReady_(src_);
145     }
146 }
147 
OnDataLoading()148 void ImageLoadingContext::OnDataLoading()
149 {
150     if (!src_.GetIsConfigurationChange()) {
151         if (auto obj = ImageProvider::QueryImageObjectFromCache(src_); obj) {
152             DataReadyCallback(obj);
153             return;
154         }
155     }
156     if (Downloadable()) {
157         if (syncLoad_) {
158             DownloadImage();
159         } else {
160             auto task = [weak = AceType::WeakClaim(this)]() {
161                 auto ctx = weak.Upgrade();
162                 CHECK_NULL_VOID(ctx);
163                 ctx->DownloadImage();
164             };
165             NG::ImageUtils::PostToBg(task);
166         }
167         return;
168     }
169     ImageProvider::CreateImageObject(src_, WeakClaim(this), syncLoad_);
170     src_.SetIsConfigurationChange(false);
171 }
172 
NotifyReadyIfCacheHit()173 bool ImageLoadingContext::NotifyReadyIfCacheHit()
174 {
175     bool dataHit = false;
176     auto cachedImageData = QueryDataFromCache(src_, dataHit);
177     CHECK_NULL_RETURN(cachedImageData, false);
178     auto notifyDataReadyTask = [weak = AceType::WeakClaim(this), data = std::move(cachedImageData), dataHit] {
179         auto ctx = weak.Upgrade();
180         CHECK_NULL_VOID(ctx);
181         auto src = ctx->GetSourceInfo();
182         // if find data or file cache only, build and cache object, cache data if file cache hit
183         RefPtr<ImageObject> imageObj = ImageProvider::BuildImageObject(src, data);
184         ImageProvider::CacheImageObject(imageObj);
185         if (!dataHit) {
186             ImageLoader::CacheImageData(ctx->GetSourceInfo().GetKey(), data);
187         }
188         ctx->DataReadyCallback(imageObj);
189     };
190     if (syncLoad_) {
191         notifyDataReadyTask();
192     } else {
193         ImageUtils::PostToUI(std::move(notifyDataReadyTask));
194     }
195     return true;
196 }
197 
Downloadable()198 bool ImageLoadingContext::Downloadable()
199 {
200     return src_.GetSrcType() == SrcType::NETWORK && SystemProperties::GetDownloadByNetworkEnabled();
201 }
202 
DownloadImage()203 void ImageLoadingContext::DownloadImage()
204 {
205     if (NotifyReadyIfCacheHit()) {
206         return;
207     }
208     PerformDownload();
209 }
210 
PerformDownload()211 void ImageLoadingContext::PerformDownload()
212 {
213     DownloadCallback downloadCallback;
214     downloadCallback.successCallback = [weak = AceType::WeakClaim(this)](
215                                            const std::string&& imageData, bool async, int32_t instanceId) {
216         ContainerScope scope(instanceId);
217         auto callback = [weak = weak, data = std::move(imageData)]() {
218             auto ctx = weak.Upgrade();
219             CHECK_NULL_VOID(ctx);
220             ctx->DownloadImageSuccess(data);
221         };
222         async ? NG::ImageUtils::PostToUI(callback) : callback();
223     };
224     downloadCallback.failCallback = [weak = AceType::WeakClaim(this)](
225                                         std::string errorMessage, bool async, int32_t instanceId) {
226         ContainerScope scope(instanceId);
227         auto callback = [weak = weak, errorMessage = errorMessage]() {
228             auto ctx = weak.Upgrade();
229             CHECK_NULL_VOID(ctx);
230             ctx->DownloadImageFailed(errorMessage);
231         };
232         async ? NG::ImageUtils::PostToUI(callback) : callback();
233     };
234     downloadCallback.cancelCallback = downloadCallback.failCallback;
235     NetworkImageLoader::DownloadImage(std::move(downloadCallback), src_.GetSrc(), syncLoad_);
236 }
237 
DownloadImageSuccess(const std::string & imageData)238 void ImageLoadingContext::DownloadImageSuccess(const std::string& imageData)
239 {
240     TAG_LOGI(AceLogTag::ACE_IMAGE, "Download image successfully, ImageData length=%{public}zu", imageData.size());
241     auto data = ImageData::MakeFromDataWithCopy(imageData.data(), imageData.size());
242     if (!Positive(imageData.size())) {
243         FailCallback("The length of imageData from netStack is not positive");
244         return;
245     }
246     // if downloading is necessary, cache object, data to file
247     RefPtr<ImageObject> imageObj = ImageProvider::BuildImageObject(GetSourceInfo(), data);
248     if (!imageObj) {
249         FailCallback("ImageObject null");
250         return;
251     }
252     ImageProvider::CacheImageObject(imageObj);
253     ImageLoader::CacheImageData(GetSourceInfo().GetKey(), data);
254     ImageLoader::WriteCacheToFile(GetSourceInfo().GetSrc(), imageData);
255     DataReadyCallback(imageObj);
256 }
257 
DownloadImageFailed(const std::string & errorMessage)258 void ImageLoadingContext::DownloadImageFailed(const std::string& errorMessage)
259 {
260     TAG_LOGI(AceLogTag::ACE_IMAGE, "Download image failed, the error message is %{public}s", errorMessage.c_str());
261     FailCallback(errorMessage);
262 }
263 
OnMakeCanvasImage()264 void ImageLoadingContext::OnMakeCanvasImage()
265 {
266     CHECK_NULL_VOID(imageObj_);
267 
268     // only update params when entered MakeCanvasImage state successfully
269     if (updateParamsCallback_) {
270         updateParamsCallback_();
271         updateParamsCallback_ = nullptr;
272     }
273     auto userDefinedSize = GetSourceSize();
274     SizeF targetSize;
275     if (userDefinedSize) {
276         ImagePainter::ApplyImageFit(imageFit_, *userDefinedSize, dstSize_, srcRect_, dstRect_);
277         targetSize = *userDefinedSize;
278     } else {
279         auto imageSize = GetImageSize();
280         // calculate the srcRect based on original image size
281         ImagePainter::ApplyImageFit(imageFit_, imageSize, dstSize_, srcRect_, dstRect_);
282 
283         bool isPixelMapResource = (SrcType::DATA_ABILITY_DECODED == GetSourceInfo().GetSrcType());
284         if (autoResize_ && !isPixelMapResource) {
285             targetSize = CalculateTargetSize(srcRect_.GetSize(), dstRect_.GetSize(), imageSize);
286             // calculate real srcRect used for paint based on resized image size
287             ImagePainter::ApplyImageFit(imageFit_, targetSize, dstSize_, srcRect_, dstRect_);
288         }
289 
290         // upscale targetSize if size level is mapped
291         if (targetSize.IsPositive() && sizeLevel_ > targetSize.Width()) {
292             targetSize.ApplyScale(sizeLevel_ / targetSize.Width());
293         }
294     }
295 
296     // step4: [MakeCanvasImage] according to [targetSize]
297     canvasKey_ = ImageUtils::GenerateImageKey(src_, targetSize);
298     imageObj_->MakeCanvasImage(Claim(this), targetSize, userDefinedSize.has_value(), syncLoad_);
299 }
300 
ResizableCalcDstSize()301 void ImageLoadingContext::ResizableCalcDstSize()
302 {
303     auto userDefinedSize = GetSourceSize();
304     if (userDefinedSize) {
305         ImagePainter::ApplyImageFit(imageFit_, *userDefinedSize, dstSize_, srcRect_, dstRect_);
306         return;
307     }
308     auto imageSize = GetImageSize();
309     // calculate the srcRect based on original image size
310     ImagePainter::ApplyImageFit(imageFit_, imageSize, dstSize_, srcRect_, dstRect_);
311 
312     bool isPixelMapResource = (SrcType::DATA_ABILITY_DECODED == GetSourceInfo().GetSrcType());
313     if (autoResize_ && !isPixelMapResource) {
314         SizeF targetSize = CalculateTargetSize(srcRect_.GetSize(), dstRect_.GetSize(), imageSize);
315         // calculate real srcRect used for paint based on resized image size
316         ImagePainter::ApplyImageFit(imageFit_, targetSize, dstSize_, srcRect_, dstRect_);
317     }
318 }
319 
DataReadyCallback(const RefPtr<ImageObject> & imageObj)320 void ImageLoadingContext::DataReadyCallback(const RefPtr<ImageObject>& imageObj)
321 {
322     CHECK_NULL_VOID(imageObj);
323     imageObj_ = imageObj->Clone();
324     stateManager_->HandleCommand(ImageLoadingCommand::LOAD_DATA_SUCCESS);
325 }
326 
SuccessCallback(const RefPtr<CanvasImage> & canvasImage)327 void ImageLoadingContext::SuccessCallback(const RefPtr<CanvasImage>& canvasImage)
328 {
329     canvasImage_ = canvasImage;
330     stateManager_->HandleCommand(ImageLoadingCommand::MAKE_CANVAS_IMAGE_SUCCESS);
331 }
332 
FailCallback(const std::string & errorMsg)333 void ImageLoadingContext::FailCallback(const std::string& errorMsg)
334 {
335     errorMsg_ = errorMsg;
336     TAG_LOGW(AceLogTag::ACE_IMAGE, "Image LoadFail, source = %{public}s, reason: %{public}s", src_.ToString().c_str(),
337         errorMsg.c_str());
338     stateManager_->HandleCommand(ImageLoadingCommand::LOAD_FAIL);
339 }
340 
GetDstRect() const341 const RectF& ImageLoadingContext::GetDstRect() const
342 {
343     return dstRect_;
344 }
345 
GetSrcRect() const346 const RectF& ImageLoadingContext::GetSrcRect() const
347 {
348     return srcRect_;
349 }
350 
MoveCanvasImage()351 RefPtr<CanvasImage> ImageLoadingContext::MoveCanvasImage()
352 {
353     return std::move(canvasImage_);
354 }
355 
MoveImageObject()356 RefPtr<ImageObject> ImageLoadingContext::MoveImageObject()
357 {
358     return std::move(imageObj_);
359 }
360 
LoadImageData()361 void ImageLoadingContext::LoadImageData()
362 {
363     stateManager_->HandleCommand(ImageLoadingCommand::LOAD_DATA);
364 }
365 
RoundUp(int32_t value)366 int32_t ImageLoadingContext::RoundUp(int32_t value)
367 {
368     CHECK_NULL_RETURN(imageObj_, -1);
369     auto res = imageObj_->GetImageSize().Width();
370     CHECK_NULL_RETURN(value > 0 && res > 0, -1);
371     while (res / 2 >= value) {
372         res /= 2;
373     }
374     return res;
375 }
376 
MakeCanvasImageIfNeed(const SizeF & dstSize,bool autoResize,ImageFit imageFit,const std::optional<SizeF> & sourceSize,bool hasValidSlice)377 bool ImageLoadingContext::MakeCanvasImageIfNeed(const SizeF& dstSize, bool autoResize, ImageFit imageFit,
378     const std::optional<SizeF>& sourceSize, bool hasValidSlice)
379 {
380     bool res = autoResize != autoResize_ || imageFit != imageFit_ || sourceSize != GetSourceSize();
381 
382     /* When function is called with a changed dstSize, assume the image will be resized frequently. To minimize
383      * MakeCanvasImage operations, map dstSize to size levels in log_2. Only Remake when the size level changes.
384      */
385     if (SizeChanging(dstSize)) {
386         res |= RoundUp(dstSize.Width()) != sizeLevel_;
387     } else if (dstSize_ == SizeF()) {
388         res |= dstSize.IsPositive();
389     }
390     if (!res && hasValidSlice) {
391         dstSize_ = dstSize;
392     }
393     CHECK_NULL_RETURN(res, res);
394     if (stateManager_->GetCurrentState() == ImageLoadingState::MAKE_CANVAS_IMAGE) {
395         pendingMakeCanvasImageTask_ = [weak = AceType::WeakClaim(this), dstSize, autoResize, imageFit, sourceSize]() {
396             auto ctx = weak.Upgrade();
397             CHECK_NULL_VOID(ctx);
398             CHECK_NULL_VOID(ctx->SizeChanging(dstSize));
399             ctx->MakeCanvasImage(dstSize, autoResize, imageFit, sourceSize);
400         };
401     } else {
402         MakeCanvasImage(dstSize, autoResize, imageFit, sourceSize);
403     }
404     return res;
405 }
406 
MakeCanvasImage(const SizeF & dstSize,bool autoResize,ImageFit imageFit,const std::optional<SizeF> & sourceSize)407 void ImageLoadingContext::MakeCanvasImage(
408     const SizeF& dstSize, bool autoResize, ImageFit imageFit, const std::optional<SizeF>& sourceSize)
409 {
410     // Because calling of this interface does not guarantee the execution of [MakeCanvasImage], so in order to avoid
411     // updating params before they are not actually used, capture the params in a function. This function will only run
412     // when it actually do [MakeCanvasImage], i.e. doing the update in [OnMakeCanvasImageTask]
413     updateParamsCallback_ = [wp = WeakClaim(this), dstSize, autoResize, imageFit, sourceSize]() {
414         auto ctx = wp.Upgrade();
415         CHECK_NULL_VOID(ctx);
416         if (ctx->SizeChanging(dstSize)) {
417             ctx->sizeLevel_ = ctx->RoundUp(dstSize.Width());
418         }
419         ctx->dstSize_ = dstSize;
420         ctx->imageFit_ = imageFit;
421         ctx->autoResize_ = autoResize;
422         ctx->SetSourceSize(sourceSize);
423     };
424     // send command to [StateManager] and waiting the callback from it to determine next step
425     stateManager_->HandleCommand(ImageLoadingCommand::MAKE_CANVAS_IMAGE);
426 }
427 
GetImageSize() const428 SizeF ImageLoadingContext::GetImageSize() const
429 {
430     return imageObj_ ? imageObj_->GetImageSize() : SizeF(-1.0, -1.0);
431 }
432 
GetImageFit() const433 ImageFit ImageLoadingContext::GetImageFit() const
434 {
435     return imageFit_;
436 }
437 
SetImageFit(ImageFit imageFit)438 void ImageLoadingContext::SetImageFit(ImageFit imageFit)
439 {
440     imageFit_ = imageFit;
441 }
442 
GetSourceInfo() const443 const ImageSourceInfo& ImageLoadingContext::GetSourceInfo() const
444 {
445     return src_;
446 }
447 
SetAutoResize(bool autoResize)448 void ImageLoadingContext::SetAutoResize(bool autoResize)
449 {
450     autoResize_ = autoResize;
451 }
452 
GetDstSize() const453 const SizeF& ImageLoadingContext::GetDstSize() const
454 {
455     return dstSize_;
456 }
457 
GetAutoResize() const458 bool ImageLoadingContext::GetAutoResize() const
459 {
460     return autoResize_;
461 }
462 
SetSourceSize(const std::optional<SizeF> & sourceSize)463 void ImageLoadingContext::SetSourceSize(const std::optional<SizeF>& sourceSize)
464 {
465     if (sourceSize.has_value()) {
466         sourceSizePtr_ = std::make_unique<SizeF>(sourceSize.value());
467     }
468 }
469 
GetSourceSize() const470 std::optional<SizeF> ImageLoadingContext::GetSourceSize() const
471 {
472     CHECK_NULL_RETURN(sourceSizePtr_, std::nullopt);
473     if (sourceSizePtr_->Width() <= 0.0 || sourceSizePtr_->Height() <= 0.0) {
474         TAG_LOGW(AceLogTag::ACE_IMAGE,
475             "Property SourceSize is at least One invalid! Use the Image Size to calculate resize target");
476         return std::nullopt;
477     }
478     return { *sourceSizePtr_ };
479 }
480 
NeedAlt() const481 bool ImageLoadingContext::NeedAlt() const
482 {
483     auto state = stateManager_->GetCurrentState();
484     return state != ImageLoadingState::LOAD_SUCCESS;
485 }
486 
ResetLoading()487 void ImageLoadingContext::ResetLoading()
488 {
489     stateManager_->HandleCommand(ImageLoadingCommand::RESET_STATE);
490 }
491 
ResumeLoading()492 void ImageLoadingContext::ResumeLoading()
493 {
494     stateManager_->HandleCommand(ImageLoadingCommand::LOAD_DATA);
495 }
496 
GetCurrentLoadingState()497 const std::string ImageLoadingContext::GetCurrentLoadingState()
498 {
499     ImageLoadingState state = ImageLoadingState::UNLOADED;
500     if (stateManager_) {
501         state = stateManager_->GetCurrentState();
502     }
503     switch (state) {
504         case ImageLoadingState::DATA_LOADING:
505             return "DATA_LOADING";
506         case ImageLoadingState::DATA_READY:
507             return "DATA_READY";
508         case ImageLoadingState::MAKE_CANVAS_IMAGE:
509             return "MAKE_CANVAS_IMAGE";
510         case ImageLoadingState::LOAD_SUCCESS:
511             return "LOAD_SUCCESS";
512         case ImageLoadingState::LOAD_FAIL:
513             return "LOAD_FAIL";
514 
515         default:
516             return "UNLOADED";
517     }
518 }
519 
GetFrameCount() const520 int32_t ImageLoadingContext::GetFrameCount() const
521 {
522     return imageObj_ ? imageObj_->GetFrameCount() : 0;
523 }
524 
525 } // namespace OHOS::Ace::NG
526