Bug 1907121 - Move YCbCrUtils API assertions to caller side r=gfx-reviewers,nical

Initially, the YCbCrUtils and yuv_convert APIs asserted success
internally. With recent changes, those assertions were removed. This
patch reintroduces those assertions on the caller's side instead, except
the `ConvertToRGBA` in ImageConversion.cpp.

Differential Revision: https://phabricator.services.mozilla.com/D216191
This commit is contained in:
Chun-Min Chang
2024-07-18 21:02:33 +00:00
parent cd2806146f
commit 95fb46d4e8
7 changed files with 64 additions and 26 deletions

View File

@@ -366,8 +366,12 @@ nsresult ImageEncoder::ExtractDataInternal(
}
data.SetCapacity(length);
ConvertYCbCrToRGB(*ycbcrImage->GetData(), format, aSize.ToUnknownSize(),
data.Elements(), stride);
rv = ConvertYCbCrToRGB(*ycbcrImage->GetData(), format,
aSize.ToUnknownSize(), data.Elements(), stride);
if (NS_FAILED(rv)) {
MOZ_ASSERT_UNREACHABLE("Failed to convert YUV into RGB data");
return rv;
}
rv = aEncoder->InitFromData(data.Elements(),
aSize.width * aSize.height * 4, aSize.width,

View File

@@ -275,8 +275,12 @@ nsresult ConvertToRGBA(Image* aImage, const SurfaceFormat& aDestFormat,
}
}
ConvertYCbCrToRGB32(*data, convertedFormat, aDestBuffer,
AssertedCast<int32_t>(aDestStride), premultOp);
nsresult result =
ConvertYCbCrToRGB32(*data, convertedFormat, aDestBuffer,
AssertedCast<int32_t>(aDestStride), premultOp);
if (NS_FAILED(result)) {
return result;
}
if (convertedFormat == aDestFormat) {
return NS_OK;

View File

@@ -448,12 +448,16 @@ already_AddRefed<VideoData> VideoData::CreateAndCopyData(
// The naming convention for libyuv and associated utils is word-order.
// The naming convention in the gfx stack is byte-order.
ConvertI420AlphaToARGB(aBuffer.mPlanes[0].mData, aBuffer.mPlanes[1].mData,
aBuffer.mPlanes[2].mData, aAlphaPlane.mData,
AssertedCast<int>(aBuffer.mPlanes[0].mStride),
AssertedCast<int>(aBuffer.mPlanes[1].mStride),
buffer.data, buffer.stride, buffer.size.width,
buffer.size.height);
nsresult result = ConvertI420AlphaToARGB(
aBuffer.mPlanes[0].mData, aBuffer.mPlanes[1].mData,
aBuffer.mPlanes[2].mData, aAlphaPlane.mData,
AssertedCast<int>(aBuffer.mPlanes[0].mStride),
AssertedCast<int>(aBuffer.mPlanes[1].mStride), buffer.data, buffer.stride,
buffer.size.width, buffer.size.height);
if (NS_FAILED(result)) {
MOZ_ASSERT_UNREACHABLE("Failed to convert I420 YUVA into RGBA data");
return nullptr;
}
return v.forget();
}

View File

@@ -689,8 +689,9 @@ nsresult PlanarYCbCrImage::BuildSurfaceDescriptorBuffer(
}
}
gfx::ConvertYCbCrToRGB(mData, format, size, buffer, stride);
return NS_OK;
rv = gfx::ConvertYCbCrToRGB(mData, format, size, buffer, stride);
MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to convert YUV into RGB data");
return rv;
}
auto ySize = pdata->YDataSize();
@@ -878,8 +879,11 @@ already_AddRefed<gfx::SourceSurface> PlanarYCbCrImage::GetAsSourceSurface() {
return nullptr;
}
gfx::ConvertYCbCrToRGB(mData, format, size, mapping.GetData(),
mapping.GetStride());
if (NS_WARN_IF(NS_FAILED(gfx::ConvertYCbCrToRGB(
mData, format, size, mapping.GetData(), mapping.GetStride())))) {
MOZ_ASSERT_UNREACHABLE("Failed to convert YUV into RGB data");
return nullptr;
}
mSourceSurface = surface;
@@ -957,8 +961,11 @@ already_AddRefed<SourceSurface> NVImage::GetAsSourceSurface() {
return nullptr;
}
gfx::ConvertYCbCrToRGB(aData, format, size, mapping.GetData(),
mapping.GetStride());
if (NS_WARN_IF(NS_FAILED(gfx::ConvertYCbCrToRGB(
aData, format, size, mapping.GetData(), mapping.GetStride())))) {
MOZ_ASSERT_UNREACHABLE("Failed to convert YUV into RGB data");
return nullptr;
}
mSourceSurface = surface;
@@ -1025,7 +1032,11 @@ nsresult NVImage::BuildSurfaceDescriptorBuffer(
}
if (!mSourceSurface) {
gfx::ConvertYCbCrToRGB(aData, format, size, output, stride);
rv = gfx::ConvertYCbCrToRGB(aData, format, size, output, stride);
if (NS_WARN_IF(NS_FAILED(rv))) {
MOZ_ASSERT_UNREACHABLE("Failed to convert YUV into RGB data");
return rv;
}
return NS_OK;
}

