Reparent surface control for Webview overlay

When the root surface control changes, the parent of the Webview surface control must be changed.

Bug: 191900007
Test: switch app, or turn on/off screen
Change-Id: Ib7b34133776fd5944afc638bfee471afe6a17349
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index 0c9711b..81cee61 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -201,6 +201,7 @@
         funcs.releaseFunc(mSurfaceControl);
     }
     mSurfaceControl = surfaceControl;
+    mSurfaceControlGenerationId++;
     mExpectSurfaceStats = surfaceControl != nullptr;
     if (mSurfaceControl != nullptr) {
         funcs.acquireFunc(mSurfaceControl);
diff --git a/libs/hwui/renderthread/CanvasContext.h b/libs/hwui/renderthread/CanvasContext.h
index 3279ccb..85af3e4 100644
--- a/libs/hwui/renderthread/CanvasContext.h
+++ b/libs/hwui/renderthread/CanvasContext.h
@@ -110,6 +110,7 @@
     GrDirectContext* getGrContext() const { return mRenderThread.getGrContext(); }
 
     ASurfaceControl* getSurfaceControl() const { return mSurfaceControl; }
+    int32_t getSurfaceControlGenerationId() const { return mSurfaceControlGenerationId; }
 
     // Won't take effect until next EGLSurface creation
     void setSwapBehavior(SwapBehavior swapBehavior);
@@ -253,6 +254,9 @@
     // The SurfaceControl reference is passed from ViewRootImpl, can be set to
     // NULL to remove the reference
     ASurfaceControl* mSurfaceControl = nullptr;
+    // id to track surface control changes and WebViewFunctor uses it to determine
+    // whether reparenting is needed
+    int32_t mSurfaceControlGenerationId = 0;
     // stopped indicates the CanvasContext will reject actual redraw operations,
     // and defer repaint until it is un-stopped
     bool mStopped = false;
diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp
index 524407d..f83c0a4 100644
--- a/libs/hwui/renderthread/RenderThread.cpp
+++ b/libs/hwui/renderthread/RenderThread.cpp
@@ -98,6 +98,10 @@
     LOG_ALWAYS_FATAL_IF(transactionApplyFunc == nullptr,
                         "Failed to find required symbol ASurfaceTransaction_apply!");
 
+    transactionReparentFunc = (AST_reparent)dlsym(handle_, "ASurfaceTransaction_reparent");
+    LOG_ALWAYS_FATAL_IF(transactionReparentFunc == nullptr,
+                        "Failed to find required symbol transactionReparentFunc!");
+
     transactionSetVisibilityFunc =
             (AST_setVisibility)dlsym(handle_, "ASurfaceTransaction_setVisibility");
     LOG_ALWAYS_FATAL_IF(transactionSetVisibilityFunc == nullptr,
diff --git a/libs/hwui/renderthread/RenderThread.h b/libs/hwui/renderthread/RenderThread.h
index c5e3746..05d225b 100644
--- a/libs/hwui/renderthread/RenderThread.h
+++ b/libs/hwui/renderthread/RenderThread.h
@@ -94,6 +94,9 @@
 typedef ASurfaceTransaction* (*AST_create)();
 typedef void (*AST_delete)(ASurfaceTransaction* transaction);
 typedef void (*AST_apply)(ASurfaceTransaction* transaction);
+typedef void (*AST_reparent)(ASurfaceTransaction* aSurfaceTransaction,
+                             ASurfaceControl* aSurfaceControl,
+                             ASurfaceControl* newParentASurfaceControl);
 typedef void (*AST_setVisibility)(ASurfaceTransaction* transaction,
                                   ASurfaceControl* surface_control, int8_t visibility);
 typedef void (*AST_setZOrder)(ASurfaceTransaction* transaction, ASurfaceControl* surface_control,
@@ -113,6 +116,7 @@
     AST_create transactionCreateFunc;
     AST_delete transactionDeleteFunc;
     AST_apply transactionApplyFunc;
+    AST_reparent transactionReparentFunc;
     AST_setVisibility transactionSetVisibilityFunc;
     AST_setZOrder transactionSetZOrderFunc;
 };