Decouple SurfaceTexture from HWUI

Remove all Skia and HWUI types from SurfaceTexture
implementation.
Move SurfaceTexture to libgui (ag/9578265).
Define private C++ API for SurfaceTexture, which is consumed
by DeferredLayerUpdater.
Move AutoBackendTextureRelease/Skia code from SurfaceTexture
to HWUI.

Test: pass CtsUiRenderingTestCases and CtsViewTestCases
Bug: 136263580
Change-Id: I3f971bb490f64a3ac0b2a66a89ba935bf7f08213
diff --git a/libs/hwui/renderthread/EglManager.cpp b/libs/hwui/renderthread/EglManager.cpp
index 1202164..eb469a8 100644
--- a/libs/hwui/renderthread/EglManager.cpp
+++ b/libs/hwui/renderthread/EglManager.cpp
@@ -16,22 +16,22 @@
 
 #include "EglManager.h"
 
+#include <EGL/eglext.h>
+#include <GLES/gl.h>
 #include <cutils/properties.h>
 #include <log/log.h>
 #include <private/gui/SyncFeatures.h>
+#include <sync/sync.h>
+#include <system/window.h>
 #include <utils/Trace.h>
-#include "utils/Color.h"
-#include "utils/StringUtils.h"
+
+#include <string>
+#include <vector>
 
 #include "Frame.h"
 #include "Properties.h"
-
-#include <EGL/eglext.h>
-#include <GLES/gl.h>
-
-#include <system/window.h>
-#include <string>
-#include <vector>
+#include "utils/Color.h"
+#include "utils/StringUtils.h"
 
 #define GLES_VERSION 2
 
@@ -508,7 +508,21 @@
     return preserved;
 }
 
