Bug 1863367 - Part 6. Implement MacIOSurfaceImage::BuildSurfaceDescriptorBuffer for OSX. r=gfx-reviewers,lsalzman

Differential Revision: https://phabricator.services.mozilla.com/D193081
This commit is contained in:
Andrew Osmond 2023-11-14 02:04:06 +00:00
parent ca9eb4d975
commit 3c3ae94229
4 changed files with 98 additions and 48 deletions

View File

@ -7,6 +7,7 @@
#include "libyuv.h"
#include "MacIOSurfaceHelpers.h"
#include "mozilla/gfx/MacIOSurface.h"
#include "mozilla/ScopeExit.h"
#include "YCbCrUtils.h"
namespace mozilla {
@ -19,34 +20,17 @@ namespace layers {
#define ALIGNEDPTR_32(x) \
reinterpret_cast<uint8_t*>((reinterpret_cast<uintptr_t>(x) + 31) & ~31)
static already_AddRefed<SourceSurface>
CreateSourceSurfaceFromLockedMacIOSurface(MacIOSurface* aSurface) {
static nsresult CopyFromLockedMacIOSurface(MacIOSurface* aSurface,
uint8_t* aData, int32_t aStride,
const IntSize& aSize,
SurfaceFormat aFormat) {
size_t bytesPerRow = aSurface->GetBytesPerRow();
size_t ioWidth = aSurface->GetDevicePixelWidth();
size_t ioHeight = aSurface->GetDevicePixelHeight();
IntSize size((int32_t)ioWidth, (int32_t)ioHeight);
SurfaceFormat ioFormat = aSurface->GetFormat();
if ((ioFormat == SurfaceFormat::NV12 || ioFormat == SurfaceFormat::YUV422) &&
(ioWidth > PlanarYCbCrImage::MAX_DIMENSION ||
ioHeight > PlanarYCbCrImage::MAX_DIMENSION)) {
return nullptr;
}
SurfaceFormat format =
(ioFormat == SurfaceFormat::NV12 || ioFormat == SurfaceFormat::YUV422)
? SurfaceFormat::B8G8R8X8
: SurfaceFormat::B8G8R8A8;
RefPtr<DataSourceSurface> dataSurface =
Factory::CreateDataSourceSurface(size, format);
if (NS_WARN_IF(!dataSurface)) {
return nullptr;
}
DataSourceSurface::MappedSurface mappedSurface;
if (!dataSurface->Map(DataSourceSurface::WRITE, &mappedSurface)) {
return nullptr;
(aSize.width > PlanarYCbCrImage::MAX_DIMENSION ||
aSize.height > PlanarYCbCrImage::MAX_DIMENSION)) {
return NS_ERROR_FAILURE;
}
if (ioFormat == SurfaceFormat::NV12) {
@ -81,33 +65,31 @@ CreateSourceSurfaceFromLockedMacIOSurface(MacIOSurface* aSurface) {
data.mCbChannel = cbPlane.get();
data.mCrChannel = crPlane.get();
data.mCbCrStride = cbCrWidth;
data.mPictureRect = IntRect(IntPoint(0, 0), size);
data.mPictureRect = IntRect(IntPoint(0, 0), aSize);
data.mYUVColorSpace = aSurface->GetYUVColorSpace();
data.mColorPrimaries = aSurface->mColorPrimaries;
data.mColorRange = aSurface->IsFullRange() ? gfx::ColorRange::FULL
: gfx::ColorRange::LIMITED;
data.mChromaSubsampling = ChromaSubsampling::HALF_WIDTH_AND_HEIGHT;
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, size, mappedSurface.mData,
mappedSurface.mStride);
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, aSize, aData, aStride);
} else if (ioFormat == SurfaceFormat::YUV422) {
if (ioWidth == ALIGNED_32(ioWidth)) {
if (aSize.width == ALIGNED_32(aSize.width)) {
// Optimization when width is aligned to 32.
libyuv::ConvertToARGB((uint8_t*)aSurface->GetBaseAddress(),
0 /* not used */, mappedSurface.mData,
mappedSurface.mStride, 0, 0, size.width,
size.height, size.width, size.height,
0 /* not used */, aData, aStride, 0, 0, aSize.width,
aSize.height, aSize.width, aSize.height,
libyuv::kRotate0, libyuv::FOURCC_YUYV);
} else {
/* Convert to YV16 */
size_t cbCrWidth = (ioWidth + 1) >> 1;
size_t cbCrHeight = ioHeight;
size_t cbCrWidth = (aSize.width + 1) >> 1;
size_t cbCrHeight = aSize.height;
// Ensure our stride is a multiple of 32 to allow for memory aligned rows.
size_t cbCrStride = ALIGNED_32(cbCrWidth);
size_t strideDelta = cbCrStride - cbCrWidth;
MOZ_ASSERT(strideDelta <= 31);
auto yPlane = MakeUnique<uint8_t[]>(cbCrStride * 2 * ioHeight + 31);
auto yPlane = MakeUnique<uint8_t[]>(cbCrStride * 2 * aSize.height + 31);
auto cbPlane = MakeUnique<uint8_t[]>(cbCrStride * cbCrHeight + 31);
auto crPlane = MakeUnique<uint8_t[]>(cbCrStride * cbCrHeight + 31);
@ -116,7 +98,7 @@ CreateSourceSurfaceFromLockedMacIOSurface(MacIOSurface* aSurface) {
uint8_t* cbDest = ALIGNEDPTR_32(cbPlane.get());
uint8_t* crDest = ALIGNEDPTR_32(crPlane.get());
for (size_t i = 0; i < ioHeight; i++) {
for (int32_t i = 0; i < aSize.height; i++) {
uint8_t* rowSrc = src + bytesPerRow * i;
for (size_t j = 0; j < cbCrWidth; j++) {
*yDest = *rowSrc;
@ -146,37 +128,87 @@ CreateSourceSurfaceFromLockedMacIOSurface(MacIOSurface* aSurface) {
data.mCbChannel = ALIGNEDPTR_32(cbPlane.get());
data.mCrChannel = ALIGNEDPTR_32(crPlane.get());
data.mCbCrStride = cbCrStride;
data.mPictureRect = IntRect(IntPoint(0, 0), size);
data.mPictureRect = IntRect(IntPoint(0, 0), aSize);
data.mYUVColorSpace = aSurface->GetYUVColorSpace();
data.mColorPrimaries = aSurface->mColorPrimaries;
data.mColorRange = aSurface->IsFullRange() ? gfx::ColorRange::FULL
: gfx::ColorRange::LIMITED;
data.mChromaSubsampling = ChromaSubsampling::HALF_WIDTH;
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, size,
mappedSurface.mData, mappedSurface.mStride);
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8, aSize, aData, aStride);
}
} else {
unsigned char* ioData = (unsigned char*)aSurface->GetBaseAddress();
for (size_t i = 0; i < ioHeight; ++i) {
memcpy(mappedSurface.mData + i * mappedSurface.mStride,
ioData + i * bytesPerRow, ioWidth * 4);
for (int32_t i = 0; i < aSize.height; ++i) {
memcpy(aData + i * aStride, ioData + i * bytesPerRow, aSize.width * 4);
}
}
dataSurface->Unmap();
return dataSurface.forget();
return NS_OK;
}
already_AddRefed<SourceSurface> CreateSourceSurfaceFromMacIOSurface(
MacIOSurface* aSurface) {
aSurface->Lock();
RefPtr<SourceSurface> result =
CreateSourceSurfaceFromLockedMacIOSurface(aSurface);
aSurface->Unlock();
return result.forget();
auto scopeExit = MakeScopeExit([&]() { aSurface->Unlock(); });
size_t ioWidth = aSurface->GetDevicePixelWidth();
size_t ioHeight = aSurface->GetDevicePixelHeight();
IntSize size((int32_t)ioWidth, (int32_t)ioHeight);
SurfaceFormat ioFormat = aSurface->GetFormat();
SurfaceFormat format =
(ioFormat == SurfaceFormat::NV12 || ioFormat == SurfaceFormat::YUV422)
? SurfaceFormat::B8G8R8X8
: SurfaceFormat::B8G8R8A8;
RefPtr<DataSourceSurface> dataSurface =
Factory::CreateDataSourceSurface(size, format);
if (NS_WARN_IF(!dataSurface)) {
return nullptr;
}
DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::WRITE);
if (NS_WARN_IF(!map.IsMapped())) {
return nullptr;
}
nsresult rv = CopyFromLockedMacIOSurface(aSurface, map.GetData(),
map.GetStride(), size, format);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
return dataSurface.forget();
}
nsresult CreateSurfaceDescriptorBufferFromMacIOSurface(
MacIOSurface* aSurface, SurfaceDescriptorBuffer& aSdBuffer,
Image::BuildSdbFlags aFlags,
const std::function<MemoryOrShmem(uint32_t)>& aAllocate) {
aSurface->Lock();
auto scopeExit = MakeScopeExit([&]() { aSurface->Unlock(); });
size_t ioWidth = aSurface->GetDevicePixelWidth();
size_t ioHeight = aSurface->GetDevicePixelHeight();
IntSize size((int32_t)ioWidth, (int32_t)ioHeight);
SurfaceFormat ioFormat = aSurface->GetFormat();
SurfaceFormat format =
(ioFormat == SurfaceFormat::NV12 || ioFormat == SurfaceFormat::YUV422)
? SurfaceFormat::B8G8R8X8
: SurfaceFormat::B8G8R8A8;
uint8_t* buffer = nullptr;
int32_t stride = 0;
nsresult rv = Image::AllocateSurfaceDescriptorBufferRgb(
size, format, buffer, aSdBuffer, stride, aAllocate);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return CopyFromLockedMacIOSurface(aSurface, buffer, stride, size, format);
}
} // namespace layers

View File

@ -7,6 +7,8 @@
#ifndef GFX_MACIOSURFACEHELPERS_H
#define GFX_MACIOSURFACEHELPERS_H
#include "ImageContainer.h"
class MacIOSurface;
template <class T>
struct already_AddRefed;
@ -24,6 +26,11 @@ namespace layers {
already_AddRefed<gfx::SourceSurface> CreateSourceSurfaceFromMacIOSurface(
MacIOSurface* aSurface);
nsresult CreateSurfaceDescriptorBufferFromMacIOSurface(
MacIOSurface* aSurface, SurfaceDescriptorBuffer& aSdBuffer,
Image::BuildSdbFlags aFlags,
const std::function<MemoryOrShmem(uint32_t)>& aAllocate);
} // namespace layers
} // namespace mozilla

View File

@ -43,6 +43,13 @@ already_AddRefed<SourceSurface> MacIOSurfaceImage::GetAsSourceSurface() {
return CreateSourceSurfaceFromMacIOSurface(mSurface);
}
nsresult MacIOSurfaceImage::BuildSurfaceDescriptorBuffer(
SurfaceDescriptorBuffer& aSdBuffer, BuildSdbFlags aFlags,
const std::function<MemoryOrShmem(uint32_t)>& aAllocate) {
return CreateSurfaceDescriptorBufferFromMacIOSurface(mSurface, aSdBuffer,
aFlags, aAllocate);
}
static inline uint16_t safeShift10BitBy6(const uint16_t& a10BitLSB) {
// a10BitLSB is a 10-bit value packed into the least significant bits of
// a 16 bit value. This function asserts that the 6 MSBs are zero, then

View File

@ -38,6 +38,10 @@ class MacIOSurfaceImage : public Image {
already_AddRefed<gfx::SourceSurface> GetAsSourceSurface() override;
nsresult BuildSurfaceDescriptorBuffer(
SurfaceDescriptorBuffer& aSdBuffer, BuildSdbFlags aFlags,
const std::function<MemoryOrShmem(uint32_t)>& aAllocate) override;
TextureClient* GetTextureClient(KnowsCompositor* aKnowsCompositor) override;
MacIOSurfaceImage* AsMacIOSurfaceImage() override { return this; }