Merge "Rebind context more frequently when posting on main thread" am: 194f04a5c1 am: b6af849725 am: 0463ff8ee7 am: d561cee818 am: 0c4f961f65 am: 9ab8a6796f

Original change: https://android-review.googlesource.com/c/device/generic/vulkan-cereal/+/2601605

Change-Id: I20c4b16551ac0dff96558f9d53000f79278b19f8
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
GitOrigin-RevId: ebd82eb358ed4f258578eaae62706b4fe6339138
diff --git a/host/PostWorker.h b/host/PostWorker.h
index 13c2ec5..1cfe105 100644
--- a/host/PostWorker.h
+++ b/host/PostWorker.h
@@ -83,14 +83,14 @@
     FrameBuffer* mFb;
     Compositor* m_compositor = nullptr;
 
-   private:
+   protected:
     // If m_mainThreadPostingOnly is true, schedule the task to UI thread by
     // using m_runOnUiThread. Otherwise, execute the task on the current thread.
+    bool m_mainThreadPostingOnly = false;
 
    private:
     using UiThreadRunner = std::function<void(UiUpdateFunc, void*, bool)>;
 
-    bool m_mainThreadPostingOnly = false;
     UiThreadRunner m_runOnUiThread = 0;
 
     std::unordered_map<uint32_t, std::shared_future<void>> m_composeTargetToComposeFuture;
diff --git a/host/PostWorkerGl.cpp b/host/PostWorkerGl.cpp
index 815fbee..8eb27da 100644
--- a/host/PostWorkerGl.cpp
+++ b/host/PostWorkerGl.cpp
@@ -64,8 +64,10 @@
 }
 
 std::shared_future<void> PostWorkerGl::postImpl(ColorBuffer* cb) {
-    if (!mContextBound) {
+    if (!mContextBound || m_mainThreadPostingOnly) {
         // This might happen on headless mode
+        // Also if posting on main thread, the context binding can get polluted easily, which
+        // requires frequent rebinds.
         setupContext();
     }
     std::shared_future<void> completedFuture = std::async(std::launch::deferred, [] {}).share();
@@ -246,7 +248,7 @@
 // displaying whatever happens to be in the back buffer,
 // clear() is useful for outputting consistent colors.
 void PostWorkerGl::clearImpl() {
-    if (!mContextBound) {
+    if (!mContextBound || m_mainThreadPostingOnly) {
         // This might happen on headless mode
         setupContext();
     }
@@ -254,7 +256,7 @@
 }
 
 std::shared_future<void> PostWorkerGl::composeImpl(const FlatComposeRequest& composeRequest) {
-    if (!mContextBound) {
+    if (!mContextBound || m_mainThreadPostingOnly) {
         // This might happen on headless mode
         setupContext();
     }