Skip to content

Commit c40b49d

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 (cherry picked from commit 215ff2e) Co-authored-by: Gray Mackall <[email protected]>
1 parent 12b1532 commit c40b49d

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)