diff --git a/frameworks/innerkitsimpl/common/src/pixel_map.cpp b/frameworks/innerkitsimpl/common/src/pixel_map.cpp index a537f99f8db6ee7a73ad3b7dd485db946eec442d..b1d08ffed2333be754105ffcfb134d34d5ebc088 100644 --- a/frameworks/innerkitsimpl/common/src/pixel_map.cpp +++ b/frameworks/innerkitsimpl/common/src/pixel_map.cpp @@ -22,12 +22,11 @@ #include "media_errors.h" #include "pixel_convert_adapter.h" #include "pixel_map_utils.h" -#include "pixel_map_parcel.h" #include "post_proc.h" #include "parcel.h" -#include "ipc_file_descriptor.h" #ifndef _WIN32 #include "securec.h" +#include "ipc_file_descriptor.h" #else #include "memory.h" #endif @@ -48,6 +47,9 @@ constexpr uint8_t BGRA_ALPHA_INDEX = 3; constexpr uint8_t BGRA_BYTES = 4; constexpr uint8_t PER_PIXEL_LEN = 1; +constexpr uint8_t FILL_NUMBER = 3; +constexpr uint8_t ALIGN_NUMBER = 4; + PixelMap::~PixelMap() { FreePixelMap(); @@ -111,12 +113,14 @@ void PixelMap::SetPixelsAddr(void *addr, void *context, uint32_t size, Allocator unique_ptr PixelMap::Create(const uint32_t *colors, uint32_t colorLength, const InitializationOptions &opts) { + HiLog::Info(LABEL, "PixelMap::Create1 enter"); return Create(colors, colorLength, 0, opts.size.width, opts); } unique_ptr PixelMap::Create(const uint32_t *colors, uint32_t colorLength, int32_t offset, int32_t stride, const InitializationOptions &opts) { + HiLog::Info(LABEL, "PixelMap::Create2 enter"); if (!CheckParams(colors, colorLength, offset, stride, opts)) { return nullptr; } @@ -194,6 +198,7 @@ bool PixelMap::CheckParams(const uint32_t *colors, uint32_t colorLength, int32_t unique_ptr PixelMap::Create(const InitializationOptions &opts) { + HiLog::Info(LABEL, "PixelMap::Create3 enter"); unique_ptr dstPixelMap = make_unique(); if (dstPixelMap == nullptr) { HiLog::Error(LABEL, "create pixelMap pointer fail"); @@ -209,6 +214,10 @@ unique_ptr PixelMap::Create(const InitializationOptions &opts) return nullptr; } uint32_t bufferSize = dstPixelMap->GetByteCount(); + if (bufferSize <= 0) { + HiLog::Error(LABEL, "calloc parameter bufferSize:[%{public}d] error.", bufferSize); + return nullptr; + } uint8_t *dstPixels = static_cast(calloc(bufferSize, 1)); if (dstPixels == nullptr) { HiLog::Error(LABEL, "allocate memory size %{public}u fail", bufferSize); @@ -245,12 +254,14 @@ void PixelMap::UpdatePixelsAlpha(const AlphaType &alphaType, const PixelFormat & unique_ptr PixelMap::Create(PixelMap &source, const InitializationOptions &opts) { + HiLog::Info(LABEL, "PixelMap::Create4 enter"); Rect rect; return Create(source, rect, opts); } unique_ptr PixelMap::Create(PixelMap &source, const Rect &srcRect, const InitializationOptions &opts) { + HiLog::Info(LABEL, "PixelMap::Create5 enter"); ImageInfo srcImageInfo; source.GetImageInfo(srcImageInfo); PostProc postProc; @@ -305,6 +316,10 @@ bool PixelMap::SourceCropAndConvert(PixelMap &source, const ImageInfo &srcImageI const Rect &srcRect, PixelMap &dstPixelMap) { uint32_t bufferSize = dstPixelMap.GetByteCount(); + if (bufferSize <= 0) { + HiLog::Error(LABEL, "malloc parameter bufferSize:[%{public}d] error.", bufferSize); + return false; + } void *dstPixels = malloc(bufferSize); if (dstPixels == nullptr) { HiLog::Error(LABEL, "allocate memory size %{public}u fail", bufferSize); @@ -365,10 +380,14 @@ void PixelMap::InitDstImageInfo(const InitializationOptions &opts, const ImageIn bool PixelMap::CopyPixelMap(PixelMap &source, PixelMap &dstPixelMap) { uint32_t bufferSize = source.GetByteCount(); - if (bufferSize == 0 || source.GetPixels() == nullptr) { + if (source.GetPixels() == nullptr) { HiLog::Error(LABEL, "source pixelMap data invalid"); return false; } + if (bufferSize <= 0) { + HiLog::Error(LABEL, "malloc parameter bufferSize:[%{public}d] error.", bufferSize); + return false; + } uint8_t *dstPixels = static_cast(malloc(bufferSize)); if (dstPixels == nullptr) { HiLog::Error(LABEL, "allocate memory size %{public}u fail", bufferSize); @@ -466,7 +485,7 @@ uint32_t PixelMap::SetImageInfo(ImageInfo &info, bool isReused) return ERR_IMAGE_TOO_LARGE; } if (info.pixelFormat == PixelFormat::ALPHA_8) { - rowDataSize_ = pixelBytes_ * ((info.size.width + 3) / 4 * 4); + rowDataSize_ = pixelBytes_ * ((info.size.width + FILL_NUMBER) / ALIGN_NUMBER * ALIGN_NUMBER); HiLog::Info(LABEL, "ALPHA_8 rowDataSize_ %{public}d.", rowDataSize_); } else { rowDataSize_ = pixelBytes_ * info.size.width; @@ -639,6 +658,7 @@ int32_t PixelMap::GetRowBytes() int32_t PixelMap::GetByteCount() { + HiLog::Debug(LABEL, "GetByteCount"); return rowDataSize_ * imageInfo_.size.height; } @@ -705,15 +725,18 @@ uint8_t PixelMap::GetARGB32ColorB(uint32_t color) bool PixelMap::IsSameImage(const PixelMap &other) { if (data_ == nullptr || other.data_ == nullptr) { + HiLog::Error(LABEL, "IsSameImage data_ is nullptr."); return false; } if (imageInfo_.size.width != other.imageInfo_.size.width || imageInfo_.size.height != other.imageInfo_.size.height || imageInfo_.pixelFormat != other.imageInfo_.pixelFormat || imageInfo_.alphaType != other.imageInfo_.alphaType) { + HiLog::Error(LABEL, "IsSameImage imageInfo check not OK."); return false; } uint64_t size = static_cast(rowDataSize_) * imageInfo_.size.height; if (memcmp(data_, other.data_, size) != 0) { + HiLog::Error(LABEL, "IsSameImage mmemcmp check not OK."); return false; } return true; @@ -1011,7 +1034,136 @@ void *PixelMap::GetFd() const return context_; } -bool PixelMap::WriteFileDescriptor(Parcel &data, int fd) const +void PixelMap::ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size) +{ + if (allocType == AllocatorType::SHARE_MEM_ALLOC) { +#if !defined(_WIN32) && !defined(_APPLE) + int *fd = static_cast(context); + if (addr != nullptr) { + ::munmap(addr, size); + } + if (fd != nullptr) { + ::close(*fd); + delete fd; + } +#endif + } else if (allocType == AllocatorType::HEAP_ALLOC) { + if (addr != nullptr) { + free(addr); + addr = nullptr; + } + } +} + +bool PixelMap::WriteImageData(Parcel &parcel, const void *data, size_t size) +{ + if (data == nullptr || size > MAX_IMAGEDATA_SIZE) { + return false; + } + + if (!parcel.WriteInt32(size)) { + return false; + } + if (size <= MIN_IMAGEDATA_SIZE) { + return parcel.WriteUnpadBuffer(data, size); + } +#if !defined(_WIN32) && !defined(_APPLE) + int fd = AshmemCreate("Parcel ImageData", size); + HiLog::Info(LABEL, "AshmemCreate:[%{public}d].", fd); + if (fd < 0) { + return false; + } + + int result = AshmemSetProt(fd, PROT_READ | PROT_WRITE); + HiLog::Info(LABEL, "AshmemSetProt:[%{public}d].", result); + if (result < 0) { + return false; + } + void *ptr = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + + if (ptr == MAP_FAILED) { + return false; + } + HiLog::Info(LABEL, "mmap success"); + + if (memcpy_s(ptr, size, data, size) != EOK) { + ::munmap(ptr, size); + HiLog::Error(LABEL, "WriteImageData memcpy_s error"); + return false; + } + + if (!WriteFileDescriptor(parcel, fd)) { + ::munmap(ptr, size); + HiLog::Error(LABEL, "WriteImageData WriteFileDescriptor error"); + return false; + } + HiLog::Debug(LABEL, "WriteImageData WriteFileDescriptor success"); + HiLog::Debug(LABEL, "WriteImageData End"); +#endif + return true; +} + +uint8_t *PixelMap::ReadImageData(Parcel &parcel, int32_t bufferSize) +{ + uint8_t *base = nullptr; + int fd = -1; + AllocatorType allocType = AllocatorType::HEAP_ALLOC; + + if (static_cast(bufferSize) <= MIN_IMAGEDATA_SIZE) { + const uint8_t *ptr = parcel.ReadUnpadBuffer(bufferSize); + if (bufferSize <= 0) { + HiLog::Error(LABEL, "malloc parameter bufferSize:[%{public}d] error.", bufferSize); + return nullptr; + } + base = static_cast(malloc(bufferSize)); + if (base == nullptr) { + HiLog::Error(LABEL, "alloc output pixel memory size:[%{public}d] error.", bufferSize); + return nullptr; + } + if (memcpy_s(base, bufferSize, ptr, bufferSize) != 0) { + free(base); + base = nullptr; + HiLog::Error(LABEL, "memcpy pixel data size:[%{public}d] error.", bufferSize); + return nullptr; + } + } else { +#if !defined(_WIN32) && !defined(_APPLE) + allocType = AllocatorType::SHARE_MEM_ALLOC; + fd = ReadFileDescriptor(parcel); + if (fd < 0) { + HiLog::Error(LABEL, "read fd :[%{public}d] error", fd); + return nullptr; + } + void *ptr = ::mmap(nullptr, bufferSize, PROT_READ, MAP_SHARED, fd, 0); + if (ptr == MAP_FAILED) { + // do not close fd here. fd will be closed in FileDescriptor, ::close(fd) + HiLog::Error(LABEL, "mmap error"); + return nullptr; + } + if (bufferSize <= 0) { + HiLog::Error(LABEL, "malloc parameter bufferSize:[%{public}d] error.", bufferSize); + return nullptr; + } + base = static_cast(malloc(bufferSize)); + if (base == nullptr) { + HiLog::Error(LABEL, "alloc output pixel memory size:[%{public}d] error.", bufferSize); + return nullptr; + } + if (memcpy_s(base, bufferSize, ptr, bufferSize) != 0) { + free(base); + base = nullptr; + HiLog::Error(LABEL, "memcpy pixel data size:[%{public}d] error.", bufferSize); + return nullptr; + } + + ReleaseMemory(allocType, ptr, &fd, bufferSize); +#endif + } + + return base; +} + +bool PixelMap::WriteFileDescriptor(Parcel &parcel, int fd) { if (fd < 0) { return false; @@ -1021,12 +1173,12 @@ bool PixelMap::WriteFileDescriptor(Parcel &data, int fd) const return false; } sptr descriptor = new IPCFileDescriptor(dupFd); - return data.WriteObject(descriptor); + return parcel.WriteObject(descriptor); } -int PixelMap::ReadFileDescriptor(Parcel &data) +int PixelMap::ReadFileDescriptor(Parcel &parcel) { - sptr descriptor = data.ReadObject(); + sptr descriptor = parcel.ReadObject(); if (descriptor == nullptr) { return -1; } @@ -1037,66 +1189,76 @@ int PixelMap::ReadFileDescriptor(Parcel &data) return dup(fd); } -bool PixelMap::Marshalling(Parcel &data) const +bool PixelMap::WriteImageInfo(Parcel &parcel) const { - int32_t PIXEL_MAP_INFO_MAX_LENGTH = 128; - int32_t bufferSize = rowDataSize_ * imageInfo_.size.height; - - if (static_cast(bufferSize + PIXEL_MAP_INFO_MAX_LENGTH) > data.GetDataCapacity() && - !data.SetDataCapacity(bufferSize + PIXEL_MAP_INFO_MAX_LENGTH)) { - HiLog::Error(LABEL, "set parcel max capacity:[%{public}d] failed.", bufferSize + PIXEL_MAP_INFO_MAX_LENGTH); + if (!parcel.WriteInt32(imageInfo_.size.width)) { + HiLog::Error(LABEL, "write image info width:[%{public}d] to parcel failed.", imageInfo_.size.width); return false; } - if (!data.WriteInt32(imageInfo_.size.width)) { - HiLog::Error(LABEL, "write pixel map width:[%{public}d] to parcel failed.", imageInfo_.size.width); + if (!parcel.WriteInt32(imageInfo_.size.height)) { + HiLog::Error(LABEL, "write image info height:[%{public}d] to parcel failed.", imageInfo_.size.height); return false; } - if (!data.WriteInt32(imageInfo_.size.height)) { - HiLog::Error(LABEL, "write pixel map height:[%{public}d] to parcel failed.", imageInfo_.size.height); + if (!parcel.WriteInt32(static_cast(imageInfo_.pixelFormat))) { + HiLog::Error(LABEL, "write image info pixel format:[%{public}d] to parcel failed.", imageInfo_.pixelFormat); return false; } - if (!data.WriteInt32(static_cast(imageInfo_.pixelFormat))) { - HiLog::Error(LABEL, "write pixel map pixel format:[%{public}d] to parcel failed.", imageInfo_.pixelFormat); + if (!parcel.WriteInt32(static_cast(imageInfo_.colorSpace))) { + HiLog::Error(LABEL, "write image info color space:[%{public}d] to parcel failed.", imageInfo_.colorSpace); return false; } - if (!data.WriteInt32(static_cast(imageInfo_.colorSpace))) { - HiLog::Error(LABEL, "write pixel map color space:[%{public}d] to parcel failed.", imageInfo_.colorSpace); + if (!parcel.WriteInt32(static_cast(imageInfo_.alphaType))) { + HiLog::Error(LABEL, "write image info alpha type:[%{public}d] to parcel failed.", imageInfo_.alphaType); return false; } - if (!data.WriteInt32(static_cast(imageInfo_.alphaType))) { - HiLog::Error(LABEL, "write pixel map alpha type:[%{public}d] to parcel failed.", imageInfo_.alphaType); + if (!parcel.WriteInt32(imageInfo_.baseDensity)) { + HiLog::Error(LABEL, "write image info base density:[%{public}d] to parcel failed.", imageInfo_.baseDensity); return false; } - if (!data.WriteInt32(imageInfo_.baseDensity)) { - HiLog::Error(LABEL, "write pixel map base density:[%{public}d] to parcel failed.", imageInfo_.baseDensity); + return true; +} + +bool PixelMap::Marshalling(Parcel &parcel) const +{ + int32_t PIXEL_MAP_INFO_MAX_LENGTH = 128; + int32_t bufferSize = rowDataSize_ * imageInfo_.size.height; + + if (static_cast(bufferSize + PIXEL_MAP_INFO_MAX_LENGTH) > parcel.GetDataCapacity() && + !parcel.SetDataCapacity(bufferSize + PIXEL_MAP_INFO_MAX_LENGTH)) { + HiLog::Error(LABEL, "set parcel max capacity:[%{public}d] failed.", bufferSize + PIXEL_MAP_INFO_MAX_LENGTH); return false; } - if (!data.WriteInt32(bufferSize)) { - HiLog::Error(LABEL, "write pixel map buffer size:[%{public}d] to parcel failed.", bufferSize); + + if (!WriteImageInfo(parcel)) { + HiLog::Error(LABEL, "write image info to parcel failed."); return false; } - if (!data.WriteInt32(static_cast(allocatorType_))) { + + if (!parcel.WriteInt32(static_cast(allocatorType_))) { HiLog::Error(LABEL, "write pixel map allocator type:[%{public}d] to parcel failed.", allocatorType_); return false; } if (allocatorType_ == AllocatorType::SHARE_MEM_ALLOC) { +#if !defined(_WIN32) && !defined(_APPLE) int *fd = static_cast(context_); if (*fd < 0) { HiLog::Error(LABEL, "write pixel map failed, fd < 0."); return false; } - if (!WriteFileDescriptor(data, *fd)) { + + if (!WriteFileDescriptor(parcel, *fd)) { HiLog::Error(LABEL, "write pixel map fd:[%{public}d] to parcel failed.", *fd); return false; } +#endif } else { const uint8_t *addr = data_; if (addr == nullptr) { HiLog::Error(LABEL, "write to parcel failed, pixel memory is null."); return false; } - if (!data.WriteBuffer(addr, bufferSize)) { + if (!WriteImageData(parcel, addr, bufferSize)) { HiLog::Error(LABEL, "write pixel map buffer to parcel failed."); return false; } @@ -1104,7 +1266,23 @@ bool PixelMap::Marshalling(Parcel &data) const return true; } -PixelMap *PixelMap::Unmarshalling(Parcel &data) +bool PixelMap::ReadImageInfo(Parcel &parcel, ImageInfo &imgInfo) +{ + imgInfo.size.width = parcel.ReadInt32(); + HiLog::Debug(LABEL, "read pixel map width:[%{public}d] to parcel.", imgInfo.size.width); + imgInfo.size.height = parcel.ReadInt32(); + HiLog::Debug(LABEL, "read pixel map height:[%{public}d] to parcel.", imgInfo.size.height); + imgInfo.pixelFormat = static_cast(parcel.ReadInt32()); + HiLog::Debug(LABEL, "read pixel map pixelFormat:[%{public}d] to parcel.", imgInfo.pixelFormat); + imgInfo.colorSpace = static_cast(parcel.ReadInt32()); + HiLog::Debug(LABEL, "read pixel map colorSpace:[%{public}d] to parcel.", imgInfo.colorSpace); + imgInfo.alphaType = static_cast(parcel.ReadInt32()); + HiLog::Debug(LABEL, "read pixel map alphaType:[%{public}d] to parcel.", imgInfo.alphaType); + imgInfo.baseDensity = parcel.ReadInt32(); + return true; +} + +PixelMap *PixelMap::Unmarshalling(Parcel &parcel) { PixelMap *pixelMap = new PixelMap(); if (pixelMap == nullptr) { @@ -1113,18 +1291,18 @@ PixelMap *PixelMap::Unmarshalling(Parcel &data) } ImageInfo imgInfo; - imgInfo.size.width = data.ReadInt32(); - imgInfo.size.height = data.ReadInt32(); - imgInfo.pixelFormat = static_cast(data.ReadInt32()); - imgInfo.colorSpace = static_cast(data.ReadInt32()); - imgInfo.alphaType = static_cast(data.ReadInt32()); - imgInfo.baseDensity = data.ReadInt32(); - int32_t bufferSize = data.ReadInt32(); - AllocatorType allocType = static_cast(data.ReadInt32()); + if (!pixelMap->ReadImageInfo(parcel,imgInfo)) { + HiLog::Error(LABEL, "read imageInfo fail"); + return nullptr; + } + + AllocatorType allocType = static_cast(parcel.ReadInt32()); + int32_t bufferSize = parcel.ReadInt32(); uint8_t *base = nullptr; void *context = nullptr; if (allocType == AllocatorType::SHARE_MEM_ALLOC) { - int fd = ReadFileDescriptor(data); +#if !defined(_WIN32) && !defined(_APPLE) + int fd = ReadFileDescriptor(parcel); if (fd < 0) { HiLog::Error(LABEL, "fd < 0"); return nullptr; @@ -1145,42 +1323,18 @@ PixelMap *PixelMap::Unmarshalling(Parcel &data) } *static_cast(context) = fd; base = static_cast(ptr); +#endif } else { - const uint8_t *addr = data.ReadBuffer(bufferSize); - if (addr == nullptr) { - HiLog::Error(LABEL, "read buffer from parcel failed, read buffer addr is null"); - return nullptr; - } - base = static_cast(malloc(bufferSize)); + base = ReadImageData(parcel, bufferSize); if (base == nullptr) { - HiLog::Error(LABEL, "alloc output pixel memory size:[%{public}d] error.", bufferSize); - return nullptr; - } - if (memcpy_s(base, bufferSize, addr, bufferSize) != 0) { - free(base); - base = nullptr; - HiLog::Error(LABEL, "memcpy pixel data size:[%{public}d] error.", bufferSize); + HiLog::Error(LABEL, "get pixel memory size:[%{public}d] error.", bufferSize); return nullptr; } } uint32_t ret = pixelMap->SetImageInfo(imgInfo); if (ret != SUCCESS) { - if (allocType == AllocatorType::SHARE_MEM_ALLOC) { - int *fd = static_cast(context); - if (base != nullptr) { - ::munmap(base, bufferSize); - } - if (fd != nullptr) { - ::close(*fd); - delete fd; - } - } else if (allocType == AllocatorType::HEAP_ALLOC) { - if (base != nullptr) { - free(base); - base = nullptr; - } - } + ReleaseMemory(allocType, base, context, bufferSize); HiLog::Error(LABEL, "create pixel map from parcel failed, set image info error."); return nullptr; } diff --git a/frameworks/innerkitsimpl/test/unittest/image_pixel_map_test.cpp b/frameworks/innerkitsimpl/test/unittest/image_pixel_map_test.cpp index c9906841482da40260ca97717113a36540ed7166..c2f56d82a37e241db1372bc384df3ac94c8943e1 100644 --- a/frameworks/innerkitsimpl/test/unittest/image_pixel_map_test.cpp +++ b/frameworks/innerkitsimpl/test/unittest/image_pixel_map_test.cpp @@ -27,6 +27,8 @@ static constexpr int32_t PIXEL_MAP_TEST_HEIGHT = 3; static constexpr int32_t PIXEL_MAP_RGB565_BYTE = 2; static constexpr int32_t PIXEL_MAP_RGB888_BYTE = 3; static constexpr int32_t PIXEL_MAP_ARGB8888_BYTE = 4; +static constexpr int32_t PIXEL_MAP_BIG_TEST_WIDTH = 4 * 1024; +static constexpr int32_t PIXEL_MAP_BIG_TEST_HEIGHT = 3 * 100; class ImagePixelMapTest : public testing::Test { public: @@ -48,7 +50,13 @@ public: int32_t rowDataSize = pixelMapWidth; uint32_t bufferSize = rowDataSize * pixelMapHeight; + if (bufferSize <= 0) { + return nullptr; + } void *buffer = malloc(bufferSize); + if (buffer == nullptr) { + return nullptr; + } char *ch = (char *)buffer; for (unsigned int i = 0; i < bufferSize; i++) { *(ch++) = (char)i; @@ -58,6 +66,30 @@ public: return pixelMap; } + + std::unique_ptr ConstructBigPixmap() + { + int32_t pixelMapWidth = PIXEL_MAP_BIG_TEST_WIDTH; + int32_t pixelMapHeight = PIXEL_MAP_BIG_TEST_HEIGHT; + std::unique_ptr pixelMap = std::make_unique(); + ImageInfo info; + info.size.width = pixelMapWidth; + info.size.height = pixelMapHeight; + info.pixelFormat = PixelFormat::RGB_888; + info.colorSpace = ColorSpace::SRGB; + pixelMap->SetImageInfo(info); + + int32_t bufferSize = pixelMap->GetByteCount(); + void *buffer = malloc(bufferSize); + char *ch = (char *)buffer; + for (int32_t i = 0; i < bufferSize; i++) { + *(ch++) = 'a'; + } + + pixelMap->SetPixelsAddr(buffer, nullptr, bufferSize, AllocatorType::HEAP_ALLOC, nullptr); + + return pixelMap; + } /** * @tc.name: ImagePixelMap001 * @tc.desc: ALPHA_8 pixel format pixel map operation @@ -469,22 +501,16 @@ HWTEST_F(ImagePixelMapTest, ImagePixelMap011, TestSize.Level3) GTEST_LOG_(INFO) << "ImagePixelMapTest: ImagePixelMap011 start"; Parcel data; - std::unique_ptr pixelmap1 = ConstructPixmap(); - bool ret = pixelmap1.get()->Marshalling(data); - EXPECT_EQ(true, ret); PixelMap *pixelmap2 = PixelMap::Unmarshalling(data); - EXPECT_EQ(pixelmap1->GetHeight(), pixelmap2->GetHeight()); EXPECT_EQ(pixelmap1->GetWidth(), pixelmap2->GetWidth()); EXPECT_EQ(pixelmap1->GetPixelFormat(), pixelmap2->GetPixelFormat()); EXPECT_EQ(pixelmap1->GetColorSpace(), pixelmap2->GetColorSpace()); - EXPECT_EQ(true, pixelmap1->IsSameImage(*pixelmap2)); - GTEST_LOG_(INFO) << "ImagePixelMapTest: ImagePixelMap011 end"; } /** @@ -513,5 +539,40 @@ HWTEST_F(ImagePixelMapTest, ImagePixelMap012, TestSize.Level3) EXPECT_EQ(newPixelMap, nullptr); GTEST_LOG_(INFO) << "ImagePixelMapTest: ImagePixelMap012 end"; } + + +/** +* @tc.name: ImagePixelMap013 +* @tc.desc: test CreateFromParcel +* @tc.type: FUNC +* @tc.require: AR000FTAMO +*/ +HWTEST_F(ImagePixelMapTest, ImagePixelMap013, TestSize.Level3) +{ + GTEST_LOG_(INFO) << "ImagePixelMapTest: ImagePixelMap013 start"; + + Parcel data; + std::unique_ptr pixelmap1 = ConstructBigPixmap(); + EXPECT_NE(pixelmap1, nullptr); + GTEST_LOG_(INFO) << "ImagePixelMap013 ConstructPixmap success"; + bool ret = pixelmap1.get()->Marshalling(data); + GTEST_LOG_(INFO) << "ImagePixelMap013 Marshalling success"; + EXPECT_EQ(true, ret); + + PixelMap *pixelmap2 = PixelMap::Unmarshalling(data); + GTEST_LOG_(INFO) << "ImagePixelMap013 Unmarshalling success"; + GTEST_LOG_(INFO) << "ImagePixelMap013 pixelmap1 GetHeight :" << pixelmap1->GetHeight(); + GTEST_LOG_(INFO) << "ImagePixelMap013 pixelmap2 GetHeight :" << pixelmap2->GetHeight(); + EXPECT_EQ(pixelmap1->GetHeight(), pixelmap2->GetHeight()); + GTEST_LOG_(INFO) << "ImagePixelMap013 GetHeight success"; + EXPECT_EQ(pixelmap1->GetWidth(), pixelmap2->GetWidth()); + GTEST_LOG_(INFO) << "ImagePixelMap013 GetWidth success"; + EXPECT_EQ(pixelmap1->GetPixelFormat(), pixelmap2->GetPixelFormat()); + GTEST_LOG_(INFO) << "ImagePixelMap013 GetPixelFormat success"; + EXPECT_EQ(pixelmap1->GetColorSpace(), pixelmap2->GetColorSpace()); + GTEST_LOG_(INFO) << "ImagePixelMap013 GetColorSpace success"; + EXPECT_EQ(true, pixelmap1->IsSameImage(*pixelmap2)); + GTEST_LOG_(INFO) << "ImagePixelMapTest: ImagePixelMap013 end"; +} } // namespace Multimedia } // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/include/pixel_map.h b/interfaces/innerkits/include/pixel_map.h index 68a5a44b8b4bdb13abd023aec94a59b48f117717..270dc20e6aff46fd36a94034693115e05480f4b4 100644 --- a/interfaces/innerkits/include/pixel_map.h +++ b/interfaces/innerkits/include/pixel_map.h @@ -120,6 +120,8 @@ public: NATIVEEXPORT bool Marshalling(Parcel &data) const override; NATIVEEXPORT static PixelMap *Unmarshalling(Parcel &data); private: + static constexpr size_t MAX_IMAGEDATA_SIZE = 128 * 1024 * 1024; // 128M + static constexpr size_t MIN_IMAGEDATA_SIZE = 32 * 1024; // 32k friend class ImageSource; static bool ALPHA8ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); static bool RGB565ToARGB(const uint8_t *in, uint32_t inCount, uint32_t *out, uint32_t outCount); @@ -162,8 +164,14 @@ private: ? false : true; } - bool WriteFileDescriptor(Parcel &data, int fd) const; - static int ReadFileDescriptor(Parcel &data); + + static void ReleaseMemory(AllocatorType allocType, void *addr, void *context, uint32_t size); + static bool WriteImageData(Parcel &parcel, const void *data, size_t size); + static uint8_t *ReadImageData(Parcel &parcel, int32_t size); + static int ReadFileDescriptor(Parcel &parcel); + static bool WriteFileDescriptor(Parcel &parcel, int fd); + bool ReadImageInfo(Parcel &parcel, ImageInfo &imgInfo); + bool WriteImageInfo(Parcel &parcel) const; uint8_t *data_ = nullptr; // this info SHOULD be the final info for decoded pixelmap, not the original image info