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