mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-17 06:09:19 +00:00
![Jamie Nicol](/assets/img/avatar_default.png)
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
259 lines
7.7 KiB
C++
259 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 "RenderAndroidHardwareBufferTextureHost.h"
|
|
|
|
#include "mozilla/layers/AndroidHardwareBuffer.h"
|
|
#include "mozilla/layers/TextureHostOGL.h"
|
|
#include "mozilla/webrender/RenderThread.h"
|
|
#include "mozilla/gfx/2D.h"
|
|
#include "GLContextEGL.h"
|
|
#include "GLLibraryEGL.h"
|
|
#include "GLReadTexImageHelper.h"
|
|
#include "OGLShaderConfig.h"
|
|
|
|
namespace mozilla {
|
|
namespace wr {
|
|
|
|
RenderAndroidHardwareBufferTextureHost::RenderAndroidHardwareBufferTextureHost(
|
|
layers::AndroidHardwareBuffer* aAndroidHardwareBuffer)
|
|
: mAndroidHardwareBuffer(aAndroidHardwareBuffer),
|
|
mEGLImage(EGL_NO_IMAGE),
|
|
mTextureHandle(0) {
|
|
MOZ_ASSERT(mAndroidHardwareBuffer);
|
|
MOZ_COUNT_CTOR_INHERITED(RenderAndroidHardwareBufferTextureHost,
|
|
RenderTextureHost);
|
|
}
|
|
|
|
RenderAndroidHardwareBufferTextureHost::
|
|
~RenderAndroidHardwareBufferTextureHost() {
|
|
MOZ_COUNT_DTOR_INHERITED(RenderAndroidHardwareBufferTextureHost,
|
|
RenderTextureHost);
|
|
DeleteTextureHandle();
|
|
DestroyEGLImage();
|
|
}
|
|
|
|
gfx::IntSize RenderAndroidHardwareBufferTextureHost::GetSize() const {
|
|
if (mAndroidHardwareBuffer) {
|
|
return mAndroidHardwareBuffer->mSize;
|
|
}
|
|
return gfx::IntSize();
|
|
}
|
|
|
|
bool RenderAndroidHardwareBufferTextureHost::EnsureLockable() {
|
|
if (!mAndroidHardwareBuffer) {
|
|
return false;
|
|
}
|
|
|
|
auto fenceFd = mAndroidHardwareBuffer->GetAndResetAcquireFence();
|
|
if (fenceFd.IsValid()) {
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
const auto& egl = gle->mEgl;
|
|
|
|
auto rawFD = fenceFd.TakePlatformHandle();
|
|
const EGLint attribs[] = {LOCAL_EGL_SYNC_NATIVE_FENCE_FD_ANDROID,
|
|
rawFD.get(), LOCAL_EGL_NONE};
|
|
|
|
EGLSync sync =
|
|
egl->fCreateSync(LOCAL_EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
|
|
if (sync) {
|
|
// Release fd here, since it is owned by EGLSync
|
|
Unused << rawFD.release();
|
|
|
|
if (egl->IsExtensionSupported(gl::EGLExtension::KHR_wait_sync)) {
|
|
egl->fWaitSync(sync, 0);
|
|
} else {
|
|
egl->fClientWaitSync(sync, 0, LOCAL_EGL_FOREVER);
|
|
}
|
|
egl->fDestroySync(sync);
|
|
} else {
|
|
gfxCriticalNote << "Failed to create EGLSync from acquire fence fd";
|
|
}
|
|
}
|
|
|
|
if (mTextureHandle) {
|
|
return true;
|
|
}
|
|
|
|
if (!mEGLImage) {
|
|
// XXX add crop handling for video
|
|
// Should only happen the first time.
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
const auto& egl = gle->mEgl;
|
|
|
|
const EGLint attrs[] = {
|
|
LOCAL_EGL_IMAGE_PRESERVED,
|
|
LOCAL_EGL_TRUE,
|
|
LOCAL_EGL_NONE,
|
|
LOCAL_EGL_NONE,
|
|
};
|
|
|
|
EGLClientBuffer clientBuffer = egl->mLib->fGetNativeClientBufferANDROID(
|
|
mAndroidHardwareBuffer->GetNativeBuffer());
|
|
mEGLImage = egl->fCreateImage(
|
|
EGL_NO_CONTEXT, LOCAL_EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
|
|
}
|
|
MOZ_ASSERT(mEGLImage);
|
|
|
|
mGL->fGenTextures(1, &mTextureHandle);
|
|
mGL->fBindTexture(LOCAL_GL_TEXTURE_EXTERNAL, mTextureHandle);
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_EXTERNAL, LOCAL_GL_TEXTURE_WRAP_T,
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_EXTERNAL, LOCAL_GL_TEXTURE_WRAP_S,
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
mGL->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_EXTERNAL, mEGLImage);
|
|
|
|
ActivateBindAndTexParameteri(mGL, LOCAL_GL_TEXTURE0,
|
|
LOCAL_GL_TEXTURE_EXTERNAL_OES, mTextureHandle);
|
|
return true;
|
|
}
|
|
|
|
wr::WrExternalImage RenderAndroidHardwareBufferTextureHost::Lock(
|
|
uint8_t aChannelIndex, gl::GLContext* aGL) {
|
|
MOZ_ASSERT(aChannelIndex == 0);
|
|
|
|
if (mGL.get() != aGL) {
|
|
if (mGL) {
|
|
// This should not happen.
|
|
MOZ_ASSERT_UNREACHABLE("Unexpected GL context");
|
|
return InvalidToWrExternalImage();
|
|
}
|
|
mGL = aGL;
|
|
}
|
|
|
|
if (!mGL || !mGL->MakeCurrent()) {
|
|
return InvalidToWrExternalImage();
|
|
}
|
|
|
|
if (!EnsureLockable()) {
|
|
return InvalidToWrExternalImage();
|
|
}
|
|
|
|
const gfx::IntSize size = GetSize();
|
|
return NativeTextureToWrExternalImage(mTextureHandle, 0.0, 0.0,
|
|
static_cast<float>(size.width),
|
|
static_cast<float>(size.height));
|
|
}
|
|
|
|
void RenderAndroidHardwareBufferTextureHost::Unlock() {}
|
|
|
|
size_t RenderAndroidHardwareBufferTextureHost::Bytes() {
|
|
return GetSize().width * GetSize().height *
|
|
BytesPerPixel(mAndroidHardwareBuffer->mFormat);
|
|
}
|
|
|
|
void RenderAndroidHardwareBufferTextureHost::DeleteTextureHandle() {
|
|
if (!mTextureHandle) {
|
|
return;
|
|
}
|
|
MOZ_ASSERT(mGL);
|
|
mGL->fDeleteTextures(1, &mTextureHandle);
|
|
mTextureHandle = 0;
|
|
}
|
|
|
|
void RenderAndroidHardwareBufferTextureHost::DestroyEGLImage() {
|
|
if (!mEGLImage) {
|
|
return;
|
|
}
|
|
MOZ_ASSERT(mGL);
|
|
const auto& gle = gl::GLContextEGL::Cast(mGL);
|
|
const auto& egl = gle->mEgl;
|
|
egl->fDestroyImage(mEGLImage);
|
|
mEGLImage = EGL_NO_IMAGE;
|
|
}
|
|
|
|
gfx::SurfaceFormat RenderAndroidHardwareBufferTextureHost::GetFormat() const {
|
|
MOZ_ASSERT(mAndroidHardwareBuffer->mFormat == gfx::SurfaceFormat::R8G8B8A8 ||
|
|
mAndroidHardwareBuffer->mFormat == gfx::SurfaceFormat::R8G8B8X8);
|
|
|
|
if (mAndroidHardwareBuffer->mFormat == gfx::SurfaceFormat::R8G8B8A8) {
|
|
return gfx::SurfaceFormat::B8G8R8A8;
|
|
}
|
|
|
|
if (mAndroidHardwareBuffer->mFormat == gfx::SurfaceFormat::R8G8B8X8) {
|
|
return gfx::SurfaceFormat::B8G8R8X8;
|
|
}
|
|
|
|
gfxCriticalNoteOnce
|
|
<< "Unexpected color format of RenderAndroidSurfaceTextureHost";
|
|
|
|
return gfx::SurfaceFormat::UNKNOWN;
|
|
}
|
|
|
|
already_AddRefed<gfx::DataSourceSurface>
|
|
RenderAndroidHardwareBufferTextureHost::ReadTexImage() {
|
|
if (!mGL) {
|
|
mGL = RenderThread::Get()->SingletonGL();
|
|
if (!mGL) {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
if (!EnsureLockable()) {
|
|
return nullptr;
|
|
}
|
|
|
|
/* Allocate resulting image surface */
|
|
int32_t stride = GetSize().width * BytesPerPixel(GetFormat());
|
|
RefPtr<gfx::DataSourceSurface> surf =
|
|
gfx::Factory::CreateDataSourceSurfaceWithStride(GetSize(), GetFormat(),
|
|
stride);
|
|
if (!surf) {
|
|
return nullptr;
|
|
}
|
|
|
|
layers::ShaderConfigOGL config = layers::ShaderConfigFromTargetAndFormat(
|
|
LOCAL_GL_TEXTURE_EXTERNAL, mAndroidHardwareBuffer->mFormat);
|
|
int shaderConfig = config.mFeatures;
|
|
|
|
bool ret = mGL->ReadTexImageHelper()->ReadTexImage(
|
|
surf, mTextureHandle, LOCAL_GL_TEXTURE_EXTERNAL, GetSize(),
|
|
gfx::Matrix4x4(), shaderConfig, /* aYInvert */ false);
|
|
if (!ret) {
|
|
return nullptr;
|
|
}
|
|
|
|
return surf.forget();
|
|
}
|
|
|
|
bool RenderAndroidHardwareBufferTextureHost::MapPlane(
|
|
RenderCompositor* aCompositor, uint8_t aChannelIndex,
|
|
PlaneInfo& aPlaneInfo) {
|
|
RefPtr<gfx::DataSourceSurface> readback = ReadTexImage();
|
|
if (!readback) {
|
|
return false;
|
|
}
|
|
|
|
gfx::DataSourceSurface::MappedSurface map;
|
|
if (!readback->Map(gfx::DataSourceSurface::MapType::READ, &map)) {
|
|
return false;
|
|
}
|
|
|
|
mReadback = readback;
|
|
aPlaneInfo.mSize = GetSize();
|
|
aPlaneInfo.mStride = map.mStride;
|
|
aPlaneInfo.mData = map.mData;
|
|
return true;
|
|
}
|
|
|
|
void RenderAndroidHardwareBufferTextureHost::UnmapPlanes() {
|
|
if (mReadback) {
|
|
mReadback->Unmap();
|
|
mReadback = nullptr;
|
|
}
|
|
}
|
|
|
|
RefPtr<layers::TextureSource>
|
|
RenderAndroidHardwareBufferTextureHost::CreateTextureSource(
|
|
layers::TextureSourceProvider* aProvider) {
|
|
return new layers::AndroidHardwareBufferTextureSource(
|
|
aProvider, mAndroidHardwareBuffer, mAndroidHardwareBuffer->mFormat,
|
|
LOCAL_GL_TEXTURE_EXTERNAL, LOCAL_GL_CLAMP_TO_EDGE, GetSize());
|
|
}
|
|
|
|
} // namespace wr
|
|
} // namespace mozilla
|