Workaround HardwareRenderer breakage in Android 14 (flutter/engine#52370)
- Destroy ImageReaders on memory trim. - Unset the VirtualDisplay's surface on memory trim. - On resume, recreate ImageReaders. - On resume, do a dumb little dance and then set the VirtualDisplay's surface Fixes: https://github.com/flutter/flutter/issues/146499 Fixes: https://github.com/flutter/flutter/issues/144219 Internal bug: b/335646931 Android Fix: https://googleplex-android-review.git.corp.google.com/c/platform/frameworks/base/+/27015418 Android 15 will include the fix. Unclear if Android 14 will be patched.
This commit is contained in:
@@ -583,6 +583,7 @@ import java.util.List;
|
||||
ensureAlive();
|
||||
if (flutterEngine != null) {
|
||||
updateSystemUiOverlays();
|
||||
flutterEngine.getPlatformViewsController().onResume();
|
||||
} else {
|
||||
Log.w(TAG, "onPostResume() invoked before FlutterFragment was attached to an Activity.");
|
||||
}
|
||||
@@ -1020,6 +1021,7 @@ import java.util.List;
|
||||
flutterEngine.getSystemChannel().sendMemoryPressureWarning();
|
||||
}
|
||||
flutterEngine.getRenderer().onTrimMemory(level);
|
||||
flutterEngine.getPlatformViewsController().onTrimMemory(level);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ package io.flutter.embedding.engine.renderer;
|
||||
import static io.flutter.Build.API_LEVELS;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.ComponentCallbacks2;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.ImageFormat;
|
||||
import android.graphics.Rect;
|
||||
@@ -418,10 +419,9 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
// Flip when debugging to see verbose logs.
|
||||
private static final boolean VERBOSE_LOGS = false;
|
||||
|
||||
// If we cleanup the ImageReaders on memory pressure it breaks VirtualDisplay
|
||||
// backed platform views. Disable for now as this is only necessary to work
|
||||
// around a Samsung-specific Android 14 bug.
|
||||
private static final boolean CLEANUP_ON_MEMORY_PRESSURE = false;
|
||||
// We must always cleanup on memory pressure on Android 14 due to a bug in Android.
|
||||
// It is safe to do on all versions so we unconditionally have this set to true.
|
||||
private static final boolean CLEANUP_ON_MEMORY_PRESSURE = true;
|
||||
|
||||
private final long id;
|
||||
|
||||
@@ -429,6 +429,8 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
// Will be true in tests and on Android API < 33.
|
||||
private boolean ignoringFence = false;
|
||||
|
||||
private boolean trimOnMemoryPressure = CLEANUP_ON_MEMORY_PRESSURE;
|
||||
|
||||
// The requested width and height are updated by setSize.
|
||||
private int requestedWidth = 1;
|
||||
private int requestedHeight = 1;
|
||||
@@ -442,6 +444,7 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
private long lastDequeueTime = 0;
|
||||
private long lastQueueTime = 0;
|
||||
private long lastScheduleTime = 0;
|
||||
private int numTrims = 0;
|
||||
|
||||
private Object lock = new Object();
|
||||
// REQUIRED: The following fields must only be accessed when lock is held.
|
||||
@@ -659,9 +662,15 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
|
||||
@Override
|
||||
public void onTrimMemory(int level) {
|
||||
if (!CLEANUP_ON_MEMORY_PRESSURE) {
|
||||
if (!trimOnMemoryPressure) {
|
||||
return;
|
||||
}
|
||||
if (level < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) {
|
||||
return;
|
||||
}
|
||||
synchronized (lock) {
|
||||
numTrims++;
|
||||
}
|
||||
cleanup();
|
||||
createNewReader = true;
|
||||
}
|
||||
@@ -878,6 +887,13 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public int numTrims() {
|
||||
synchronized (lock) {
|
||||
return numTrims;
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public int numImages() {
|
||||
int r = 0;
|
||||
|
||||
@@ -9,6 +9,7 @@ import static android.view.MotionEvent.PointerProperties;
|
||||
import static io.flutter.Build.API_LEVELS;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.ComponentCallbacks2;
|
||||
import android.content.Context;
|
||||
import android.content.MutableContextWrapper;
|
||||
import android.os.Build;
|
||||
@@ -1053,6 +1054,24 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
}
|
||||
}
|
||||
|
||||
// Invoked when the Android system is requesting we reduce memory usage.
|
||||
public void onTrimMemory(int level) {
|
||||
if (level < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) {
|
||||
return;
|
||||
}
|
||||
for (VirtualDisplayController vdc : vdControllers.values()) {
|
||||
vdc.clearSurface();
|
||||
}
|
||||
}
|
||||
|
||||
// Called after the application has been resumed.
|
||||
// This is where we undo whatever may have been done in onTrimMemory.
|
||||
public void onResume() {
|
||||
for (VirtualDisplayController vdc : vdControllers.values()) {
|
||||
vdc.resetSurface();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes a single
|
||||
*
|
||||
|
||||
@@ -284,6 +284,49 @@ class VirtualDisplayController {
|
||||
presentation.dispatchTouchEvent(event);
|
||||
}
|
||||
|
||||
public void clearSurface() {
|
||||
virtualDisplay.setSurface(null);
|
||||
}
|
||||
|
||||
public void resetSurface() {
|
||||
final int width = getRenderTargetWidth();
|
||||
final int height = getRenderTargetHeight();
|
||||
final boolean isFocused = getView().isFocused();
|
||||
final SingleViewPresentation.PresentationState presentationState = presentation.detachState();
|
||||
|
||||
// We detach the surface to prevent it being destroyed when releasing the vd.
|
||||
virtualDisplay.setSurface(null);
|
||||
virtualDisplay.release();
|
||||
final DisplayManager displayManager =
|
||||
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
|
||||
int flags = 0;
|
||||
virtualDisplay =
|
||||
displayManager.createVirtualDisplay(
|
||||
"flutter-vd#" + viewId,
|
||||
width,
|
||||
height,
|
||||
densityDpi,
|
||||
renderTarget.getSurface(),
|
||||
flags,
|
||||
callback,
|
||||
null /* handler */);
|
||||
// Create a new SingleViewPresentation and show() it before we cancel() the existing
|
||||
// presentation. Calling show() and cancel() in this order fixes
|
||||
// https://github.com/flutter/flutter/issues/26345 and maintains seamless transition
|
||||
// of the contents of the presentation.
|
||||
SingleViewPresentation newPresentation =
|
||||
new SingleViewPresentation(
|
||||
context,
|
||||
virtualDisplay.getDisplay(),
|
||||
accessibilityEventsDelegate,
|
||||
presentationState,
|
||||
focusChangeListener,
|
||||
isFocused);
|
||||
newPresentation.show();
|
||||
presentation.cancel();
|
||||
presentation = newPresentation;
|
||||
}
|
||||
|
||||
static class OneTimeOnDrawListener implements ViewTreeObserver.OnDrawListener {
|
||||
static void schedule(View view, Runnable runnable) {
|
||||
OneTimeOnDrawListener listener = new OneTimeOnDrawListener(view, runnable);
|
||||
|
||||
@@ -647,13 +647,41 @@ public class FlutterRendererTest {
|
||||
assertEquals(1, texture.numImageReaders());
|
||||
assertEquals(1, texture.numImages());
|
||||
|
||||
// Invoke the onTrimMemory callback.
|
||||
// Invoke the onTrimMemory callback with level 0.
|
||||
// This should do nothing.
|
||||
texture.onTrimMemory(0);
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
assertEquals(1, texture.numImageReaders());
|
||||
assertEquals(1, texture.numImages());
|
||||
assertEquals(0, texture.numTrims());
|
||||
|
||||
// Invoke the onTrimMemory callback with level 40.
|
||||
// This should result in a trim.
|
||||
texture.onTrimMemory(40);
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
assertEquals(0, texture.numImageReaders());
|
||||
assertEquals(0, texture.numImages());
|
||||
assertEquals(1, texture.numTrims());
|
||||
|
||||
// Request the surface, this should result in a new image reader.
|
||||
surface = texture.getSurface();
|
||||
assertEquals(1, texture.numImageReaders());
|
||||
assertEquals(0, texture.numImages());
|
||||
assertEquals(1, texture.numTrims());
|
||||
|
||||
// Render an image.
|
||||
canvas = surface.lockHardwareCanvas();
|
||||
canvas.drawARGB(255, 255, 0, 0);
|
||||
surface.unlockCanvasAndPost(canvas);
|
||||
|
||||
// Let callbacks run, this will produce a single frame.
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
assertEquals(1, texture.numImageReaders());
|
||||
assertEquals(1, texture.numImages());
|
||||
assertEquals(1, texture.numTrims());
|
||||
}
|
||||
|
||||
// A 0x0 ImageReader is a runtime error.
|
||||
|
||||
Reference in New Issue
Block a user