Support passing existing SurfaceTexture to TextureRegistry (flutter/engine#28358)

This commit is contained in:
ColdPaleLight
2021-09-02 18:16:02 +08:00
committed by GitHub
parent 4e314d517b
commit 8e7d4341dd
5 changed files with 54 additions and 1 deletions

View File

@@ -90,12 +90,22 @@ public class FlutterRenderer implements TextureRegistry {
// ------ START TextureRegistry IMPLEMENTATION -----
/**
* Creates and returns a new {@link SurfaceTexture} that is also made available to Flutter code.
* Creates and returns a new {@link SurfaceTexture} managed by the Flutter engine that is also
* made available to Flutter code.
*/
@Override
public SurfaceTextureEntry createSurfaceTexture() {
Log.v(TAG, "Creating a SurfaceTexture.");
final SurfaceTexture surfaceTexture = new SurfaceTexture(0);
return registerSurfaceTexture(surfaceTexture);
}
/**
* Registers and returns a {@link SurfaceTexture} managed by the Flutter engine that is also made
* available to Flutter code.
*/
@Override
public SurfaceTextureEntry registerSurfaceTexture(@NonNull SurfaceTexture surfaceTexture) {
surfaceTexture.detachFromGLContext();
final SurfaceTextureRegistryEntry entry =
new SurfaceTextureRegistryEntry(nextTextureId.getAndIncrement(), surfaceTexture);

View File

@@ -873,6 +873,12 @@ public class FlutterView extends SurfaceView
@Override
public TextureRegistry.SurfaceTextureEntry createSurfaceTexture() {
final SurfaceTexture surfaceTexture = new SurfaceTexture(0);
return registerSurfaceTexture(surfaceTexture);
}
@Override
public TextureRegistry.SurfaceTextureEntry registerSurfaceTexture(
@NonNull SurfaceTexture surfaceTexture) {
surfaceTexture.detachFromGLContext();
final SurfaceTextureRegistryEntry entry =
new SurfaceTextureRegistryEntry(nextTextureId.getAndIncrement(), surfaceTexture);

View File

@@ -5,6 +5,7 @@
package io.flutter.view;
import android.graphics.SurfaceTexture;
import androidx.annotation.NonNull;
// TODO(mattcarroll): re-evalute docs in this class and add nullability annotations.
/**
@@ -20,6 +21,13 @@ public interface TextureRegistry {
*/
SurfaceTextureEntry createSurfaceTexture();
/**
* Registers a SurfaceTexture managed by the Flutter engine.
*
* @return A SurfaceTextureEntry.
*/
SurfaceTextureEntry registerSurfaceTexture(@NonNull SurfaceTexture surfaceTexture);
/** A registry entry for a managed SurfaceTexture. */
interface SurfaceTextureEntry {
/** @return The managed SurfaceTexture. */

View File

@@ -1,5 +1,6 @@
package io.flutter.embedding.engine.renderer;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -7,6 +8,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import android.graphics.SurfaceTexture;
import android.os.Looper;
import android.view.Surface;
import io.flutter.embedding.engine.FlutterJNI;
@@ -122,6 +124,28 @@ public class FlutterRendererTest {
verify(fakeFlutterJNI, times(0)).markTextureFrameAvailable(eq(entry.id()));
}
@Test
public void itRegistersExistingSurfaceTexture() {
// Setup the test.
FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
fakeFlutterJNI.detachFromNativeAndReleaseResources();
SurfaceTexture surfaceTexture = new SurfaceTexture(0);
// Execute the behavior under test.
FlutterRenderer.SurfaceTextureRegistryEntry entry =
(FlutterRenderer.SurfaceTextureRegistryEntry)
flutterRenderer.registerSurfaceTexture(surfaceTexture);
flutterRenderer.startRenderingToSurface(fakeSurface);
// Verify behavior under test.
assertEquals(surfaceTexture, entry.surfaceTexture());
verify(fakeFlutterJNI, times(1)).registerTexture(eq(entry.id()), eq(entry.textureWrapper()));
}
@Test
public void itUnregistersTextureWhenSurfaceTextureFinalized() {
// Setup the test.

View File

@@ -849,6 +849,11 @@ public class PlatformViewsControllerTest {
@Override
public SurfaceTextureEntry createSurfaceTexture() {
return registerSurfaceTexture(mock(SurfaceTexture.class));
}
@Override
public SurfaceTextureEntry registerSurfaceTexture(SurfaceTexture surfaceTexture) {
return new SurfaceTextureEntry() {
@Override
public SurfaceTexture surfaceTexture() {