1 /*
2 * Copyright (c) 2024 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 <string>
17
18 #include "bridge/cj_frontend/cppview/render_image.h"
19 #include "core/image/image_source_info.h"
20 #include "core/pipeline/pipeline_base.h"
21
22 using namespace OHOS;
23
24 namespace OHOS::Ace::Framework {
CJRenderImage()25 CJRenderImage::CJRenderImage() : FFIData() {}
CJRenderImage(const int32_t unit)26 CJRenderImage::CJRenderImage(const int32_t unit) : FFIData()
27 {
28 SetUnit(static_cast<CanvasUnit>(unit));
29 }
30
InitCJRenderImage(const std::string & src)31 void CJRenderImage::InitCJRenderImage(const std::string& src)
32 {
33 this->LoadImage(src);
34 }
35
InitCJRenderImage(const RefPtr<OHOS::Ace::PixelMap> & pixmap)36 void CJRenderImage::InitCJRenderImage(const RefPtr<OHOS::Ace::PixelMap>& pixmap)
37 {
38 this->LoadImage(pixmap);
39 }
40
LoadImage(const ImageSourceInfo & sourceInfo)41 void CJRenderImage::LoadImage(const ImageSourceInfo& sourceInfo)
42 {
43 auto dataReadyCallback = [this](const ImageSourceInfo& sourceInfo) { this->OnImageDataReady(); };
44 auto loadSuccessCallback = [this](const ImageSourceInfo& sourceInfo) { this->OnImageLoadSuccess(); };
45 auto loadFailCallback = [this](const ImageSourceInfo& sourceInfo, const std::string& errorMsg) {
46 this->OnImageLoadFail(errorMsg);
47 };
48 NG::LoadNotifier loadNotifier(dataReadyCallback, loadSuccessCallback, loadFailCallback);
49 loadingCtx_ = AceType::MakeRefPtr<NG::ImageLoadingContext>(sourceInfo, std::move(loadNotifier), true);
50 loadingCtx_->LoadImageData();
51 }
52
LoadImage(const std::string & src)53 void CJRenderImage::LoadImage(const std::string& src)
54 {
55 src_ = src;
56 auto sourceInfo = ImageSourceInfo(src);
57 sourceInfo_ = sourceInfo;
58 LoadImage(sourceInfo);
59 }
60
LoadImage(const RefPtr<OHOS::Ace::PixelMap> & pixmap)61 void CJRenderImage::LoadImage(const RefPtr<OHOS::Ace::PixelMap>& pixmap)
62 {
63 auto sourceInfo = ImageSourceInfo(pixmap);
64 sourceInfo_ = sourceInfo;
65 LoadImage(sourceInfo);
66 }
67
OnImageDataReady()68 void CJRenderImage::OnImageDataReady()
69 {
70 CHECK_NULL_VOID(loadingCtx_);
71 width_ = loadingCtx_->GetImageSize().Width();
72 height_ = loadingCtx_->GetImageSize().Height();
73 loadingCtx_->MakeCanvasImageIfNeed(loadingCtx_->GetImageSize(), true, ImageFit::NONE);
74 }
75
OnImageLoadSuccess()76 void CJRenderImage::OnImageLoadSuccess()
77 {
78 CHECK_NULL_VOID(loadingCtx_);
79 image_ = loadingCtx_->MoveCanvasImage();
80 CHECK_NULL_VOID(image_);
81 pixelMap_ = image_->GetPixelMap();
82 imageFit_ = loadingCtx_->GetImageFit();
83 }
84
OnImageLoadFail(const std::string & errorMsg)85 void CJRenderImage::OnImageLoadFail(const std::string& errorMsg)
86 {
87 width_ = 0;
88 height_ = 0;
89 pixelMap_ = nullptr;
90 }
91
GetHeight()92 double CJRenderImage::GetHeight()
93 {
94 double density = !NearZero(GetDensity()) ? GetDensity() : 1.0;
95 return height_ / density;
96 }
97
GetWidth()98 double CJRenderImage::GetWidth()
99 {
100 double density = !NearZero(GetDensity()) ? GetDensity() : 1.0;
101 return width_ / density;
102 }
103
GetDensity()104 double CJRenderImage::GetDensity()
105 {
106 double density = PipelineBase::GetCurrentDensity();
107 return ((GetUnit() == CanvasUnit::DEFAULT) && !NearZero(density)) ? density : 1.0;
108 }
109 } // namespace OHOS::Ace::Framework