Files
palemoon27/gfx/layers/opengl/GrallocTextureClient.cpp
T
roytam1 cbd579ebc8 import changes from wicknix/Arctic-Fox:
- backport of Bug 1188696 - Remove the XPCOM dependencies in nsRefPtr.h (6c2860799)
- backport of Bug 1188696 - Hoist nsRefPtr.h into MFBT (e892acb58)
- Backport of Bug 1138967 - Part 3: Add D3D11 YCbCr texture clients and upload on the client side (2e4218167)
- Bug 1038536 - Flatten image/src/ directory (3adb2d635)
- Bug 1038536 - Flatten image/public/ directory (22329f3b4)
- Bug 1038536 - Flatten image/decoders/icon/qt/public/ directory. (7b5b8b2af)
- Bug 1038536 - Update header guard after flatten image/build (20e1614ac)
- Bug 1116905 - part 2 - add MakeAndAddRef helper function to facilitate constructing TemporaryRef (9c85f45a0)
- update (ab2c6eccf)
- Bug 1139781 - Implement VideoPlaybackQuality for MediaCodecReader. (1a7c6c0a7)
- Bug 1138825 - Fix the crash at mAudioPromise: call decode audio data when the audio queue is empty and check the mAudioPromise is empty or not. (3f5d3a1c5)
- Bug 875247 - Add support for DXVA2 via D3D11 (2ca491206)
- Bug 1145513 - Upload YCbCr image data on the client side when using d3d9 layers. (50f7a69fa)
- Bug 1053563 - Use a static create function to replace InitWith for TextureClient. (dd1c8fc89)
- Bug 1145764 - Add some default-disabled logging to TextureClientPool (1cfc0d1b9)
- Bug 1120780 - Fallback on lock_ycbcr when ColorConvertor fails (3a9b893f0)
- Bug 1161815: Use a single ID2D1SolidColorBrush per DrawTarget. (a70b72ef6)
- Bug 1160485 - remove implicit conversion from RefPtr<T> to TemporaryR…ef<T>; (e6e6224c5)
- Bug 1116905 - part 3 - remove dependence on implicit conversion from T* to TemporaryRef<T>, gfx changes; (f71d3ffa8)
- Bug 1116905 - part 1 - remove dependence on implicit conversion from T* to TemporaryRef<T>, non-gfx changes; (f66714955)
- implement Event.srcElement as alias (6c1ee1c6d)
- 1116905 - part 4 - remove implicit conversion from non-nullptr T* to TemporaryRef<T> (f94c680f9)
- Bug 1031152 - Define a JS public API for working with SavedFrame instances (2aa41721a)
- Update TLD's from ESR 60 (b9dbe0ca3)
with some changes to fix building, reported to upstream:
- https://github.com/wicknix/Arctic-Fox/commit/2e421816773b6a57502907ab22c285d994d8b024#r31893045
- https://github.com/wicknix/Arctic-Fox/commit/50f7a69fa9a36634aef1ae5a221415ca98284435#r31892913
- https://github.com/wicknix/Arctic-Fox/commit/f667149556ae0e64c9dbce08836bb5d957db2464#r31892928
2019-01-10 15:30:35 +08:00

