mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-12-01 00:32:11 +00:00
0c43a18e35
On Android, SurfaceTextures provide a transform that should be applied to texture coordinates when sampling from the texture. Usually this is simply a y-flip, but sometimes it includes a scale and slight translation, eg when the video frame is contained within a larger texture. Previously we ignored this transform but performed a y-flip, meaning we rendered correctly most of the time, but not all of the time. Our first attempt to fix this was in bug 1731980. When rendering as a compositor surface with RenderCompositorOGLSWGL, we supplied the transform to CompositorOGL's shaders, which correctly fixed the bug for this rendering path. However, the attempted fix for hardware webrender in fact made things worse. As UV coordinates are supplied to webrender unnormalized, then the shaders normalize them by dividing by the actual texture size, this effectively handled the scale component of the transform. (Though not quite scaling by the correct amount, and ignoring the translation component, sometimes resulting in a pixel-wide green seam being visible at the video's edges.) When we additionally applied the transformation to the coordinates, it resulted in the scale being applied twice, and the video being rendered too far zoomed in. To make matters worse, when we received subsequent bug reports of incorrect rendering on various devices we mistakenly assumed that the devices must be buggy, rather than our code being incorrect. We therefore reverted to ignoring the transform on these devices, thereby breaking the software webrender path again. Additionally, on devices without GL_OES_EGL_image_external_essl3 support, we must sample from the SurfaceTexture using an ESSL1 shader. This means we do not have access to the correct texture size, meaning we cannot correctly normalize the UV coordinates. This results in the video being rendered too far zoomed out. And in the non-compositor-surface software webrender path, we were accidentally downscaling the texture when reading back into a CPU buffer, resulting in the video being rendered at the correct zoom, but being very blurry. This patch aims to handle the transform correctly, in all rendering paths, hopefully once and for all. For hardware webrender, we now supply the texture coordinates to webrender already normalized, using the functionality added in the previous patch. This avoids the shaders scaling the coordinates again, or using an incorrect texture size to do so. For RenderCompositorOGLSWGL, we continue to apply the transform using CompositorOGL's shaders. In the non-compositor-surface software webrender path, we make GLReadPixelsHelper apply the transform when reading from the SurfaceTexture in to the CPU buffer. Again using functionality added earlier in this patch series. This avoids downscaling the image. We can then provide the default untransformed and unnormalized UVs to webrender. As a result we can now remove the virtual function RenderTextureHost::GetUvCoords(), added in bug 1731980, as it no longer serves any purpose: we no longer want to share the implementation between RenderAndroidSurfaceTextureHost::Lock and RenderTextureHostSWGL::LockSWGL. Finally, we remove all transform overrides on the devices we mistakenly assumed were buggy. Differential Revision: https://phabricator.services.mozilla.com/D220582
267 lines
7.7 KiB
C++
267 lines
7.7 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* 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/. */
|
|
|
|
#include "RenderExternalTextureHost.h"
|
|
|
|
#include "mozilla/gfx/Logging.h"
|
|
#include "mozilla/layers/ImageDataSerializer.h"
|
|
|
|
#include "GLContext.h"
|
|
|
|
namespace mozilla {
|
|
namespace wr {
|
|
|
|
RenderExternalTextureHost::RenderExternalTextureHost(
|
|
uint8_t* aBuffer, const layers::BufferDescriptor& aDescriptor)
|
|
: mBuffer(aBuffer),
|
|
mDescriptor(aDescriptor),
|
|
mInitialized(false),
|
|
mTextureUpdateNeeded(true) {
|
|
MOZ_COUNT_CTOR_INHERITED(RenderExternalTextureHost, RenderTextureHost);
|
|
|
|
switch (mDescriptor.type()) {
|
|
case layers::BufferDescriptor::TYCbCrDescriptor: {
|
|
const layers::YCbCrDescriptor& ycbcr = mDescriptor.get_YCbCrDescriptor();
|
|
mSize = ycbcr.display().Size();
|
|
mFormat = gfx::SurfaceFormat::YUV420;
|
|
break;
|
|
}
|
|
case layers::BufferDescriptor::TRGBDescriptor: {
|
|
const layers::RGBDescriptor& rgb = mDescriptor.get_RGBDescriptor();
|
|
mSize = rgb.size();
|
|
mFormat = rgb.format();
|
|
break;
|
|
}
|
|
default:
|
|
gfxCriticalError() << "Bad buffer host descriptor "
|
|
<< (int)mDescriptor.type();
|
|
MOZ_CRASH("GFX: Bad descriptor");
|
|
}
|
|
}
|
|
|
|
RenderExternalTextureHost::~RenderExternalTextureHost() {
|
|
MOZ_COUNT_DTOR_INHERITED(RenderExternalTextureHost, RenderTextureHost);
|
|
|
|
if (NS_WARN_IF(!IsReadyForDeletion())) {
|
|
gfxCriticalNote << "RenderExternalTextureHost sync failed";
|
|
}
|
|
|
|
DeleteTextures();
|
|
}
|
|
|
|
bool RenderExternalTextureHost::CreateSurfaces() {
|
|
if (!IsYUV()) {
|
|
mSurfaces[0] = gfx::Factory::CreateWrappingDataSourceSurface(
|
|
GetBuffer(),
|
|
layers::ImageDataSerializer::GetRGBStride(
|
|
mDescriptor.get_RGBDescriptor()),
|
|
mSize, mFormat);
|
|
} else {
|
|
const layers::YCbCrDescriptor& desc = mDescriptor.get_YCbCrDescriptor();
|
|
const gfx::SurfaceFormat surfaceFormat =
|
|
SurfaceFormatForColorDepth(desc.colorDepth());
|
|
auto cbcrSize = layers::ImageDataSerializer::GetCroppedCbCrSize(desc);
|
|
|
|
mSurfaces[0] = gfx::Factory::CreateWrappingDataSourceSurface(
|
|
layers::ImageDataSerializer::GetYChannel(GetBuffer(), desc),
|
|
desc.yStride(), desc.display().Size(), surfaceFormat);
|
|
mSurfaces[1] = gfx::Factory::CreateWrappingDataSourceSurface(
|
|
layers::ImageDataSerializer::GetCbChannel(GetBuffer(), desc),
|
|
desc.cbCrStride(), cbcrSize, surfaceFormat);
|
|
mSurfaces[2] = gfx::Factory::CreateWrappingDataSourceSurface(
|
|
layers::ImageDataSerializer::GetCrChannel(GetBuffer(), desc),
|
|
desc.cbCrStride(), cbcrSize, surfaceFormat);
|
|
}
|
|
|
|
for (size_t i = 0; i < PlaneCount(); ++i) {
|
|
if (NS_WARN_IF(!mSurfaces[i])) {
|
|
gfxCriticalNote << "Surface is null";
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void RenderExternalTextureHost::DeleteSurfaces() {
|
|
for (size_t i = 0; i < PlaneCount(); ++i) {
|
|
mSurfaces[i] = nullptr;
|
|
}
|
|
}
|
|
|
|
void RenderExternalTextureHost::DeleteTextures() {
|
|
for (size_t i = 0; i < PlaneCount(); ++i) {
|
|
mTextureSources[i] = nullptr;
|
|
mImages[i] = InvalidToWrExternalImage();
|
|
}
|
|
}
|
|
|
|
bool RenderExternalTextureHost::InitializeIfNeeded() {
|
|
if (mInitialized) {
|
|
return true;
|
|
}
|
|
|
|
if (!GetBuffer()) {
|
|
// We hit some problems to get the shmem.
|
|
gfxCriticalNote << "GetBuffer Failed";
|
|
return false;
|
|
}
|
|
|
|
if (!CreateSurfaces()) {
|
|
DeleteSurfaces();
|
|
return false;
|
|
}
|
|
|
|
mInitialized = true;
|
|
return mInitialized;
|
|
}
|
|
|
|
bool RenderExternalTextureHost::IsReadyForDeletion() {
|
|
if (!mInitialized) {
|
|
return true;
|
|
}
|
|
|
|
auto& textureSource = mTextureSources[0];
|
|
if (textureSource) {
|
|
return textureSource->Sync(false);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
wr::WrExternalImage RenderExternalTextureHost::Lock(uint8_t aChannelIndex,
|
|
gl::GLContext* aGL) {
|
|
if (mGL.get() != aGL) {
|
|
mGL = aGL;
|
|
mGL->MakeCurrent();
|
|
}
|
|
|
|
if (!mGL || !mGL->MakeCurrent()) {
|
|
return InvalidToWrExternalImage();
|
|
}
|
|
|
|
if (!InitializeIfNeeded()) {
|
|
return InvalidToWrExternalImage();
|
|
}
|
|
|
|
UpdateTextures();
|
|
return mImages[aChannelIndex];
|
|
}
|
|
|
|
void RenderExternalTextureHost::PrepareForUse() { mTextureUpdateNeeded = true; }
|
|
|
|
void RenderExternalTextureHost::Unlock() {}
|
|
|
|
void RenderExternalTextureHost::UpdateTexture(size_t aIndex) {
|
|
MOZ_ASSERT(mSurfaces[aIndex]);
|
|
|
|
auto& texture = mTextureSources[aIndex];
|
|
|
|
if (texture) {
|
|
texture->Update(mSurfaces[aIndex]);
|
|
} else {
|
|
texture = new layers::DirectMapTextureSource(mGL, mSurfaces[aIndex]);
|
|
|
|
const GLuint handle = texture->GetTextureHandle();
|
|
const gfx::IntSize size = texture->GetSize();
|
|
mImages[aIndex] = NativeTextureToWrExternalImage(
|
|
handle, 0.0, 0.0, static_cast<float>(size.width),
|
|
static_cast<float>(size.height));
|
|
}
|
|
|
|
MOZ_ASSERT(mGL->GetError() == LOCAL_GL_NO_ERROR);
|
|
}
|
|
|
|
void RenderExternalTextureHost::UpdateTextures() {
|
|
if (!mTextureUpdateNeeded) {
|
|
// Nothing to do here.
|
|
return;
|
|
}
|
|
|
|
for (size_t i = 0; i < PlaneCount(); ++i) {
|
|
UpdateTexture(i);
|
|
}
|
|
|
|
mTextureSources[0]->MaybeFenceTexture();
|
|
mTextureUpdateNeeded = false;
|
|
}
|
|
|
|
size_t RenderExternalTextureHost::GetPlaneCount() const { return PlaneCount(); }
|
|
|
|
gfx::SurfaceFormat RenderExternalTextureHost::GetFormat() const {
|
|
return mFormat;
|
|
}
|
|
|
|
gfx::ColorDepth RenderExternalTextureHost::GetColorDepth() const {
|
|
switch (mDescriptor.type()) {
|
|
case layers::BufferDescriptor::TYCbCrDescriptor:
|
|
return mDescriptor.get_YCbCrDescriptor().colorDepth();
|
|
default:
|
|
return gfx::ColorDepth::COLOR_8;
|
|
}
|
|
}
|
|
|
|
gfx::YUVRangedColorSpace RenderExternalTextureHost::GetYUVColorSpace() const {
|
|
switch (mDescriptor.type()) {
|
|
case layers::BufferDescriptor::TYCbCrDescriptor:
|
|
return gfx::GetYUVRangedColorSpace(mDescriptor.get_YCbCrDescriptor());
|
|
default:
|
|
return gfx::YUVRangedColorSpace::Default;
|
|
}
|
|
}
|
|
|
|
bool RenderExternalTextureHost::MapPlane(RenderCompositor* aCompositor,
|
|
uint8_t aChannelIndex,
|
|
PlaneInfo& aPlaneInfo) {
|
|
if (!mBuffer) {
|
|
// We hit some problems to get the shmem.
|
|
gfxCriticalNote << "GetBuffer Failed";
|
|
return false;
|
|
}
|
|
|
|
switch (mDescriptor.type()) {
|
|
case layers::BufferDescriptor::TYCbCrDescriptor: {
|
|
const layers::YCbCrDescriptor& desc = mDescriptor.get_YCbCrDescriptor();
|
|
switch (aChannelIndex) {
|
|
case 0:
|
|
aPlaneInfo.mData =
|
|
layers::ImageDataSerializer::GetYChannel(mBuffer, desc);
|
|
aPlaneInfo.mStride = desc.yStride();
|
|
aPlaneInfo.mSize = desc.display().Size();
|
|
break;
|
|
case 1:
|
|
aPlaneInfo.mData =
|
|
layers::ImageDataSerializer::GetCbChannel(mBuffer, desc);
|
|
aPlaneInfo.mStride = desc.cbCrStride();
|
|
aPlaneInfo.mSize =
|
|
layers::ImageDataSerializer::GetCroppedCbCrSize(desc);
|
|
break;
|
|
case 2:
|
|
aPlaneInfo.mData =
|
|
layers::ImageDataSerializer::GetCrChannel(mBuffer, desc);
|
|
aPlaneInfo.mStride = desc.cbCrStride();
|
|
aPlaneInfo.mSize =
|
|
layers::ImageDataSerializer::GetCroppedCbCrSize(desc);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
default: {
|
|
const layers::RGBDescriptor& desc = mDescriptor.get_RGBDescriptor();
|
|
aPlaneInfo.mData = mBuffer;
|
|
aPlaneInfo.mStride = layers::ImageDataSerializer::GetRGBStride(desc);
|
|
aPlaneInfo.mSize = desc.size();
|
|
break;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void RenderExternalTextureHost::UnmapPlanes() {}
|
|
|
|
} // namespace wr
|
|
} // namespace mozilla
|