Files
tubestation/gfx/webrender_bindings/RenderAndroidSurfaceTextureHost.h
Jamie Nicol fc8a98e8d9 Bug 1913568 - Handle SurfaceTexture transforms in webrender, for reals this time. r=gfx-reviewers,media-playback-reviewers,padenot,nical
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
2024-09-09 14:06:26 +00:00

97 lines
2.8 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/. */
#ifndef MOZILLA_GFX_RENDERANDROIDSURFACETEXTUREHOST_H
#define MOZILLA_GFX_RENDERANDROIDSURFACETEXTUREHOST_H
#include "mozilla/java/GeckoSurfaceTextureWrappers.h"
#include "mozilla/layers/TextureHostOGL.h"
#include "RenderTextureHostSWGL.h"
namespace mozilla {
namespace gfx {
class DataSourceSurface;
}
namespace wr {
class RenderAndroidSurfaceTextureHost final : public RenderTextureHostSWGL {
public:
explicit RenderAndroidSurfaceTextureHost(
const java::GeckoSurfaceTexture::GlobalRef& aSurfTex, gfx::IntSize aSize,
gfx::SurfaceFormat aFormat, bool aContinuousUpdate,
Maybe<gfx::Matrix4x4> aTransformOverride, bool aIsRemoteTexture);
wr::WrExternalImage Lock(uint8_t aChannelIndex, gl::GLContext* aGL) override;
void Unlock() override;
size_t Bytes() override {
return mSize.width * mSize.height * BytesPerPixel(mFormat);
}
void PrepareForUse() override;
void NotifyForUse() override;
void NotifyNotUsed() override;
// RenderTextureHostSWGL
gfx::SurfaceFormat GetFormat() const override;
gfx::ColorDepth GetColorDepth() const override {
return gfx::ColorDepth::COLOR_8;
}
size_t GetPlaneCount() const override { return 1; }
bool MapPlane(RenderCompositor* aCompositor, uint8_t aChannelIndex,
PlaneInfo& aPlaneInfo) override;
void UnmapPlanes() override;
RefPtr<layers::TextureSource> CreateTextureSource(
layers::TextureSourceProvider* aProvider) override;
RenderAndroidSurfaceTextureHost* AsRenderAndroidSurfaceTextureHost()
override {
return this;
}
void UpdateTexImageIfNecessary();
mozilla::java::GeckoSurfaceTexture::GlobalRef mSurfTex;
const gfx::IntSize mSize;
const gfx::SurfaceFormat mFormat;
// mContinuousUpdate was used for rendering video in the past.
// It is not used on current gecko.
const bool mContinuousUpdate;
const Maybe<gfx::Matrix4x4> mTransformOverride;
private:
virtual ~RenderAndroidSurfaceTextureHost();
bool EnsureAttachedToGLContext();
gfx::Matrix4x4 GetTextureTransform() const;
already_AddRefed<gfx::DataSourceSurface> ReadTexImage();
enum PrepareStatus {
STATUS_NONE,
STATUS_MIGHT_BE_USED_BY_WR,
STATUS_UPDATE_TEX_IMAGE_NEEDED,
STATUS_PREPARED
};
PrepareStatus mPrepareStatus;
bool mAttachedToGLContext;
RefPtr<gl::GLContext> mGL;
RefPtr<gfx::DataSourceSurface> mReadback;
bool mIsRemoteTexture;
};
} // namespace wr
} // namespace mozilla
#endif // MOZILLA_GFX_RENDERANDROIDSURFACETEXTUREHOST_H