Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Surface reference after resizing render target in VirtualDisplay based platform views #50971

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

@TargetApi(20)
@TargetApi(21)
class VirtualDisplayController {
private static String TAG = "VirtualDisplayController";

private static VirtualDisplay.Callback callback =
new VirtualDisplay.Callback() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question for my learning: Is using this callback with onPaused() and onResumed() overridden to do nothing somehow different from passing null as the callback to createVirtualDisplay? Presumably onStopped will also do nothing without an explicit definition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback is currently a no-op but I wanted to leave the structure in the code for future debugging.

Interesting is that I've never had one of these methods be invoked and I've found some Stackoverflow posts asking why that is the case 🤷

@Override
public void onPaused() {}

@Override
public void onResumed() {}
};

public static VirtualDisplayController create(
Context context,
AccessibilityEventsDelegate accessibilityEventsDelegate,
Expand All @@ -40,6 +49,7 @@ public static VirtualDisplayController create(
DisplayManager displayManager =
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
final DisplayMetrics metrics = context.getResources().getDisplayMetrics();

// Virtual Display crashes for some PlatformViews if the width or height is bigger
// than the physical screen size. We have tried to clamp or scale down the size to prevent
// the crash, but both solutions lead to unwanted behavior because the
Expand All @@ -51,14 +61,17 @@ public static VirtualDisplayController create(
// virtual display and AndroidPlatformView widget.
// https://github.com/flutter/flutter/issues/93115
renderTarget.resize(width, height);
int flags = 0;
VirtualDisplay virtualDisplay =
displayManager.createVirtualDisplay(
"flutter-vd#" + viewId,
width,
height,
metrics.densityDpi,
renderTarget.getSurface(),
0);
flags,
callback,
null /* handler */);

if (virtualDisplay == null) {
return null;
Expand Down Expand Up @@ -148,9 +161,17 @@ public void resize(final int width, final int height, final Runnable onNewSizeFr
final DisplayManager displayManager =
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
renderTarget.resize(width, height);
int flags = 0;
virtualDisplay =
displayManager.createVirtualDisplay(
"flutter-vd#" + viewId, width, height, densityDpi, renderTarget.getSurface(), 0);
"flutter-vd#" + viewId,
width,
height,
densityDpi,
renderTarget.getSurface(),
flags,
callback,
null /* handler */);

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

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

Expand Down