From 215ff2e7feab950286cf1dfc5159db7331b8fa74 Mon Sep 17 00:00:00 2001
From: Gray Mackall <34871572+gmackall@users.noreply.github.com>
Date: Thu, 15 Jan 2026 18:30:12 -0800
Subject: [PATCH] Fix hcpp flicker when scrolling off/on screen (#181009)
Don't apply the transaction immediately, but let it be applied with
flutter frame timing.
Fixes https://github.com/flutter/flutter/issues/175547
See videos:
| With Change |
Without Change |
|
|
|
|
|
|
## 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.
[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
---
.../external_view_embedder_2.cc | 6 +--
.../platform/PlatformViewsController2.java | 6 +--
.../PlatformViewsController2Test.java | 47 +++++++++++++++++++
3 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc b/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc
index d1a7d19ebca..defcd11467d 100644
--- a/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc
+++ b/engine/src/flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc
@@ -83,7 +83,8 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
jni_facade->hidePlatformView2(view_id);
}
- jni_facade->applyTransaction();
+ jni_facade->swapTransaction();
+ jni_facade->onEndFrame2();
}));
views_visible_last_frame_.clear();
return;
@@ -170,13 +171,12 @@ void AndroidExternalViewEmbedder2::SubmitFlutterView(
slices = std::move(slices_),
views_visible_last_frame = views_visible_last_frame_,
overlay_layer_has_content_this_frame_]() mutable -> void {
- jni_facade->swapTransaction();
-
if (overlay_layer_has_content_this_frame_) {
ShowOverlayLayerIfNeeded();
} else {
HideOverlayLayerIfNeeded();
}
+ jni_facade->swapTransaction();
for (int64_t view_id : composition_order) {
DlRect view_rect = GetViewRect(view_id, view_params);
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController2.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController2.java
index a5b4cbad9b7..cee347b0b1e 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController2.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController2.java
@@ -608,9 +608,8 @@ public class PlatformViewsController2 implements PlatformViewsAccessibilityDeleg
if (overlaySurfaceControl == null) {
return;
}
- SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
+ SurfaceControl.Transaction tx = createTransaction();
tx.setVisibility(overlaySurfaceControl, /*visible=*/ true);
- tx.apply();
}
@RequiresApi(API_LEVELS.API_34)
@@ -618,9 +617,8 @@ public class PlatformViewsController2 implements PlatformViewsAccessibilityDeleg
if (overlaySurfaceControl == null) {
return;
}
- SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
+ SurfaceControl.Transaction tx = createTransaction();
tx.setVisibility(overlaySurfaceControl, /*visible=*/ false);
- tx.apply();
}
public boolean isHcppEnabled() {
diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsController2Test.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsController2Test.java
index 3df7d5ee7e7..0b532a37dc2 100644
--- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsController2Test.java
+++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsController2Test.java
@@ -14,8 +14,10 @@ import android.content.res.AssetManager;
import android.graphics.SurfaceTexture;
import android.media.Image;
import android.util.SparseArray;
+import android.view.AttachedSurfaceControl;
import android.view.MotionEvent;
import android.view.Surface;
+import android.view.SurfaceControl;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
@@ -506,6 +508,51 @@ public class PlatformViewsController2Test {
verify(platformView, times(1)).dispose();
}
+ // Class member variable
+ private SurfaceControl.Transaction mCapturedTx;
+
+ @Test
+ @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
+ public void showOverlaySurfaceDefersTransactionUntilEndFrame() {
+
+ PlatformViewsController2 controller =
+ new PlatformViewsController2() {
+ @Override
+ public SurfaceControl.Transaction createTransaction() {
+ // Call super to ensure the real transaction is added to the private
+ // 'pendingTransactions' list
+ SurfaceControl.Transaction realTx = super.createTransaction();
+ // Spy on it so we can verify calls like 'apply()'
+ mCapturedTx = spy(realTx);
+ return mCapturedTx;
+ }
+ };
+
+ PlatformViewRegistryImpl registry = new PlatformViewRegistryImpl();
+ controller.setRegistry(registry);
+
+ // Mocks
+ FlutterView mockFlutterView = mock(FlutterView.class);
+ AttachedSurfaceControl mockAttachedSurfaceControl = mock(AttachedSurfaceControl.class);
+
+ when(mockFlutterView.getRootSurfaceControl()).thenReturn(mockAttachedSurfaceControl);
+ when(mockAttachedSurfaceControl.buildReparentTransaction(any()))
+ .thenReturn(new SurfaceControl.Transaction());
+
+ controller.attachToView(mockFlutterView);
+ controller.createOverlaySurface();
+
+ controller.showOverlaySurface();
+ assertNotNull("Transaction should have been created", mCapturedTx);
+ verify(mCapturedTx, never()).apply();
+
+ controller.swapTransactions();
+ controller.onEndFrame();
+
+ verify(mockAttachedSurfaceControl, times(1))
+ .applyTransactionOnDraw(any(SurfaceControl.Transaction.class));
+ }
+
private static ByteBuffer encodeMethodCall(MethodCall call) {
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
buffer.rewind();