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 #include <shared_mutex> 24 #ifdef IMAGE_COLORSPACE_FLAG 25 #include "color_space.h" 26 #endif 27 #include "image_type.h" 28 #include "parcel.h" 29 #ifdef IMAGE_PURGEABLE_PIXELMAP 30 #include "purgeable_mem_base.h" 31 #include "purgeable_mem_builder.h" 32 #endif 33 34 namespace OHOS { 35 namespace Media { 36 struct HdrMetadata; 37 enum class ImageHdrType : int32_t; 38 using TransColorProc = bool (*)(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 39 using CustomFreePixelMap = void (*)(void *addr, void *context, uint32_t size); 40 41 typedef struct { 42 float scaleX; 43 float scaleY; 44 float rotateD; 45 float cropLeft; 46 float cropTop; 47 float cropWidth; 48 float cropHeight; 49 float translateX; 50 float translateY; 51 bool flipX; 52 bool flipY; 53 } TransformData; 54 55 struct InitializationOptions { 56 Size size; 57 PixelFormat srcPixelFormat = PixelFormat::BGRA_8888; 58 PixelFormat pixelFormat = PixelFormat::UNKNOWN; 59 AlphaType alphaType = AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 60 ScaleMode scaleMode = ScaleMode::FIT_TARGET_SIZE; 61 int32_t srcRowStride = 0; 62 bool editable = false; 63 bool useSourceIfMatch = false; 64 bool useDMA = false; 65 }; 66 struct TransInfos; 67 68 // Build ARGB_8888 pixel value 69 constexpr uint8_t ARGB_MASK = 0xFF; 70 constexpr uint8_t ARGB_A_SHIFT = 24; 71 constexpr uint8_t ARGB_R_SHIFT = 16; 72 constexpr uint8_t ARGB_G_SHIFT = 8; 73 constexpr uint8_t ARGB_B_SHIFT = 0; 74 // Define pixel map malloc max size 600MB 75 // Memory copy will be performed twice on heap memory during IPC, so the size has to be limited 76 constexpr int32_t PIXEL_MAP_MAX_RAM_SIZE = 600 * 1024 * 1024; 77 78 typedef struct PixelMapError { 79 uint32_t errorCode = 0; 80 std::string errorInfo = ""; 81 } PIXEL_MAP_ERR; 82 83 typedef struct BuildParam { 84 int32_t offset_ = 0; 85 int32_t width_ = 0; 86 bool flag_ = true; 87 } BUILD_PARAM; 88 89 struct PixelMemInfo { 90 uint8_t* base = nullptr; 91 void* context = nullptr; 92 int32_t bufferSize = 0; 93 AllocatorType allocatorType = AllocatorType::SHARE_MEM_ALLOC; 94 bool isAstc = false; 95 }; 96 97 class ExifMetadata; 98 99 class PixelMap : public Parcelable, public PIXEL_MAP_ERR { 100 public: 101 static std::atomic<uint32_t> currentId; PixelMap()102 PixelMap() 103 { 104 uniqueId_ = currentId.fetch_add(1, std::memory_order_relaxed); 105 } 106 virtual ~PixelMap(); 107 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, 108 const InitializationOptions &opts); 109 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, int32_t offset, 110 int32_t stride, const InitializationOptions &opts); 111 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, int32_t offset, 112 int32_t stride, const InitializationOptions &opts, bool useCustomFormat); 113 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const uint32_t *colors, uint32_t colorLength, 114 BUILD_PARAM &info, const InitializationOptions &opts, int &errorCode); 115 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(const InitializationOptions &opts); 116 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const InitializationOptions &opts); 117 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const Rect &srcRect, 118 const InitializationOptions &opts); 119 NATIVEEXPORT static std::unique_ptr<PixelMap> Create(PixelMap &source, const Rect &srcRect, 120 const InitializationOptions &opts, int32_t &errorCode); 121 122 NATIVEEXPORT virtual uint32_t SetImageInfo(ImageInfo &info); 123 NATIVEEXPORT virtual uint32_t SetImageInfo(ImageInfo &info, bool isReused); 124 NATIVEEXPORT virtual const uint8_t *GetPixel(int32_t x, int32_t y); 125 NATIVEEXPORT virtual const uint8_t *GetPixel8(int32_t x, int32_t y); 126 NATIVEEXPORT virtual const uint16_t *GetPixel16(int32_t x, int32_t y); 127 NATIVEEXPORT virtual const uint32_t *GetPixel32(int32_t x, int32_t y); 128 NATIVEEXPORT virtual bool GetARGB32Color(int32_t x, int32_t y, uint32_t &color); 129 NATIVEEXPORT virtual void SetPixelsAddr(void *addr, void *context, uint32_t size, AllocatorType type, 130 CustomFreePixelMap func); 131 NATIVEEXPORT virtual int32_t GetPixelBytes(); 132 NATIVEEXPORT virtual int32_t GetRowBytes(); 133 NATIVEEXPORT virtual int32_t GetByteCount(); 134 NATIVEEXPORT virtual int32_t GetWidth(); 135 NATIVEEXPORT virtual int32_t GetHeight(); GetAstcRealSize(Size & size)136 NATIVEEXPORT void GetAstcRealSize(Size &size) 137 { 138 size = astcrealSize_; 139 } SetAstcRealSize(Size size)140 NATIVEEXPORT void SetAstcRealSize(Size size) 141 { 142 astcrealSize_ = size; 143 } 144 NATIVEEXPORT void GetTransformData(TransformData &transformData); 145 NATIVEEXPORT void SetTransformData(TransformData transformData); 146 NATIVEEXPORT virtual int32_t GetBaseDensity(); 147 NATIVEEXPORT virtual void scale(float xAxis, float yAxis); 148 NATIVEEXPORT virtual void scale(float xAxis, float yAxis, const AntiAliasingOption &option); 149 NATIVEEXPORT virtual bool resize(float xAxis, float yAxis); 150 NATIVEEXPORT virtual void translate(float xAxis, float yAxis); 151 NATIVEEXPORT virtual void rotate(float degrees); 152 NATIVEEXPORT virtual void flip(bool xAxis, bool yAxis); 153 NATIVEEXPORT virtual uint32_t crop(const Rect &rect); 154 NATIVEEXPORT virtual void GetImageInfo(ImageInfo &imageInfo); 155 NATIVEEXPORT virtual PixelFormat GetPixelFormat(); 156 NATIVEEXPORT virtual ColorSpace GetColorSpace(); 157 NATIVEEXPORT virtual AlphaType GetAlphaType(); 158 NATIVEEXPORT virtual uint32_t SetAlpha(const float percent); 159 NATIVEEXPORT virtual const uint8_t *GetPixels(); 160 NATIVEEXPORT virtual uint8_t GetARGB32ColorA(uint32_t color); 161 NATIVEEXPORT virtual uint8_t GetARGB32ColorR(uint32_t color); 162 NATIVEEXPORT virtual uint8_t GetARGB32ColorG(uint32_t color); 163 NATIVEEXPORT virtual uint8_t GetARGB32ColorB(uint32_t color); 164 // Config the pixel map parameter 165 NATIVEEXPORT virtual bool IsSameImage(const PixelMap &other); 166 NATIVEEXPORT virtual uint32_t ReadPixels(const uint64_t &bufferSize, const uint32_t &offset, const uint32_t &stride, 167 const Rect ®ion, uint8_t *dst); 168 NATIVEEXPORT virtual uint32_t ReadPixels(const uint64_t &bufferSize, uint8_t *dst); 169 NATIVEEXPORT virtual uint32_t ReadARGBPixels(const uint64_t &bufferSize, uint8_t *dst); 170 NATIVEEXPORT virtual uint32_t ReadPixel(const Position &pos, uint32_t &dst); 171 NATIVEEXPORT virtual uint32_t ResetConfig(const Size &size, const PixelFormat &format); 172 NATIVEEXPORT virtual bool SetAlphaType(const AlphaType &alphaType); 173 NATIVEEXPORT virtual uint32_t WritePixel(const Position &pos, const uint32_t &color); 174 NATIVEEXPORT virtual uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize, const uint32_t &offset, 175 const uint32_t &stride, const Rect ®ion); 176 NATIVEEXPORT virtual uint32_t WritePixels(const uint8_t *source, const uint64_t &bufferSize); 177 NATIVEEXPORT virtual bool WritePixels(const uint32_t &color); 178 NATIVEEXPORT virtual void FreePixelMap(); 179 NATIVEEXPORT bool IsStrideAlignment(); 180 NATIVEEXPORT virtual AllocatorType GetAllocatorType(); 181 NATIVEEXPORT virtual void *GetFd() const; 182 NATIVEEXPORT virtual void SetFreePixelMapProc(CustomFreePixelMap func); 183 NATIVEEXPORT virtual void SetTransformered(bool isTransformered); 184 NATIVEEXPORT uint32_t ConvertAlphaFormat(PixelMap &wPixelMap, const bool isPremul); SetPixelMapError(uint32_t code,std::string info)185 NATIVEEXPORT void SetPixelMapError(uint32_t code, std::string info) 186 { 187 errorCode = code; 188 errorInfo = info; 189 } 190 ConstructPixelMapError(PIXEL_MAP_ERR & err,uint32_t code,std::string info)191 NATIVEEXPORT static void ConstructPixelMapError(PIXEL_MAP_ERR &err, uint32_t code, std::string info) 192 { 193 err.errorCode = code; 194 err.errorInfo = info; 195 } 196 197 NATIVEEXPORT virtual void SetRowStride(uint32_t stride); GetRowStride()198 NATIVEEXPORT virtual int32_t GetRowStride() 199 { 200 return rowStride_; 201 } GetCapacity()202 NATIVEEXPORT virtual uint32_t GetCapacity() 203 { 204 return pixelsSize_; 205 } 206 IsEditable()207 NATIVEEXPORT virtual bool IsEditable() 208 { 209 return editable_; 210 } 211 IsTransformered()212 NATIVEEXPORT virtual bool IsTransformered() 213 { 214 return isTransformered_; 215 } 216 217 // judgement whether create pixelmap use source as result IsSourceAsResponse()218 NATIVEEXPORT virtual bool IsSourceAsResponse() 219 { 220 return useSourceAsResponse_; 221 } 222 GetWritablePixels()223 NATIVEEXPORT virtual void *GetWritablePixels() const 224 { 225 return static_cast<void *>(data_); 226 } 227 GetUniqueId()228 NATIVEEXPORT virtual uint32_t GetUniqueId() const 229 { 230 return uniqueId_; 231 } 232 233 NATIVEEXPORT virtual bool Marshalling(Parcel &data) const override; 234 NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &data); 235 NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &parcel, PIXEL_MAP_ERR &error); 236 NATIVEEXPORT virtual bool EncodeTlv(std::vector<uint8_t> &buff) const; 237 NATIVEEXPORT static PixelMap *DecodeTlv(std::vector<uint8_t> &buff); SetImageYUVInfo(YUVDataInfo & yuvinfo)238 NATIVEEXPORT virtual void SetImageYUVInfo(YUVDataInfo &yuvinfo) 239 { 240 yuvDataInfo_ = yuvinfo; 241 } 242 NATIVEEXPORT virtual void AssignYuvDataOnType(PixelFormat format, int32_t width, int32_t height); 243 NATIVEEXPORT virtual void UpdateYUVDataInfo(PixelFormat format, int32_t width, int32_t height, 244 YUVStrideInfo &strides); GetImageYUVInfo(YUVDataInfo & yuvInfo)245 NATIVEEXPORT virtual void GetImageYUVInfo(YUVDataInfo &yuvInfo) const 246 { 247 yuvInfo = yuvDataInfo_; 248 } 249 #ifdef IMAGE_COLORSPACE_FLAG 250 // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----begin---- 251 NATIVEEXPORT void InnerSetColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace, bool direct = false); 252 NATIVEEXPORT OHOS::ColorManager::ColorSpace InnerGetGrColorSpace(); InnerGetGrColorSpacePtr()253 NATIVEEXPORT std::shared_ptr<OHOS::ColorManager::ColorSpace> InnerGetGrColorSpacePtr() 254 { 255 return grColorSpace_; 256 } 257 NATIVEEXPORT virtual uint32_t ApplyColorSpace(const OHOS::ColorManager::ColorSpace &grColorSpace); 258 // -------[inner api for ImageSource/ImagePacker codec] it will get a colorspace object pointer----end------- 259 #endif 260 261 #ifdef IMAGE_PURGEABLE_PIXELMAP IsPurgeable()262 NATIVEEXPORT bool IsPurgeable() const 263 { 264 return purgeableMemPtr_ != nullptr; 265 } 266 GetPurgeableMemPtr()267 NATIVEEXPORT std::shared_ptr<PurgeableMem::PurgeableMemBase> GetPurgeableMemPtr() const 268 { 269 return purgeableMemPtr_; 270 } 271 SetPurgeableMemPtr(std::shared_ptr<PurgeableMem::PurgeableMemBase> pmPtr)272 NATIVEEXPORT void SetPurgeableMemPtr(std::shared_ptr<PurgeableMem::PurgeableMemBase> pmPtr) 273 { 274 purgeableMemPtr_ = pmPtr; 275 } 276 #endif 277 IsAstc()278 NATIVEEXPORT bool IsAstc() 279 { 280 return isAstc_; 281 } 282 SetAstc(bool isAstc)283 NATIVEEXPORT void SetAstc(bool isAstc) 284 { 285 isAstc_ = isAstc; 286 } 287 GetExifMetadata()288 NATIVEEXPORT std::shared_ptr<ExifMetadata> GetExifMetadata() 289 { 290 return exifMetadata_; 291 } 292 SetExifMetadata(std::shared_ptr<ExifMetadata> & ptr)293 NATIVEEXPORT void SetExifMetadata(std::shared_ptr<ExifMetadata> &ptr) 294 { 295 exifMetadata_ = ptr; 296 } 297 GetHdrMetadata()298 NATIVEEXPORT std::shared_ptr<HdrMetadata> GetHdrMetadata() 299 { 300 return hdrMetadata_; 301 } 302 SetHdrMetadata(const std::shared_ptr<HdrMetadata> & metadata)303 NATIVEEXPORT void SetHdrMetadata(const std::shared_ptr<HdrMetadata> &metadata) 304 { 305 hdrMetadata_ = metadata; 306 } 307 GetHdrType()308 NATIVEEXPORT ImageHdrType GetHdrType() 309 { 310 return hdrType_; 311 } 312 SetHdrType(ImageHdrType hdrType)313 NATIVEEXPORT void SetHdrType(ImageHdrType hdrType) 314 { 315 hdrType_ = hdrType; 316 } 317 318 NATIVEEXPORT uint32_t GetImagePropertyInt(const std::string &key, int32_t &value); 319 NATIVEEXPORT uint32_t GetImagePropertyString(const std::string &key, std::string &value); 320 NATIVEEXPORT uint32_t ModifyImageProperty(const std::string &key, const std::string &value); 321 NATIVEEXPORT uint32_t SetMemoryName(std::string pixelMapName); 322 323 NATIVEEXPORT bool IsHdr(); 324 NATIVEEXPORT uint32_t ToSdr(); 325 // format support rgba8888, nv12, nv21. The default value is rgba8888 326 // If toSRGB is false, pixelmap will be converted to display_p3 327 NATIVEEXPORT uint32_t ToSdr(PixelFormat format, bool toSRGB); 328 // use for hdr pixelmap, If isSRGB is false, the colorspace is p3 when converting to SDR. 329 NATIVEEXPORT void SetToSdrColorSpaceIsSRGB(bool isSRGB); 330 NATIVEEXPORT bool GetToSdrColorSpaceIsSRGB(); 331 SetAllocatorType(AllocatorType allocatorType)332 NATIVEEXPORT void SetAllocatorType(AllocatorType allocatorType) 333 { 334 allocatorType_ = allocatorType; 335 } 336 337 // unmap方案, 减少RenderService内存占用 338 NATIVEEXPORT bool UnMap(); 339 NATIVEEXPORT bool ReMap(); IsUnMap()340 NATIVEEXPORT bool IsUnMap() 341 { 342 std::lock_guard<std::mutex> lock(*unmapMutex_); 343 return isUnMap_; 344 } IncreaseUseCount()345 NATIVEEXPORT void IncreaseUseCount() 346 { 347 std::lock_guard<std::mutex> lock(*unmapMutex_); 348 useCount_ += 1; 349 } DecreaseUseCount()350 NATIVEEXPORT void DecreaseUseCount() 351 { 352 std::lock_guard<std::mutex> lock(*unmapMutex_); 353 if (useCount_ > 0) { 354 useCount_ -= 1; 355 } 356 } ResetUseCount()357 NATIVEEXPORT void ResetUseCount() 358 { 359 std::lock_guard<std::mutex> lock(*unmapMutex_); 360 useCount_ = 0; 361 } GetUseCount()362 NATIVEEXPORT uint64_t GetUseCount() 363 { 364 std::lock_guard<std::mutex> lock(*unmapMutex_); 365 return useCount_; 366 } GetUnMapCount()367 NATIVEEXPORT uint64_t GetUnMapCount() 368 { 369 std::lock_guard<std::mutex> lock(*unmapMutex_); 370 return unMapCount_; 371 } 372 373 static int32_t GetRGBxRowDataSize(const ImageInfo& info); 374 static int32_t GetRGBxByteCount(const ImageInfo& info); 375 static int32_t GetYUVByteCount(const ImageInfo& info); 376 static int32_t GetAllocatedByteCount(const ImageInfo& info); setAllocatorType(AllocatorType allocatorType)377 NATIVEEXPORT void setAllocatorType(AllocatorType allocatorType) 378 { 379 allocatorType_ = allocatorType; 380 } 381 382 protected: 383 static constexpr uint8_t TLV_VARINT_BITS = 7; 384 static constexpr uint8_t TLV_VARINT_MASK = 0x7F; 385 static constexpr uint8_t TLV_VARINT_MORE = 0x80; 386 static constexpr uint8_t TLV_END = 0x00; 387 static constexpr uint8_t TLV_IMAGE_WIDTH = 0x01; 388 static constexpr uint8_t TLV_IMAGE_HEIGHT = 0x02; 389 static constexpr uint8_t TLV_IMAGE_PIXELFORMAT = 0x03; 390 static constexpr uint8_t TLV_IMAGE_COLORSPACE = 0x04; 391 static constexpr uint8_t TLV_IMAGE_ALPHATYPE = 0x05; 392 static constexpr uint8_t TLV_IMAGE_BASEDENSITY = 0x06; 393 static constexpr uint8_t TLV_IMAGE_ALLOCATORTYPE = 0x07; 394 static constexpr uint8_t TLV_IMAGE_DATA = 0x08; 395 static constexpr size_t MAX_IMAGEDATA_SIZE = 128 * 1024 * 1024; // 128M 396 static constexpr size_t MIN_IMAGEDATA_SIZE = 32 * 1024; // 32k 397 friend class ImageSource; 398 static bool ALPHA8ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 399 static bool RGB565ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 400 static bool ARGB8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 401 static bool RGBA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 402 static bool BGRA8888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 403 static bool RGB888ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); 404 static bool CheckParams(const uint32_t *colors, uint32_t colorLength, int32_t offset, int32_t stride, 405 const InitializationOptions &opts); 406 static void UpdatePixelsAlpha(const AlphaType &alphaType, const PixelFormat &pixelFormat, uint8_t *dstPixels, 407 PixelMap &dstPixelMap); 408 static void InitDstImageInfo(const InitializationOptions &opts, const ImageInfo &srcImageInfo, 409 ImageInfo &dstImageInfo); 410 static bool CopyPixMapToDst(PixelMap &source, void* &dstPixels, int &fd, uint32_t bufferSize); 411 static bool CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap, int32_t &error); 412 static bool CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap); 413 static bool SourceCropAndConvert(PixelMap &source, const ImageInfo &srcImageInfo, const ImageInfo &dstImageInfo, 414 const Rect &srcRect, PixelMap &dstPixelMap); 415 static bool IsSameSize(const Size &src, const Size &dst); 416 static bool ScalePixelMap(const Size &targetSize, const Size &dstSize, const ScaleMode &scaleMode, 417 PixelMap &dstPixelMap); 418 static bool IsYuvFormat(PixelFormat format); 419 bool GetPixelFormatDetail(const PixelFormat format); 420 uint32_t CheckAlphaFormatInput(PixelMap &wPixelMap, const bool isPremul); 421 bool CheckPixelsInput(const uint8_t *dst, const uint64_t &bufferSize, const uint32_t &offset, 422 const uint32_t &stride, const Rect ®ion); 423 void ReleaseSharedMemory(void *addr, void *context, uint32_t size); 424 static void ReleaseBuffer(AllocatorType allocatorType, int fd, uint64_t dataSize, void **buffer); 425 static void *AllocSharedMemory(const uint64_t bufferSize, int &fd, uint32_t uniqueId); 426 bool WritePropertiesToParcel(Parcel &parcel) const; 427 bool ReadPropertiesFromParcel(Parcel &parcel, ImageInfo &imgInfo, AllocatorType &allocatorType, 428 int32_t &bufferSize, PIXEL_MAP_ERR &error); 429 bool WriteMemInfoToParcel(Parcel &parcel, const int32_t &bufferSize) const; 430 static bool ReadMemInfoFromParcel(Parcel &parcel, PixelMemInfo &pixelMemInfo, PIXEL_MAP_ERR &error); 431 bool WriteTransformDataToParcel(Parcel &parcel) const; 432 bool ReadTransformData(Parcel &parcel, PixelMap *pixelMap); 433 bool WriteAstcRealSizeToParcel(Parcel &parcel) const; 434 bool ReadAstcRealSize(Parcel &parcel, PixelMap *pixelMap); 435 bool WriteYuvDataInfoToParcel(Parcel &parcel) const; 436 bool ReadYuvDataInfoFromParcel(Parcel &parcel, PixelMap *pixelMap); 437 uint32_t SetRowDataSizeForImageInfo(ImageInfo info); SetEditable(bool editable)438 void SetEditable(bool editable) 439 { 440 editable_ = editable; 441 } 442 ResetPixelMap()443 void ResetPixelMap() 444 { 445 rowDataSize_ = 0; 446 pixelBytes_ = 0; 447 colorProc_ = nullptr; 448 } 449 CheckValidParam(int32_t x,int32_t y)450 bool CheckValidParam(int32_t x, int32_t y) 451 { 452 return isUnMap_ || (data_ == nullptr) || 453 (x >= imageInfo_.size.width) || (x < 0) || (y >= imageInfo_.size.height) || 454 (y < 0) || (pixelsSize_ < static_cast<uint64_t>(rowDataSize_) * imageInfo_.size.height) 455 ? false 456 : true; 457 } 458 459 static void ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size); 460 static bool UpdatePixelMapMemInfo(PixelMap *pixelMap, ImageInfo &imgInfo, PixelMemInfo &pixelMemInfo); 461 bool WriteImageData(Parcel &parcel, size_t size) const; 462 bool WriteAshmemDataToParcel(Parcel &parcel, size_t size) const; 463 static uint8_t *ReadImageData(Parcel &parcel, int32_t size); 464 static uint8_t *ReadHeapDataFromParcel(Parcel &parcel, int32_t bufferSize); 465 static uint8_t *ReadAshmemDataFromParcel(Parcel &parcel, int32_t bufferSize); 466 static int ReadFileDescriptor(Parcel &parcel); 467 static bool WriteFileDescriptor(Parcel &parcel, int fd); 468 static bool ReadImageInfo(Parcel &parcel, ImageInfo &imgInfo); 469 bool WriteImageInfo(Parcel &parcel) const; 470 void WriteUint8(std::vector<uint8_t> &buff, uint8_t value) const; 471 static uint8_t ReadUint8(std::vector<uint8_t> &buff, int32_t &cursor); 472 uint8_t GetVarintLen(int32_t value) const; 473 void WriteVarint(std::vector<uint8_t> &buff, int32_t value) const; 474 static int32_t ReadVarint(std::vector<uint8_t> &buff, int32_t &cursor); 475 void WriteData(std::vector<uint8_t> &buff, const uint8_t *data, 476 const int32_t &height, const int32_t &rowDataSize, const int32_t &rowStride) const; 477 static uint8_t *ReadData(std::vector<uint8_t> &buff, int32_t size, int32_t &cursor); 478 static void ReadTlvAttr(std::vector<uint8_t> &buff, ImageInfo &info, int32_t &type, int32_t &size, uint8_t **data); 479 bool DoTranslation(TransInfos &infos, const AntiAliasingOption &option = AntiAliasingOption::NONE); 480 void UpdateImageInfo(); 481 bool IsYuvFormat() const; 482 static int32_t ConvertPixelAlpha(const void *srcPixels, const int32_t srcLength, const ImageInfo &srcInfo, 483 void *dstPixels, const ImageInfo &dstInfo); 484 uint8_t *data_ = nullptr; 485 // this info SHOULD be the final info for decoded pixelmap, not the original image info 486 ImageInfo imageInfo_; 487 int32_t rowDataSize_ = 0; 488 int32_t rowStride_ = 0; 489 int32_t pixelBytes_ = 0; 490 TransColorProc colorProc_ = nullptr; 491 void *context_ = nullptr; 492 CustomFreePixelMap custFreePixelMap_ = nullptr; 493 CustomFreePixelMap freePixelMapProc_ = nullptr; 494 AllocatorType allocatorType_ = AllocatorType::SHARE_MEM_ALLOC; 495 uint32_t pixelsSize_ = 0; 496 bool editable_ = false; 497 bool useSourceAsResponse_ = false; 498 bool isTransformered_ = false; 499 std::shared_ptr<std::mutex> transformMutex_ = std::make_shared<std::mutex>(); 500 501 // only used by rosen backend 502 uint32_t uniqueId_ = 0; 503 bool isAstc_ = false; 504 TransformData transformData_ = {1, 1, 0, 0, 0, 0, 0, 0, 0, false, false}; 505 Size astcrealSize_; 506 std::shared_ptr<HdrMetadata> hdrMetadata_ = nullptr; 507 ImageHdrType hdrType_; 508 509 #ifdef IMAGE_COLORSPACE_FLAG 510 std::shared_ptr<OHOS::ColorManager::ColorSpace> grColorSpace_ = nullptr; 511 #else 512 std::shared_ptr<uint8_t> grColorSpace_ = nullptr; 513 #endif 514 515 #ifdef IMAGE_PURGEABLE_PIXELMAP 516 std::shared_ptr<PurgeableMem::PurgeableMemBase> purgeableMemPtr_ = nullptr; 517 #else 518 std::shared_ptr<uint8_t> purgeableMemPtr_ = nullptr; 519 #endif 520 YUVDataInfo yuvDataInfo_; 521 std::shared_ptr<ExifMetadata> exifMetadata_ = nullptr; 522 std::shared_ptr<std::mutex> metadataMutex_ = std::make_shared<std::mutex>(); 523 std::shared_ptr<std::mutex> translationMutex_ = std::make_shared<std::mutex>(); 524 bool toSdrColorIsSRGB_ = false; 525 private: 526 // unmap方案, 减少RenderService内存占用 527 bool isUnMap_ = false; 528 uint64_t useCount_ = 0ULL; 529 uint64_t unMapCount_ = 0; 530 std::shared_ptr<std::mutex> unmapMutex_ = std::make_shared<std::mutex>(); 531 }; 532 } // namespace Media 533 } // namespace OHOS 534 535 #endif // INTERFACES_INNERKITS_INCLUDE_PIXEL_MAP_H_ 536