Add a new external texture type to Android embedder (flutter/engine#48803)
The `SurfaceProducer` interface exposes only exposes a Surface to render on, abstracting away the consumer side of the external texture. The `ImageReaderSurfaceProducer` implementation of this interface is included as well as a basic test of that code. Also, a small refactor so that `ImageTexture` and `ImageReaderSurfaceProducer` can use the same native C++ code in the engine. Subsequent CLs will need to address the following: - A SurfaceTextureSurfaceProducer (your eyes are probably bleeding from that name) implementation is needed so we can support GL based systems. - Update Platform Views to use this new SurfaceProducer type instead of the legacy types. - Deprecate SurfaceTexture and ImageTexture external texture types. Related issue [#139702](https://github.com/flutter/flutter/issues/139702)
This commit is contained in:
@@ -48,7 +48,7 @@ class MockPlatformViewAndroidJNI : public PlatformViewAndroidJNI {
|
||||
(JavaLocalRef surface_texture),
|
||||
(override));
|
||||
MOCK_METHOD(JavaLocalRef,
|
||||
ImageTextureEntryAcquireLatestImage,
|
||||
ImageProducerTextureEntryAcquireLatestImage,
|
||||
(JavaLocalRef image_texture_entry),
|
||||
(override));
|
||||
MOCK_METHOD(JavaLocalRef,
|
||||
|
||||
@@ -73,8 +73,9 @@ JavaLocalRef ImageExternalTexture::AcquireLatestImage() {
|
||||
FML_CHECK(env != nullptr);
|
||||
|
||||
// ImageTextureEntry.acquireLatestImage.
|
||||
JavaLocalRef image_java = jni_facade_->ImageTextureEntryAcquireLatestImage(
|
||||
JavaLocalRef(image_texture_entry_));
|
||||
JavaLocalRef image_java =
|
||||
jni_facade_->ImageProducerTextureEntryAcquireLatestImage(
|
||||
JavaLocalRef(image_texture_entry_));
|
||||
return image_java;
|
||||
}
|
||||
|
||||
|
||||
@@ -921,19 +921,19 @@ public class FlutterJNI {
|
||||
*/
|
||||
@UiThread
|
||||
public void registerImageTexture(
|
||||
long textureId, @NonNull TextureRegistry.ImageTextureEntry imageTextureEntry) {
|
||||
long textureId, @NonNull TextureRegistry.ImageConsumer imageTexture) {
|
||||
ensureRunningOnMainThread();
|
||||
ensureAttachedToNative();
|
||||
nativeRegisterImageTexture(
|
||||
nativeShellHolderId,
|
||||
textureId,
|
||||
new WeakReference<TextureRegistry.ImageTextureEntry>(imageTextureEntry));
|
||||
new WeakReference<TextureRegistry.ImageConsumer>(imageTexture));
|
||||
}
|
||||
|
||||
private native void nativeRegisterImageTexture(
|
||||
long nativeShellHolderId,
|
||||
long textureId,
|
||||
@NonNull WeakReference<TextureRegistry.ImageTextureEntry> imageTextureEntry);
|
||||
@NonNull WeakReference<TextureRegistry.ImageConsumer> imageTexture);
|
||||
|
||||
/**
|
||||
* Call this method to inform Flutter that a texture previously registered with {@link
|
||||
|
||||
@@ -6,10 +6,13 @@ package io.flutter.embedding.engine.renderer;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.hardware.HardwareBuffer;
|
||||
import android.hardware.SyncFence;
|
||||
import android.media.Image;
|
||||
import android.media.ImageReader;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.view.Surface;
|
||||
@@ -151,6 +154,22 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
}
|
||||
|
||||
// ------ START TextureRegistry IMPLEMENTATION -----
|
||||
|
||||
/**
|
||||
* Creates and returns a new external texture {@link SurfaceProducer} managed by the Flutter
|
||||
* engine that is also made available to Flutter code.
|
||||
*/
|
||||
@Override
|
||||
public SurfaceProducer createSurfaceProducer() {
|
||||
// TODO(matanl, johnmccutchan): Implement a SurfaceTexture version and switch on whether or
|
||||
// not impeller is enabled.
|
||||
final ImageReaderSurfaceProducer entry =
|
||||
new ImageReaderSurfaceProducer(nextTextureId.getAndIncrement());
|
||||
Log.v(TAG, "New SurfaceProducer ID: " + entry.id());
|
||||
registerImageTexture(isRenderingToImageViewCount, (TextureRegistry.ImageConsumer) entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a new {@link SurfaceTexture} managed by the Flutter engine that is also
|
||||
* made available to Flutter code.
|
||||
@@ -182,7 +201,7 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
final ImageTextureRegistryEntry entry =
|
||||
new ImageTextureRegistryEntry(nextTextureId.getAndIncrement());
|
||||
Log.v(TAG, "New ImageTextureEntry ID: " + entry.id());
|
||||
registerImageTexture(entry.id(), entry);
|
||||
registerImageTexture(entry.id(), (TextureRegistry.ImageConsumer) entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
@@ -338,7 +357,277 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
}
|
||||
|
||||
@Keep
|
||||
final class ImageTextureRegistryEntry implements TextureRegistry.ImageTextureEntry {
|
||||
@TargetApi(29)
|
||||
final class ImageReaderSurfaceProducer
|
||||
implements TextureRegistry.SurfaceProducer, TextureRegistry.ImageConsumer {
|
||||
private static final String TAG = "ImageReaderSurfaceProducer";
|
||||
private static final int MAX_IMAGES = 4;
|
||||
|
||||
private final long id;
|
||||
|
||||
private boolean released;
|
||||
private boolean ignoringFence = false;
|
||||
|
||||
private int requestedWidth = 0;
|
||||
private int requestedHeight = 0;
|
||||
|
||||
private ImageReader activeReader;
|
||||
private ImageReader toBeClosedReader;
|
||||
private Image latestImage;
|
||||
|
||||
private final Handler onImageAvailableHandler = new Handler();
|
||||
private final ImageReader.OnImageAvailableListener onImageAvailableListener =
|
||||
new ImageReader.OnImageAvailableListener() {
|
||||
@Override
|
||||
public void onImageAvailable(ImageReader reader) {
|
||||
Image image = null;
|
||||
try {
|
||||
image = reader.acquireLatestImage();
|
||||
} catch (IllegalStateException e) {
|
||||
Log.e(TAG, "onImageAvailable acquireLatestImage failed: " + e.toString());
|
||||
}
|
||||
if (image == null) {
|
||||
return;
|
||||
}
|
||||
onImage(image);
|
||||
maybeCloseReader();
|
||||
}
|
||||
};
|
||||
|
||||
ImageReaderSurfaceProducer(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
if (released) {
|
||||
return;
|
||||
}
|
||||
released = true;
|
||||
if (this.latestImage != null) {
|
||||
this.latestImage.close();
|
||||
this.latestImage = null;
|
||||
}
|
||||
if (this.toBeClosedReader != null) {
|
||||
this.toBeClosedReader.close();
|
||||
this.toBeClosedReader = null;
|
||||
}
|
||||
if (this.activeReader != null) {
|
||||
this.activeReader.close();
|
||||
this.activeReader = null;
|
||||
}
|
||||
unregisterTexture(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(int width, int height) {
|
||||
if (requestedWidth == width && requestedHeight == height) {
|
||||
// No size change.
|
||||
return;
|
||||
}
|
||||
this.requestedHeight = height;
|
||||
this.requestedWidth = width;
|
||||
// Because the size was changed we will need to close the currently active reader.
|
||||
// Instead of closing it eagerly we wait until the a frame is produced at the new
|
||||
// size, ensuring that we don't render a blank frame in the app.
|
||||
maybeMarkReaderForClose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return this.requestedWidth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return this.requestedHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Surface getSurface() {
|
||||
maybeCreateReader();
|
||||
return activeReader.getSurface();
|
||||
}
|
||||
|
||||
@Override
|
||||
@TargetApi(29)
|
||||
public Image acquireLatestImage() {
|
||||
Image r;
|
||||
synchronized (this) {
|
||||
r = this.latestImage;
|
||||
this.latestImage = null;
|
||||
}
|
||||
maybeWaitOnFence(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
private void maybeMarkReaderForClose() {
|
||||
synchronized (this) {
|
||||
if (this.toBeClosedReader != null) {
|
||||
// We only ever have two readers:
|
||||
// 1) The reader to be closed after the next image is produced.
|
||||
// 2) The reader being used to produce images.
|
||||
return;
|
||||
}
|
||||
this.toBeClosedReader = this.activeReader;
|
||||
this.activeReader = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void maybeCloseReader() {
|
||||
if (this.toBeClosedReader == null) {
|
||||
return;
|
||||
}
|
||||
this.toBeClosedReader.close();
|
||||
this.toBeClosedReader = null;
|
||||
}
|
||||
|
||||
private void maybeCreateReader() {
|
||||
if (this.activeReader != null) {
|
||||
return;
|
||||
}
|
||||
this.activeReader = createImageReader();
|
||||
}
|
||||
|
||||
/** Invoked for each method that is available. */
|
||||
private void onImage(Image image) {
|
||||
if (released) {
|
||||
return;
|
||||
}
|
||||
Image toClose;
|
||||
synchronized (this) {
|
||||
toClose = this.latestImage;
|
||||
this.latestImage = image;
|
||||
}
|
||||
// Close the previously pushed buffer.
|
||||
if (toClose != null) {
|
||||
Log.e(TAG, "RawSurfaceTexture frame was not acquired in a timely manner.");
|
||||
toClose.close();
|
||||
}
|
||||
if (image != null) {
|
||||
// Mark that we have a new frame available. Eventually the raster thread will
|
||||
// call acquireLatestImage.
|
||||
markTextureFrameAvailable(id);
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(33)
|
||||
private void waitOnFence(Image image) {
|
||||
try {
|
||||
SyncFence fence = image.getFence();
|
||||
boolean signaled = fence.awaitForever();
|
||||
if (!signaled) {
|
||||
Log.e(TAG, "acquireLatestImage image's fence was never signalled.");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Drop.
|
||||
}
|
||||
}
|
||||
|
||||
private void maybeWaitOnFence(Image image) {
|
||||
if (image == null) {
|
||||
return;
|
||||
}
|
||||
if (ignoringFence) {
|
||||
return;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
// The fence API is only available on Android >= 33.
|
||||
waitOnFence(image);
|
||||
return;
|
||||
}
|
||||
if (!ignoringFence) {
|
||||
// Log once per ImageTextureEntry.
|
||||
ignoringFence = true;
|
||||
Log.w(TAG, "ImageTextureEntry can't wait on the fence on Android < 33");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
if (released) {
|
||||
return;
|
||||
}
|
||||
if (latestImage != null) {
|
||||
// Be sure to finalize any cached image.
|
||||
latestImage.close();
|
||||
latestImage = null;
|
||||
}
|
||||
if (this.toBeClosedReader != null) {
|
||||
this.toBeClosedReader.close();
|
||||
}
|
||||
if (this.activeReader != null) {
|
||||
this.activeReader.close();
|
||||
}
|
||||
released = true;
|
||||
handler.post(new TextureFinalizerRunnable(id, flutterJNI));
|
||||
} finally {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(33)
|
||||
private ImageReader createImageReader33() {
|
||||
final ImageReader.Builder builder = new ImageReader.Builder(requestedWidth, requestedHeight);
|
||||
// Allow for double buffering.
|
||||
builder.setMaxImages(MAX_IMAGES);
|
||||
// Use PRIVATE image format so that we can support video decoding.
|
||||
// TODO(johnmccutchan): Should we always use PRIVATE here? It may impact our
|
||||
// ability to read back texture data. If we don't always want to use it, how do
|
||||
// we
|
||||
// decide when to use it or not? Perhaps PlatformViews can indicate if they may
|
||||
// contain
|
||||
// DRM'd content.
|
||||
// I need to investigate how PRIVATE impacts our ability to take screenshots or
|
||||
// capture
|
||||
// the output of Flutter application.
|
||||
builder.setImageFormat(ImageFormat.PRIVATE);
|
||||
// Hint that consumed images will only be read by GPU.
|
||||
builder.setUsage(HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE);
|
||||
final ImageReader reader = builder.build();
|
||||
reader.setOnImageAvailableListener(this.onImageAvailableListener, onImageAvailableHandler);
|
||||
return reader;
|
||||
}
|
||||
|
||||
@TargetApi(29)
|
||||
private ImageReader createImageReader29() {
|
||||
final ImageReader reader =
|
||||
ImageReader.newInstance(
|
||||
requestedWidth,
|
||||
requestedHeight,
|
||||
ImageFormat.PRIVATE,
|
||||
MAX_IMAGES,
|
||||
HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE);
|
||||
reader.setOnImageAvailableListener(this.onImageAvailableListener, onImageAvailableHandler);
|
||||
return reader;
|
||||
}
|
||||
|
||||
private ImageReader createImageReader() {
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
return createImageReader33();
|
||||
} else if (Build.VERSION.SDK_INT >= 29) {
|
||||
return createImageReader29();
|
||||
}
|
||||
throw new UnsupportedOperationException(
|
||||
"ImageReaderPlatformViewRenderTarget requires API version 29+");
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void disableFenceForTest() {
|
||||
// Roboelectric's implementation of SyncFence is borked.
|
||||
ignoringFence = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Keep
|
||||
final class ImageTextureRegistryEntry
|
||||
implements TextureRegistry.ImageTextureEntry, TextureRegistry.ImageConsumer {
|
||||
private static final String TAG = "ImageTextureRegistryEntry";
|
||||
private final long id;
|
||||
private boolean released;
|
||||
@@ -408,6 +697,9 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
if (image == null) {
|
||||
return;
|
||||
}
|
||||
if (ignoringFence) {
|
||||
return;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
// The fence API is only available on Android >= 33.
|
||||
waitOnFence(image);
|
||||
@@ -472,7 +764,8 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
public void startRenderingToSurface(@NonNull Surface surface, boolean onlySwap) {
|
||||
if (!onlySwap) {
|
||||
// Stop rendering to the surface releases the associated native resources, which
|
||||
// causes a glitch when toggling between rendering to an image view (hybrid composition) and
|
||||
// causes a glitch when toggling between rendering to an image view (hybrid
|
||||
// composition) and
|
||||
// rendering directly to a Surface or Texture view. For more,
|
||||
// https://github.com/flutter/flutter/issues/95343
|
||||
stopRenderingToSurface();
|
||||
@@ -645,8 +938,8 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
}
|
||||
|
||||
private void registerImageTexture(
|
||||
long textureId, @NonNull TextureRegistry.ImageTextureEntry textureEntry) {
|
||||
flutterJNI.registerImageTexture(textureId, textureEntry);
|
||||
long textureId, @NonNull TextureRegistry.ImageConsumer imageTexture) {
|
||||
flutterJNI.registerImageTexture(textureId, imageTexture);
|
||||
}
|
||||
|
||||
// TODO(mattcarroll): describe the native behavior that this invokes
|
||||
|
||||
@@ -905,6 +905,12 @@ public class FlutterView extends SurfaceView
|
||||
throw new UnsupportedOperationException("Image textures are not supported in this mode.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SurfaceProducer createSurfaceProducer() {
|
||||
throw new UnsupportedOperationException(
|
||||
"SurfaceProducer textures are not supported in this mode.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public TextureRegistry.SurfaceTextureEntry registerSurfaceTexture(
|
||||
|
||||
@@ -6,6 +6,7 @@ package io.flutter.view;
|
||||
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.media.Image;
|
||||
import android.view.Surface;
|
||||
import androidx.annotation.Keep;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -17,6 +18,14 @@ import androidx.annotation.Nullable;
|
||||
* href="https://api.flutter.dev/flutter/widgets/Texture-class.html">Texture</a> widget.
|
||||
*/
|
||||
public interface TextureRegistry {
|
||||
/**
|
||||
* Creates and registers a SurfaceProducer texture managed by the Flutter engine.
|
||||
*
|
||||
* @return A SurfaceProducer.
|
||||
*/
|
||||
@NonNull
|
||||
SurfaceProducer createSurfaceProducer();
|
||||
|
||||
/**
|
||||
* Creates and registers a SurfaceTexture managed by the Flutter engine.
|
||||
*
|
||||
@@ -57,7 +66,38 @@ public interface TextureRegistry {
|
||||
void release();
|
||||
}
|
||||
|
||||
/** Uses a Surface to populate the texture. */
|
||||
@Keep
|
||||
interface SurfaceProducer extends TextureEntry {
|
||||
/** @return The identity of this texture. */
|
||||
long id();
|
||||
|
||||
/** Deregisters and releases all resources . */
|
||||
void release();
|
||||
|
||||
/** Specify the size of this texture in physical pixels */
|
||||
void setSize(int width, int height);
|
||||
|
||||
/** @return The currently specified width (physical pixels) */
|
||||
int getWidth();
|
||||
|
||||
/** @return The currently specified height (physical pixels) */
|
||||
int getHeight();
|
||||
|
||||
/**
|
||||
* Get a Surface that can be used to update the texture contents.
|
||||
*
|
||||
* <p>NOTE: You should not cache the returned surface but instead invoke getSurface each time
|
||||
* you need to draw. The surface may change when the texture is resized or has its format
|
||||
* changed.
|
||||
*
|
||||
* @return a Surface to use for a drawing target for various APIs.
|
||||
*/
|
||||
Surface getSurface();
|
||||
};
|
||||
|
||||
/** A registry entry for a managed SurfaceTexture. */
|
||||
@Keep
|
||||
interface SurfaceTextureEntry extends TextureEntry {
|
||||
/** @return The identity of this texture. */
|
||||
long id();
|
||||
@@ -93,15 +133,6 @@ public interface TextureRegistry {
|
||||
* last frame pushed will be used (dropping the missed frames).
|
||||
*/
|
||||
void pushImage(Image image);
|
||||
|
||||
/**
|
||||
* Retrieve the last Image pushed.
|
||||
*
|
||||
* <p>NOTE: Caller must call Image.close() on returned image.
|
||||
*
|
||||
* @return Image or null.
|
||||
*/
|
||||
Image acquireLatestImage();
|
||||
}
|
||||
|
||||
/** Listener invoked when the most recent image has been consumed. */
|
||||
@@ -118,4 +149,16 @@ public interface TextureRegistry {
|
||||
/** This method will be invoked when a memory pressure warning was forward. */
|
||||
void onTrimMemory(int level);
|
||||
}
|
||||
|
||||
@Keep
|
||||
interface ImageConsumer extends TextureEntry {
|
||||
/**
|
||||
* Retrieve the last Image produced. Drops all previously produced images.
|
||||
*
|
||||
* <p>NOTE: Caller must call Image.close() on returned image.
|
||||
*
|
||||
* @return Image or null.
|
||||
*/
|
||||
public Image acquireLatestImage();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ class JNIMock final : public PlatformViewAndroidJNI {
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(JavaLocalRef,
|
||||
ImageTextureEntryAcquireLatestImage,
|
||||
ImageProducerTextureEntryAcquireLatestImage,
|
||||
(JavaLocalRef image_texture_entry),
|
||||
(override));
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ class PlatformViewAndroidJNI {
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Acquire the latest image available.
|
||||
///
|
||||
virtual JavaLocalRef ImageTextureEntryAcquireLatestImage(
|
||||
virtual JavaLocalRef ImageProducerTextureEntryAcquireLatestImage(
|
||||
JavaLocalRef image_texture_entry) = 0;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
@@ -51,8 +51,8 @@ static fml::jni::ScopedJavaGlobalRef<jclass>* g_java_weak_reference_class =
|
||||
|
||||
static fml::jni::ScopedJavaGlobalRef<jclass>* g_texture_wrapper_class = nullptr;
|
||||
|
||||
static fml::jni::ScopedJavaGlobalRef<jclass>* g_image_texture_entry_class =
|
||||
nullptr;
|
||||
static fml::jni::ScopedJavaGlobalRef<jclass>*
|
||||
g_image_consumer_texture_registry_interface = nullptr;
|
||||
|
||||
static fml::jni::ScopedJavaGlobalRef<jclass>* g_image_class = nullptr;
|
||||
|
||||
@@ -1182,25 +1182,26 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
|
||||
FML_LOG(ERROR) << "Could not locate detachFromGlContext method";
|
||||
return false;
|
||||
}
|
||||
g_image_texture_entry_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
|
||||
env, env->FindClass("io/flutter/view/TextureRegistry$ImageTextureEntry"));
|
||||
if (g_image_texture_entry_class->is_null()) {
|
||||
FML_LOG(ERROR) << "Could not locate ImageTextureEntry class";
|
||||
g_image_consumer_texture_registry_interface =
|
||||
new fml::jni::ScopedJavaGlobalRef<jclass>(
|
||||
env, env->FindClass("io/flutter/view/TextureRegistry$ImageConsumer"));
|
||||
if (g_image_consumer_texture_registry_interface->is_null()) {
|
||||
FML_LOG(ERROR) << "Could not locate TextureRegistry.ImageConsumer class";
|
||||
return false;
|
||||
}
|
||||
|
||||
g_acquire_latest_image_method =
|
||||
env->GetMethodID(g_image_texture_entry_class->obj(), "acquireLatestImage",
|
||||
"()Landroid/media/Image;");
|
||||
env->GetMethodID(g_image_consumer_texture_registry_interface->obj(),
|
||||
"acquireLatestImage", "()Landroid/media/Image;");
|
||||
if (g_acquire_latest_image_method == nullptr) {
|
||||
FML_LOG(ERROR) << "Could not locate acquireLatestImage on "
|
||||
"ImageTextureEntry class";
|
||||
"TextureRegistry.ImageConsumer class";
|
||||
return false;
|
||||
}
|
||||
|
||||
g_image_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
|
||||
env, env->FindClass("android/media/Image"));
|
||||
if (g_image_texture_entry_class->is_null()) {
|
||||
if (g_image_class->is_null()) {
|
||||
FML_LOG(ERROR) << "Could not locate Image class";
|
||||
return false;
|
||||
}
|
||||
@@ -1551,28 +1552,29 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(
|
||||
FML_CHECK(fml::jni::CheckException(env));
|
||||
}
|
||||
|
||||
JavaLocalRef PlatformViewAndroidJNIImpl::ImageTextureEntryAcquireLatestImage(
|
||||
JavaLocalRef image_texture_entry) {
|
||||
JavaLocalRef
|
||||
PlatformViewAndroidJNIImpl::ImageProducerTextureEntryAcquireLatestImage(
|
||||
JavaLocalRef image_producer_texture_entry) {
|
||||
JNIEnv* env = fml::jni::AttachCurrentThread();
|
||||
|
||||
if (image_texture_entry.is_null()) {
|
||||
if (image_producer_texture_entry.is_null()) {
|
||||
// Return null.
|
||||
return JavaLocalRef();
|
||||
}
|
||||
|
||||
// Convert the weak reference to ImageTextureEntry into a strong local
|
||||
// reference.
|
||||
fml::jni::ScopedJavaLocalRef<jobject> image_texture_entry_local_ref(
|
||||
env, env->CallObjectMethod(image_texture_entry.obj(),
|
||||
fml::jni::ScopedJavaLocalRef<jobject> image_producer_texture_entry_local_ref(
|
||||
env, env->CallObjectMethod(image_producer_texture_entry.obj(),
|
||||
g_java_weak_reference_get_method));
|
||||
|
||||
if (image_texture_entry_local_ref.is_null()) {
|
||||
if (image_producer_texture_entry_local_ref.is_null()) {
|
||||
// Return null.
|
||||
return JavaLocalRef();
|
||||
}
|
||||
|
||||
JavaLocalRef r = JavaLocalRef(
|
||||
env, env->CallObjectMethod(image_texture_entry_local_ref.obj(),
|
||||
env, env->CallObjectMethod(image_producer_texture_entry_local_ref.obj(),
|
||||
g_acquire_latest_image_method));
|
||||
FML_CHECK(fml::jni::CheckException(env));
|
||||
return r;
|
||||
|
||||
@@ -52,7 +52,7 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {
|
||||
|
||||
void SurfaceTextureDetachFromGLContext(JavaLocalRef surface_texture) override;
|
||||
|
||||
JavaLocalRef ImageTextureEntryAcquireLatestImage(
|
||||
JavaLocalRef ImageProducerTextureEntryAcquireLatestImage(
|
||||
JavaLocalRef image_texture_entry) override;
|
||||
|
||||
JavaLocalRef ImageGetHardwareBuffer(JavaLocalRef image) override;
|
||||
|
||||
@@ -3,6 +3,8 @@ package io.flutter.embedding.engine.renderer;
|
||||
import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.anyFloat;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -14,8 +16,10 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.media.Image;
|
||||
import android.os.Looper;
|
||||
import android.view.Surface;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
@@ -445,4 +449,57 @@ public class FlutterRendererTest {
|
||||
verify(fakeFlutterJNI, times(0)).onSurfaceCreated(eq(fakeSurface2));
|
||||
verify(fakeFlutterJNI, times(1)).onSurfaceWindowChanged(eq(fakeSurface2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ImageRawSurfaceTextureProducesImageOfCorrectSize() {
|
||||
FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
|
||||
FlutterRenderer.ImageReaderSurfaceProducer texture =
|
||||
flutterRenderer.new ImageReaderSurfaceProducer(0);
|
||||
texture.disableFenceForTest();
|
||||
|
||||
// Returns a null image when one hasn't been produced.
|
||||
assertNull(texture.acquireLatestImage());
|
||||
|
||||
// Give the texture an initial size.
|
||||
texture.setSize(1, 1);
|
||||
|
||||
// Render a frame.
|
||||
Surface surface = texture.getSurface();
|
||||
assertNotNull(surface);
|
||||
Canvas canvas = surface.lockHardwareCanvas();
|
||||
canvas.drawARGB(255, 255, 0, 0);
|
||||
surface.unlockCanvasAndPost(canvas);
|
||||
|
||||
// Let callbacks run.
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
// Extract the image and check its size.
|
||||
Image image = texture.acquireLatestImage();
|
||||
assertEquals(1, image.getWidth());
|
||||
assertEquals(1, image.getHeight());
|
||||
image.close();
|
||||
|
||||
// Resize the texture.
|
||||
texture.setSize(5, 5);
|
||||
|
||||
// Render a frame.
|
||||
surface = texture.getSurface();
|
||||
assertNotNull(surface);
|
||||
canvas = surface.lockHardwareCanvas();
|
||||
canvas.drawARGB(255, 255, 0, 0);
|
||||
surface.unlockCanvasAndPost(canvas);
|
||||
|
||||
// Let callbacks run.
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
// Extract the image and check its size.
|
||||
image = texture.acquireLatestImage();
|
||||
assertEquals(5, image.getWidth());
|
||||
assertEquals(5, image.getHeight());
|
||||
image.close();
|
||||
|
||||
assertNull(texture.acquireLatestImage());
|
||||
|
||||
texture.release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1583,9 +1583,35 @@ public class PlatformViewsControllerTest {
|
||||
|
||||
@Override
|
||||
public void pushImage(Image image) {}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public SurfaceProducer createSurfaceProducer() {
|
||||
return new SurfaceProducer() {
|
||||
@Override
|
||||
public long id() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image acquireLatestImage() {
|
||||
public void release() {}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(int width, int height) {}
|
||||
|
||||
@Override
|
||||
public Surface getSurface() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user