1 /*
2  * Copyright (c) 2023-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 "skia_pixmap.h"
17 #include "skia_image_info.h"
18 
19 #include "image/pixmap.h"
20 #include "utils/log.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
SkiaPixmap()25 SkiaPixmap::SkiaPixmap() : skiaPixmap_() {}
SkiaPixmap(const ImageInfo & imageInfo,const void * addr,size_t rowBytes)26 SkiaPixmap::SkiaPixmap(const ImageInfo& imageInfo, const void* addr, size_t rowBytes)
27 {
28     auto skImageInfo = SkiaImageInfo::ConvertToSkImageInfo(imageInfo);
29     skiaPixmap_ = SkPixmap(skImageInfo, addr, rowBytes);
30 }
31 
ImportSkiaPixmap(const SkPixmap & skPixmap)32 void SkiaPixmap::ImportSkiaPixmap(const SkPixmap& skPixmap)
33 {
34     skiaPixmap_ = skPixmap;
35 }
36 
ExportSkiaPixmap() const37 const SkPixmap& SkiaPixmap::ExportSkiaPixmap() const
38 {
39     return skiaPixmap_;
40 }
41 
~SkiaPixmap()42 SkiaPixmap::~SkiaPixmap() {}
43 
GetColorSpace() const44 std::shared_ptr<ColorSpace> SkiaPixmap::GetColorSpace() const
45 {
46     std::shared_ptr<SkiaColorSpace> skiaColorSpace = std::make_shared<SkiaColorSpace>();
47     skiaColorSpace->SetColorSpace(skiaPixmap_.refColorSpace());
48     return ColorSpace::CreateFromImpl(skiaColorSpace);
49 }
50 
GetColorType() const51 ColorType SkiaPixmap::GetColorType() const
52 {
53     return SkiaImageInfo::ConvertToColorType(skiaPixmap_.colorType());
54 }
55 
GetAlphaType() const56 AlphaType SkiaPixmap::GetAlphaType() const
57 {
58     return SkiaImageInfo::ConvertToAlphaType(skiaPixmap_.alphaType());
59 }
60 
GetColor(int x,int y) const61 ColorQuad SkiaPixmap::GetColor(int x, int y) const
62 {
63     SkColor color = skiaPixmap_.getColor(x, y);
64     return static_cast<ColorQuad>(color);
65 }
66 
GetRowBytes() const67 size_t SkiaPixmap::GetRowBytes() const
68 {
69     return skiaPixmap_.rowBytes();
70 }
71 
GetAddr() const72 const void* SkiaPixmap::GetAddr() const
73 {
74     return skiaPixmap_.addr();
75 }
76 
GetWidth() const77 int32_t SkiaPixmap::GetWidth() const
78 {
79     return skiaPixmap_.width();
80 }
81 
GetHeight() const82 int32_t SkiaPixmap::GetHeight() const
83 {
84     return skiaPixmap_.height();
85 }
86 
ScalePixels(const Pixmap & dst,const SamplingOptions & options) const87 bool SkiaPixmap::ScalePixels(const Pixmap& dst, const SamplingOptions& options) const
88 {
89     SkSamplingOptions skSamplingOptions;
90     if (options.GetUseCubic()) {
91         skSamplingOptions = SkSamplingOptions({ options.GetCubicCoffB(), options.GetCubicCoffC() });
92     } else {
93         skSamplingOptions = SkSamplingOptions(static_cast<SkFilterMode>(options.GetFilterMode()),
94             static_cast<SkMipmapMode>(options.GetMipmapMode()));
95     }
96     return skiaPixmap_.scalePixels(dst.GetImpl<SkiaPixmap>()->ExportSkiaPixmap(), skSamplingOptions);
97 }
98 } // namespace Drawing
99 } // namespace Rosen
100 } // namespace OHOS
101