Fix SurfaceFlinger OOM issue.

Fixed issue where new SurfaceControl instances
were being created on each render to the front
buffer instead of reusing the same SurfaceControl
instance. Updated the buildReparentTransaction
implementation to always pass the parentSurfaceControl
instead of the SurfaceView directly. This was because
the implementation of SurfaceControlCompat.Transaction#
reparent that consumed a SurfaceView internally created
a SurfaceControl each time.

Relnote: "Removed SurfaceControlCompat.Transaction
reparent API that consumed a SurfaceView as it internally
created SurfaceControl instances that were never released
and consuming resources indefinitely."

Fixes: 244489658
Test: Added test to GLFrontBufferedRenderer test
Change-Id: Ib7ff25337ef763c219045780a48101c43770cf19
diff --git a/graphics/graphics-core/api/current.txt b/graphics/graphics-core/api/current.txt
index 52b7c4d..1fe1a05 100644
--- a/graphics/graphics-core/api/current.txt
+++ b/graphics/graphics-core/api/current.txt
@@ -229,7 +229,6 @@
     method public void commit();
     method @RequiresApi(android.os.Build.VERSION_CODES.TIRAMISU) public void commitTransactionOnDraw(android.view.AttachedSurfaceControl attachedSurfaceControl);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, androidx.graphics.surface.SurfaceControlCompat? newParent);
-    method public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.view.SurfaceView surfaceView);
     method @RequiresApi(android.os.Build.VERSION_CODES.TIRAMISU) public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.view.AttachedSurfaceControl attachedSurfaceControl);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction setAlpha(androidx.graphics.surface.SurfaceControlCompat surfaceControl, float alpha);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction setBuffer(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.hardware.HardwareBuffer buffer, optional androidx.graphics.lowlatency.SyncFenceCompat? fence, optional kotlin.jvm.functions.Function0<kotlin.Unit>? releaseCallback);
diff --git a/graphics/graphics-core/api/public_plus_experimental_current.txt b/graphics/graphics-core/api/public_plus_experimental_current.txt
index 52b7c4d..1fe1a05 100644
--- a/graphics/graphics-core/api/public_plus_experimental_current.txt
+++ b/graphics/graphics-core/api/public_plus_experimental_current.txt
@@ -229,7 +229,6 @@
     method public void commit();
     method @RequiresApi(android.os.Build.VERSION_CODES.TIRAMISU) public void commitTransactionOnDraw(android.view.AttachedSurfaceControl attachedSurfaceControl);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, androidx.graphics.surface.SurfaceControlCompat? newParent);
-    method public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.view.SurfaceView surfaceView);
     method @RequiresApi(android.os.Build.VERSION_CODES.TIRAMISU) public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.view.AttachedSurfaceControl attachedSurfaceControl);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction setAlpha(androidx.graphics.surface.SurfaceControlCompat surfaceControl, float alpha);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction setBuffer(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.hardware.HardwareBuffer buffer, optional androidx.graphics.lowlatency.SyncFenceCompat? fence, optional kotlin.jvm.functions.Function0<kotlin.Unit>? releaseCallback);
diff --git a/graphics/graphics-core/api/restricted_current.txt b/graphics/graphics-core/api/restricted_current.txt
index 2643905..d2ba87f 100644
--- a/graphics/graphics-core/api/restricted_current.txt
+++ b/graphics/graphics-core/api/restricted_current.txt
@@ -230,7 +230,6 @@
     method public void commit();
     method @RequiresApi(android.os.Build.VERSION_CODES.TIRAMISU) public void commitTransactionOnDraw(android.view.AttachedSurfaceControl attachedSurfaceControl);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, androidx.graphics.surface.SurfaceControlCompat? newParent);
-    method public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.view.SurfaceView surfaceView);
     method @RequiresApi(android.os.Build.VERSION_CODES.TIRAMISU) public androidx.graphics.surface.SurfaceControlCompat.Transaction reparent(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.view.AttachedSurfaceControl attachedSurfaceControl);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction setAlpha(androidx.graphics.surface.SurfaceControlCompat surfaceControl, float alpha);
     method public androidx.graphics.surface.SurfaceControlCompat.Transaction setBuffer(androidx.graphics.surface.SurfaceControlCompat surfaceControl, android.hardware.HardwareBuffer buffer, optional androidx.graphics.lowlatency.SyncFenceCompat? fence, optional kotlin.jvm.functions.Function0<kotlin.Unit>? releaseCallback);
