1111import android .graphics .PixelFormat ;
1212import android .graphics .Rect ;
1313import android .graphics .RectF ;
14+ import android .os .Build ;
1415import android .util .SparseArray ;
1516import android .view .AttachedSurfaceControl ;
1617import android .view .Gravity ;
@@ -75,8 +76,17 @@ public class PlatformViewsController2 implements PlatformViewsAccessibilityDeleg
7576 private final SparseArray <FlutterMutatorView > platformViewParent ;
7677 private final MotionEventTracker motionEventTracker ;
7778
78- private final ArrayList <SurfaceControl .Transaction > pendingTransactions ;
79- private final ArrayList <SurfaceControl .Transaction > activeTransactions ;
79+ // Guarded by transactionLock: the raster thread appends; the platform thread drains.
80+ private final ArrayList <SurfaceControl .Transaction > pendingRasterTransactions ;
81+ private final Object transactionLock = new Object ();
82+
83+ // Platform-thread only: populated by swapTransactions(), drained by onEndFrame().
84+ private final ArrayList <SurfaceControl .Transaction > activeRasterTransactions ;
85+
86+ // Platform-thread only. Clips and overlay visibility share one transaction per frame.
87+ private SurfaceControl .Transaction pendingPlatformTransaction ;
88+ private SurfaceControl .Transaction activePlatformTransaction ;
89+
8090 private Surface overlayerSurface = null ;
8191 private SurfaceControl overlaySurfaceControl = null ;
8292
@@ -86,8 +96,8 @@ public PlatformViewsController2() {
8696 accessibilityEventsDelegate = new AccessibilityEventsDelegate ();
8797 platformViews = new SparseArray <>();
8898 platformViewParent = new SparseArray <>();
89- pendingTransactions = new ArrayList <>();
90- activeTransactions = new ArrayList <>();
99+ pendingRasterTransactions = new ArrayList <>();
100+ activeRasterTransactions = new ArrayList <>();
91101 motionEventTracker = MotionEventTracker .getInstance ();
92102 }
93103
@@ -269,6 +279,10 @@ public void detachFromView() {
269279 destroyOverlaySurface ();
270280 flutterView = null ;
271281
282+ if (Build .VERSION .SDK_INT >= API_LEVELS .API_34 ) {
283+ dropTransactions ();
284+ }
285+
272286 // Notify that the platform view have been detached from FlutterView.
273287 for (int index = 0 ; index < platformViews .size (); index ++) {
274288 final PlatformView view = platformViews .valueAt (index );
@@ -616,7 +630,7 @@ private void maybeApplyClipToSurfaceView(
616630 return ;
617631 }
618632 SurfaceControl .Transaction tx =
619- createTransaction ().setAlpha (sc , opacity ).setCrop (sc , screenRect );
633+ platformTransaction ().setAlpha (sc , opacity ).setCrop (sc , screenRect );
620634 }
621635
622636 @ RequiresApi (API_LEVELS .API_34 )
@@ -631,7 +645,7 @@ public void surfaceCreated(@NonNull SurfaceHolder holder) {
631645 SurfaceControl surfaceControl = surfaceView .getSurfaceControl ();
632646 if (surfaceControl != null && surfaceControl .isValid ()) {
633647 SurfaceControl .Transaction tx =
634- createTransaction ()
648+ platformTransaction ()
635649 .setAlpha (surfaceControl , opacity )
636650 .setCrop (surfaceControl , screenRect );
637651 } else {
@@ -671,22 +685,29 @@ public void hidePlatformView(int viewId) {
671685 parentView .setVisibility (View .GONE );
672686 }
673687
688+ @ UiThread
674689 @ RequiresApi (API_LEVELS .API_34 )
675690 public void onEndFrame () {
691+ final SurfaceControl .Transaction platformTx = activePlatformTransaction ;
692+ activePlatformTransaction = null ;
693+
694+ // Use a fresh destination so cleanup cannot close a raster input still in native use.
695+ // Bypass newTransaction(), which tests may override to return an existing input.
676696 SurfaceControl .Transaction tx = new SurfaceControl .Transaction ();
677- for (int i = 0 ; i < activeTransactions .size (); i ++) {
678- tx = tx .merge (activeTransactions .get (i ));
697+ if (platformTx != null ) {
698+ tx = tx .merge (platformTx );
699+ platformTx .close ();
679700 }
680- activeTransactions .clear ();
701+ for (SurfaceControl .Transaction rasterTx : activeRasterTransactions ) {
702+ tx = tx .merge (rasterTx );
703+ }
704+ activeRasterTransactions .clear ();
681705
682- // This runs on the platform thread but is posted from the raster thread, so by the time it
683- // runs the FlutterView may have been detached from the controller, or detached from its
684- // window (in which case getRootSurfaceControl() returns null). Throwing here is fatal rather
685- // than merely wrong: the JNI caller, onEndFrame2(), turns a pending Java exception into an
686- // abort() via FML_CHECK(fml::jni::CheckException(env)).
706+ // The view or its window may detach before this posted frame runs.
687707 final AttachedSurfaceControl rootSurfaceControl =
688708 flutterView == null ? null : flutterView .getRootSurfaceControl ();
689709 if (rootSurfaceControl == null ) {
710+ // Release the unapplied transaction and its owned fence FDs.
690711 tx .close ();
691712 return ;
692713 }
@@ -695,30 +716,77 @@ public void onEndFrame() {
695716 rootSurfaceControl .applyTransactionOnDraw (tx );
696717 }
697718
698- // NOT called from UI thread.
699- public synchronized void swapTransactions () {
700- activeTransactions .clear ();
701- activeTransactions .addAll (pendingTransactions );
702- pendingTransactions .clear ();
719+ @ UiThread
720+ @ RequiresApi (API_LEVELS .API_34 )
721+ public void swapTransactions () {
722+ // Do not close discarded raster inputs; native producers may still use them.
723+ activeRasterTransactions .clear ();
724+ synchronized (transactionLock ) {
725+ activeRasterTransactions .addAll (pendingRasterTransactions );
726+ pendingRasterTransactions .clear ();
727+ }
728+
729+ if (activePlatformTransaction != null ) {
730+ // Discard a platform transaction whose onEndFrame() never ran.
731+ activePlatformTransaction .close ();
732+ }
733+ activePlatformTransaction = pendingPlatformTransaction ;
734+ pendingPlatformTransaction = null ;
735+ }
736+
737+ /**
738+ * Discards the transaction state of the view that just detached.
739+ *
740+ * <p>Platform transactions are closed here: they only touch SurfaceControls that this detach
741+ * invalidates, including the overlay that {@link #destroyOverlaySurface()} just released, and
742+ * nothing outside the platform thread references them. Raster transactions are dropped without
743+ * closing them, because their native producers may still be writing into them.
744+ */
745+ @ UiThread
746+ @ RequiresApi (API_LEVELS .API_34 )
747+ private void dropTransactions () {
748+ activeRasterTransactions .clear ();
749+ synchronized (transactionLock ) {
750+ pendingRasterTransactions .clear ();
751+ }
752+
753+ if (activePlatformTransaction != null ) {
754+ activePlatformTransaction .close ();
755+ activePlatformTransaction = null ;
756+ }
757+ if (pendingPlatformTransaction != null ) {
758+ pendingPlatformTransaction .close ();
759+ pendingPlatformTransaction = null ;
760+ }
761+ }
762+
763+ @ UiThread
764+ @ RequiresApi (API_LEVELS .API_34 )
765+ private SurfaceControl .Transaction platformTransaction () {
766+ if (pendingPlatformTransaction == null ) {
767+ pendingPlatformTransaction = newTransaction ();
768+ }
769+ return pendingPlatformTransaction ;
703770 }
704771
705- // NOT called from UI thread.
772+ // Called from the raster thread through FlutterJNI .
706773 @ RequiresApi (API_LEVELS .API_34 )
707774 public SurfaceControl .Transaction createTransaction () {
708- SurfaceControl .Transaction tx = new SurfaceControl .Transaction ();
709- pendingTransactions .add (tx );
775+ final SurfaceControl .Transaction tx = newTransaction ();
776+ // This lock protects the list, not AHBSwapchainImplVK::Present's later native writes.
777+ // Those can race merging or GC freeing the transaction. Fix both hazards by retaining it
778+ // natively and publishing only after the writes finish.
779+ synchronized (transactionLock ) {
780+ pendingRasterTransactions .add (tx );
781+ }
710782 return tx ;
711783 }
712784
713- // NOT called from UI thread.
785+ /** Allocates a transaction so tests can spy on the instance retained by the controller. */
786+ @ VisibleForTesting
714787 @ RequiresApi (API_LEVELS .API_34 )
715- public void applyTransactions () {
716- SurfaceControl .Transaction tx = new SurfaceControl .Transaction ();
717- for (int i = 0 ; i < pendingTransactions .size (); i ++) {
718- tx = tx .merge (pendingTransactions .get (i ));
719- }
720- tx .apply ();
721- pendingTransactions .clear ();
788+ SurfaceControl .Transaction newTransaction () {
789+ return new SurfaceControl .Transaction ();
722790 }
723791
724792 @ RequiresApi (API_LEVELS .API_34 )
@@ -755,7 +823,7 @@ public void showOverlaySurface() {
755823 if (overlaySurfaceControl == null ) {
756824 return ;
757825 }
758- SurfaceControl .Transaction tx = createTransaction ();
826+ SurfaceControl .Transaction tx = platformTransaction ();
759827 tx .setVisibility (overlaySurfaceControl , /*visible=*/ true );
760828 }
761829
@@ -764,7 +832,7 @@ public void hideOverlaySurface() {
764832 if (overlaySurfaceControl == null ) {
765833 return ;
766834 }
767- SurfaceControl .Transaction tx = createTransaction ();
835+ SurfaceControl .Transaction tx = platformTransaction ();
768836 tx .setVisibility (overlaySurfaceControl , /*visible=*/ false );
769837 }
770838
0 commit comments