Add API to WebView functor to support overlays

This CL adds api that exposes SurfaceControl to allow webview to
submit ASurfaceTransaction synchronously with hwui draw.

Test: just new API stubs, nothing to test.
Change-Id: I3a70ad12d04e5a1655887faf3782426a9131a999
diff --git a/libs/hwui/WebViewFunctorManager.cpp b/libs/hwui/WebViewFunctorManager.cpp
index 68541b4..671c66f 100644
--- a/libs/hwui/WebViewFunctorManager.cpp
+++ b/libs/hwui/WebViewFunctorManager.cpp
@@ -26,6 +26,35 @@
 
 namespace android::uirenderer {
 
+namespace {
+class ScopedCurrentFunctor {
+public:
+    ScopedCurrentFunctor(WebViewFunctor* functor) {
+        ALOG_ASSERT(!sCurrentFunctor);
+        ALOG_ASSERT(functor);
+        sCurrentFunctor = functor;
+    }
+    ~ScopedCurrentFunctor() {
+        ALOG_ASSERT(sCurrentFunctor);
+        sCurrentFunctor = nullptr;
+    }
+
+    static ASurfaceControl* getSurfaceControl() {
+        ALOG_ASSERT(sCurrentFunctor);
+        return sCurrentFunctor->getSurfaceControl();
+    }
+    static void mergeTransaction(ASurfaceTransaction* transaction) {
+        ALOG_ASSERT(sCurrentFunctor);
+        sCurrentFunctor->mergeTransaction(transaction);
+    }
+
+private:
+    static WebViewFunctor* sCurrentFunctor;
+};
+
+WebViewFunctor* ScopedCurrentFunctor::sCurrentFunctor = nullptr;
+}  // namespace
+
 RenderMode WebViewFunctor_queryPlatformRenderMode() {
     auto pipelineType = Properties::getRenderPipelineType();
     switch (pipelineType) {
@@ -83,7 +112,15 @@
     if (!mHasContext) {
         mHasContext = true;
     }
-    mCallbacks.gles.draw(mFunctor, mData, drawInfo);
+    ScopedCurrentFunctor currentFunctor(this);
+
+    WebViewOverlayData overlayParams = {
+            // TODO:
+            .overlaysMode = OverlaysMode::Disabled,
+            .getSurfaceControl = currentFunctor.getSurfaceControl,
+            .mergeTransaction = currentFunctor.mergeTransaction,
+    };
+    mCallbacks.gles.draw(mFunctor, mData, drawInfo, overlayParams);
 }
 
 void WebViewFunctor::initVk(const VkFunctorInitParams& params) {
@@ -98,7 +135,15 @@
 
 void WebViewFunctor::drawVk(const VkFunctorDrawParams& params) {
     ATRACE_NAME("WebViewFunctor::drawVk");
-    mCallbacks.vk.draw(mFunctor, mData, params);
+    ScopedCurrentFunctor currentFunctor(this);
+
+    WebViewOverlayData overlayParams = {
+            // TODO
+            .overlaysMode = OverlaysMode::Disabled,
+            .getSurfaceControl = currentFunctor.getSurfaceControl,
+            .mergeTransaction = currentFunctor.mergeTransaction,
+    };
+    mCallbacks.vk.draw(mFunctor, mData, params, overlayParams);
 }
 
 void WebViewFunctor::postDrawVk() {
@@ -118,6 +163,20 @@
     }
 }
 
+void WebViewFunctor::removeOverlays() {
+    ScopedCurrentFunctor currentFunctor(this);
+    mCallbacks.removeOverlays(mFunctor, mData, currentFunctor.mergeTransaction);
+}
+
+ASurfaceControl* WebViewFunctor::getSurfaceControl() {
+    // TODO
+    return nullptr;
+}
+
+void WebViewFunctor::mergeTransaction(ASurfaceTransaction* transaction) {
+    // TODO
+}
+
 WebViewFunctorManager& WebViewFunctorManager::instance() {
     static WebViewFunctorManager sInstance;
     return sInstance;
diff --git a/libs/hwui/WebViewFunctorManager.h b/libs/hwui/WebViewFunctorManager.h
index 675b738..737d605 100644
--- a/libs/hwui/WebViewFunctorManager.h
+++ b/libs/hwui/WebViewFunctorManager.h
@@ -56,6 +56,8 @@
 
         void postDrawVk() { mReference.postDrawVk(); }
 
+        void removeOverlays() { mReference.removeOverlays(); }
+
     private:
         friend class WebViewFunctor;
 
@@ -71,6 +73,10 @@
     void drawVk(const VkFunctorDrawParams& params);
     void postDrawVk();
     void destroyContext();
+    void removeOverlays();
+
+    ASurfaceControl* getSurfaceControl();
+    void mergeTransaction(ASurfaceTransaction* transaction);
 
     sp<Handle> createHandle() {
         LOG_ALWAYS_FATAL_IF(mCreatedHandle);
diff --git a/libs/hwui/private/hwui/WebViewFunctor.h b/libs/hwui/private/hwui/WebViewFunctor.h
index 96da947..22ae59e 100644
--- a/libs/hwui/private/hwui/WebViewFunctor.h
+++ b/libs/hwui/private/hwui/WebViewFunctor.h
@@ -17,6 +17,15 @@
 #ifndef FRAMEWORKS_BASE_WEBVIEWFUNCTOR_H
 #define FRAMEWORKS_BASE_WEBVIEWFUNCTOR_H
 
+#ifdef __ANDROID__  // Layoutlib does not support surface control
+#include <android/surface_control.h>
+#else
+// To avoid ifdefs around overlay implementation all over the place we typedef these to void *. They
+// won't be used.
+typedef void* ASurfaceControl;
+typedef void* ASurfaceTransaction;
+#endif
+
 #include <cutils/compiler.h>
 #include <private/hwui/DrawGlInfo.h>
 #include <private/hwui/DrawVkInfo.h>
@@ -28,6 +37,14 @@
     Vulkan,
 };
 
+enum class OverlaysMode {
+    // Indicated that webview should not promote anything to overlays this draw
+    // and remove all visible overlays.
+    Disabled,
+    // Indicates that webview can use overlays.
+    Enabled
+};
+
 // Static for the lifetime of the process
 ANDROID_API RenderMode WebViewFunctor_queryPlatformRenderMode();
 
@@ -35,6 +52,23 @@
     bool applyForceDark;
 };
 
+struct WebViewOverlayData {
+    // Desired overlay mode for this draw.
+    OverlaysMode overlaysMode;
+
+    // Returns parent ASurfaceControl for WebView overlays. It will be have same
+    // geometry as the surface we draw into and positioned below it (underlay).
+    // This does not pass ownership to webview, but guaranteed to be alive until
+    // transaction from next removeOverlays call or functor destruction will be
+    // finished.
+    ASurfaceControl* (*getSurfaceControl)();
+
+    // Merges WebView transaction to be applied synchronously with current draw.
+    // This doesn't pass ownership of the transaction, changes will be copied and
+    // webview can free transaction right after the call.
+    void (*mergeTransaction)(ASurfaceTransaction*);
+};
+
 struct WebViewFunctorCallbacks {
     // kModeSync, called on RenderThread
     void (*onSync)(int functor, void* data, const WebViewSyncData& syncData);
@@ -48,16 +82,23 @@
     // this functor had ever been drawn.
     void (*onDestroyed)(int functor, void* data);
 
+    // Called on render thread to force webview hide all overlays and stop updating them.
+    // Should happen during hwui draw (e.g can be called instead of draw if webview
+    // isn't visible and won't receive draw) and support MergeTransaction call.
+    void (*removeOverlays)(int functor, void* data, void (*mergeTransaction)(ASurfaceTransaction*));
+
     union {
         struct {
             // Called on RenderThread. initialize is guaranteed to happen before this call
-            void (*draw)(int functor, void* data, const DrawGlInfo& params);
+            void (*draw)(int functor, void* data, const DrawGlInfo& params,
+                         const WebViewOverlayData& overlayParams);
         } gles;
         struct {
             // Called either the first time the functor is used or the first time it's used after
             // a call to onContextDestroyed.
             void (*initialize)(int functor, void* data, const VkFunctorInitParams& params);
-            void (*draw)(int functor, void* data, const VkFunctorDrawParams& params);
+            void (*draw)(int functor, void* data, const VkFunctorDrawParams& params,
+                         const WebViewOverlayData& overlayParams);
             void (*postDraw)(int functor, void*);
         } vk;
     };
diff --git a/libs/hwui/tests/common/TestUtils.h b/libs/hwui/tests/common/TestUtils.h
index 36c5a8c1..c1d8b76 100644
--- a/libs/hwui/tests/common/TestUtils.h
+++ b/libs/hwui/tests/common/TestUtils.h
@@ -323,7 +323,8 @@
         };
         switch (mode) {
             case RenderMode::OpenGL_ES:
-                callbacks.gles.draw = [](int functor, void* client_data, const DrawGlInfo& params) {
+                callbacks.gles.draw = [](int functor, void* client_data, const DrawGlInfo& params,
+                                         const WebViewOverlayData& overlay_params) {
                     expectOnRenderThread("draw");
                     sMockFunctorCounts[functor].glesDraw++;
                 };