View File

@@ -9,6 +9,7 @@
#include "YCbCrUtils.h" // for YCbCr conversions
#include "gfx2DGlue.h" // for SurfaceFormatToImageFormat
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
#include "mozilla/DebugOnly.h" // for DebugOnly
#include "mozilla/gfx/2D.h" // for DataSourceSurface, Factory
#include "mozilla/gfx/Logging.h" // for gfxDebug
#include "mozilla/gfx/Tools.h" // for GetAlignedStride, etc
@@ -319,8 +320,12 @@ already_AddRefed<DataSourceSurface> DataSourceSurfaceFromYCbCrDescriptor(
ycbcrData.mColorDepth = aDescriptor.colorDepth();
ycbcrData.mChromaSubsampling = aDescriptor.chromaSubsampling();
gfx::ConvertYCbCrToRGB(ycbcrData, gfx::SurfaceFormat::B8G8R8X8, size,
map.mData, map.mStride);
if (NS_WARN_IF(NS_FAILED(
gfx::ConvertYCbCrToRGB(ycbcrData, gfx::SurfaceFormat::B8G8R8X8, size,
map.mData, map.mStride)))) {
MOZ_ASSERT_UNREACHABLE("Failed to convert YUV into RGB data");
return nullptr;
}
result->Unmap();
return result.forget();
@@ -345,8 +350,9 @@ void ConvertAndScaleFromYCbCrDescriptor(uint8_t* aBuffer,
ycbcrData.mColorDepth = aDescriptor.colorDepth();
ycbcrData.mChromaSubsampling = aDescriptor.chromaSubsampling();
gfx::ConvertYCbCrToRGB(ycbcrData, aDestFormat, aDestSize, aDestBuffer,
aStride);
DebugOnly<nsresult> result = gfx::ConvertYCbCrToRGB(
ycbcrData, aDestFormat, aDestSize, aDestBuffer, aStride);
MOZ_ASSERT(NS_SUCCEEDED(result), "Failed to convert YUV into RGB data");
}
gfx::IntSize GetCroppedCbCrSize(const YCbCrDescriptor& aDescriptor) {

View File

@@ -72,8 +72,13 @@ static nsresult CopyFromLockedMacIOSurface(MacIOSurface* aSurface,
: gfx::ColorRange::LIMITED;
data.mChromaSubsampling = ChromaSubsampling::HALF_WIDTH_AND_HEIGHT;
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, aSize, aData, aStride);
} else if (ioFormat == SurfaceFormat::YUV422) {
nsresult result =
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, aSize, aData, aStride);
MOZ_ASSERT(NS_SUCCEEDED(result), "Failed to convert YUV into RGB data");
return result;
}
if (ioFormat == SurfaceFormat::YUV422) {
if (aSize.width == ALIGNED_32(aSize.width)) {
// Optimization when width is aligned to 32.
libyuv::ConvertToARGB((uint8_t*)aSurface->GetBaseAddress(),
@@ -135,7 +140,10 @@ static nsresult CopyFromLockedMacIOSurface(MacIOSurface* aSurface,
: gfx::ColorRange::LIMITED;
data.mChromaSubsampling = ChromaSubsampling::HALF_WIDTH;
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, aSize, aData, aStride);
nsresult result = ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, aSize,
aData, aStride);
MOZ_ASSERT(NS_SUCCEEDED(result), "Failed to convert YUV into RGB data");
return result;
}
} else {
unsigned char* ioData = (unsigned char*)aSurface->GetBaseAddress();

View File

@@ -1922,8 +1922,9 @@ nsAVIFDecoder::DecodeResult nsAVIFDecoder::DoDecodeInternal(
MOZ_LOG(sAVIFLog, LogLevel::Debug,
("[this=%p] calling gfx::ConvertYCbCrToRGB32 premultOp: %p", this,
premultOp));
gfx::ConvertYCbCrToRGB32(*decodedData, format, rgbBuf.get(),
rgbStride.value(), premultOp);
DebugOnly<nsresult> result = gfx::ConvertYCbCrToRGB32(
*decodedData, format, rgbBuf.get(), rgbStride.value(), premultOp);
MOZ_ASSERT(NS_SUCCEEDED(result), "Failed to convert YUV into RGB data");
MOZ_LOG(sAVIFLog, LogLevel::Debug,
("[this=%p] calling SurfacePipeFactory::CreateSurfacePipe", this));