diff --git a/graphics/graphics-core/src/androidTest/java/androidx/graphics/lowlatency/GLFrontBufferedRendererTest.kt b/graphics/graphics-core/src/androidTest/java/androidx/graphics/lowlatency/GLFrontBufferedRendererTest.kt
index ff42184d..246dced 100644
--- a/graphics/graphics-core/src/androidTest/java/androidx/graphics/lowlatency/GLFrontBufferedRendererTest.kt
+++ b/graphics/graphics-core/src/androidTest/java/androidx/graphics/lowlatency/GLFrontBufferedRendererTest.kt
@@ -277,14 +277,76 @@
         )
     }
 
+    @Test
+    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.Q)
+    fun testRenderFrontBufferSeveralTimes() {
+        if (!deviceSupportsNativeAndroidFence()) {
+            // If the Android device does not support the corresponding extensions to create
+            // a file descriptor from an EGLSync object then skip the test
+            Log.w(TAG, "Skipping testDoubleBufferedLayerRender, no native android fence support")
+            return
+        }
+
+        val callbacks = object : GLFrontBufferedRenderer.Callback<Any> {
+
+            var red = 1f
+            var blue = 0f
+
+            override fun onDrawFrontBufferedLayer(eglManager: EGLManager, param: Any) {
+                GLES20.glViewport(
+                    0,
+                    0,
+                    FrontBufferedRendererTestActivity.WIDTH,
+                    FrontBufferedRendererTestActivity.HEIGHT
+                )
+                GLES20.glClearColor(red, 0.0f, blue, 1.0f)
+                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
+                val tmp = red
+                red = blue
+                blue = tmp
+            }
+
+            override fun onDrawDoubleBufferedLayer(
+                eglManager: EGLManager,
+                params: Collection<Any>
+            ) {
+                GLES20.glViewport(
+                    0,
+                    0,
+                    FrontBufferedRendererTestActivity.WIDTH,
+                    FrontBufferedRendererTestActivity.HEIGHT
+                )
+                GLES20.glClearColor(0.0f, 0.0f, 1.0f, 1.0f)
+                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
+            }
+        }
+        var renderer: GLFrontBufferedRenderer<Any>? = null
+        try {
+            val scenario = ActivityScenario.launch(FrontBufferedRendererTestActivity::class.java)
+                .moveToState(Lifecycle.State.CREATED)
+                .onActivity {
+                    renderer = GLFrontBufferedRenderer(it.getSurfaceView(), callbacks)
+                }
+
+            scenario.moveToState(Lifecycle.State.RESUMED).onActivity {
+                val param = Any()
+                repeat(4000) {
+                    renderer?.renderFrontBufferedLayer(param)
+                }
+            }
+        } finally {
+            renderer.blockingRelease(10000)
+        }
+    }
+
     @RequiresApi(Build.VERSION_CODES.Q)
