1 /*
2  * Copyright (c) 2021-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 "image/image.h"
17 
18 #include "engine_adapter/skia_adapter/skia_gpu_context.h"
19 #include "image/gpu_context.h"
20 #include "impl_factory.h"
21 #include "skia_adapter/skia_image.h"
22 #include "static_factory.h"
23 #include "src/core/SkImagePriv.h"
24 #include "src/image/SkImage_Base.h"
25 #include "utils/system_properties.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
BackendTexture()30 BackendTexture::BackendTexture() noexcept
31     : isValid_(false) {}
32 
BackendTexture(bool isValid)33 BackendTexture::BackendTexture(bool isValid) noexcept
34     : isValid_(isValid) {}
35 
IsValid() const36 bool BackendTexture::IsValid() const
37 {
38     return isValid_;
39 }
40 
SetTextureInfo(const TextureInfo & textureInfo)41 void BackendTexture::SetTextureInfo(const TextureInfo& textureInfo)
42 {
43     textureInfo_ = textureInfo;
44 }
45 
GetTextureInfo() const46 const TextureInfo& BackendTexture::GetTextureInfo() const
47 {
48     return textureInfo_;
49 }
50 
Image()51 Image::Image() noexcept : imageImplPtr(ImplFactory::CreateImageImpl()) {}
52 
Image(std::shared_ptr<ImageImpl> imageImpl)53 Image::Image(std::shared_ptr<ImageImpl> imageImpl) : imageImplPtr(imageImpl) {}
54 
Image(void * rawImg)55 Image::Image(void* rawImg) noexcept : imageImplPtr(ImplFactory::CreateImageImpl(rawImg)) {}
56 
BuildFromBitmap(const Bitmap & bitmap)57 bool Image::BuildFromBitmap(const Bitmap& bitmap)
58 {
59     return imageImplPtr->BuildFromBitmap(bitmap);
60 }
61 
MakeFromRaster(const Pixmap & pixmap,RasterReleaseProc rasterReleaseProc,ReleaseContext releaseContext)62 std::shared_ptr<Image> Image::MakeFromRaster(const Pixmap& pixmap,
63     RasterReleaseProc rasterReleaseProc, ReleaseContext releaseContext)
64 {
65     return StaticFactory::MakeFromRaster(pixmap, rasterReleaseProc, releaseContext);
66 }
67 
MakeRasterData(const ImageInfo & info,std::shared_ptr<Data> pixels,size_t rowBytes)68 std::shared_ptr<Image> Image::MakeRasterData(const ImageInfo& info, std::shared_ptr<Data> pixels,
69     size_t rowBytes)
70 {
71     return StaticFactory::MakeRasterData(info, pixels, rowBytes);
72 }
73 
74 #ifdef RS_ENABLE_GPU
MakeFromYUVAPixmaps(GPUContext & gpuContext,const YUVInfo & info,void * memory)75 std::shared_ptr<Image> Image::MakeFromYUVAPixmaps(GPUContext& gpuContext, const YUVInfo& info, void* memory)
76 {
77     return StaticFactory::MakeFromYUVAPixmaps(gpuContext, info, memory);
78 }
79 
BuildFromBitmap(GPUContext & gpuContext,const Bitmap & bitmap)80 bool Image::BuildFromBitmap(GPUContext& gpuContext, const Bitmap& bitmap)
81 {
82     return imageImplPtr->BuildFromBitmap(gpuContext, bitmap);
83 }
84 
MakeFromEncoded(const std::shared_ptr<Data> & data)85 bool Image::MakeFromEncoded(const std::shared_ptr<Data>& data)
86 {
87     return imageImplPtr->MakeFromEncoded(data);
88 }
89 
BuildFromCompressed(GPUContext & gpuContext,const std::shared_ptr<Data> & data,int width,int height,CompressedType type,const std::shared_ptr<ColorSpace> & colorSpace)90 bool Image::BuildFromCompressed(GPUContext& gpuContext, const std::shared_ptr<Data>& data, int width, int height,
91     CompressedType type, const std::shared_ptr<ColorSpace>& colorSpace)
92 {
93     return imageImplPtr->BuildFromCompressed(gpuContext, data, width, height, type, colorSpace);
94 }
95 
BuildFromSurface(GPUContext & gpuContext,Surface & surface,TextureOrigin origin,BitmapFormat bitmapFormat,const std::shared_ptr<ColorSpace> & colorSpace)96 bool Image::BuildFromSurface(GPUContext& gpuContext, Surface& surface, TextureOrigin origin,
97     BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace)
98 {
99     return imageImplPtr->BuildFromSurface(gpuContext, surface, origin, bitmapFormat, colorSpace);
100 }
101 
BuildFromTexture(GPUContext & gpuContext,const TextureInfo & info,TextureOrigin origin,BitmapFormat bitmapFormat,const std::shared_ptr<ColorSpace> & colorSpace,void (* deleteFunc)(void *),void * cleanupHelper)102 bool Image::BuildFromTexture(GPUContext& gpuContext, const TextureInfo& info, TextureOrigin origin,
103     BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace,
104     void (*deleteFunc)(void*), void* cleanupHelper)
105 {
106     return imageImplPtr->BuildFromTexture(gpuContext, info, origin, bitmapFormat,
107         colorSpace, deleteFunc, cleanupHelper);
108 }
109 
BuildSubset(const std::shared_ptr<Image> & image,const RectI & rect,GPUContext & gpuContext)110 bool Image::BuildSubset(const std::shared_ptr<Image>& image, const RectI& rect, GPUContext& gpuContext)
111 {
112     return imageImplPtr->BuildSubset(image, rect, gpuContext);
113 }
114 
GetBackendTexture(bool flushPendingGrContextIO,TextureOrigin * origin) const115 BackendTexture Image::GetBackendTexture(bool flushPendingGrContextIO, TextureOrigin* origin) const
116 {
117     return imageImplPtr->GetBackendTexture(flushPendingGrContextIO, origin);
118 }
119 
IsValid(GPUContext * context) const120 bool Image::IsValid(GPUContext* context) const
121 {
122     return imageImplPtr->IsValid(context);
123 }
124 #endif
125 
AsLegacyBitmap(Bitmap & bitmap) const126 bool Image::AsLegacyBitmap(Bitmap& bitmap) const
127 {
128     return imageImplPtr->AsLegacyBitmap(bitmap);
129 }
130 
GetWidth() const131 int Image::GetWidth() const
132 {
133     return imageImplPtr->GetWidth();
134 }
135 
GetHeight() const136 int Image::GetHeight() const
137 {
138     return imageImplPtr->GetHeight();
139 }
140 
GetColorType() const141 ColorType Image::GetColorType() const
142 {
143     return imageImplPtr->GetColorType();
144 }
145 
GetAlphaType() const146 AlphaType Image::GetAlphaType() const
147 {
148     return imageImplPtr->GetAlphaType();
149 }
150 
GetColorSpace() const151 std::shared_ptr<ColorSpace> Image::GetColorSpace() const
152 {
153     return imageImplPtr->GetColorSpace();
154 }
155 
GetUniqueID() const156 uint32_t Image::GetUniqueID() const
157 {
158     return imageImplPtr->GetUniqueID();
159 }
160 
GetImageInfo()161 ImageInfo Image::GetImageInfo()
162 {
163     return imageImplPtr->GetImageInfo();
164 }
165 
ReadPixels(Bitmap & bitmap,int x,int y)166 bool Image::ReadPixels(Bitmap& bitmap, int x, int y)
167 {
168     return imageImplPtr->ReadPixels(bitmap, x, y);
169 }
170 
ReadPixels(Pixmap & pixmap,int x,int y)171 bool Image::ReadPixels(Pixmap& pixmap, int x, int y)
172 {
173     return imageImplPtr->ReadPixels(pixmap, x, y);
174 }
175 
ReadPixels(const ImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes,int32_t srcX,int32_t srcY) const176 bool Image::ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
177     int32_t srcX, int32_t srcY) const
178 {
179     return imageImplPtr->ReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY);
180 }
181 
IsTextureBacked() const182 bool Image::IsTextureBacked() const
183 {
184     return imageImplPtr->IsTextureBacked();
185 }
186 
ScalePixels(const Bitmap & bitmap,const SamplingOptions & sampling,bool allowCachingHint) const187 bool Image::ScalePixels(const Bitmap& bitmap, const SamplingOptions& sampling, bool allowCachingHint) const
188 {
189     return imageImplPtr->ScalePixels(bitmap, sampling, allowCachingHint);
190 }
191 
EncodeToData(EncodedImageFormat encodedImageFormat,int quality) const192 std::shared_ptr<Data> Image::EncodeToData(EncodedImageFormat encodedImageFormat, int quality) const
193 {
194     return imageImplPtr->EncodeToData(encodedImageFormat, quality);
195 }
196 
IsLazyGenerated() const197 bool Image::IsLazyGenerated() const
198 {
199     return imageImplPtr->IsLazyGenerated();
200 }
201 
GetROPixels(Bitmap & bitmap) const202 bool Image::GetROPixels(Bitmap& bitmap) const
203 {
204     return imageImplPtr->GetROPixels(bitmap);
205 }
206 
MakeRasterImage() const207 std::shared_ptr<Image> Image::MakeRasterImage() const
208 {
209     return imageImplPtr->MakeRasterImage();
210 }
211 
CanPeekPixels() const212 bool Image::CanPeekPixels() const
213 {
214     return imageImplPtr->CanPeekPixels();
215 }
216 
IsOpaque() const217 bool Image::IsOpaque() const
218 {
219     return imageImplPtr->IsOpaque();
220 }
221 
HintCacheGpuResource() const222 void Image::HintCacheGpuResource() const
223 {
224     imageImplPtr->HintCacheGpuResource();
225 }
226 
Serialize() const227 std::shared_ptr<Data> Image::Serialize() const
228 {
229     return imageImplPtr->Serialize();
230 }
231 
Deserialize(std::shared_ptr<Data> data)232 bool Image::Deserialize(std::shared_ptr<Data> data)
233 {
234     return imageImplPtr->Deserialize(data);
235 }
236 } // namespace Drawing
237 } // namespace Rosen
238 } // namespace OHOS
239