From a15a7f327be9086091fd8b03e137e4067ff43dd9 Mon Sep 17 00:00:00 2001 From: cenfarm Date: Wed, 9 Apr 2025 18:28:18 +0800 Subject: [PATCH 1/3] modify yuv formatType when use GPU Signed-off-by: cenfarm --- frameworks/native/render_environment/render_environment.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/frameworks/native/render_environment/render_environment.cpp b/frameworks/native/render_environment/render_environment.cpp index 8589558..3fbe6db 100644 --- a/frameworks/native/render_environment/render_environment.cpp +++ b/frameworks/native/render_environment/render_environment.cpp @@ -260,6 +260,8 @@ void RenderEnvironment::GenTex(const std::shared_ptr &source, std: output->tex = GenMainTex(source, false); break; } + output->bufferInfo_->formatType_ = output->tex->Format() == GL_RGBA8 ? IEffectFormat::RGBA8888 + : IEffectFormat::RGBA_1010102; } void RenderEnvironment::DrawFlipTex(RenderTexturePtr input, RenderTexturePtr output) @@ -772,4 +774,4 @@ void RenderEnvironment::ReleaseParam() } } // namespace Effect } // namespace Media -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee From 7df46e72e47a86a553a8554d041f85d19fa813a8 Mon Sep 17 00:00:00 2001 From: cenfarm Date: Tue, 15 Apr 2025 20:15:20 +0800 Subject: [PATCH 2/3] convert rgba to yuv when use gpu Signed-off-by: cenfarm --- .../native/utils/common/common_utils.cpp | 55 ++++++++++++++++++- frameworks/native/utils/common/common_utils.h | 3 +- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/frameworks/native/utils/common/common_utils.cpp b/frameworks/native/utils/common/common_utils.cpp index 422f962..1ef6fa9 100644 --- a/frameworks/native/utils/common/common_utils.cpp +++ b/frameworks/native/utils/common/common_utils.cpp @@ -921,9 +921,62 @@ ErrorCode CommonUtils::ModifyPixelMapPropertyForTexture(PixelMap *pixelMap, cons context->renderEnvironment_->ReadPixelsFromTex(buffer->tex, memoryData->data, buffer->bufferInfo_->width_, buffer->bufferInfo_->height_, memoryData->memoryInfo.bufferInfo.rowStride_ / RGBA_BYTES_PER_PIXEL); + EffectBuffer* src = context->renderStrategy_->GetInput(); + IEffectFormat srcEffectFormat = src->bufferInfo_->formatType_; + if (srcEffectFormat != buffer->bufferInfo_->formatType_ && (srcEffectFormat == IEffectFormat::YUVNV21 || + srcEffectFormat == IEffectFormat::YUVNV12)) { + EFFECT_LOGI("ModifyPixelMapProperty: Convert to yuv, buffer formatType_=%{public}d, src formatType_=%{public}d", + buffer->bufferInfo_->formatType_, srcEffectFormat); + MemoryInfo yuvMemInfo = { + .isAutoRelease = false, + .bufferInfo = { + .width_ = src->bufferInfo_->width_, + .height_ = src->bufferInfo_->height_, + .len_ = FormatHelper::CalculateSize(src->bufferInfo_->width_, src->bufferInfo_->height_, + src->bufferInfo_->formatType_), + .rowStride_ = FormatHelper::CalculateRowStride(src->bufferInfo_->width_, src->bufferInfo_->formatType_), + .formatType_ = src->bufferInfo_->formatType_, + }, + .bufferType = bufferType, + }; + std::shared_ptr yuvMemoryData = memory->Alloc(yuvMemInfo); + CHECK_AND_RETURN_RET_LOG(yuvMemoryData != nullptr, ErrorCode::ERR_ALLOC_MEMORY_FAIL, "Alloc yuv memory fail!"); + + MemoryInfo& allocMemInfo = yuvMemoryData->memoryInfo; + auto yuvBufferInfo = std::make_shared(); + CommonUtils::CopyBufferInfo(allocMemInfo.bufferInfo, *yuvBufferInfo); + yuvBufferInfo->surfaceBuffer_ = static_cast(allocMemInfo.extra); + yuvBufferInfo->xtStyleMetadata = src->bufferInfo_->xtStyleMetadata; + yuvBufferInfo->rfDataBMetadata = src->bufferInfo_->rfDataBMetadata; + auto extraInfo = std::make_shared(); + CommonUtils::CopyExtraInfo(*(src->extraInfo_), *extraInfo); + extraInfo->bufferType = allocMemInfo.bufferType; + + auto yuvEffectBuffer = std::make_shared(yuvBufferInfo, yuvMemoryData->data, extraInfo); + auto effectBuffer = std::make_shared(buffer->bufferInfo_, memoryData->data, buffer->extraInfo_); + ConvertRgba2Yuv(effectBuffer.get(), yuvEffectBuffer.get()); + return ModifyPixelMapPropertyInner(yuvMemoryData, pixelMap, allocatorType, isUpdateExif, context); + } + return ModifyPixelMapPropertyInner(memoryData, pixelMap, allocatorType, isUpdateExif); } +ErrorCode CommonUtils::ConvertRgba2Yuv(EffectBuffer *rgba, EffectBuffer *yuv) +{ + CHECK_AND_RETURN_RET_LOG(rgba != nullptr && yuv != nullptr, ErrorCode::ERR_INPUT_NULL, + "ConvertRgba2Yuv: input error! rgba=%{public}d, yuv =%{public}d", rgba == nullptr, yuv == nullptr); + FormatConverterInfo rgbaFormatInfo = { + .bufferInfo = *rgba->bufferInfo_, + .buffer = rgba->buffer_, + }; + FormatConverterInfo yuvFormatInfo = { + .bufferInfo = *yuv->bufferInfo_, + .buffer = yuv->buffer_, + }; + + return FormatHelper::ConvertFormat(rgbaFormatInfo, yuvFormatInfo); +} + std::shared_ptr CommonUtils::GetImageSourceFromPath(const std::string path) { std::shared_ptr imageSource; @@ -936,4 +989,4 @@ std::shared_ptr CommonUtils::GetImageSourceFromPath(const std::stri } } // namespace Effect } // namespace Media -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/native/utils/common/common_utils.h b/frameworks/native/utils/common/common_utils.h index e85bb29..d4912dd 100644 --- a/frameworks/native/utils/common/common_utils.h +++ b/frameworks/native/utils/common/common_utils.h @@ -72,6 +72,7 @@ public: static bool IsEnableCopyMetaData(int numBuffers, ...); static std::shared_ptr GetImageSourceFromPath(std::string path); + static ErrorCode ConvertRgba2Yuv(EffectBuffer *rgba, EffectBuffer *yuv); template static ErrorCode ParseAny(Plugin::Any any, ValueType &value) { @@ -126,4 +127,4 @@ private: } // namespace Media } // namespace OHOS -#endif // IMAGE_EFFECT_COMMON_UTILS_H \ No newline at end of file +#endif // IMAGE_EFFECT_COMMON_UTILS_H -- Gitee From fe0ed51bc99cad9f01864befb9e66cc794eca3ce Mon Sep 17 00:00:00 2001 From: cenfarm Date: Tue, 15 Apr 2025 20:18:26 +0800 Subject: [PATCH 3/3] add calculate rowStride and len Signed-off-by: cenfarm --- .../native/render_environment/render_environment.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/frameworks/native/render_environment/render_environment.cpp b/frameworks/native/render_environment/render_environment.cpp index 3fbe6db..ede015b 100644 --- a/frameworks/native/render_environment/render_environment.cpp +++ b/frameworks/native/render_environment/render_environment.cpp @@ -260,8 +260,14 @@ void RenderEnvironment::GenTex(const std::shared_ptr &source, std: output->tex = GenMainTex(source, false); break; } - output->bufferInfo_->formatType_ = output->tex->Format() == GL_RGBA8 ? IEffectFormat::RGBA8888 - : IEffectFormat::RGBA_1010102; + + output->bufferInfo_->formatType_ = output->tex->Format() == GL_RGBA8 ? + IEffectFormat::RGBA8888 : IEffectFormat::RGBA_1010102; + + output->bufferInfo_->rowStride_ = FormatHelper::CalculateRowStride(output->bufferInfo_->width_, + output->bufferInfo_->formatType_); + output->bufferInfo_->len_ = FormatHelper::CalculateSize(output->bufferInfo_->width_, + output->bufferInfo_->height_, output->bufferInfo_->formatType_); } void RenderEnvironment::DrawFlipTex(RenderTexturePtr input, RenderTexturePtr output) -- Gitee