Fix memory leak in PlatformViewsController (flutter/engine#27915)

This commit is contained in:
Emmanuel Garcia
2021-08-09 12:50:02 -07:00
committed by GitHub
parent 580802961b
commit 1af82bec75
2 changed files with 111 additions and 17 deletions

View File

@@ -59,7 +59,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
private View flutterView;
// The texture registry maintaining the textures into which the embedded views will be rendered.
private TextureRegistry textureRegistry;
@Nullable private TextureRegistry textureRegistry;
@Nullable private TextInputPlugin textInputPlugin;
@@ -79,7 +79,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
// Since each virtual display has it's unique context this allows associating any view with the
// platform view that
// it is associated with(e.g if a platform view creates other views in the same virtual display.
private final HashMap<Context, View> contextToPlatformView;
@VisibleForTesting /* package */ final HashMap<Context, View> contextToPlatformView;
// The views returned by `PlatformView#getView()`.
//
@@ -711,6 +711,10 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
while (platformViews.size() > 0) {
channelHandler.disposeAndroidViewForPlatformView(platformViews.keyAt(0));
}
if (contextToPlatformView.size() > 0) {
contextToPlatformView.clear();
}
}
private void initializeRootImageViewIfNeeded() {

View File

@@ -7,6 +7,7 @@ import static org.mockito.Mockito.*;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.SurfaceTexture;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.Surface;
@@ -14,6 +15,7 @@ import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout.LayoutParams;
import io.flutter.embedding.android.FlutterImageView;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.android.MotionEventTracker;
@@ -32,6 +34,7 @@ import io.flutter.embedding.engine.systemchannels.TextInputChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.StandardMethodCodec;
import io.flutter.plugin.localization.LocalizationPlugin;
import io.flutter.view.TextureRegistry;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
@@ -232,7 +235,7 @@ public class PlatformViewsControllerTest {
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
platformViewsController.initializePlatformViewIfNeeded(platformViewId);
@@ -259,7 +262,7 @@ public class PlatformViewsControllerTest {
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
verify(viewFactory, times(1)).create(any(), eq(platformViewId), any());
}
@@ -281,7 +284,7 @@ public class PlatformViewsControllerTest {
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
assertEquals(ShadowFlutterJNI.getResponses().size(), 1);
assertThrows(
@@ -291,6 +294,66 @@ public class PlatformViewsControllerTest {
});
}
@Test
@Config(shadows = {ShadowFlutterJNI.class})
public void onDetachedFromJNI_clearsPlatformViewContext() {
PlatformViewsController platformViewsController = new PlatformViewsController();
int platformViewId = 0;
assertNull(platformViewsController.getPlatformViewById(platformViewId));
PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);
View pv = mock(View.class);
when(pv.getLayoutParams()).thenReturn(new LayoutParams(1, 1));
when(platformView.getView()).thenReturn(pv);
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
FlutterJNI jni = new FlutterJNI();
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(
jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ false);
assertFalse(platformViewsController.contextToPlatformView.isEmpty());
platformViewsController.onDetachedFromJNI();
assertTrue(platformViewsController.contextToPlatformView.isEmpty());
}
@Test
@Config(shadows = {ShadowFlutterJNI.class})
public void onPreEngineRestart_clearsPlatformViewContext() {
PlatformViewsController platformViewsController = new PlatformViewsController();
int platformViewId = 0;
assertNull(platformViewsController.getPlatformViewById(platformViewId));
PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);
View pv = mock(View.class);
when(pv.getLayoutParams()).thenReturn(new LayoutParams(1, 1));
when(platformView.getView()).thenReturn(pv);
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
FlutterJNI jni = new FlutterJNI();
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(
jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ false);
assertFalse(platformViewsController.contextToPlatformView.isEmpty());
platformViewsController.onDetachedFromJNI();
assertTrue(platformViewsController.contextToPlatformView.isEmpty());
}
@Test
@Config(shadows = {ShadowFlutterJNI.class})
public void createPlatformViewMessage__throwsIfViewHasParent() {
@@ -311,7 +374,7 @@ public class PlatformViewsControllerTest {
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
assertEquals(ShadowFlutterJNI.getResponses().size(), 1);
assertThrows(
@@ -343,7 +406,7 @@ public class PlatformViewsControllerTest {
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
platformViewsController.initializePlatformViewIfNeeded(platformViewId);
assertNotNull(androidView.getParent());
@@ -354,7 +417,7 @@ public class PlatformViewsControllerTest {
assertNull(androidView.getParent());
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
platformViewsController.initializePlatformViewIfNeeded(platformViewId);
assertNotNull(androidView.getParent());
@@ -384,7 +447,7 @@ public class PlatformViewsControllerTest {
jni.onFirstFrame();
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
// Produce a frame that displays a platform view and an overlay surface.
platformViewsController.onBeginFrame();
@@ -447,7 +510,7 @@ public class PlatformViewsControllerTest {
jni.onFirstFrame();
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
// Simulate first frame from the framework.
jni.onFirstFrame();
@@ -484,7 +547,7 @@ public class PlatformViewsControllerTest {
jni.onFirstFrame();
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
platformViewsController.initializePlatformViewIfNeeded(platformViewId);
assertEquals(flutterView.getChildCount(), 2);
@@ -520,7 +583,7 @@ public class PlatformViewsControllerTest {
jni.onFirstFrame();
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
// Produce a frame that displays a platform view and an overlay surface.
platformViewsController.onBeginFrame();
@@ -653,7 +716,7 @@ public class PlatformViewsControllerTest {
jni.onFirstFrame();
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
// Produce a frame that displays a platform view and an overlay surface.
platformViewsController.onBeginFrame();
@@ -702,7 +765,7 @@ public class PlatformViewsControllerTest {
synchronizeToNativeViewHierarchy(jni, platformViewsController, false);
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType");
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
// Produce a frame that displays a platform view and an overlay surface.
platformViewsController.onBeginFrame();
@@ -734,12 +797,16 @@ public class PlatformViewsControllerTest {
FlutterJNI jni,
PlatformViewsController platformViewsController,
int platformViewId,
String viewType) {
String viewType,
boolean hybrid) {
final Map<String, Object> platformViewCreateArguments = new HashMap<>();
platformViewCreateArguments.put("hybrid", true);
platformViewCreateArguments.put("hybrid", hybrid);
platformViewCreateArguments.put("id", platformViewId);
platformViewCreateArguments.put("viewType", viewType);
platformViewCreateArguments.put("direction", 0);
platformViewCreateArguments.put("width", 1.0);
platformViewCreateArguments.put("height", 1.0);
final MethodCall platformCreateMethodCall =
new MethodCall("create", platformViewCreateArguments);
@@ -776,7 +843,30 @@ public class PlatformViewsControllerTest {
executor.onAttachedToJNI();
final Context context = RuntimeEnvironment.application.getApplicationContext();
platformViewsController.attach(context, null, executor);
final TextureRegistry registry =
new TextureRegistry() {
public void TextureRegistry() {}
@Override
public SurfaceTextureEntry createSurfaceTexture() {
return new SurfaceTextureEntry() {
@Override
public SurfaceTexture surfaceTexture() {
return mock(SurfaceTexture.class);
}
@Override
public long id() {
return 0;
}
@Override
public void release() {}
};
}
};
platformViewsController.attach(context, registry, executor);
final FlutterView view =
new FlutterView(context, FlutterView.RenderMode.surface) {