-    private fun GLFrontBufferedRenderer<*>?.blockingRelease() {
+    private fun GLFrontBufferedRenderer<*>?.blockingRelease(timeoutMillis: Long = 3000) {
         if (this != null) {
             val destroyLatch = CountDownLatch(1)
             release(false) {
                 destroyLatch.countDown()
             }
-            assertTrue(destroyLatch.await(3000, TimeUnit.MILLISECONDS))
+            assertTrue(destroyLatch.await(timeoutMillis, TimeUnit.MILLISECONDS))
         } else {
             fail("GLFrontBufferedRenderer is not initialized")
         }
diff --git a/graphics/graphics-core/src/main/java/androidx/graphics/lowlatency/SurfaceViewRenderLayer.kt b/graphics/graphics-core/src/main/java/androidx/graphics/lowlatency/SurfaceViewRenderLayer.kt
index 3d71b07..460209b 100644
--- a/graphics/graphics-core/src/main/java/androidx/graphics/lowlatency/SurfaceViewRenderLayer.kt
+++ b/graphics/graphics-core/src/main/java/androidx/graphics/lowlatency/SurfaceViewRenderLayer.kt
@@ -46,7 +46,7 @@
         child: SurfaceControlCompat,
         transaction: SurfaceControlCompat.Transaction
     ) {
-        transaction.reparent(child, surfaceView)
+        transaction.reparent(child, mParentSurfaceControl)
     }
 
     override fun setParent(builder: SurfaceControlCompat.Builder) {
diff --git a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlCompat.kt b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlCompat.kt
index dbcb0ac..21da3e2 100644
--- a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlCompat.kt
+++ b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlCompat.kt
@@ -200,20 +200,6 @@
         }
 
         /**
-         * Re-parents a given [SurfaceControlCompat] to be a child of the [Surface] associated with
-         * the provided [SurfaceView]. Children inherit transform
-         * (position, scaling) crop, visibility, and Z-ordering from their parents, as if the
-         * children were pixels within the parent [Surface].
-         * @param surfaceControl Target [SurfaceControlCompat] instance to reparent
-         * @param surfaceView [SurfaceView] instance that acts as the new parent of the provided
-         * [SurfaceControlCompat] instance.
-         */
-        fun reparent(surfaceControl: SurfaceControlCompat, surfaceView: SurfaceView): Transaction {
-            mImpl.reparent(surfaceControl.scImpl, surfaceView)
-            return this
-        }
-
-        /**
          * Re-parents a given [SurfaceControlCompat] to be a child of the [AttachedSurfaceControl].
          * Children inherit transform (position, scaling) crop, visibility, and Z-ordering from
          * their parents, as if the children were pixels within the parent [Surface].
diff --git a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlImpl.kt b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlImpl.kt
index e8258b6..b3f2bde 100644
--- a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlImpl.kt
+++ b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlImpl.kt
@@ -126,17 +126,6 @@
         ): Transaction
 
         /**
-         * Re-parents a given [SurfaceControlImpl] to be a child of the [Surface] associated with
-         * the provided [SurfaceView]. Children inherit transform
-         * (position, scaling) crop, visibility, and Z-ordering from their parents, as if the
-         * children were pixels within the parent [Surface].
-         * @param surfaceControl Target [SurfaceControlImpl] instance to reparent
-         * @param surfaceView [SurfaceView] instance that acts as the new parent of the provided
-         * [SurfaceControlImpl] instance.
-         */
-        fun reparent(surfaceControl: SurfaceControlImpl, surfaceView: SurfaceView): Transaction
-
-        /**
          * Re-parents a given [SurfaceControlImpl] to be a child of the [AttachedSurfaceControl].
          * Children inherit transform (position, scaling) crop, visibility, and Z-ordering from
          * their parents, as if the children were pixels within the parent [Surface].
diff --git a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV29.kt b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV29.kt
index 1257c221..8787759 100644
--- a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV29.kt
+++ b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV29.kt
@@ -128,23 +128,6 @@
         }
 
         /**
-         * See [SurfaceControlWrapper.Transaction.reparent]
-         */
-        override fun reparent(
-            surfaceControl: SurfaceControlImpl,
-            surfaceView: SurfaceView
-        ): SurfaceControlImpl.Transaction {
-            transaction.reparent(
-                surfaceControl.asWrapperSurfaceControl(),
-                SurfaceControlWrapper.Builder()
-                    .setParent(surfaceView.holder.surface)
-                    .setDebugName(surfaceView.toString())
-                    .build()
-            )
-            return this
-        }
-
-        /**
          * See [SurfaceControlWrapper.Transaction.setBuffer]
          */
         override fun setBuffer(
diff --git a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV33.kt b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV33.kt
index 34ed9c7..e52a2e0 100644
--- a/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV33.kt
+++ b/graphics/graphics-core/src/main/java/androidx/graphics/surface/SurfaceControlV33.kt
@@ -155,20 +155,6 @@
          */
         override fun reparent(
             surfaceControl: SurfaceControlImpl,
-            surfaceView: SurfaceView
-        ): SurfaceControlImpl.Transaction {
-            mTransaction.reparent(
-                surfaceControl.asFrameworkSurfaceControl(),
-                surfaceView.surfaceControl
-            )
-            return this
-        }
-
-        /**
-         * See [SurfaceControlImpl.Transaction.reparent]
-         */
-        override fun reparent(
-            surfaceControl: SurfaceControlImpl,
             attachedSurfaceControl: AttachedSurfaceControl
         ): SurfaceControlImpl.Transaction {
             val reparentTransaction = attachedSurfaceControl