Merge "Rebind context more frequently when posting on main thread"

GitOrigin-RevId: 194f04a5c1131c828442385d39d56460db428b08
Change-Id: I384a4b13aa34cd4cd9f1ec97da2304abb146a036
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();
     }