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:

<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 <mackall@google.com>
This commit is contained in:
Gray Mackall 2026-01-15 18:30:12 -08:00 committed by GitHub
parent f89275a408
commit 215ff2e7fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 7 deletions

View File

@ -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);

View File

@ -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() {

View File

@ -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();