Merge "composer: do not allow TUI if target display is not powered on"
diff --git a/composer/QtiComposerClient.cpp b/composer/QtiComposerClient.cpp
index a8e4a9c..2d37d74 100644
--- a/composer/QtiComposerClient.cpp
+++ b/composer/QtiComposerClient.cpp
@@ -17,6 +17,13 @@
  * limitations under the License.
  */
 
+/*
+ * Changes from Qualcomm Innovation Center are provided under the following license:
+ *
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #include <vector>
 #include <string>
 
@@ -2105,15 +2112,14 @@
 Error QtiComposerClient::CommandReader::lookupBuffer(BufferCache cache, uint32_t slot,
                                                      bool useCache, buffer_handle_t handle,
                                                      buffer_handle_t* outHandle) {
+  std::lock_guard<std::mutex> lock(mClient.mDisplayDataMutex);
+  BufferCacheEntry* entry;
+  Error error = lookupBufferCacheEntryLocked(cache, slot, &entry);
+  if (error != Error::NONE) {
+    return error;
+  }
+
   if (useCache) {
-    std::lock_guard<std::mutex> lock(mClient.mDisplayDataMutex);
-
-    BufferCacheEntry* entry;
-    Error error = lookupBufferCacheEntryLocked(cache, slot, &entry);
-    if (error != Error::NONE) {
-      return error;
-    }
-
     // input handle is ignored
     *outHandle = entry->getHandle();
   } else if (cache == BufferCache::LAYER_SIDEBAND_STREAMS) {
diff --git a/composer/hwc_display_builtin.cpp b/composer/hwc_display_builtin.cpp
index 35051f6..a8e559b 100644
--- a/composer/hwc_display_builtin.cpp
+++ b/composer/hwc_display_builtin.cpp
@@ -1386,10 +1386,6 @@
   HandleFrameOutput();
   PostCommitStitchLayers();
 
-  if (flush_ && layer_stack_.output_buffer == nullptr) {
-    display_intf_->FlushConcurrentWriteback();
-  }
-
   auto status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
 /*  display_intf_->GetConfig(&fixed_info);
   is_cmd_mode_ = fixed_info.is_cmdmode;
diff --git a/composer/hwc_display_pluggable.cpp b/composer/hwc_display_pluggable.cpp
index 53efc3c..5c970c6 100644
--- a/composer/hwc_display_pluggable.cpp
+++ b/composer/hwc_display_pluggable.cpp
@@ -181,10 +181,6 @@
   auto status = HWC2::Error::None;
 
   HandleFrameOutput();
-
-  if (flush_ && layer_stack_.output_buffer == nullptr) {
-    display_intf_->FlushConcurrentWriteback();
-  }
   status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
 
   return status;
diff --git a/sdm/libs/dal/hw_device_drm.cpp b/sdm/libs/dal/hw_device_drm.cpp
index 1662462..d93e006 100644
--- a/sdm/libs/dal/hw_device_drm.cpp
+++ b/sdm/libs/dal/hw_device_drm.cpp
@@ -1949,11 +1949,25 @@
   // dpps commit feature ops doesn't use the obj id, set it as -1
   drm_atomic_intf_->Perform(DRMOps::DPPS_COMMIT_FEATURE, -1);
 
+  if (cwb_config_.cwb_disp_id == display_id_ && cwb_config_.enabled) {
+    drm_atomic_intf_->Perform(DRMOps::CONNECTOR_SET_CRTC, cwb_config_.token.conn_id, 0);
+    DLOGI("Tearing down the CWB topology");
+  }
+
   int ret = NullCommit(sync_commit /* synchronous */, false /* retain_planes*/);
   if (ret) {
     DLOGE("failed with error %d", ret);
     return kErrorHardware;
   }
+
+  if (cwb_config_.cwb_disp_id == display_id_) {
+    if (cwb_config_.enabled) {
+      FlushConcurrentWriteback();
+    } else {
+      cwb_config_.cwb_disp_id = -1;
+    }
+  }
+
   return kErrorNone;
 }