1 /*
2 * Copyright (c) 2021-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 "pixel_map_ohos.h"
17
18 #include <sstream>
19
20 #include "drawable_descriptor.h"
21 #include "pixel_map_manager.h"
22
23 #include "base/log/log_wrapper.h"
24 #include "base/utils/utils.h"
25
26 namespace OHOS::Ace {
27
PixelFormatConverter(Media::PixelFormat pixelFormat)28 PixelFormat PixelMapOhos::PixelFormatConverter(Media::PixelFormat pixelFormat)
29 {
30 switch (pixelFormat) {
31 case Media::PixelFormat::RGB_565:
32 return PixelFormat::RGB_565;
33 case Media::PixelFormat::RGBA_8888:
34 return PixelFormat::RGBA_8888;
35 case Media::PixelFormat::BGRA_8888:
36 return PixelFormat::BGRA_8888;
37 case Media::PixelFormat::ALPHA_8:
38 return PixelFormat::ALPHA_8;
39 case Media::PixelFormat::RGBA_F16:
40 return PixelFormat::RGBA_F16;
41 case Media::PixelFormat::UNKNOWN:
42 return PixelFormat::UNKNOWN;
43 case Media::PixelFormat::ARGB_8888:
44 return PixelFormat::ARGB_8888;
45 case Media::PixelFormat::RGB_888:
46 return PixelFormat::RGB_888;
47 case Media::PixelFormat::NV21:
48 return PixelFormat::NV21;
49 case Media::PixelFormat::NV12:
50 return PixelFormat::NV12;
51 case Media::PixelFormat::CMYK:
52 return PixelFormat::CMYK;
53 default:
54 return PixelFormat::UNKNOWN;
55 }
56 }
57
AlphaTypeConverter(Media::AlphaType alphaType)58 AlphaType PixelMapOhos::AlphaTypeConverter(Media::AlphaType alphaType)
59 {
60 switch (alphaType) {
61 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
62 return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
63 case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
64 return AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
65 case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
66 return AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
67 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
68 return AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
69 default:
70 return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
71 }
72 }
73
Create(std::unique_ptr<Media::PixelMap> && pixmap)74 RefPtr<PixelMap> PixelMap::Create(std::unique_ptr<Media::PixelMap>&& pixmap)
75 {
76 return AceType::MakeRefPtr<PixelMapOhos>(std::move(pixmap));
77 }
78
CreatePixelMap(void * rawPtr)79 RefPtr<PixelMap> PixelMap::CreatePixelMap(void* rawPtr)
80 {
81 auto* pixmapPtr = reinterpret_cast<std::shared_ptr<Media::PixelMap>*>(rawPtr);
82 if (pixmapPtr == nullptr || *pixmapPtr == nullptr) {
83 LOGW("pixmap pointer is nullptr when CreatePixelMap.");
84 return nullptr;
85 }
86 return AceType::MakeRefPtr<PixelMapOhos>(*pixmapPtr);
87 }
88
GetFromDrawable(void * ptr)89 RefPtr<PixelMap> PixelMap::GetFromDrawable(void* ptr)
90 {
91 CHECK_NULL_RETURN(ptr, nullptr);
92 auto* drawable = reinterpret_cast<Napi::DrawableDescriptor*>(ptr);
93 return AceType::MakeRefPtr<PixelMapOhos>(drawable->GetPixelMap());
94 }
95
CreatePixelMapFromDataAbility(void * ptr)96 RefPtr<PixelMap> PixelMap::CreatePixelMapFromDataAbility(void* ptr)
97 {
98 auto* pixmap = reinterpret_cast<Media::PixelMap*>(ptr);
99 CHECK_NULL_RETURN(pixmap, nullptr);
100 return AceType::MakeRefPtr<PixelMapOhos>(std::shared_ptr<Media::PixelMap>(pixmap));
101 }
102
GetWidth() const103 int32_t PixelMapOhos::GetWidth() const
104 {
105 CHECK_NULL_RETURN(pixmap_, 0);
106 return pixmap_->GetWidth();
107 }
108
GetHeight() const109 int32_t PixelMapOhos::GetHeight() const
110 {
111 CHECK_NULL_RETURN(pixmap_, 0);
112 return pixmap_->GetHeight();
113 }
114
GetPixels() const115 const uint8_t* PixelMapOhos::GetPixels() const
116 {
117 CHECK_NULL_RETURN(pixmap_, nullptr);
118 return pixmap_->GetPixels();
119 }
120
GetPixelFormat() const121 PixelFormat PixelMapOhos::GetPixelFormat() const
122 {
123 CHECK_NULL_RETURN(pixmap_, PixelFormat::UNKNOWN);
124 return PixelFormatConverter(pixmap_->GetPixelFormat());
125 }
126
GetAlphaType() const127 AlphaType PixelMapOhos::GetAlphaType() const
128 {
129 CHECK_NULL_RETURN(pixmap_, AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN);
130 return AlphaTypeConverter(pixmap_->GetAlphaType());
131 }
132
GetRowBytes() const133 int32_t PixelMapOhos::GetRowBytes() const
134 {
135 CHECK_NULL_RETURN(pixmap_, 0);
136 return pixmap_->GetRowBytes();
137 }
138
GetByteCount() const139 int32_t PixelMapOhos::GetByteCount() const
140 {
141 CHECK_NULL_RETURN_NOLOG(pixmap_, 0);
142 return pixmap_->GetByteCount();
143 }
144
GetPixelManager() const145 void* PixelMapOhos::GetPixelManager() const
146 {
147 Media::InitializationOptions opts;
148 CHECK_NULL_RETURN_NOLOG(pixmap_, nullptr);
149 auto newPixelMap = Media::PixelMap::Create(*pixmap_, opts);
150 return reinterpret_cast<void*>(new Media::PixelMapManager(newPixelMap.release()));
151 }
152
GetRawPixelMapPtr() const153 void* PixelMapOhos::GetRawPixelMapPtr() const
154 {
155 CHECK_NULL_RETURN(pixmap_, nullptr);
156 return pixmap_.get();
157 }
158
GetId()159 std::string PixelMapOhos::GetId()
160 {
161 // using pixmap addr
162 CHECK_NULL_RETURN(pixmap_, "nullptr");
163 std::stringstream strm;
164 strm << pixmap_.get();
165 return strm.str();
166 }
167
GetModifyId()168 std::string PixelMapOhos::GetModifyId()
169 {
170 return {};
171 }
172
GetPixelMapSharedPtr()173 std::shared_ptr<Media::PixelMap> PixelMapOhos::GetPixelMapSharedPtr()
174 {
175 return pixmap_;
176 }
177
GetWritablePixels() const178 void* PixelMapOhos::GetWritablePixels() const
179 {
180 CHECK_NULL_RETURN(pixmap_, nullptr);
181 return pixmap_->GetWritablePixels();
182 }
183
ConvertSkImageToPixmap(const uint32_t * colors,uint32_t colorLength,int32_t width,int32_t height)184 RefPtr<PixelMap> PixelMap::ConvertSkImageToPixmap(
185 const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height)
186 {
187 Media::InitializationOptions opts;
188 opts.size.width = width;
189 opts.size.height = height;
190 opts.editable = true;
191 std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(colors, colorLength, opts);
192 CHECK_NULL_RETURN(pixmap, nullptr);
193 std::shared_ptr<Media::PixelMap> sharedPixelmap(pixmap.release());
194 return AceType::MakeRefPtr<PixelMapOhos>(sharedPixelmap);
195 }
196
197 } // namespace OHOS::Ace
198