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/DeferredLayerUpdater.h b/libs/hwui/DeferredLayerUpdater.h
index 1491f99..289f65c 100644
--- a/libs/hwui/DeferredLayerUpdater.h
+++ b/libs/hwui/DeferredLayerUpdater.h
@@ -19,21 +19,26 @@
 #include <SkColorFilter.h>
 #include <SkImage.h>
 #include <SkMatrix.h>
+#include <android/hardware_buffer.h>
 #include <cutils/compiler.h>
-#include <map>
-#include <system/graphics.h>
-#include <utils/StrongPointer.h>
+// TODO: Use public SurfaceTexture APIs once available and include public NDK header file instead.
+#include <gui/surfacetexture/surface_texture_platform.h>
 
-#include "renderstate/RenderState.h"
-#include "surfacetexture/SurfaceTexture.h"
+#include <map>
+#include <memory>
+
 #include "Layer.h"
 #include "Rect.h"
+#include "renderstate/RenderState.h"
 
 namespace android {
 namespace uirenderer {
 
+class AutoBackendTextureRelease;
 class RenderState;
 
+typedef std::unique_ptr<ASurfaceTexture> AutoTextureRelease;
+
 // Container to hold the properties a layer should be set to at the start
 // of a render pass
 class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback {
@@ -64,7 +69,7 @@
         return false;
     }
 
-    ANDROID_API void setSurfaceTexture(const sp<SurfaceTexture>& consumer);
+    ANDROID_API void setSurfaceTexture(AutoTextureRelease&& consumer);
 
     ANDROID_API void updateTexImage() { mUpdateTexImage = true; }
 
@@ -92,6 +97,39 @@
     void onContextDestroyed() override;
 
 private:
+    /**
+     * ImageSlot contains the information and object references that
+     * DeferredLayerUpdater maintains about a slot. Slot id comes from
+     * ASurfaceTexture_dequeueBuffer. Usually there are at most 3 slots active at a time.
+     */
+    class ImageSlot {
+    public:
+        ~ImageSlot() { clear(); }
+
+        sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace,
+                                      bool forceCreate, GrContext* context);
+
+    private:
+        void clear();
+
+        // the dataspace associated with the current image
+        android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN;
+
+        AHardwareBuffer* mBuffer = nullptr;
+
+        /**
+         * mTextureRelease may outlive DeferredLayerUpdater, if the last ref is held by an SkImage.
+         * DeferredLayerUpdater holds one ref to mTextureRelease, which is decremented by "clear".
+         */
+        AutoBackendTextureRelease* mTextureRelease = nullptr;
+    };
+
+    /**
+     * DeferredLayerUpdater stores the SkImages that have been allocated by the BufferQueue
+     * for each buffer slot.
+     */
+    std::map<int, ImageSlot> mImageSlots;
+
     RenderState& mRenderState;
 
     // Generic properties
@@ -101,7 +139,7 @@
     sk_sp<SkColorFilter> mColorFilter;
     int mAlpha = 255;
     SkBlendMode mMode = SkBlendMode::kSrcOver;
-    sp<SurfaceTexture> mSurfaceTexture;
+    AutoTextureRelease mSurfaceTexture;
     SkMatrix* mTransform;
     bool mGLContextAttached;
     bool mUpdateTexImage;