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 ®ion, 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 ®ion); 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 131 NATIVEEXPORT bool Marshalling(Parcel &data) const override; 132 NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &data); 133 NATIVEEXPORT bool EncodeTlv(std::vector<uint8_t> &buff) const; 134 NATIVEEXPORT static PixelMap *DecodeTlv(std::vector<uint8_t> &buff); 135 136 #ifdef IMAGE_COLORSPACE_FLAG 137 // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----begin---- 138 NATIVEEXPORT void InnerSetColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace); 139 NATIVEEXPORT OHOS::ColorManager::ColorSpace InnerGetGrColorSpace(); InnerGetGrColorSpacePtr()140 NATIVEEXPORT std::shared_ptr<OHOS::ColorManager::ColorSpace> InnerGetGrColorSpacePtr() 141 { 142 return grColorSpace_; 143 } 144 // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----end------- 145 #endif 146 147 private: 148 static constexpr uint8_t TLV_VARINT_BITS = 7; 149 static constexpr uint8_t TLV_VARINT_MASK = 0x7F; 150 static constexpr uint8_t TLV_VARINT_MORE = 0x80; 151 static constexpr uint8_t TLV_END = 0x00; 152 static constexpr uint8_t TLV_IMAGE_WIDTH = 0x01; 153 static constexpr uint8_t TLV_IMAGE_HEIGHT = 0x02; 154 static constexpr uint8_t TLV_IMAGE_PIXELFORMAT = 0x03; 155 static constexpr uint8_t TLV_IMAGE_COLORSPACE = 0x04; 156 static constexpr uint8_t TLV_IMAGE_ALPHATYPE = 0x05; 157 static constexpr uint8_t TLV_IMAGE_BASEDENSITY = 0x06; 158 static constexpr uint8_t TLV_IMAGE_ALLOCATORTYPE = 0x07; 159 static constexpr uint8_t TLV_IMAGE_DATA = 0x08; 160 static constexpr size_t MAX_IMAGEDATA_SIZE = 128 * 1024 * 1024; // 128M 161 static constexpr size_t MIN_IMAGEDATA_SIZE = 32 * 1024; // 32k 162 friend class ImageSource; 163 static bool ALPHA8ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 164 static bool RGB565ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 165 static bool ARGB8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 166 static bool RGBA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 167 static bool BGRA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 168 static bool RGB888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 169 static bool CheckParams(const uint32_t *colors, uint32_t colorLength, int32_t offset, int32_t stride, 170 const InitializationOptions &opts); 171 static void UpdatePixelsAlpha(const AlphaType &alphaType, const PixelFormat &pixelFormat, uint8_t *dstPixels, 172 PixelMap dstPixelMap); 173 static void InitDstImageInfo(const InitializationOptions &opts, const ImageInfo &srcImageInfo, 174 ImageInfo &dstImageInfo); 175 static bool CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap); 176 static bool SourceCropAndConvert(PixelMap &source, const ImageInfo &srcImageInfo, const ImageInfo &dstImageInfo, 177 const Rect &srcRect, PixelMap &dstPixelMap); 178 static bool IsSameSize(const Size &src, const Size &dst); 179 static bool ScalePixelMap(const Size &targetSize, const Size &dstSize, const ScaleMode &scaleMode, 180 PixelMap &dstPixelMap); 181 bool GetPixelFormatDetail(const PixelFormat format); 182 bool CheckPixelsInput(const uint8_t *dst, const uint64_t &bufferSize, const uint32_t &offset, 183 const uint32_t &stride, const Rect ®ion); 184 void ReleaseSharedMemory(void *addr, void *context, uint32_t size); SetEditable(bool editable)185 void SetEditable(bool editable) 186 { 187 editable_ = editable; 188 } 189 ResetPixelMap()190 void ResetPixelMap() 191 { 192 rowDataSize_ = 0; 193 pixelBytes_ = 0; 194 colorProc_ = nullptr; 195 } 196 CheckValidParam(int32_t x,int32_t y)197 bool CheckValidParam(int32_t x, int32_t y) 198 { 199 return (data_ == nullptr) || (x >= imageInfo_.size.width) || (x < 0) || (y >= imageInfo_.size.height) || 200 (y < 0) || (pixelsSize_ < static_cast<uint64_t>(rowDataSize_) * imageInfo_.size.height) 201 ? false 202 : true; 203 } 204 205 static void ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size); 206 bool WriteImageData(Parcel &parcel, size_t size) const; 207 static uint8_t *ReadImageData(Parcel &parcel, int32_t size); 208 static int ReadFileDescriptor(Parcel &parcel); 209 static bool WriteFileDescriptor(Parcel &parcel, int fd); 210 bool ReadImageInfo(Parcel &parcel, ImageInfo &imgInfo); 211 bool WriteImageInfo(Parcel &parcel) const; 212 void WriteUint8(std::vector<uint8_t> &buff, uint8_t value) const; 213 static uint8_t ReadUint8(std::vector<uint8_t> &buff, int32_t &cursor); 214 uint8_t GetVarintLen(int32_t value) const; 215 void WriteVarint(std::vector<uint8_t> &buff, int32_t value) const; 216 static int32_t ReadVarint(std::vector<uint8_t> &buff, int32_t &cursor); 217 void WriteData(std::vector<uint8_t> &buff, const uint8_t *data, int32_t size) const; 218 static uint8_t *ReadData(std::vector<uint8_t> &buff, int32_t size, int32_t &cursor); 219 static void ReadTlvAttr(std::vector<uint8_t> &buff, ImageInfo &info, int32_t &type, int32_t &size, uint8_t **data); 220 221 uint8_t *data_ = nullptr; 222 // this info SHOULD be the final info for decoded pixelmap, not the original image info 223 ImageInfo imageInfo_; 224 int32_t rowDataSize_ = 0; 225 int32_t pixelBytes_ = 0; 226 TransColorProc colorProc_ = nullptr; 227 void *context_ = nullptr; 228 CustomFreePixelMap custFreePixelMap_ = nullptr; 229 AllocatorType allocatorType_ = AllocatorType::HEAP_ALLOC; 230 uint32_t pixelsSize_ = 0; 231 bool editable_ = false; 232 bool useSourceAsResponse_ = false; 233 234 // only used by rosen backend 235 std::shared_ptr<RosenImageWrapper> rosenImageWrapper_; 236 friend class PixelMapRosenUtils; 237 238 #ifdef IMAGE_COLORSPACE_FLAG 239 std::shared_ptr<OHOS::ColorManager::ColorSpace> grColorSpace_ = nullptr; 240 #else 241 std::shared_ptr<uint8_t> grColorSpace_ = nullptr; 242 #endif 243 }; 244 } // namespace Media 245 } // namespace OHOS 246 247 #endif // INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_ 248