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