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

Commit d8129b9

Browse files
Update Surface reference after resizing render target in VirtualDisplay based platform views (#50971)
Fixes flutter/flutter#142952
1 parent adbd1f4 commit d8129b9

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@
1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.VisibleForTesting;
2121

22-
@TargetApi(20)
22+
@TargetApi(21)
2323
class VirtualDisplayController {
2424
private static String TAG = "VirtualDisplayController";
2525

26+
private static VirtualDisplay.Callback callback =
27+
new VirtualDisplay.Callback() {
28+
@Override
29+
public void onPaused() {}
30+
31+
@Override
32+
public void onResumed() {}
33+
};
34+
2635
public static VirtualDisplayController create(
2736
Context context,
2837
AccessibilityEventsDelegate accessibilityEventsDelegate,
@@ -40,6 +49,7 @@ public static VirtualDisplayController create(
4049
DisplayManager displayManager =
4150
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
4251
final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
52+
4353
// Virtual Display crashes for some PlatformViews if the width or height is bigger
4454
// than the physical screen size. We have tried to clamp or scale down the size to prevent
4555
// the crash, but both solutions lead to unwanted behavior because the
@@ -51,14 +61,17 @@ public static VirtualDisplayController create(
5161
// virtual display and AndroidPlatformView widget.
5262
// https://github.com/flutter/flutter/issues/93115
5363
renderTarget.resize(width, height);
64+
int flags = 0;
5465
VirtualDisplay virtualDisplay =
5566
displayManager.createVirtualDisplay(
5667
"flutter-vd#" + viewId,
5768
width,
5869
height,
5970
metrics.densityDpi,
6071
renderTarget.getSurface(),
61-
0);
72+
flags,
73+
callback,
74+
null /* handler */);
6275

6376
if (virtualDisplay == null) {
6477
return null;
@@ -148,9 +161,17 @@ public void resize(final int width, final int height, final Runnable onNewSizeFr
148161
final DisplayManager displayManager =
149162
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
150163
renderTarget.resize(width, height);
164+
int flags = 0;
151165
virtualDisplay =
152166
displayManager.createVirtualDisplay(
153-
"flutter-vd#" + viewId, width, height, densityDpi, renderTarget.getSurface(), 0);
167+
"flutter-vd#" + viewId,
168+
width,
169+
height,
170+
densityDpi,
171+
renderTarget.getSurface(),
172+
flags,
173+
callback,
174+
null /* handler */);
154175

155176
final View embeddedView = getView();
156177
// There's a bug in Android version older than O where view tree observer onDrawListeners don't
@@ -210,12 +231,14 @@ public void dispose() {
210231
renderTarget.release();
211232
}
212233

234+
// On Android versions 31+ resizing of a Virtual Display's Presentation is natively supported.
213235
@TargetApi(31)
214236
private void resize31(
215237
View embeddedView, int width, int height, final Runnable onNewSizeFrameAvailable) {
216238
renderTarget.resize(width, height);
217-
// On Android versions 31+ resizing of a Virtual Display's Presentation is natively supported.
218239
virtualDisplay.resize(width, height, densityDpi);
240+
// Must update the surface to match the renderTarget's current surface.
241+
virtualDisplay.setSurface(renderTarget.getSurface());
219242
embeddedView.postDelayed(onNewSizeFrameAvailable, 0);
220243
}
221244

0 commit comments

Comments
 (0)