Bug 1200595 - Gralloc TextureData implementation. r=sotaro

This commit is contained in:
Nicolas Silva
2015-10-15 17:53:37 +02:00
parent 75de75b2f5
commit bd5414e7ad
20 changed files with 535 additions and 524 deletions

View File

@@ -24,148 +24,28 @@ namespace layers {
using namespace mozilla::gfx;
using namespace android;
GrallocTextureClientOGL::GrallocTextureClientOGL(ISurfaceAllocator* aAllocator,
gfx::SurfaceFormat aFormat,
gfx::BackendType aMoz2dBackend,
TextureFlags aFlags)
: BufferTextureClient(aAllocator, aFormat, aMoz2dBackend, aFlags)
, mGrallocHandle(null_t())
, mMappedBuffer(nullptr)
, mMediaBuffer(nullptr)
, mIsOpaque(gfx::IsOpaque(aFormat))
static bool
DisableGralloc(SurfaceFormat aFormat, const gfx::IntSize& aSizeHint)
{
MOZ_COUNT_CTOR(GrallocTextureClientOGL);
}
GrallocTextureClientOGL::~GrallocTextureClientOGL()
{
MOZ_COUNT_DTOR(GrallocTextureClientOGL);
ISurfaceAllocator* allocator = GetAllocator();
if (ShouldDeallocateInDestructor()) {
allocator->DeallocGrallocBuffer(&mGrallocHandle);
} else {
allocator->DropGrallocBuffer(&mGrallocHandle);
}
}
already_AddRefed<TextureClient>
GrallocTextureClientOGL::CreateSimilar(TextureFlags aFlags,
TextureAllocationFlags aAllocFlags) const
{
RefPtr<TextureClient> tex = new GrallocTextureClientOGL(
mAllocator, mFormat, mBackend, mFlags | aFlags
);
if (!tex->AllocateForSurface(mSize, aAllocFlags)) {
return nullptr;
}
return tex.forget();
}
bool
GrallocTextureClientOGL::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
{
MOZ_ASSERT(IsValid());
if (!IsAllocated()) {
return false;
}
aOutDescriptor = NewSurfaceDescriptorGralloc(mGrallocHandle, mIsOpaque);
return true;
}
void
GrallocTextureClientOGL::SetRemoveFromCompositableWaiter(AsyncTransactionWaiter* aWaiter)
{
mRemoveFromCompositableWaiter = aWaiter;
}
void
GrallocTextureClientOGL::WaitForBufferOwnership(bool aWaitReleaseFence)
{
if (mRemoveFromCompositableWaiter) {
mRemoveFromCompositableWaiter->WaitComplete();
mRemoveFromCompositableWaiter = nullptr;
}
if (!aWaitReleaseFence) {
return;
}
#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17
if (mReleaseFenceHandle.IsValid()) {
RefPtr<FenceHandle::FdObj> fdObj = mReleaseFenceHandle.GetAndResetFdObj();
android::sp<Fence> fence = new Fence(fdObj->GetAndResetFd());
#if ANDROID_VERSION == 17
fence->waitForever(1000, "GrallocTextureClientOGL::Lock");
// 1000 is what Android uses. It is a warning timeout in ms.
// This timeout was removed in ANDROID_VERSION 18.
#else
fence->waitForever("GrallocTextureClientOGL::Lock");
#endif
mReleaseFenceHandle = FenceHandle();
}
#endif
}
bool
GrallocTextureClientOGL::Lock(OpenMode aMode)
{
MOZ_ASSERT(!mIsLocked);
MOZ_ASSERT(IsValid());
if (!IsValid() || !IsAllocated()) {
return false;
}
if (mMappedBuffer) {
if (gfxPrefs::DisableGralloc()) {
return true;
}
if (aFormat == gfx::SurfaceFormat::A8) {
return true;
}
#if ANDROID_VERSION <= 15
// Adreno 200 has a problem of drawing gralloc buffer width less than 64 and
// drawing gralloc buffer with a height 9px-16px.
// See Bug 983971.
if (aSizeHint.width < 64 || aSizeHint.height < 32) {
return true;
}
#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 21
WaitForBufferOwnership(false /* aWaitReleaseFence */);
#else
WaitForBufferOwnership();
#endif
uint32_t usage = 0;
if (aMode & OpenMode::OPEN_READ) {
usage |= GRALLOC_USAGE_SW_READ_OFTEN;
}
if (aMode & OpenMode::OPEN_WRITE) {
usage |= GRALLOC_USAGE_SW_WRITE_OFTEN;
}
#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 21
RefPtr<FenceHandle::FdObj> fdObj = mReleaseFenceHandle.GetAndResetFdObj();
int32_t rv = mGraphicBuffer->lockAsync(usage,
reinterpret_cast<void**>(&mMappedBuffer),
fdObj->GetAndResetFd());
#else
int32_t rv = mGraphicBuffer->lock(usage,
reinterpret_cast<void**>(&mMappedBuffer));
#endif
if (rv) {
mMappedBuffer = nullptr;
NS_WARNING("Couldn't lock graphic buffer");
return false;
}
mIsLocked = true;
return true;
return false;
}
void
GrallocTextureClientOGL::Unlock()
{
MOZ_ASSERT(mIsLocked);
mIsLocked = false;
mDrawTarget = nullptr;
if (mMappedBuffer) {
mMappedBuffer = nullptr;
mGraphicBuffer->unlock();
}
}
static gfx::SurfaceFormat
gfx::SurfaceFormat
SurfaceFormatForPixelFormat(android::PixelFormat aFormat)
{
switch (aFormat) {
@@ -180,56 +60,188 @@ SurfaceFormatForPixelFormat(android::PixelFormat aFormat)
case HAL_PIXEL_FORMAT_YV12:
return gfx::SurfaceFormat::YUV;
default:
MOZ_CRASH("Unknown gralloc pixel format");
return gfx::SurfaceFormat::UNKNOWN;
}
return gfx::SurfaceFormat::R8G8B8A8;
}
gfx::DrawTarget*
GrallocTextureClientOGL::BorrowDrawTarget()
bool
IsGrallocRBSwapped(gfx::SurfaceFormat aFormat) {
switch (aFormat) {
case gfx::SurfaceFormat::B8G8R8A8:
case gfx::SurfaceFormat::B8G8R8X8:
return true;
default:
return false;
}
}
uint32_t GetAndroidFormat(gfx::SurfaceFormat aFormat)
{
MOZ_ASSERT(IsValid());
MOZ_ASSERT(mMappedBuffer, "Calling TextureClient::BorrowDrawTarget without locking :(");
if (!IsValid() || !IsAllocated() || !mMappedBuffer) {
return nullptr;
switch (aFormat) {
case gfx::SurfaceFormat::R8G8B8A8:
case gfx::SurfaceFormat::B8G8R8A8:
return android::PIXEL_FORMAT_RGBA_8888;
case gfx::SurfaceFormat::R8G8B8X8:
case gfx::SurfaceFormat::B8G8R8X8:
return android::PIXEL_FORMAT_RGBX_8888;
case gfx::SurfaceFormat::R5G6B5_UINT16:
return android::PIXEL_FORMAT_RGB_565;
case gfx::SurfaceFormat::YUV:
return HAL_PIXEL_FORMAT_YV12;
case gfx::SurfaceFormat::A8:
NS_WARNING("gralloc does not support SurfaceFormat::A8");
default:
NS_WARNING("Unsupported surface format");
return android::PIXEL_FORMAT_UNKNOWN;
}
}
if (mDrawTarget) {
return mDrawTarget;
}
GrallocTextureData::GrallocTextureData(MaybeMagicGrallocBufferHandle aGrallocHandle,
gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
gfx::BackendType aMoz2DBackend)
: mSize(aSize)
, mFormat(aFormat)
, mMoz2DBackend(aMoz2DBackend)
, mGrallocHandle(aGrallocHandle)
, mMappedBuffer(nullptr)
, mMediaBuffer(nullptr)
{
mGraphicBuffer = GetGraphicBufferFrom(aGrallocHandle);
MOZ_COUNT_CTOR(GrallocTextureData);
}
gfx::SurfaceFormat format = SurfaceFormatForPixelFormat(mGraphicBuffer->getPixelFormat());
long pixelStride = mGraphicBuffer->getStride();
long byteStride = pixelStride * BytesPerPixel(format);
mDrawTarget = gfxPlatform::GetPlatform()->CreateDrawTargetForData(GetBuffer(),
mSize,
byteStride,
mFormat);
return mDrawTarget;
GrallocTextureData::~GrallocTextureData()
{
MOZ_COUNT_DTOR(GrallocTextureData);
}
void
GrallocTextureClientOGL::UpdateFromSurface(gfx::SourceSurface* aSurface)
GrallocTextureData::Deallocate(ISurfaceAllocator* aAllocator)
{
aAllocator->DeallocGrallocBuffer(&mGrallocHandle);
mGrallocHandle = null_t();
mGraphicBuffer = nullptr;
}
void
GrallocTextureData::Forget(ISurfaceAllocator* aAllocator)
{
aAllocator->DropGrallocBuffer(&mGrallocHandle);
mGrallocHandle = null_t();
mGraphicBuffer = nullptr;
}
bool
GrallocTextureData::Serialize(SurfaceDescriptor& aOutDescriptor)
{
aOutDescriptor = NewSurfaceDescriptorGralloc(mGrallocHandle, gfx::IsOpaque(mFormat));
return true;
}
void
GrallocTextureData::WaitForFence(FenceHandle* aFence)
{
#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION < 21 && ANDROID_VERSION >= 17
if (aFence->IsValid()) {
RefPtr<FenceHandle::FdObj> fdObj = aFence->GetAndResetFdObj();
android::sp<Fence> fence = new Fence(fdObj->GetAndResetFd());
#if ANDROID_VERSION == 17
fence->waitForever(1000, "GrallocTextureData::Lock");
// 1000 is what Android uses. It is a warning timeout in ms.
// This timeout was removed in ANDROID_VERSION 18.
#else
fence->waitForever("GrallocTextureData::Lock");
#endif
}
#endif
}
bool
GrallocTextureData::Lock(OpenMode aMode, FenceHandle* aReleaseFence)
{
MOZ_ASSERT(!mMappedBuffer);
WaitForFence(aReleaseFence);
uint32_t usage = 0;
if (aMode & OpenMode::OPEN_READ) {
usage |= GRALLOC_USAGE_SW_READ_OFTEN;
}
if (aMode & OpenMode::OPEN_WRITE) {
usage |= GRALLOC_USAGE_SW_WRITE_OFTEN;
}
#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 21
RefPtr<FenceHandle::FdObj> fdObj = aReleaseFence->GetAndResetFdObj();
int32_t rv = mGraphicBuffer->lockAsync(usage,
reinterpret_cast<void**>(&mMappedBuffer),
fdObj->GetAndResetFd());
#else
int32_t rv = mGraphicBuffer->lock(usage,
reinterpret_cast<void**>(&mMappedBuffer));
#endif
if (rv) {
mMappedBuffer = nullptr;
NS_WARNING("Couldn't lock graphic buffer");
return false;
}
return true;
}
void
GrallocTextureData::Unlock()
{
MOZ_ASSERT(mMappedBuffer);
mMappedBuffer = nullptr;
mGraphicBuffer->unlock();
}
already_AddRefed<gfx::DrawTarget>
GrallocTextureData::BorrowDrawTarget()
{
MOZ_ASSERT(mMappedBuffer);
if (!mMappedBuffer) {
return nullptr;
}
long byteStride = mGraphicBuffer->getStride() * BytesPerPixel(mFormat);
return gfxPlatform::GetPlatform()->CreateDrawTargetForData(mMappedBuffer, mSize,
byteStride, mFormat);
}
bool
GrallocTextureData::BorrowMappedData(MappedTextureData& aMap)
{
if (mFormat == gfx::SurfaceFormat::YUV || !mMappedBuffer) {
return false;
}
aMap.data = mMappedBuffer;
aMap.size = mSize;
aMap.stride = mGraphicBuffer->getStride() * BytesPerPixel(mFormat);
aMap.format = mFormat;
return true;
}
bool
GrallocTextureData::UpdateFromSurface(gfx::SourceSurface* aSurface)
{
MOZ_ASSERT(IsValid());
MOZ_ASSERT(mMappedBuffer, "Calling TextureClient::BorrowDrawTarget without locking :(");
if (!IsValid() || !IsAllocated() || !mMappedBuffer) {
return;
if (!mMappedBuffer) {
return false;
}
RefPtr<DataSourceSurface> srcSurf = aSurface->GetDataSurface();
if (!srcSurf) {
gfxCriticalError() << "Failed to GetDataSurface in UpdateFromSurface.";
return;
return false;
}
gfx::SurfaceFormat format = SurfaceFormatForPixelFormat(mGraphicBuffer->getPixelFormat());
if (mSize != srcSurf->GetSize() || mFormat != srcSurf->GetFormat()) {
gfxCriticalError() << "Attempt to update texture client from a surface with a different size or format! This: " << mSize << " " << format << " Other: " << srcSurf->GetSize() << " " << srcSurf->GetFormat();
return;
return false;
}
long pixelStride = mGraphicBuffer->getStride();
@@ -239,144 +251,149 @@ GrallocTextureClientOGL::UpdateFromSurface(gfx::SourceSurface* aSurface)
if (!srcSurf->Map(DataSourceSurface::READ, &sourceMap)) {
gfxCriticalError() << "Failed to map source surface for UpdateFromSurface.";
return;
return false;
}
uint8_t* buffer = GetBuffer();
for (int y = 0; y < srcSurf->GetSize().height; y++) {
memcpy(buffer + byteStride * y,
memcpy(mMappedBuffer + byteStride * y,
sourceMap.mData + sourceMap.mStride * y,
srcSurf->GetSize().width * BytesPerPixel(srcSurf->GetFormat()));
}
srcSurf->Unmap();
return true;
}
bool
GrallocTextureClientOGL::AllocateForSurface(gfx::IntSize aSize,
TextureAllocationFlags)
// static
GrallocTextureData*
GrallocTextureData::Create(gfx::IntSize aSize, AndroidFormat aAndroidFormat,
gfx::BackendType aMoz2dBackend, uint32_t aUsage,
ISurfaceAllocator* aAllocator)
{
MOZ_ASSERT(IsValid());
uint32_t format;
uint32_t usage = android::GraphicBuffer::USAGE_SW_READ_OFTEN |
android::GraphicBuffer::USAGE_SW_WRITE_OFTEN |
android::GraphicBuffer::USAGE_HW_TEXTURE;
switch (mFormat) {
case gfx::SurfaceFormat::R8G8B8A8:
format = android::PIXEL_FORMAT_RGBA_8888;
break;
case gfx::SurfaceFormat::B8G8R8A8:
format = android::PIXEL_FORMAT_RGBA_8888;
mFlags |= TextureFlags::RB_SWAPPED;
break;
case gfx::SurfaceFormat::R8G8B8X8:
format = android::PIXEL_FORMAT_RGBX_8888;
break;
case gfx::SurfaceFormat::B8G8R8X8:
format = android::PIXEL_FORMAT_RGBX_8888;
mFlags |= TextureFlags::RB_SWAPPED;
break;
case gfx::SurfaceFormat::R5G6B5_UINT16:
format = android::PIXEL_FORMAT_RGB_565;
break;
case gfx::SurfaceFormat::YUV:
format = HAL_PIXEL_FORMAT_YV12;
break;
case gfx::SurfaceFormat::A8:
NS_WARNING("gralloc does not support gfx::SurfaceFormat::A8");
return false;
default:
NS_WARNING("Unsupported surface format");
return false;
int32_t maxSize = aAllocator->GetMaxTextureSize();
if (aSize.width > maxSize || aSize.height > maxSize) {
return nullptr;
}
return AllocateGralloc(aSize, format, usage);
}
bool
GrallocTextureClientOGL::AllocateForYCbCr(gfx::IntSize aYSize, gfx::IntSize aCbCrSize, StereoMode aStereoMode)
{
MOZ_ASSERT(IsValid());
return AllocateGralloc(aYSize,
HAL_PIXEL_FORMAT_YV12,
android::GraphicBuffer::USAGE_SW_READ_OFTEN);
}
bool
GrallocTextureClientOGL::AllocateForGLRendering(gfx::IntSize aSize)
{
MOZ_ASSERT(IsValid());
uint32_t format;
uint32_t usage = android::GraphicBuffer::USAGE_HW_RENDER |
android::GraphicBuffer::USAGE_HW_TEXTURE;
switch (mFormat) {
case gfx::SurfaceFormat::R8G8B8A8:
case gfx::SurfaceFormat::B8G8R8A8:
format = android::PIXEL_FORMAT_RGBA_8888;
gfx::SurfaceFormat format;
switch (aAndroidFormat) {
case android::PIXEL_FORMAT_RGBA_8888:
format = gfx::SurfaceFormat::B8G8R8A8;
break;
case gfx::SurfaceFormat::R8G8B8X8:
case gfx::SurfaceFormat::B8G8R8X8:
// there is no android BGRX format?
format = android::PIXEL_FORMAT_RGBX_8888;
case android::PIXEL_FORMAT_BGRA_8888:
format = gfx::SurfaceFormat::B8G8R8A8;
break;
case gfx::SurfaceFormat::R5G6B5_UINT16:
format = android::PIXEL_FORMAT_RGB_565;
case android::PIXEL_FORMAT_RGBX_8888:
format = gfx::SurfaceFormat::B8G8R8X8;
break;
case android::PIXEL_FORMAT_RGB_565:
format = gfx::SurfaceFormat::R5G6B5_UINT16;
break;
case HAL_PIXEL_FORMAT_YV12:
format = gfx::SurfaceFormat::YUV;
break;
default:
NS_WARNING("Unsupported surface format");
return false;
format = gfx::SurfaceFormat::UNKNOWN;
}
return AllocateGralloc(aSize, format, usage);
}
bool
GrallocTextureClientOGL::AllocateGralloc(gfx::IntSize aSize,
uint32_t aAndroidFormat,
uint32_t aUsage)
{
MOZ_ASSERT(IsValid());
ISurfaceAllocator* allocator = GetAllocator();
if (DisableGralloc(format, aSize)) {
return nullptr;
}
MaybeMagicGrallocBufferHandle handle;
bool allocateResult =
allocator->AllocGrallocBuffer(aSize,
aAndroidFormat,
aUsage,
&handle);
if (!allocateResult) {
return false;
if (!aAllocator->AllocGrallocBuffer(aSize, aAndroidFormat, aUsage, &handle)) {
return nullptr;
}
sp<GraphicBuffer> graphicBuffer = GetGraphicBufferFrom(handle);
if (!graphicBuffer.get()) {
return false;
return nullptr;
}
if (graphicBuffer->initCheck() != NO_ERROR) {
return false;
return nullptr;
}
mGrallocHandle = handle;
mGraphicBuffer = graphicBuffer;
mSize = aSize;
return true;
return new GrallocTextureData(handle, aSize, format, aMoz2dBackend);
}
bool
GrallocTextureClientOGL::IsAllocated() const
// static
GrallocTextureData*
GrallocTextureData::CreateForDrawing(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
gfx::BackendType aMoz2dBackend,
ISurfaceAllocator* aAllocator)
{
return !!mGraphicBuffer.get();
if (DisableGralloc(aFormat, aSize)) {
return nullptr;
}
uint32_t usage = android::GraphicBuffer::USAGE_SW_READ_OFTEN |
android::GraphicBuffer::USAGE_SW_WRITE_OFTEN |
android::GraphicBuffer::USAGE_HW_TEXTURE;
auto data = GrallocTextureData::Create(aSize, GetAndroidFormat(aFormat),
aMoz2dBackend, usage, aAllocator);
if (!data) {
return nullptr;
}
DebugOnly<gfx::SurfaceFormat> grallocFormat =
SurfaceFormatForPixelFormat(data->mGraphicBuffer->getPixelFormat());
// mFormat may be different from the format the graphic buffer reports if we
// swap the R and B channels but we should always have at least the same bytes
// per pixel!
MOZ_ASSERT(BytesPerPixel(data->mFormat) == BytesPerPixel(grallocFormat));
return data;
}
/*static*/ already_AddRefed<TextureClient>
GrallocTextureClientOGL::FromSharedSurface(gl::SharedSurface* abstractSurf,
TextureFlags flags)
already_AddRefed<TextureClient>
CreateGrallocTextureClientForDrawing(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
gfx::BackendType aMoz2dBackend,
TextureFlags aFlags,
ISurfaceAllocator* aAllocator)
{
TextureData* data = GrallocTextureData::CreateForDrawing(aSize, aFormat, aMoz2dBackend,
aAllocator);
if (!data) {
return nullptr;
}
if (IsGrallocRBSwapped(aFormat)) {
aFlags |= TextureFlags::RB_SWAPPED;
}
return MakeAndAddRef<ClientTexture>(data, aFlags, aAllocator);
}
// static
GrallocTextureData*
GrallocTextureData::CreateForYCbCr(gfx::IntSize aYSize, gfx::IntSize aCbCrSize,
ISurfaceAllocator* aAllocator)
{
MOZ_ASSERT(aYSize.width == aCbCrSize.width * 2);
MOZ_ASSERT(aYSize.height == aCbCrSize.height * 2);
return GrallocTextureData::Create(aYSize, HAL_PIXEL_FORMAT_YV12,
gfx::BackendType::NONE,
android::GraphicBuffer::USAGE_SW_READ_OFTEN,
aAllocator);
}
// static
GrallocTextureData*
GrallocTextureData::CreateForGLRendering(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
ISurfaceAllocator* aAllocator)
{
if (aFormat == gfx::SurfaceFormat::YUV) {
return nullptr;
}
uint32_t usage = android::GraphicBuffer::USAGE_HW_RENDER |
android::GraphicBuffer::USAGE_HW_TEXTURE;
return GrallocTextureData::Create(aSize, GetAndroidFormat(aFormat),
gfx::BackendType::NONE, usage, aAllocator);
}
// static
already_AddRefed<TextureClient>
GrallocTextureData::TextureClientFromSharedSurface(gl::SharedSurface* abstractSurf,
TextureFlags flags)
{
auto surf = gl::SharedSurface_Gralloc::Cast(abstractSurf);
@@ -397,6 +414,18 @@ GrallocTextureClientOGL::FromSharedSurface(gl::SharedSurface* abstractSurf,
return ret.forget();
}
TextureData*
GrallocTextureData::CreateSimilar(ISurfaceAllocator* aAllocator,
TextureFlags aFlags,
TextureAllocationFlags aAllocFlags) const
{
if (mFormat == gfx::SurfaceFormat::YUV) {
return GrallocTextureData::CreateForYCbCr(mSize, mSize*2, aAllocator);
} else {
return GrallocTextureData::CreateForDrawing(mSize, mFormat, mMoz2DBackend, aAllocator);
}
}
} // namesapace layers
} // namesapace mozilla