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