380 lines
10 KiB
C++

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef MOZ_WIDGET_GONK
#include "mozilla/gfx/2D.h"
#include "mozilla/layers/AsyncTransactionTracker.h" // for AsyncTransactionTracker
#include "mozilla/layers/GrallocTextureClient.h"
#include "mozilla/layers/CompositableForwarder.h"
#include "mozilla/layers/ISurfaceAllocator.h"
#include "mozilla/layers/ShadowLayerUtilsGralloc.h"
#include "gfx2DGlue.h"
#include "SharedSurfaceGralloc.h"
namespace mozilla {
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))
{
MOZ_COUNT_CTOR(GrallocTextureClientOGL);
}
GrallocTextureClientOGL::~GrallocTextureClientOGL()
{
MOZ_COUNT_DTOR(GrallocTextureClientOGL);
ISurfaceAllocator* allocator = GetAllocator();
if (ShouldDeallocateInDestructor()) {
allocator->DeallocGrallocBuffer(&mGrallocHandle);
} else {
allocator->DropGrallocBuffer(&mGrallocHandle);
}
}
TemporaryRef<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, mSize, mIsOpaque);
return true;
}
void
GrallocTextureClientOGL::SetRemoveFromCompositableTracker(AsyncTransactionTracker* aTracker)
{
mRemoveFromCompositableTracker = aTracker;
}
void
GrallocTextureClientOGL::WaitForBufferOwnership(bool aWaitReleaseFence)
{
if (mRemoveFromCompositableTracker) {
mRemoveFromCompositableTracker->WaitComplete();
mRemoveFromCompositableTracker = nullptr;
}
if (!aWaitReleaseFence) {
return;
}
#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17
if (mReleaseFenceHandle.IsValid()) {
android::sp<Fence> fence = mReleaseFenceHandle.mFence;
#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(IsValid());
if (!IsValid() || !IsAllocated()) {
return false;
}
if (mMappedBuffer) {
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
android::sp<Fence> fence = android::Fence::NO_FENCE;
if (mReleaseFenceHandle.IsValid()) {
fence = mReleaseFenceHandle.mFence;
}
mReleaseFenceHandle = FenceHandle();
int32_t rv = mGraphicBuffer->lockAsync(usage,
reinterpret_cast<void**>(&mMappedBuffer),
fence->dup());
#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 BufferTextureClient::Lock(aMode);
}
void
GrallocTextureClientOGL::Unlock()
{
BufferTextureClient::Unlock();
mDrawTarget = nullptr;
if (mMappedBuffer) {
mMappedBuffer = nullptr;
mGraphicBuffer->unlock();
}
}
uint8_t*
GrallocTextureClientOGL::GetBuffer() const
{
MOZ_ASSERT(IsValid());
NS_WARN_IF_FALSE(mMappedBuffer, "Trying to get a gralloc buffer without getting the lock?");
return mMappedBuffer;
}
static gfx::SurfaceFormat
SurfaceFormatForPixelFormat(android::PixelFormat aFormat)
{
switch (aFormat) {
case PIXEL_FORMAT_RGBA_8888:
return gfx::SurfaceFormat::R8G8B8A8;
case PIXEL_FORMAT_BGRA_8888:
return gfx::SurfaceFormat::B8G8R8A8;
case PIXEL_FORMAT_RGBX_8888:
return gfx::SurfaceFormat::R8G8B8X8;
case PIXEL_FORMAT_RGB_565:
return gfx::SurfaceFormat::R5G6B5;
case HAL_PIXEL_FORMAT_YV12:
return gfx::SurfaceFormat::YUV;
default:
MOZ_CRASH("Unknown gralloc pixel format");
}
return gfx::SurfaceFormat::R8G8B8A8;
}
gfx::DrawTarget*
GrallocTextureClientOGL::BorrowDrawTarget()
{
MOZ_ASSERT(IsValid());
MOZ_ASSERT(mMappedBuffer, "Calling TextureClient::BorrowDrawTarget without locking :(");
if (!IsValid() || !IsAllocated() || !mMappedBuffer) {
return nullptr;
}
if (mDrawTarget) {
return mDrawTarget;
}
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;
}
bool
GrallocTextureClientOGL::AllocateForSurface(gfx::IntSize aSize,
TextureAllocationFlags)
{
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:
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;
}
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;
break;
case gfx::SurfaceFormat::R8G8B8X8:
case gfx::SurfaceFormat::B8G8R8X8:
// there is no android BGRX format?
format = android::PIXEL_FORMAT_RGBX_8888;
break;
case gfx::SurfaceFormat::R5G6B5:
format = android::PIXEL_FORMAT_RGB_565;
break;
default:
NS_WARNING("Unsupported surface format");
return false;
}
return AllocateGralloc(aSize, format, usage);
}
bool
GrallocTextureClientOGL::AllocateGralloc(gfx::IntSize aSize,
uint32_t aAndroidFormat,
uint32_t aUsage)
{
MOZ_ASSERT(IsValid());
ISurfaceAllocator* allocator = GetAllocator();
MaybeMagicGrallocBufferHandle handle;
bool allocateResult =
allocator->AllocGrallocBuffer(aSize,
aAndroidFormat,
aUsage,
&handle);
if (!allocateResult) {
return false;
}
sp<GraphicBuffer> graphicBuffer = GetGraphicBufferFrom(handle);
if (!graphicBuffer.get()) {
return false;
}
if (graphicBuffer->initCheck() != NO_ERROR) {
return false;
}
mGrallocHandle = handle;
mGraphicBuffer = graphicBuffer;
mSize = aSize;
return true;
}
bool
GrallocTextureClientOGL::IsAllocated() const
{
return !!mGraphicBuffer.get();
}
bool
GrallocTextureClientOGL::Allocate(uint32_t aSize)
{
// see Bug 908196
MOZ_CRASH("This method should never be called.");
return false;
}
size_t
GrallocTextureClientOGL::GetBufferSize() const
{
// see Bug 908196
MOZ_CRASH("This method should never be called.");
return 0;
}
/*static*/ TemporaryRef<TextureClient>
GrallocTextureClientOGL::FromSharedSurface(gl::SharedSurface* abstractSurf,
TextureFlags flags)
{
auto surf = gl::SharedSurface_Gralloc::Cast(abstractSurf);
RefPtr<TextureClient> ret = surf->GetTextureClient();
TextureFlags mask = TextureFlags::ORIGIN_BOTTOM_LEFT |
TextureFlags::RB_SWAPPED |
TextureFlags::NON_PREMULTIPLIED;
TextureFlags required = flags & mask;
TextureFlags present = ret->GetFlags() & mask;
if (present != required) {
printf_stderr("Present flags: 0x%x. Required: 0x%x.\n",
(uint32_t)present,
(uint32_t)required);
MOZ_CRASH("Flag requirement mismatch.");
}
return ret.forget();
}
} // namesapace layers
} // namesapace mozilla
#endif // MOZ_WIDGET_GONK