Merge c017f0ed6019257c611216ece298394110029f3c on remote branch

Change-Id: I8867dedaa92ac01e34b6141ec9aaac21e4f7ad1c
diff --git a/composer/hwc_session.cpp b/composer/hwc_session.cpp
index 4d3908e..0edb1d6 100644
--- a/composer/hwc_session.cpp
+++ b/composer/hwc_session.cpp
@@ -4253,7 +4253,7 @@
 }
 
 int HWCSession::WaitForCommitDoneAsync(hwc2_display_t display, int client_id) {
-  std::chrono::milliseconds span(5000);
+  std::chrono::milliseconds span(2000);
   if (commit_done_future_.valid()) {
     std::future_status status = commit_done_future_.wait_for(std::chrono::milliseconds(0));
     if (status != std::future_status::ready) {
@@ -4491,7 +4491,7 @@
 
   if (needs_refresh) {
     DLOGI("Waiting for device unassign");
-    int ret = WaitForCommitDone(target_display, kClientTrustedUI);
+    int ret = WaitForCommitDoneAsync(target_display, kClientTrustedUI);
     if (ret != 0) {
       DLOGE("Device unassign failed with error %d", ret);
       return -EINVAL;
diff --git a/composer/service.cpp b/composer/service.cpp
index de8c0f5..9adede5 100644
--- a/composer/service.cpp
+++ b/composer/service.cpp
@@ -26,6 +26,39 @@
  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+
+/*
+Changes from Qualcomm Innovation Center are provided under the following license:
+Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted (subject to the limitations in the
+disclaimer below) provided that the following conditions are met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of Qualcomm Innovation Center, Inc. nor the
+      names of its contributors may be used to endorse or promote
+      products derived from this software without specific prior
+      written permission.
+
+NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
+GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
+HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
 #include <android-base/logging.h>
 #include <android/binder_manager.h>
 #include <android/binder_process.h>
@@ -33,6 +66,7 @@
 
 #include "DisplayConfigAIDL.h"
 #include "QtiComposer.h"
+#include "hwc_debugger.h"
 
 using aidl::vendor::qti::hardware::display::config::DisplayConfigAIDL;
 using android::ProcessState;
@@ -46,11 +80,18 @@
 int main(int, char **) {
   ALOGI("Creating Display HW Composer HAL");
 
+  int composer_thread_count = 8;
+  sdm::HWCDebugHandler::Get()->GetProperty(COMPOSER_THREAD_COUNT, &composer_thread_count);
+  if (composer_thread_count < 4 || composer_thread_count > 15) {
+    composer_thread_count = 4;
+  }
+  ALOGI("composer_thread_count: %d", composer_thread_count);
+
   // TODO(user): double-check for SCHED_FIFO logic
   // the conventional HAL might start binder services
   ProcessState::initWithDriver("/dev/vndbinder");
   sp<ProcessState> ps(ProcessState::self());
-  ps->setThreadPoolMaxThreadCount(4);
+  ps->setThreadPoolMaxThreadCount(composer_thread_count);
   ps->startThreadPool();
   ALOGI("ProcessState initialization completed");
 
@@ -73,7 +114,7 @@
   }
 
   ALOGI("Configuring RPC threadpool");
-  configureRpcThreadpool(4, true /*callerWillJoin*/);
+  configureRpcThreadpool(composer_thread_count, true /*callerWillJoin*/);
   ALOGI("Configuring RPC threadpool...done!");
 
   ALOGI("Registering Display HW Composer HAL as a service");
diff --git a/include/display_properties.h b/include/display_properties.h
index 6694a2b..8d33e5a 100644
--- a/include/display_properties.h
+++ b/include/display_properties.h
@@ -74,6 +74,7 @@
 #define EXTERNAL_ACTION_SAFE_HEIGHT_PROP     DISPLAY_PROP("external_action_safe_height")
 #define FB_WIDTH_PROP                        DISPLAY_PROP("fb_width")
 #define FB_HEIGHT_PROP                       DISPLAY_PROP("fb_height")
+#define COMPOSER_THREAD_COUNT                DISPLAY_PROP("composer_thread_count")
 #define DISABLE_METADATA_DYNAMIC_FPS_PROP    DISPLAY_PROP("disable_metadata_dynamic_fps")
 #define DISABLE_SKIP_VALIDATE_PROP           DISPLAY_PROP("disable_skip_validate")
 #define DISABLE_DESTINATION_SCALER_PROP      DISPLAY_PROP("disable_dest_scaler")
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index edcc64a..6654ba0 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -1838,7 +1838,11 @@
 
   case kStateOn:
     if (display_type_ == kHDMI && first_cycle_) {
-      hw_events_intf_->SetEventState(HWEvent::POWER_EVENT, true);
+      error = hw_events_intf_->SetEventState(HWEvent::POWER_EVENT, true);
+      if (error != kErrorNone) {
+        Abort();
+        return error;
+      }
     }
 
     cached_qos_data_.clock_hz =
diff --git a/sdm/libs/dal/hw_events_drm.cpp b/sdm/libs/dal/hw_events_drm.cpp
index 765ff13..8813134 100644
--- a/sdm/libs/dal/hw_events_drm.cpp
+++ b/sdm/libs/dal/hw_events_drm.cpp
@@ -403,7 +403,9 @@
       }
     } break;
     case HWEvent::POWER_EVENT: {
-      RegisterPowerEvents(enable);
+      if (RegisterPowerEvents(enable) != kErrorNone) {
+        return kErrorResources;
+      }
     } break;
     case HWEvent::PANEL_DEAD: {
       RegisterPanelDead(enable);
@@ -655,8 +657,8 @@
     } else {
       DLOGE("Failed to %s event. Event_thread_name : %s, Ret=%d", (enable) ? "Register" :
             "DeRegister", event_thread_name_.c_str(), ret);
-      return kErrorResources;
     }
+      return kErrorResources;
   }
   return kErrorNone;
 }
diff --git a/sdm/libs/dal/hw_peripheral_drm.cpp b/sdm/libs/dal/hw_peripheral_drm.cpp
index c534f3b..711c302 100644
--- a/sdm/libs/dal/hw_peripheral_drm.cpp
+++ b/sdm/libs/dal/hw_peripheral_drm.cpp
@@ -541,6 +541,7 @@
     pending_power_state_ = kPowerStateOn;
     return kErrorDeferred;
   }
+  SetVMReqState();
 
   if (switch_mode_valid_ && doze_poms_switch_done_ && (current_mode_index_ == cmd_mode_index_)) {
     HWDeviceDRM::SetDisplayMode(kModeVideo);
@@ -567,6 +568,7 @@
   idle_pc_enabled_ = true;
   pending_poms_switch_ = false;
   active_ = true;
+  SetTUIState();
 
   CacheDestScalarData();