• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_
17 #define INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <memory>
22 #include <mutex>
23 #ifdef IMAGE_COLORSPACE_FLAG
24 #include "color_space.h"
25 #endif
26 #include "image_type.h"
27 #include "parcel.h"
28 #ifdef IMAGE_PURGEABLE_PIXELMAP
29 #include "purgeable_mem_base.h"
30 #include "purgeable_mem_builder.h"
31 #endif
32 
33 namespace OHOS {
34 namespace Media {
35 using TransColorProc = bool (*)(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
36 using CustomFreePixelMap = void (*)(void *addr, void *context, uint32_t size);
37 
38 struct InitializationOptions {
39     Size size;
40     PixelFormat pixelFormat = PixelFormat::UNKNOWN;
41     AlphaType alphaType = AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
42     ScaleMode scaleMode = ScaleMode::FIT_TARGET_SIZE;
43     bool editable = false;
44     bool useSourceIfMatch = false;
45 };
46 struct TransInfos;
47 
48 // Build ARGB_8888 pixel value
49 constexpr uint8_t ARGB_MASK = 0xFF;
50 constexpr uint8_t ARGB_A_SHIFT = 24;
51 constexpr uint8_t ARGB_R_SHIFT = 16;
52 constexpr uint8_t ARGB_G_SHIFT = 8;
53 constexpr uint8_t ARGB_B_SHIFT = 0;
54 // Define pixel map malloc max size 600MB
55 constexpr int32_t PIXEL_MAP_MAX_RAM_SIZE = 600 * 1024 * 1024;
56 
57 class PixelMap : public Parcelable {
58 public:
PixelMap()59     PixelMap()
60     {
61         static std::atomic<uint32_t> currentId = 0;
62         uniqueId_ = currentId.fetch_add(1, std::memory_order_relaxed);
63     }
64     virtual ~PixelMap();
65     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength,
66                                                          const InitializationOptions &opts);
67     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, int32_t offset,
68                                                          int32_t stride, const InitializationOptions &opts);
69     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, int32_t offset,
70         int32_t stride, const InitializationOptions &opts, bool useCustomFormat);
71     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const InitializationOptions &opts);
72     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const InitializationOptions &opts);
73     NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const Rect &srcRect,
74                                                          const InitializationOptions &opts);
75     NATIVEEXPORT uint32_t SetImageInfo(ImageInfo &info);
76     NATIVEEXPORT uint32_t SetImageInfo(ImageInfo &info, bool isReused);
77     NATIVEEXPORT const uint8_t *GetPixel(int32_t x, int32_t y);
78     NATIVEEXPORT const uint8_t *GetPixel8(int32_t x, int32_t y);
79     NATIVEEXPORT const uint16_t *GetPixel16(int32_t x, int32_t y);
80     NATIVEEXPORT const uint32_t *GetPixel32(int32_t x, int32_t y);
81     NATIVEEXPORT bool GetARGB32Color(int32_t x, int32_t y, uint32_t &color);
82     NATIVEEXPORT void SetPixelsAddr(void *addr, void *context, uint32_t size, AllocatorType type,
83                                     CustomFreePixelMap func);
84     NATIVEEXPORT int32_t GetPixelBytes();
85     NATIVEEXPORT int32_t GetRowBytes();
86     NATIVEEXPORT int32_t GetByteCount();
87     NATIVEEXPORT int32_t GetWidth();
88     NATIVEEXPORT int32_t GetHeight();
89     NATIVEEXPORT int32_t GetBaseDensity();
90     NATIVEEXPORT void scale(float xAxis, float yAxis);
91     NATIVEEXPORT bool resize(float xAxis, float yAxis);
92     NATIVEEXPORT void translate(float xAxis, float yAxis);
93     NATIVEEXPORT void rotate(float degrees);
94     NATIVEEXPORT void flip(bool xAxis, bool yAxis);
95     NATIVEEXPORT uint32_t crop(const Rect &rect);
96     NATIVEEXPORT void GetImageInfo(ImageInfo &imageInfo);
97     NATIVEEXPORT PixelFormat GetPixelFormat();
98     NATIVEEXPORT ColorSpace GetColorSpace();
99     NATIVEEXPORT AlphaType GetAlphaType();
100     NATIVEEXPORT uint32_t SetAlpha(const float percent);
101     NATIVEEXPORT const uint8_t *GetPixels();
102     NATIVEEXPORT uint8_t GetARGB32ColorA(uint32_t color);
103     NATIVEEXPORT uint8_t GetARGB32ColorR(uint32_t color);
104     NATIVEEXPORT uint8_t GetARGB32ColorG(uint32_t color);
105     NATIVEEXPORT uint8_t GetARGB32ColorB(uint32_t color);
106     // Config the pixel map parameter
107     NATIVEEXPORT bool IsSameImage(const PixelMap &other);
108     NATIVEEXPORT uint32_t ReadPixels(const uint64_t &bufferSize, const uint32_t &offset, const uint32_t &stride,
109                                      const Rect &region, uint8_t *dst);
110     NATIVEEXPORT uint32_t ReadPixels(const uint64_t &bufferSize, uint8_t *dst);
111     NATIVEEXPORT uint32_t ReadPixel(const Position &pos, uint32_t &dst);
112     NATIVEEXPORT uint32_t ResetConfig(const Size &size, const PixelFormat &format);
113     NATIVEEXPORT bool SetAlphaType(const AlphaType &alphaType);
114     NATIVEEXPORT uint32_t WritePixel(const Position &pos, const uint32_t &color);
115     NATIVEEXPORT uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize, const uint32_t &offset,
116                          const uint32_t &stride, const Rect &region);
117     NATIVEEXPORT uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize);
118     NATIVEEXPORT bool WritePixels(const uint32_t &color);
119     NATIVEEXPORT void FreePixelMap();
120     NATIVEEXPORT AllocatorType GetAllocatorType();
121     NATIVEEXPORT void *GetFd() const;
122     NATIVEEXPORT void SetFreePixelMapProc(CustomFreePixelMap func);
123     NATIVEEXPORT void SetTransformered(bool isTransformered);
124 
125     NATIVEEXPORT void SetRowStride(uint32_t stride);
GetRowStride()126     NATIVEEXPORT uint32_t GetRowStride()
127     {
128         return rowStride_;
129     }
GetCapacity()130     NATIVEEXPORT uint32_t GetCapacity()
131     {
132         return pixelsSize_;
133     }
134 
IsEditable()135     NATIVEEXPORT bool IsEditable()
136     {
137         return editable_;
138     }
139 
IsTransformered()140     NATIVEEXPORT bool IsTransformered()
141     {
142         return isTransformered_;
143     }
144 
145     // judgement whether create pixelmap use source as result
IsSourceAsResponse()146     NATIVEEXPORT bool IsSourceAsResponse()
147     {
148         return useSourceAsResponse_;
149     }
150 
GetWritablePixels()151     NATIVEEXPORT void *GetWritablePixels() const
152     {
153         return static_cast<void *>(data_);
154     }
155 
GetUniqueId()156     NATIVEEXPORT uint32_t GetUniqueId() const
157     {
158         return uniqueId_;
159     }
160 
161     NATIVEEXPORT bool Marshalling(Parcel &data) const override;
162     NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &data);
163     NATIVEEXPORT bool EncodeTlv(std::vector<uint8_t> &buff) const;
164     NATIVEEXPORT static PixelMap *DecodeTlv(std::vector<uint8_t> &buff);
165 
166 #ifdef IMAGE_COLORSPACE_FLAG
167     // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----begin----
168     NATIVEEXPORT void InnerSetColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace);
169     NATIVEEXPORT OHOS::ColorManager::ColorSpace InnerGetGrColorSpace();
InnerGetGrColorSpacePtr()170     NATIVEEXPORT std::shared_ptr<OHOS::ColorManager::ColorSpace> InnerGetGrColorSpacePtr()
171     {
172         return grColorSpace_;
173     }
174     // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----end-------
175 #endif
176 
177 #ifdef IMAGE_PURGEABLE_PIXELMAP
IsPurgeable()178     NATIVEEXPORT bool IsPurgeable() const
179     {
180         return purgeableMemPtr_ != nullptr;
181     }
182 
GetPurgeableMemPtr()183     NATIVEEXPORT std::shared_ptr<PurgeableMem::PurgeableMemBase> GetPurgeableMemPtr() const
184     {
185         return purgeableMemPtr_;
186     }
187 
SetPurgeableMemPtr(std::shared_ptr<PurgeableMem::PurgeableMemBase> pmPtr)188     NATIVEEXPORT void SetPurgeableMemPtr(std::shared_ptr<PurgeableMem::PurgeableMemBase> pmPtr)
189     {
190         purgeableMemPtr_ = pmPtr;
191     }
192 #endif
193 
194 private:
195     static constexpr uint8_t TLV_VARINT_BITS = 7;
196     static constexpr uint8_t TLV_VARINT_MASK = 0x7F;
197     static constexpr uint8_t TLV_VARINT_MORE = 0x80;
198     static constexpr uint8_t TLV_END = 0x00;
199     static constexpr uint8_t TLV_IMAGE_WIDTH = 0x01;
200     static constexpr uint8_t TLV_IMAGE_HEIGHT = 0x02;
201     static constexpr uint8_t TLV_IMAGE_PIXELFORMAT = 0x03;
202     static constexpr uint8_t TLV_IMAGE_COLORSPACE = 0x04;
203     static constexpr uint8_t TLV_IMAGE_ALPHATYPE = 0x05;
204     static constexpr uint8_t TLV_IMAGE_BASEDENSITY = 0x06;
205     static constexpr uint8_t TLV_IMAGE_ALLOCATORTYPE = 0x07;
206     static constexpr uint8_t TLV_IMAGE_DATA = 0x08;
207     static constexpr size_t MAX_IMAGEDATA_SIZE = 128 * 1024 * 1024; // 128M
208     static constexpr size_t MIN_IMAGEDATA_SIZE = 32 * 1024;         // 32k
209     friend class ImageSource;
210     static bool ALPHA8ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
211     static bool RGB565ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
212     static bool ARGB8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
213     static bool RGBA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
214     static bool BGRA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
215     static bool RGB888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount);
216     static bool CheckParams(const uint32_t *colors, uint32_t colorLength, int32_t offset, int32_t stride,
217                             const InitializationOptions &opts);
218     static void UpdatePixelsAlpha(const AlphaType &alphaType, const PixelFormat &pixelFormat, uint8_t *dstPixels,
219                                   PixelMap dstPixelMap);
220     static void InitDstImageInfo(const InitializationOptions &opts, const ImageInfo &srcImageInfo,
221                                  ImageInfo &dstImageInfo);
222     static bool CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap);
223     static bool SourceCropAndConvert(PixelMap &source, const ImageInfo &srcImageInfo, const ImageInfo &dstImageInfo,
224                                      const Rect &srcRect, PixelMap &dstPixelMap);
225     static bool IsSameSize(const Size &src, const Size &dst);
226     static bool ScalePixelMap(const Size &targetSize, const Size &dstSize, const ScaleMode &scaleMode,
227                               PixelMap &dstPixelMap);
228     bool GetPixelFormatDetail(const PixelFormat format);
229     bool CheckPixelsInput(const uint8_t *dst, const uint64_t &bufferSize, const uint32_t &offset,
230                           const uint32_t &stride, const Rect &region);
231     void ReleaseSharedMemory(void *addr, void *context, uint32_t size);
232     static void ReleaseBuffer(AllocatorType allocatorType, int fd, uint64_t dataSize, void **buffer);
233     static void *AllocSharedMemory(const uint64_t bufferSize, int &fd, uint32_t uniqueId);
234     bool WriteInfoToParcel(Parcel &parcel) const;
SetEditable(bool editable)235     void SetEditable(bool editable)
236     {
237         editable_ = editable;
238     }
239 
ResetPixelMap()240     void ResetPixelMap()
241     {
242         rowDataSize_ = 0;
243         pixelBytes_ = 0;
244         colorProc_ = nullptr;
245     }
246 
CheckValidParam(int32_t x,int32_t y)247     bool CheckValidParam(int32_t x, int32_t y)
248     {
249         return (data_ == nullptr) || (x >= imageInfo_.size.width) || (x < 0) || (y >= imageInfo_.size.height) ||
250                        (y < 0) || (pixelsSize_ < static_cast<uint64_t>(rowDataSize_) * imageInfo_.size.height)
251                    ? false
252                    : true;
253     }
254 
255     static void ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size);
256     bool WriteImageData(Parcel &parcel, size_t size) const;
257     static uint8_t *ReadImageData(Parcel &parcel, int32_t size);
258     static int ReadFileDescriptor(Parcel &parcel);
259     static bool WriteFileDescriptor(Parcel &parcel, int fd);
260     bool ReadImageInfo(Parcel &parcel, ImageInfo &imgInfo);
261     bool WriteImageInfo(Parcel &parcel) const;
262     void WriteUint8(std::vector<uint8_t> &buff, uint8_t value) const;
263     static uint8_t ReadUint8(std::vector<uint8_t> &buff, int32_t &cursor);
264     uint8_t GetVarintLen(int32_t value) const;
265     void WriteVarint(std::vector<uint8_t> &buff, int32_t value) const;
266     static int32_t ReadVarint(std::vector<uint8_t> &buff, int32_t &cursor);
267     void WriteData(std::vector<uint8_t> &buff, const uint8_t *data, int32_t size) const;
268     static uint8_t *ReadData(std::vector<uint8_t> &buff, int32_t size, int32_t &cursor);
269     static void ReadTlvAttr(std::vector<uint8_t> &buff, ImageInfo &info, int32_t &type, int32_t &size, uint8_t **data);
270     bool DoTranslation(TransInfos &infos);
271 
272     uint8_t *data_ = nullptr;
273     // this info SHOULD be the final info for decoded pixelmap, not the original image info
274     ImageInfo imageInfo_;
275     int32_t rowDataSize_ = 0;
276     int32_t rowStride_ = 0;
277     int32_t pixelBytes_ = 0;
278     TransColorProc colorProc_ = nullptr;
279     void *context_ = nullptr;
280     CustomFreePixelMap custFreePixelMap_ = nullptr;
281     CustomFreePixelMap freePixelMapProc_ = nullptr;
282     AllocatorType allocatorType_ = AllocatorType::SHARE_MEM_ALLOC;
283     uint32_t pixelsSize_ = 0;
284     bool editable_ = false;
285     bool useSourceAsResponse_ = false;
286     bool isTransformered_ = false;
287     std::shared_ptr<std::mutex> transformMutex_ = std::make_shared<std::mutex>();
288 
289     // only used by rosen backend
290     uint32_t uniqueId_ = 0;
291 
292 #ifdef IMAGE_COLORSPACE_FLAG
293     std::shared_ptr<OHOS::ColorManager::ColorSpace> grColorSpace_ = nullptr;
294 #else
295     std::shared_ptr<uint8_t> grColorSpace_ = nullptr;
296 #endif
297 
298 #ifdef IMAGE_PURGEABLE_PIXELMAP
299     std::shared_ptr<PurgeableMem::PurgeableMemBase> purgeableMemPtr_ = nullptr;
300 #else
301     std::shared_ptr<uint8_t> purgeableMemPtr_ = nullptr;
302 #endif
303 };
304 } // namespace Media
305 } // namespace OHOS
306 
307 #endif // INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_
308