ASTC解码接口支持指定内存类型

Signed-off-by: renhw001 <renhongwei9@huawei.com>
This commit is contained in:
renhw001 2024-10-28 10:48:35 +08:00
parent 2a77e7f640
commit 09c2283b3c
3 changed files with 50 additions and 7 deletions

View File

@ -577,7 +577,7 @@ unique_ptr<PixelMap> ImageSource::CreatePixelMapEx(uint32_t index, const DecodeO
}
}
if (isAstc_.has_value() && isAstc_.value()) {
return CreatePixelMapForASTC(errorCode, opts.fastAstc);
return CreatePixelMapForASTC(errorCode, opts);
}
#endif
@ -3171,15 +3171,16 @@ static bool FormatIsSUT(const uint8_t *fileData, size_t fileSize)
#endif
static bool ReadFileAndResoveAstc(size_t fileSize, size_t astcSize, unique_ptr<PixelAstc> &pixelAstc,
const uint8_t *sourceFilePtr)
const uint8_t *sourceFilePtr, const DecodeOptions &opts)
{
#if !(defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM))
Size desiredSize = {astcSize, 1};
MemoryData memoryData = {nullptr, astcSize, "CreatePixelMapForASTC Data", desiredSize, pixelAstc->GetPixelFormat()};
ImageInfo pixelAstcInfo;
pixelAstc->GetImageInfo(pixelAstcInfo);
AllocatorType allocatorType = IsSupportAstcZeroCopy(pixelAstcInfo.size) ?
AllocatorType::DMA_ALLOC : AllocatorType::SHARE_MEM_ALLOC;
AllocatorType allocatorType = (opts.allocatorType == AllocatorType::DEFAULT) ?
(IsSupportAstcZeroCopy(pixelAstcInfo.size) ? AllocatorType::DMA_ALLOC : AllocatorType::SHARE_MEM_ALLOC) :
opts.allocatorType;
std::unique_ptr<AbsMemory> dstMemory = MemoryManager::CreateMemory(allocatorType, memoryData);
if (dstMemory == nullptr) {
IMAGE_LOGE("ReadFileAndResoveAstc CreateMemory failed");
@ -3212,7 +3213,7 @@ static bool ReadFileAndResoveAstc(size_t fileSize, size_t astcSize, unique_ptr<P
return true;
}
unique_ptr<PixelMap> ImageSource::CreatePixelMapForASTC(uint32_t &errorCode, bool fastAstc)
unique_ptr<PixelMap> ImageSource::CreatePixelMapForASTC(uint32_t &errorCode, const DecodeOptions &opts)
#if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
{
errorCode = ERROR;
@ -3246,7 +3247,7 @@ unique_ptr<PixelMap> ImageSource::CreatePixelMapForASTC(uint32_t &errorCode, boo
#else
size_t astcSize = ImageUtils::GetAstcBytesCount(info);
#endif
if (!ReadFileAndResoveAstc(fileSize, astcSize, pixelAstc, sourceFilePtr)) {
if (!ReadFileAndResoveAstc(fileSize, astcSize, pixelAstc, sourceFilePtr, opts)) {
IMAGE_LOGE("[ImageSource] astc ReadFileAndResoveAstc failed.");
return nullptr;
}

View File

@ -693,5 +693,47 @@ HWTEST_F(PixelAstcTest, PixelAstcTest026, TestSize.Level3)
}
GTEST_LOG_(INFO) << "PixelAstcTest: PixelAstcTest026 end";
}
/**
* @tc.name: PixelAstcTest027
* @tc.desc: PixelAstc
* @tc.type: FUNC
*/
HWTEST_F(PixelAstcTest, PixelAstcTest027, TestSize.Level3)
{
GTEST_LOG_(INFO) << "PixelAstcTest: PixelAstcTest027 start";
size_t width = 256; // ASTC width
size_t height = 256; // ASTC height
// 4 means ASTC compression format is 4x4
size_t blockNum = ((width + 4 - 1) / 4) * ((height + 4 - 1) / 4);
// 16 means ASTC per block bytes and header bytes
size_t size = blockNum * 16 + 16;
uint8_t* data = (uint8_t*)malloc(size);
// 4 means blockSize
if (!GenAstcHeader(data, 4, width, height)) {
GTEST_LOG_(INFO) << "GenAstcHeader failed";
}
// 16 means header bytes
if (!ConstructAstcBody(data + 16, blockNum, ASTC_BLOCK4X4_FIT_SUT_ASTC_EXAMPLE0)) {
GTEST_LOG_(INFO) << "ConstructAstcBody failed";
}
uint32_t errorCode = 0;
SourceOptions opts;
std::unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(data, size, opts, errorCode);
ASSERT_EQ(errorCode, SUCCESS);
ASSERT_NE(imageSource.get(), nullptr);
DecodeOptions decodeOpts;
decodeOpts.allocatorType = AllocatorType::DMA_ALLOC;
std::unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
ASSERT_EQ(errorCode, SUCCESS);
if (data != nullptr) {
free(data);
data = nullptr;
}
GTEST_LOG_(INFO) << "PixelAstcTest: PixelAstcTest027 end";
}
}
}

View File

@ -302,7 +302,7 @@ private:
bool GetImageInfoForASTC(ImageInfo& imageInfo, const uint8_t *sourceFilePtr);
bool ConvertYUV420ToRGBA(uint8_t *data, uint32_t size, bool isSupportOdd, bool isAddUV, uint32_t &errorCode);
std::unique_ptr<PixelMap> CreatePixelMapForYUV(uint32_t &errorCode);
std::unique_ptr<PixelMap> CreatePixelMapForASTC(uint32_t &errorCode, bool fastAstc = false);
std::unique_ptr<PixelMap> CreatePixelMapForASTC(uint32_t &errorCode, const DecodeOptions &opts);
uint32_t GetFormatExtended(std::string &format);
static std::unique_ptr<ImageSource> DoImageSourceCreate(
std::function<std::unique_ptr<SourceStream>(void)> stream,