-status_t EglManager::fenceWait(sp<Fence>& fence) {
+static status_t waitForeverOnFence(int fence, const char* logname) {
+    ATRACE_CALL();
+    if (fence == -1) {
+        return NO_ERROR;
+    }
+    constexpr int warningTimeout = 3000;
+    int err = sync_wait(fence, warningTimeout);
+    if (err < 0 && errno == ETIME) {
+        ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
+        err = sync_wait(fence, -1);
+    }
+    return err < 0 ? -errno : status_t(NO_ERROR);
+}
+
+status_t EglManager::fenceWait(int fence) {
     if (!hasEglContext()) {
         ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
         return INVALID_OPERATION;
@@ -518,7 +532,7 @@
         SyncFeatures::getInstance().useNativeFenceSync()) {
         // Block GPU on the fence.
         // Create an EGLSyncKHR from the current fence.
-        int fenceFd = fence->dup();
+        int fenceFd = ::dup(fence);
         if (fenceFd == -1) {
             ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
             return -errno;
@@ -543,7 +557,7 @@
         }
     } else {
         // Block CPU on the fence.
-        status_t err = fence->waitForever("EglManager::fenceWait");
+        status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
         if (err != NO_ERROR) {
             ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
             return err;
@@ -552,8 +566,8 @@
     return OK;
 }
 
-status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
-                                        sp<Fence>& nativeFence) {
+status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
+    *nativeFence = -1;
     if (!hasEglContext()) {
         ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
         return INVALID_OPERATION;
@@ -574,7 +588,7 @@
                   eglGetError());
             return UNKNOWN_ERROR;
         }
-        nativeFence = new Fence(fenceFd);
+        *nativeFence = fenceFd;
         *eglFence = EGL_NO_SYNC_KHR;
     } else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
         if (*eglFence != EGL_NO_SYNC_KHR) {
diff --git a/libs/hwui/renderthread/EglManager.h b/libs/hwui/renderthread/EglManager.h
index 27d41d2..a893e24 100644
--- a/libs/hwui/renderthread/EglManager.h
+++ b/libs/hwui/renderthread/EglManager.h
@@ -21,9 +21,9 @@
 #include <SkImageInfo.h>
 #include <SkRect.h>
 #include <cutils/compiler.h>
-#include <ui/Fence.h>
 #include <ui/GraphicBuffer.h>
 #include <utils/StrongPointer.h>
+
 #include "IRenderPipeline.h"
 #include "utils/Result.h"
 
@@ -74,11 +74,11 @@
 
     // Inserts a wait on fence command into the OpenGL ES command stream. If EGL extension
     // support is missing, block the CPU on the fence.
-    status_t fenceWait(sp<Fence>& fence);
+    status_t fenceWait(int fence);
 
     // Creates a fence that is signaled, when all the pending GL commands are flushed.
     // Depending on installed extensions, the result is either Android native fence or EGL fence.
-    status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, sp<Fence>& nativeFence);
+    status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence);
 
 private:
     enum class SwapBehavior {
diff --git a/libs/hwui/renderthread/RenderThread.h b/libs/hwui/renderthread/RenderThread.h
index bdd80721..da79e97 100644
--- a/libs/hwui/renderthread/RenderThread.h
+++ b/libs/hwui/renderthread/RenderThread.h
@@ -17,33 +17,32 @@
 #ifndef RENDERTHREAD_H_
 #define RENDERTHREAD_H_
 
-#include "RenderTask.h"
+#include <GrContext.h>
+#include <SkBitmap.h>
+#include <cutils/compiler.h>
+#include <thread/ThreadBase.h>
+#include <utils/Looper.h>
+#include <utils/Thread.h>
+
+#include <memory>
+#include <mutex>
+#include <set>
 
 #include "CacheManager.h"
 #include "ProfileDataContainer.h"
+#include "RenderTask.h"
 #include "TimeLord.h"
 #include "WebViewFunctorManager.h"
 #include "thread/ThreadBase.h"
 #include "utils/TimeUtils.h"
 
-#include <GrContext.h>
-#include <SkBitmap.h>
-#include <cutils/compiler.h>
-#include <utils/Looper.h>
-#include <utils/Thread.h>
-
-#include <thread/ThreadBase.h>
-#include <memory>
-#include <mutex>
-#include <set>
-
 namespace android {
 
 class Bitmap;
-class AutoBackendTextureRelease;
 
 namespace uirenderer {
 
+class AutoBackendTextureRelease;
 class Readback;
 class RenderState;
 class TestUtils;
@@ -137,7 +136,7 @@
     friend class DispatchFrameCallbacks;
     friend class RenderProxy;
     friend class DummyVsyncSource;
-    friend class android::AutoBackendTextureRelease;
+    friend class android::uirenderer::AutoBackendTextureRelease;
     friend class android::uirenderer::TestUtils;
     friend class android::uirenderer::WebViewFunctor;
     friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
diff --git a/libs/hwui/renderthread/VulkanManager.cpp b/libs/hwui/renderthread/VulkanManager.cpp
index 35abc57..a5355fc 100644
--- a/libs/hwui/renderthread/VulkanManager.cpp
+++ b/libs/hwui/renderthread/VulkanManager.cpp
@@ -16,9 +16,15 @@
 
 #include "VulkanManager.h"
 
-#include <android/sync.h>
 #include <EGL/egl.h>
 #include <EGL/eglext.h>
+#include <GrBackendSemaphore.h>
+#include <GrBackendSurface.h>
+#include <GrContext.h>
+#include <GrTypes.h>
+#include <android/sync.h>
+#include <vk/GrVkExtensions.h>
+#include <vk/GrVkTypes.h>
 
 #include "Properties.h"
 #include "RenderThread.h"
@@ -26,13 +32,6 @@
 #include "utils/FatVector.h"
 #include "utils/TraceUtils.h"
 
-#include <GrBackendSemaphore.h>
-#include <GrBackendSurface.h>
-#include <GrContext.h>
-#include <GrTypes.h>
-#include <vk/GrVkExtensions.h>
-#include <vk/GrVkTypes.h>
-
 namespace android {
 namespace uirenderer {
 namespace renderthread {
@@ -482,7 +481,7 @@
     int mRefs = 2;
 
     DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device,
-            VkSemaphore semaphore)
+                         VkSemaphore semaphore)
             : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {}
 };
 
@@ -524,12 +523,11 @@
     backendSemaphore.initVulkan(semaphore);
 
     int fenceFd = -1;
-    DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice,
-                                                                 semaphore);
-    GrSemaphoresSubmitted submitted =
-            bufferInfo->skSurface->flush(SkSurface::BackendSurfaceAccess::kPresent,
-                                         kNone_GrFlushFlags, 1, &backendSemaphore,
-                                         destroy_semaphore, destroyInfo);
+    DestroySemaphoreInfo* destroyInfo =
+            new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
+    GrSemaphoresSubmitted submitted = bufferInfo->skSurface->flush(
+            SkSurface::BackendSurfaceAccess::kPresent, kNone_GrFlushFlags, 1, &backendSemaphore,
+            destroy_semaphore, destroyInfo);
     if (submitted == GrSemaphoresSubmitted::kYes) {
         VkSemaphoreGetFdInfoKHR getFdInfo;
         getFdInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR;
@@ -571,14 +569,14 @@
                                  *this, extraBuffers);
 }
 
-status_t VulkanManager::fenceWait(sp<Fence>& fence, GrContext* grContext) {
+status_t VulkanManager::fenceWait(int fence, GrContext* grContext) {
     if (!hasVkContext()) {
         ALOGE("VulkanManager::fenceWait: VkDevice not initialized");
         return INVALID_OPERATION;
     }
 
     // Block GPU on the fence.
-    int fenceFd = fence->dup();
+    int fenceFd = ::dup(fence);
     if (fenceFd == -1) {
         ALOGE("VulkanManager::fenceWait: error dup'ing fence fd: %d", errno);
         return -errno;
@@ -619,7 +617,8 @@
     return OK;
 }
 
-status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence, GrContext* grContext) {
+status_t VulkanManager::createReleaseFence(int* nativeFence, GrContext* grContext) {
+    *nativeFence = -1;
     if (!hasVkContext()) {
         ALOGE("VulkanManager::createReleaseFence: VkDevice not initialized");
         return INVALID_OPERATION;
@@ -644,14 +643,13 @@
     GrBackendSemaphore backendSemaphore;
     backendSemaphore.initVulkan(semaphore);
 
-    DestroySemaphoreInfo* destroyInfo = new DestroySemaphoreInfo(mDestroySemaphore, mDevice,
-                                                                 semaphore);
+    DestroySemaphoreInfo* destroyInfo =
+            new DestroySemaphoreInfo(mDestroySemaphore, mDevice, semaphore);
     // Even if Skia fails to submit the semaphore, it will still call the destroy_semaphore callback
     // which will remove its ref to the semaphore. The VulkanManager must still release its ref,
     // when it is done with the semaphore.
-    GrSemaphoresSubmitted submitted =
-            grContext->flush(kNone_GrFlushFlags, 1, &backendSemaphore,
-                             destroy_semaphore, destroyInfo);
+    GrSemaphoresSubmitted submitted = grContext->flush(kNone_GrFlushFlags, 1, &backendSemaphore,
+                                                       destroy_semaphore, destroyInfo);
 
     if (submitted == GrSemaphoresSubmitted::kNo) {
         ALOGE("VulkanManager::createReleaseFence: Failed to submit semaphore");
@@ -673,7 +671,7 @@
         ALOGE("VulkanManager::createReleaseFence: Failed to get semaphore Fd");
         return INVALID_OPERATION;
     }
-    nativeFence = new Fence(fenceFd);
+    *nativeFence = fenceFd;
 
     return OK;
 }
diff --git a/libs/hwui/renderthread/VulkanManager.h b/libs/hwui/renderthread/VulkanManager.h
index 4c6a755..8b19f13 100644
--- a/libs/hwui/renderthread/VulkanManager.h
+++ b/libs/hwui/renderthread/VulkanManager.h
@@ -20,14 +20,13 @@
 #if !defined(VK_USE_PLATFORM_ANDROID_KHR)
 #define VK_USE_PLATFORM_ANDROID_KHR
 #endif
-#include <vulkan/vulkan.h>
-
 #include <GrContextOptions.h>
 #include <SkSurface.h>
-#include <ui/Fence.h>
 #include <utils/StrongPointer.h>
 #include <vk/GrVkBackendContext.h>
 #include <vk/GrVkExtensions.h>
+#include <vulkan/vulkan.h>
+
 #include "Frame.h"
 #include "IRenderPipeline.h"
 #include "VulkanSurface.h"
@@ -71,11 +70,11 @@
     void destroy();
 
     // Inserts a wait on fence command into the Vulkan command buffer.
-    status_t fenceWait(sp<Fence>& fence, GrContext* grContext);
+    status_t fenceWait(int fence, GrContext* grContext);
 
     // Creates a fence that is signaled when all the pending Vulkan commands are finished on the
     // GPU.
-    status_t createReleaseFence(sp<Fence>& nativeFence, GrContext* grContext);
+    status_t createReleaseFence(int* nativeFence, GrContext* grContext);
 
     // Returned pointers are owned by VulkanManager.
     // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to