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 ACE_ENABLE_GPU
BuildFromBitmap(GPUContext & gpuContext,const Bitmap & bitmap)75 bool Image::BuildFromBitmap(GPUContext& gpuContext, const Bitmap& bitmap)
76 {
77 return imageImplPtr->BuildFromBitmap(gpuContext, bitmap);
78 }
79
MakeFromEncoded(const std::shared_ptr<Data> & data)80 bool Image::MakeFromEncoded(const std::shared_ptr<Data>& data)
81 {
82 return imageImplPtr->MakeFromEncoded(data);
83 }
84
BuildFromCompressed(GPUContext & gpuContext,const std::shared_ptr<Data> & data,int width,int height,CompressedType type)85 bool Image::BuildFromCompressed(GPUContext& gpuContext, const std::shared_ptr<Data>& data, int width, int height,
86 CompressedType type)
87 {
88 return imageImplPtr->BuildFromCompressed(gpuContext, data, width, height, type);
89 }
90
BuildFromSurface(GPUContext & gpuContext,Surface & surface,TextureOrigin origin,BitmapFormat bitmapFormat,const std::shared_ptr<ColorSpace> & colorSpace)91 bool Image::BuildFromSurface(GPUContext& gpuContext, Surface& surface, TextureOrigin origin,
92 BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace)
93 {
94 return imageImplPtr->BuildFromSurface(gpuContext, surface, origin, bitmapFormat, colorSpace);
95 }
96
BuildFromTexture(GPUContext & gpuContext,const TextureInfo & info,TextureOrigin origin,BitmapFormat bitmapFormat,const std::shared_ptr<ColorSpace> & colorSpace,void (* deleteFunc)(void *),void * cleanupHelper)97 bool Image::BuildFromTexture(GPUContext& gpuContext, const TextureInfo& info, TextureOrigin origin,
98 BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace,
99 void (*deleteFunc)(void*), void* cleanupHelper)
100 {
101 return imageImplPtr->BuildFromTexture(gpuContext, info, origin, bitmapFormat,
102 colorSpace, deleteFunc, cleanupHelper);
103 }
104
BuildSubset(const std::shared_ptr<Image> & image,const RectI & rect,GPUContext & gpuContext)105 bool Image::BuildSubset(const std::shared_ptr<Image>& image, const RectI& rect, GPUContext& gpuContext)
106 {
107 return imageImplPtr->BuildSubset(image, rect, gpuContext);
108 }
109
GetBackendTexture(bool flushPendingGrContextIO,TextureOrigin * origin) const110 BackendTexture Image::GetBackendTexture(bool flushPendingGrContextIO, TextureOrigin* origin) const
111 {
112 return imageImplPtr->GetBackendTexture(flushPendingGrContextIO, origin);
113 }
114
IsValid(GPUContext * context) const115 bool Image::IsValid(GPUContext* context) const
116 {
117 return imageImplPtr->IsValid(context);
118 }
119
pinAsTexture(GPUContext & context)120 bool Image::pinAsTexture(GPUContext& context)
121 {
122 auto image = ExportSkImage().get();
123 auto skGpuContext = context.GetImpl<SkiaGPUContext>();
124 if (skGpuContext == nullptr) {
125 return false;
126 }
127 auto skContext = skGpuContext->GetGrContext().get();
128 return image != nullptr && skContext != nullptr && SkImage_pinAsTexture(image, skContext);
129 }
130 #endif
131
AsLegacyBitmap(Bitmap & bitmap) const132 bool Image::AsLegacyBitmap(Bitmap& bitmap) const
133 {
134 return imageImplPtr->AsLegacyBitmap(bitmap);
135 }
136
GetWidth() const137 int Image::GetWidth() const
138 {
139 return imageImplPtr->GetWidth();
140 }
141
GetHeight() const142 int Image::GetHeight() const
143 {
144 return imageImplPtr->GetHeight();
145 }
146
GetColorType() const147 ColorType Image::GetColorType() const
148 {
149 return imageImplPtr->GetColorType();
150 }
151
GetAlphaType() const152 AlphaType Image::GetAlphaType() const
153 {
154 return imageImplPtr->GetAlphaType();
155 }
156
GetColorSpace() const157 std::shared_ptr<ColorSpace> Image::GetColorSpace() const
158 {
159 return imageImplPtr->GetColorSpace();
160 }
161
GetUniqueID() const162 uint32_t Image::GetUniqueID() const
163 {
164 return imageImplPtr->GetUniqueID();
165 }
166
GetImageInfo()167 ImageInfo Image::GetImageInfo()
168 {
169 return imageImplPtr->GetImageInfo();
170 }
171
ReadPixels(Bitmap & bitmap,int x,int y)172 bool Image::ReadPixels(Bitmap& bitmap, int x, int y)
173 {
174 return imageImplPtr->ReadPixels(bitmap, x, y);
175 }
176
ReadPixels(Pixmap & pixmap,int x,int y)177 bool Image::ReadPixels(Pixmap& pixmap, int x, int y)
178 {
179 return imageImplPtr->ReadPixels(pixmap, x, y);
180 }
181
ReadPixels(const ImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes,int32_t srcX,int32_t srcY) const182 bool Image::ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
183 int32_t srcX, int32_t srcY) const
184 {
185 return imageImplPtr->ReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY);
186 }
187
IsTextureBacked() const188 bool Image::IsTextureBacked() const
189 {
190 return imageImplPtr->IsTextureBacked();
191 }
192
ScalePixels(const Bitmap & bitmap,const SamplingOptions & sampling,bool allowCachingHint) const193 bool Image::ScalePixels(const Bitmap& bitmap, const SamplingOptions& sampling, bool allowCachingHint) const
194 {
195 return imageImplPtr->ScalePixels(bitmap, sampling, allowCachingHint);
196 }
197
EncodeToData(EncodedImageFormat encodedImageFormat,int quality) const198 std::shared_ptr<Data> Image::EncodeToData(EncodedImageFormat encodedImageFormat, int quality) const
199 {
200 return imageImplPtr->EncodeToData(encodedImageFormat, quality);
201 }
202
IsLazyGenerated() const203 bool Image::IsLazyGenerated() const
204 {
205 return imageImplPtr->IsLazyGenerated();
206 }
207
GetROPixels(Bitmap & bitmap) const208 bool Image::GetROPixels(Bitmap& bitmap) const
209 {
210 return imageImplPtr->GetROPixels(bitmap);
211 }
212
MakeRasterImage() const213 std::shared_ptr<Image> Image::MakeRasterImage() const
214 {
215 return imageImplPtr->MakeRasterImage();
216 }
217
CanPeekPixels() const218 bool Image::CanPeekPixels() const
219 {
220 return imageImplPtr->CanPeekPixels();
221 }
222
IsOpaque() const223 bool Image::IsOpaque() const
224 {
225 return imageImplPtr->IsOpaque();
226 }
227
Serialize() const228 std::shared_ptr<Data> Image::Serialize() const
229 {
230 return imageImplPtr->Serialize();
231 }
232
Deserialize(std::shared_ptr<Data> data)233 bool Image::Deserialize(std::shared_ptr<Data> data)
234 {
235 return imageImplPtr->Deserialize(data);
236 }
237
ExportSkImage()238 const sk_sp<SkImage> Image::ExportSkImage()
239 {
240 return GetImpl<SkiaImage>()->GetImage();
241 }
242 } // namespace Drawing
243 } // namespace Rosen
244 } // namespace OHOS
245