1 /*
2 * Copyright (C) 2025 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/pixelmap_drawable_descriptor.h"
17
18 #include <cstdint>
19
20 #include "render_service_base/include/pipeline/rs_recording_canvas.h"
21
22 #include "loader/image_loader.h"
23 #include "utils/task_pool.h"
24
25 namespace OHOS {
26 namespace Ace {
27 namespace Drawable {
28 namespace {
29 constexpr int32_t BORDER_RADIUS_ARRAY_SIZE = 4;
30 };
31
FetchSync()32 DrawableInfo PixelmapDrawableDescriptor::FetchSync()
33 {
34 if (pixelMap_) {
35 return { pixelMap_->GetWidth(), pixelMap_->GetHeight() };
36 }
37 LoadImageData();
38 Decode();
39 if (pixelMap_) {
40 return { pixelMap_->GetWidth(), pixelMap_->GetHeight() };
41 }
42 return { 0, 0 };
43 }
44
Fetch()45 void PixelmapDrawableDescriptor::Fetch()
46 {
47 if (pixelMap_) {
48 return;
49 }
50 // post load and decode task to pool
51 TaskPool::GetInstance()->PostTask(
52 [this] {
53 LoadImageData();
54 Decode();
55 Validate();
56 },
57 "PixelMapDrawableDescriptorFetch");
58 }
59
LoadImageData()60 void PixelmapDrawableDescriptor::LoadImageData()
61 {
62 auto loader = ImageLoader::CreateImageLoader(src_.GetSrcType());
63 data_ = loader->LoadImageData(src_);
64 }
65
Decode()66 void PixelmapDrawableDescriptor::Decode()
67 {
68 pixelMap_ = data_->CreatePixelMap({ 0, 0 });
69 }
70
Validate()71 void PixelmapDrawableDescriptor::Validate()
72 {
73 // flush component register redraw callback
74 for (const auto& callback : redrawCallbacks_) {
75 if (callback) {
76 callback();
77 }
78 }
79 }
80
GetOriginalWidth() const81 int32_t PixelmapDrawableDescriptor::GetOriginalWidth() const
82 {
83 return 0;
84 }
85
GetOriginalHeight() const86 int32_t PixelmapDrawableDescriptor::GetOriginalHeight() const
87 {
88 return 0;
89 }
90
GetPixelMap()91 std::shared_ptr<Media::PixelMap> PixelmapDrawableDescriptor::GetPixelMap()
92 {
93 if (pixelMap_) {
94 return pixelMap_->GetPixelMapSharedPtr();
95 }
96 return nullptr;
97 }
98
SetPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap)99 void PixelmapDrawableDescriptor::SetPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap)
100 {
101 pixelMap_ = PixelMap::Create(pixelMap);
102 }
103
GetDrawableType() const104 DrawableType PixelmapDrawableDescriptor::GetDrawableType() const
105 {
106 return DrawableType::PIXELMAP;
107 }
108
RegisterRedrawCallback(RedrawCallback && callback)109 void PixelmapDrawableDescriptor::RegisterRedrawCallback(RedrawCallback&& callback)
110 {
111 redrawCallbacks_.push_back(callback);
112 }
113
Draw(RSCanvas & canvas,PaintConfig config)114 void PixelmapDrawableDescriptor::Draw(RSCanvas& canvas, PaintConfig config)
115 {
116 if (!pixelMap_) {
117 return;
118 }
119
120 RSPoint pointRadius[BORDER_RADIUS_ARRAY_SIZE] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
121
122 RSSamplingOptions options;
123 RSAdaptiveImageInfo rsImageInfo;
124 rsImageInfo.fitNum = static_cast<int32_t>(config.imageFit);
125 rsImageInfo.repeatNum = 0;
126 rsImageInfo.scale = 1.0;
127 rsImageInfo.dynamicRangeMode = 2;
128 rsImageInfo.radius[0] = pointRadius[0];
129 rsImageInfo.radius[1] = pointRadius[1];
130 rsImageInfo.radius[2] = pointRadius[2];
131 rsImageInfo.radius[3] = pointRadius[3];
132 auto& recordingCanvas = static_cast<Rosen::ExtendRecordingCanvas&>(canvas);
133 recordingCanvas.DrawPixelMapWithParm(pixelMap_->GetPixelMapSharedPtr(), rsImageInfo, options);
134 }
135 } // namespace Drawable
136 } // namespace Ace
137 } // namespace OHOS
138