Fix handling of external texture transforms on Android (flutter/engine#24888)

This commit is contained in:
Dwayne Slater
2022-08-04 17:12:04 -07:00
committed by GitHub
parent 9c735ec7dc
commit 86d2aa6f59
3 changed files with 58 additions and 27 deletions

View File

@@ -118,7 +118,7 @@ targets:
- DEPS
- .ci.yaml
- testing/**
- shell/platforms/android/**
- shell/platform/android/**
- name: Linux Benchmarks
enabled_branches:

View File

@@ -64,23 +64,44 @@ void AndroidExternalTextureGL::Paint(SkCanvas& canvas,
kPremul_SkAlphaType, nullptr);
if (image) {
SkAutoCanvasRestore autoRestore(&canvas, true);
canvas.translate(bounds.x(), bounds.y());
canvas.scale(bounds.width(), bounds.height());
if (!transform.isIdentity()) {
SkMatrix transformAroundCenter(transform);
transformAroundCenter.preTranslate(-0.5, -0.5);
transformAroundCenter.postScale(1, -1);
transformAroundCenter.postTranslate(0.5, 0.5);
canvas.concat(transformAroundCenter);
// The incoming texture is vertically flipped, so we flip it
// back. OpenGL's coordinate system has Positive Y equivalent to up, while
// Skia's coordinate system has Negative Y equvalent to up.
canvas.translate(bounds.x(), bounds.y() + bounds.height());
canvas.scale(bounds.width(), -bounds.height());
if (!transform.isIdentity()) {
sk_sp<SkShader> shader = image->makeShader(
SkTileMode::kRepeat, SkTileMode::kRepeat, sampling, transform);
SkPaint paintWithShader;
if (paint) {
paintWithShader = *paint;
}
paintWithShader.setShader(shader);
canvas.drawRect(SkRect::MakeWH(1, 1), paintWithShader);
} else {
canvas.drawImage(image, 0, 0, sampling, paint);
}
canvas.drawImage(image, 0, 0, sampling, paint);
}
}
void AndroidExternalTextureGL::UpdateTransform() {
jni_facade_->SurfaceTextureGetTransformMatrix(
fml::jni::ScopedJavaLocalRef<jobject>(surface_texture_), transform);
// Android's SurfaceTexture transform matrix works on texture coordinate
// lookups in the range 0.0-1.0, while Skia's Shader transform matrix works on
// the image itself, as if it were inscribed inside a clip rect.
// An Android transform that scales lookup by 0.5 (displaying 50% of the
// texture) is the same as a Skia transform by 2.0 (scaling 50% of the image
// outside of the virtual "clip rect"), so we invert the incoming matrix.
SkMatrix inverted;
if (!transform.invert(&inverted)) {
FML_LOG(FATAL) << "Invalid SurfaceTexture transformation matrix";
}
transform = inverted;
}
void AndroidExternalTextureGL::OnGrContextDestroyed() {

View File

@@ -1319,18 +1319,6 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage(
FML_CHECK(fml::jni::CheckException(env));
}
// The bounds we set for the canvas are post composition.
// To fill the canvas we need to ensure that the transformation matrix
// on the `SurfaceTexture` will be scaled to fill. We rescale and preseve
// the scaled aspect ratio.
SkSize ScaleToFill(float scaleX, float scaleY) {
const double epsilon = std::numeric_limits<double>::epsilon();
// scaleY is negative.
const double minScale = fmin(scaleX, fabs(scaleY));
const double rescale = 1.0f / (minScale + epsilon);
return SkSize::Make(scaleX * rescale, scaleY * rescale);
}
void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
JavaLocalRef surface_texture,
SkMatrix& transform) {
@@ -1355,12 +1343,34 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
FML_CHECK(fml::jni::CheckException(env));
float* m = env->GetFloatArrayElements(transformMatrix.obj(), nullptr);
float scaleX = m[0], scaleY = m[5];
const SkSize scaled = ScaleToFill(scaleX, scaleY);
// SurfaceTexture 4x4 Column Major -> Skia 3x3 Row Major
// SurfaceTexture 4x4 (Column Major):
// | m[0] m[4] m[ 8] m[12] |
// | m[1] m[5] m[ 9] m[13] |
// | m[2] m[6] m[10] m[14] |
// | m[3] m[7] m[11] m[15] |
// According to Android documentation, the 4x4 matrix returned should be used
// with texture coordinates in the form (s, t, 0, 1). Since the z component is
// always 0.0, we are free to ignore any element that multiplies with the z
// component. Converting this to a 3x3 matrix is easy:
// SurfaceTexture 3x3 (Column Major):
// | m[0] m[4] m[12] |
// | m[1] m[5] m[13] |
// | m[3] m[7] m[15] |
// Skia (Row Major):
// | m[0] m[1] m[2] |
// | m[3] m[4] m[5] |
// | m[6] m[7] m[8] |
SkScalar matrix3[] = {
scaled.fWidth, m[1], m[2], //
m[4], scaled.fHeight, m[6], //
m[8], m[9], m[10], //
m[0], m[4], m[12], //
m[1], m[5], m[13], //
m[3], m[7], m[15], //
};
env->ReleaseFloatArrayElements(transformMatrix.obj(), m, JNI_ABORT);
transform.set9(matrix3);