Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 127b4a3

Browse files
author
auto-submit[bot]
committed
Revert "Reverts "Add SurfaceProducer#onSurfaceAvailable, deprecate onSurfaceCreated. (#55418)" (#55450)"
This reverts commit 7af5b38.
1 parent 11a86fd commit 127b4a3

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ public FlutterRenderer(@NonNull FlutterJNI flutterJNI) {
108108
public void onResume(@NonNull LifecycleOwner owner) {
109109
Log.v(TAG, "onResume called; notifying SurfaceProducers");
110110
for (ImageReaderSurfaceProducer producer : imageReaderProducers) {
111-
if (producer.callback != null) {
112-
producer.callback.onSurfaceCreated();
111+
if (producer.callback != null && producer.notifiedDestroy) {
112+
producer.notifiedDestroy = false;
113+
producer.callback.onSurfaceAvailable();
113114
}
114115
}
115116
}
@@ -462,6 +463,13 @@ final class ImageReaderSurfaceProducer
462463
// will be produced at that size.
463464
private boolean createNewReader = true;
464465

466+
/**
467+
* Stores whether {@link Callback#onSurfaceDestroyed()} was previously invoked.
468+
*
469+
* <p>Used to avoid signaling {@link Callback#onSurfaceAvailable()} unnecessarily.
470+
*/
471+
private boolean notifiedDestroy = false;
472+
465473
// State held to track latency of various stages.
466474
private long lastDequeueTime = 0;
467475
private long lastQueueTime = 0;
@@ -689,6 +697,7 @@ public void onTrimMemory(int level) {
689697
cleanup();
690698
createNewReader = true;
691699
if (this.callback != null) {
700+
notifiedDestroy = true;
692701
this.callback.onSurfaceDestroyed();
693702
}
694703
}

shell/platform/android/io/flutter/view/TextureRegistry.java

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ interface SurfaceProducer extends TextureEntry {
9696

9797
/**
9898
* Sets a callback that is notified when a previously created {@link Surface} returned by {@link
99-
* SurfaceProducer#getSurface()} is no longer valid, either due to being destroyed or being
100-
* changed.
99+
* SurfaceProducer#getSurface()} is no longer valid due to being destroyed, or a new surface is
100+
* now available (after the previous one was destroyed) for rendering.
101101
*
102102
* @param callback The callback to notify, or null to remove the callback.
103103
*/
@@ -106,18 +106,65 @@ interface SurfaceProducer extends TextureEntry {
106106
/** Callback invoked by {@link #setCallback(Callback)}. */
107107
interface Callback {
108108
/**
109-
* Invoked when a previous surface is now invalid and a new surface is now available.
109+
* An alias for {@link Callback#onSurfaceAvailable()} with a less accurate name.
110+
*
111+
* @deprecated Override and use {@link Callback#onSurfaceAvailable()} instead.
112+
*/
113+
@Deprecated(since = "Flutter 3.27", forRemoval = true)
114+
default void onSurfaceCreated() {}
115+
116+
/**
117+
* Invoked when an Android application is resumed after {@link Callback#onSurfaceDestroyed()}.
110118
*
111-
* <p>Typically plugins will use this callback as a signal to redraw, such as due to the
112-
* texture being resized, the format being changed, or the application being resumed after
113-
* being suspended in the background.
119+
* <p>Applications should now call {@link SurfaceProducer#getSurface()} to get a new
120+
* {@link Surface}, as the previous one was destroyed and released as a result of a low memory
121+
* event from the Android OS.
122+
*
123+
* <pre>
124+
* {@code
125+
* void example(SurfaceProducer producer) {
126+
* producer.setCallback(new SurfaceProducer.Callback() {
127+
* @override
128+
* public void onSurfaceAvailable() {
129+
* Surface surface = producer.getSurface();
130+
* redrawOrUse(surface);
131+
* }
132+
*
133+
* // ...
134+
* });
135+
* }
136+
* }
137+
* </pre>
114138
*/
115-
void onSurfaceCreated();
139+
default void onSurfaceAvailable() {
140+
this.onSurfaceCreated();
141+
}
116142

117143
/**
118-
* Invoked when a previous surface is now invalid.
144+
* Invoked when a {@link Surface} returned by {@link SurfaceProducer#getSurface()} is invalid.
145+
*
146+
* <p>In a low memory environment, the Android OS will signal to Flutter to release resources,
147+
* such as surfaces, that are not currently in use, such as when the application is in the
148+
* background, and this method is subsequently called to notify a plugin author to stop
149+
* using or rendering to the last surface.
150+
*
151+
* <p>Use {@link Callback#onSurfaceAvailable()} to be notified to resume rendering.
152+
*
153+
* <pre>
154+
* {@code
155+
* void example(SurfaceProducer producer) {
156+
* producer.setCallback(new SurfaceProducer.Callback() {
157+
* @override
158+
* public void onSurfaceDestroyed() {
159+
* // Store information about the last frame, if necessary.
160+
* // Potentially release other dependent resources.
161+
* }
119162
*
120-
* <p>Typically plugins will use this callback as a signal to release resources.
163+
* // ...
164+
* });
165+
* }
166+
* }
167+
* </pre>
121168
*/
122169
void onSurfaceDestroyed();
123170
}

shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ public void ImageReaderSurfaceProducerIsCreatedOnLifecycleResume() throws Except
785785
TextureRegistry.SurfaceProducer.Callback callback =
786786
new TextureRegistry.SurfaceProducer.Callback() {
787787
@Override
788-
public void onSurfaceCreated() {
788+
public void onSurfaceAvailable() {
789789
latch.countDown();
790790
}
791791

@@ -794,6 +794,9 @@ public void onSurfaceDestroyed() {}
794794
};
795795
producer.setCallback(callback);
796796

797+
// Trim memory.
798+
((FlutterRenderer.ImageReaderSurfaceProducer) producer).onTrimMemory(40);
799+
797800
// Trigger a resume.
798801
((LifecycleRegistry) ProcessLifecycleOwner.get().getLifecycle())
799802
.setCurrentState(Lifecycle.State.RESUMED);

0 commit comments

Comments
 (0)