Skip to content

Commit 9babc9c

Browse files
Fix Content Sizing Race Condition (#182326)
Only call MaybeResizeSurfaceView if the expected frame_size is not the same size as what is returned from the Dart layout. Also, remove the gating function that prevents viewport metrics from being sent on a resize. The AtomicBoolean that is gating calls back into the engine has to be removed. This is necessary because the buffer for the SurfaceView is only updated once the surface finishes drawing to the screen. Since the frame can be submitted before the buffer is updated (but after the resize call happens), the UI rejects the frame because the buffer dimensions do not match. Therefore, the callback with the updated viewport metrics is necessary for the frame to be re-tried. I believe this AtomicBoolean is ultimately the cause of the initial flake. Since rotations are actually changing the size, the AtomicBoolean would have been set to not send the viewport metrics back when necessary. The call from the engine should remain non-blocking to prevent deadlocks. Fixes: #182242 ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent c477400 commit 9babc9c

4 files changed

Lines changed: 17 additions & 25 deletions

File tree

‎engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder.cc‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,17 @@ void AndroidExternalViewEmbedder::PrepareFlutterView(
224224
// the existing surfaces in the pool can't be recycled.
225225
if (frame_size_ != frame_size) {
226226
DestroySurfaces();
227+
228+
// This should not block to prevent deadlocks with
229+
// setViewportMetrics.
230+
task_runners_.GetPlatformTaskRunner()->PostTask(fml::MakeCopyable(
231+
[jni_facade = jni_facade_, frame_size = frame_size]() {
232+
jni_facade->MaybeResizeSurfaceView(frame_size.width,
233+
frame_size.height);
234+
}));
227235
}
228236
surface_pool_->SetFrameSize(frame_size);
229237

230-
task_runners_.GetPlatformTaskRunner()->PostTask(
231-
fml::MakeCopyable([jni_facade = jni_facade_, frame_size = frame_size]() {
232-
jni_facade->MaybeResizeSurfaceView(frame_size.width, frame_size.height);
233-
}));
234-
235238
frame_size_ = frame_size;
236239
device_pixel_ratio_ = device_pixel_ratio;
237240
}

‎engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,17 @@ void AndroidExternalViewEmbedder2::PrepareFlutterView(
243243
// the existing surfaces in the pool can't be recycled.
244244
if (frame_size_ != frame_size) {
245245
DestroySurfaces();
246+
247+
// This should not block to prevent deadlocks with
248+
// setViewportMetrics.
249+
task_runners_.GetPlatformTaskRunner()->PostTask(fml::MakeCopyable(
250+
[jni_facade = jni_facade_, frame_size = frame_size]() {
251+
jni_facade->MaybeResizeSurfaceView(frame_size.width,
252+
frame_size.height);
253+
}));
246254
}
247255
surface_pool_->SetFrameSize(frame_size);
248256

249-
task_runners_.GetPlatformTaskRunner()->PostTask(
250-
fml::MakeCopyable([jni_facade = jni_facade_, frame_size = frame_size]() {
251-
jni_facade->MaybeResizeSurfaceView(frame_size.width, frame_size.height);
252-
}));
253-
254257
frame_size_ = frame_size;
255258
device_pixel_ratio_ = device_pixel_ratio;
256259
}

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
import java.util.HashSet;
7979
import java.util.List;
8080
import java.util.Set;
81-
import java.util.concurrent.atomic.AtomicBoolean;
8281

8382
/**
8483
* Displays a Flutter UI on an Android device.
@@ -115,11 +114,6 @@ public class FlutterView extends FrameLayout
115114
private static final String TAG = "FlutterView";
116115
private static final String GBOARD_PACKAGE_NAME = "com.google.android.inputmethod.latin";
117116

118-
// Boolean that gates if view port metrics should be sent. When the
119-
// engine informs the embedder of a resize, it is not necessary to send
120-
// the viewport metrics back to the engine.
121-
private AtomicBoolean shouldSendViewportMetrics = new AtomicBoolean(true);
122-
123117
// Maximum size allowed for a content sized view.
124118
@VisibleForTesting static final int CONTENT_SIZING_MAX = 2 << 12;
125119

@@ -210,7 +204,6 @@ public void resizeEngineView(int width, int height) {
210204
surfaceParams.width = width;
211205
}
212206
if (changed) {
213-
shouldSendViewportMetrics.set(false);
214207
flutterEngineView.setLayoutParams(surfaceParams);
215208
}
216209
} else {
@@ -543,13 +536,7 @@ protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
543536
viewportMetrics.maxWidth = viewportMetrics.width;
544537
}
545538

546-
if (shouldSendViewportMetrics.compareAndSet(false, true)) {
547-
Log.d(
548-
TAG,
549-
"Resize was in response to the engine resizing the view. Not sending viewport metrics.");
550-
} else {
551-
sendViewportMetricsToFlutter();
552-
}
539+
sendViewportMetricsToFlutter();
553540
}
554541

555542
@VisibleForTesting()

‎engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ static void SetViewportMetrics(JNIEnv* env,
379379
env->GetIntArrayRegion(javaDisplayFeaturesState, 0, stateSize,
380380
&displayFeaturesState[0]);
381381

382-
// TODO(boetger): update for https://github.com/flutter/flutter/issues/149033
383382
const flutter::ViewportMetrics metrics{
384383
static_cast<double>(devicePixelRatio), // p_device_pixel_ratio
385384
static_cast<double>(physicalWidth), // p_physical_width

0 commit comments

Comments
 (0)