Skip to content

Commit ae77d2e

Browse files
gmackallmackall-work
authored andcommitted
Fix hcpp flicker when scrolling off/on screen (flutter#181009)
Don't apply the transaction immediately, but let it be applied with flutter frame timing. Fixes flutter#175547 See videos: <table width="100%"> <thead> <tr> <th align="center">With Change</th> <th align="center">Without Change</th> </tr> </thead> <tbody> <tr> <td align="center" width="50%"> <video src="https://github.com/user-attachments/assets/0370a7bc-88b5-49f0-8702-ac844309a50d" controls="controls" style="max-width: 100%;"></video> </td> <td align="center" width="50%"> <video src="https://github.com/user-attachments/assets/5f18cdbe-4218-4c7d-b582-5d175aa964b6" controls="controls" style="max-width: 100%;"></video> </td> </tr> <tr> <td align="center"> <video src="https://github.com/user-attachments/assets/21ef1c37-47cc-4d04-ae49-d8acb9145aa8" controls="controls" style="max-width: 100%;"></video> </td> <td align="center"> <video src="https://github.com/user-attachments/assets/091c9ff5-0e25-4696-a006-73823fc0fd8d" controls="controls" style="max-width: 100%;"></video> </td> </tr> </tbody> </table> ## 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 `///`). - [ ] 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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Gray Mackall <[email protected]>
1 parent c3f986e commit ae77d2e

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
8383
jni_facade->hidePlatformView2(view_id);
8484
}
8585

86-
jni_facade->applyTransaction();
86+
jni_facade->swapTransaction();
87+
jni_facade->onEndFrame2();
8788
}));
8889
views_visible_last_frame_.clear();
8990
return;
@@ -170,13 +171,12 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
170171
slices = std::move(slices_),
171172
views_visible_last_frame = views_visible_last_frame_,
172173
overlay_layer_has_content_this_frame_]() mutable -> void {
173-
jni_facade->swapTransaction();
174-
175174
if (overlay_layer_has_content_this_frame_) {
176175
ShowOverlayLayerIfNeeded();
177176
} else {
178177
HideOverlayLayerIfNeeded();
179178
}
179+
jni_facade->swapTransaction();
180180

181181
for (int64_t view_id : composition_order) {
182182
DlRect view_rect = GetViewRect(view_id, view_params);

‎engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController2.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,19 +608,17 @@ public void showOverlaySurface() {
608608
if (overlaySurfaceControl == null) {
609609
return;
610610
}
611-
SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
611+
SurfaceControl.Transaction tx = createTransaction();
612612
tx.setVisibility(overlaySurfaceControl, /*visible=*/ true);
613-
tx.apply();
614613
}
615614

616615
@RequiresApi(API_LEVELS.API_34)
617616
public void hideOverlaySurface() {
618617
if (overlaySurfaceControl == null) {
619618
return;
620619
}
621-
SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
620+
SurfaceControl.Transaction tx = createTransaction();
622621
tx.setVisibility(overlaySurfaceControl, /*visible=*/ false);
623-
tx.apply();
624622
}
625623

626624
public boolean isHcppEnabled() {

‎engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsController2Test.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import android.graphics.SurfaceTexture;
1515
import android.media.Image;
1616
import android.util.SparseArray;
17+
import android.view.AttachedSurfaceControl;
1718
import android.view.MotionEvent;
1819
import android.view.Surface;
20+
import android.view.SurfaceControl;
1921
import android.view.SurfaceHolder;
2022
import android.view.SurfaceView;
2123
import android.view.View;
@@ -506,6 +508,51 @@ public void disposeNullAndroidView() {
506508
verify(platformView, times(1)).dispose();
507509
}
508510

511+
// Class member variable
512+
private SurfaceControl.Transaction mCapturedTx;
513+
514+
@Test
515+
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
516+
public void showOverlaySurfaceDefersTransactionUntilEndFrame() {
517+
518+
PlatformViewsController2 controller =
519+
new PlatformViewsController2() {
520+
@Override
521+
public SurfaceControl.Transaction createTransaction() {
522+
// Call super to ensure the real transaction is added to the private
523+
// 'pendingTransactions' list
524+
SurfaceControl.Transaction realTx = super.createTransaction();
525+
// Spy on it so we can verify calls like 'apply()'
526+
mCapturedTx = spy(realTx);
527+
return mCapturedTx;
528+
}
529+
};
530+
531+
PlatformViewRegistryImpl registry = new PlatformViewRegistryImpl();
532+
controller.setRegistry(registry);
533+
534+
// Mocks
535+
FlutterView mockFlutterView = mock(FlutterView.class);
536+
AttachedSurfaceControl mockAttachedSurfaceControl = mock(AttachedSurfaceControl.class);
537+
538+
when(mockFlutterView.getRootSurfaceControl()).thenReturn(mockAttachedSurfaceControl);
539+
when(mockAttachedSurfaceControl.buildReparentTransaction(any()))
540+
.thenReturn(new SurfaceControl.Transaction());
541+
542+
controller.attachToView(mockFlutterView);
543+
controller.createOverlaySurface();
544+
545+
controller.showOverlaySurface();
546+
assertNotNull("Transaction should have been created", mCapturedTx);
547+
verify(mCapturedTx, never()).apply();
548+
549+
controller.swapTransactions();
550+
controller.onEndFrame();
551+
552+
verify(mockAttachedSurfaceControl, times(1))
553+
.applyTransactionOnDraw(any(SurfaceControl.Transaction.class));
554+
}
555+
509556
private static ByteBuffer encodeMethodCall(MethodCall call) {
510557
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
511558
buffer.rewind();

0 commit comments

Comments
 (0)