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

Minor refactor: move flutterViewConvertedToImageView to FlutterView from PlatformViewsController. #31321

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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 @@ -135,6 +135,8 @@ public class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseC
private final FlutterRenderer.ViewportMetrics viewportMetrics =
new FlutterRenderer.ViewportMetrics();

// Tracks whether the flutterView has been converted to use a FlutterImageView.
private boolean hasConvertedToImageView = false;
private final AccessibilityBridge.OnAccessibilityChangeListener onAccessibilityChangeListener =
new AccessibilityBridge.OnAccessibilityChangeListener() {
@Override
Expand Down Expand Up @@ -406,6 +408,15 @@ public boolean hasRenderedFirstFrame() {
return isFlutterUiDisplayed;
}

/**
* Whether this {@code FlutterView} has been converted to use a {@link FlutterImageView}.
*
* <p>Returns ture if the surface is rendered by a {@link FlutterImageView}.
*/
public boolean hasConvertedToImageView() {
return hasConvertedToImageView;
}

/**
* Adds the given {@code listener} to this {@code FlutterView}, to be notified upon Flutter's
* first rendered frame.
Expand Down Expand Up @@ -1264,6 +1275,8 @@ public void detachFromFlutterEngine() {
removeView(flutterImageView);
flutterImageView = null;
}

hasConvertedToImageView = false;
previousRenderSurface = null;
flutterEngine = null;
}
Expand All @@ -1280,6 +1293,10 @@ public FlutterImageView createImageView() {
* Otherwise, it resizes the {@link FlutterImageView} based on the current view size.
*/
public void convertToImageView() {
if (hasConvertedToImageView) {
return;
}

renderSurface.pause();

if (flutterImageView == null) {
Expand All @@ -1294,6 +1311,8 @@ public void convertToImageView() {
if (flutterEngine != null) {
renderSurface.attachToRenderer(flutterEngine.getRenderer());
}

hasConvertedToImageView = true;
}

/**
Expand All @@ -1314,6 +1333,8 @@ public void revertImageView(@NonNull Runnable onDone) {
}
renderSurface = previousRenderSurface;
previousRenderSurface = null;
hasConvertedToImageView = false;

if (flutterEngine == null) {
flutterImageView.detachFromRenderer();
onDone.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
// Next available unique ID for use in overlayLayerViews.
private int nextOverlayLayerId = 0;

// Tracks whether the flutterView has been converted to use a FlutterImageView.
private boolean flutterViewConvertedToImageView = false;

// When adding platform views using Hybrid Composition, the engine converts the render surface
// to a FlutterImageView to help improve animation synchronization on Android. This flag allows
// disabling this conversion through the PlatformView platform channel.
Expand Down Expand Up @@ -510,7 +507,6 @@ public void detachFromView() {
destroyOverlaySurfaces();
removeOverlaySurfaces();
this.flutterView = null;
flutterViewConvertedToImageView = false;

// Inform all existing platform views that they are no longer associated with
// a Flutter View.
Expand Down Expand Up @@ -726,9 +722,8 @@ private void flushAllViews() {
}

private void initializeRootImageViewIfNeeded() {
if (synchronizeToNativeViewHierarchy && !flutterViewConvertedToImageView) {
if (synchronizeToNativeViewHierarchy) {
flutterView.convertToImageView();
flutterViewConvertedToImageView = true;
}
}

Expand Down Expand Up @@ -863,8 +858,7 @@ public void onEndFrame() {
// then revert the image view surface and use the previous surface.
//
// Otherwise, acquire the latest image.
if (flutterViewConvertedToImageView && currentFrameUsedPlatformViewIds.isEmpty()) {
flutterViewConvertedToImageView = false;
if (flutterView.hasConvertedToImageView() && currentFrameUsedPlatformViewIds.isEmpty()) {
flutterView.revertImageView(
() -> {
// Destroy overlay surfaces once the surface reversion is completed.
Expand All @@ -882,7 +876,7 @@ public void onEndFrame() {
// dropped.
// For example, a toolbar widget painted by Flutter may not be rendered.
final boolean isFrameRenderedUsingImageReaders =
flutterViewConvertedToImageView && flutterView.acquireLatestImageViewFrame();
flutterView.hasConvertedToImageView() && flutterView.acquireLatestImageViewFrame();
finishFrame(isFrameRenderedUsingImageReaders);
}

Expand All @@ -899,7 +893,7 @@ private void finishFrame(boolean isFrameRenderedUsingImageReaders) {
// If the background surface isn't rendered by the image view, then the
// overlay surfaces can be detached from the rendered.
// This releases resources used by the ImageReader.
if (!flutterViewConvertedToImageView) {
if (!flutterView.hasConvertedToImageView()) {
overlayView.detachFromRenderer();
}
// Hide overlay surfaces that aren't rendered in the current frame.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ public void detachFromFlutterEngine_revertImageView() {

flutterView.convertToImageView();
assertTrue(flutterView.renderSurface instanceof FlutterImageView);
assertTrue(flutterView.hasConvertedToImageView());

flutterView.detachFromFlutterEngine();
assertFalse(flutterView.renderSurface instanceof FlutterImageView);
assertFalse(flutterView.hasConvertedToImageView());
}

@Test
Expand Down