Merge 5107d2b2b82489fdf44b2511815de211cea5dd55 on remote branch

Change-Id: Ifa8336792c32bc67f4f7131caa55fe5c88add859
diff --git a/config/display-product.mk b/config/display-product.mk
index 9acab69..4eb47e9 100644
--- a/config/display-product.mk
+++ b/config/display-product.mk
@@ -84,7 +84,8 @@
     vendor.display.enable_rc_support=1 \
     vendor.display.disable_sdr_histogram=1 \
     vendor.display.enable_hdr10_gpu_target=1 \
-    debug.sf.predict_hwc_composition_strategy=0
+    debug.sf.predict_hwc_composition_strategy=0 \
+    debug.sf.treat_170m_as_sRGB=1
 
 # Enable offline rotator for Bengal.
 ifneq ($(TARGET_BOARD_PLATFORM),bengal)
diff --git a/sdm/include/private/hw_events_interface.h b/sdm/include/private/hw_events_interface.h
index 6901938..98cc145 100644
--- a/sdm/include/private/hw_events_interface.h
+++ b/sdm/include/private/hw_events_interface.h
@@ -22,6 +22,42 @@
 * 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.
+*/
+
 #ifndef __HW_EVENTS_INTERFACE_H__
 #define __HW_EVENTS_INTERFACE_H__
 
@@ -50,6 +86,7 @@
   MMRM,
   POWER_EVENT,
   VM_RELEASE_EVENT,
+  HW_EVENT_MAX,
 };
 
 class HWEventsInterface {
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index 0d182fe..6cc9c55 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -1796,6 +1796,7 @@
   if (state == state_) {
     if (pending_power_state_ != kPowerStateNone) {
       hw_intf_->CancelDeferredPowerMode();
+      pending_power_state_ = kPowerStateNone;
     }
     DLOGI("Same state transition is requested.");
     return kErrorNone;
@@ -1835,6 +1836,8 @@
       hw_events_intf_->SetEventState(HWEvent::POWER_EVENT, true);
     }
 
+    cached_qos_data_.clock_hz =
+      std::max(cached_qos_data_.clock_hz, disp_layer_stack_.info.qos_data.clock_hz);
     error = hw_intf_->PowerOn(cached_qos_data_, &sync_points);
     if (error != kErrorNone) {
       if (error == kErrorDeferred) {
diff --git a/sdm/libs/dal/hw_events_drm.cpp b/sdm/libs/dal/hw_events_drm.cpp
index 1cb091d..765ff13 100644
--- a/sdm/libs/dal/hw_events_drm.cpp
+++ b/sdm/libs/dal/hw_events_drm.cpp
@@ -27,6 +27,42 @@
 * 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 <drm_master.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -184,10 +220,7 @@
         poll_fds_[i].events = POLLIN | POLLPRI | POLLERR;
         vm_release_event_index_ = i;
       } break;
-      case HWEvent::CEC_READ_MESSAGE:
-      case HWEvent::SHOW_BLANK_EVENT:
-      case HWEvent::THERMAL_LEVEL:
-      case HWEvent::PINGPONG_TIMEOUT:
+      default:
         break;
     }
   }
@@ -326,21 +359,30 @@
 }
 
 DisplayError HWEventsDRM::SetEventState(HWEvent event, bool enable, void *arg) {
+  if (event != HWEvent::VSYNC) {
+    if (enable == registered_hw_events_.test(event)) {
+      DLOGW("%s of %sregistered hw event %d occurred!!",
+            enable ? "Registration" : "Deregistration",
+            enable ? "already " : "un-", event);
+      return kErrorNone;
+    }
+  }
   DisplayError error = kErrorNone;
   switch (event) {
     case HWEvent::VSYNC: {
       std::lock_guard<std::mutex> lock(vsync_mutex_);
       vsync_enabled_ = enable;
-      if (vsync_enabled_ && !vsync_registered_) {
+      if (vsync_enabled_ && !registered_hw_events_.test(HWEvent::VSYNC)) {
         error = RegisterVSync();
         if (error != kErrorNone) {
           return error;
         }
-        vsync_registered_ = true;
+        registered_hw_events_.set(event);
       } else if (!vsync_enabled_) {
-        vsync_registered_ = false;
+        registered_hw_events_.reset(event);
       }
-    } break;
+      return kErrorNone;
+    }
     case HWEvent::BACKLIGHT_EVENT: {
       std::lock_guard<std::mutex> lock(backlight_mutex_);
       if (backlight_event_index_ == UINT32_MAX) {
@@ -391,6 +433,7 @@
       DLOGE("Event not supported");
       return kErrorNotSupported;
   }
+  registered_hw_events_.set(event, enable);
 
   return kErrorNone;
 }
@@ -529,6 +572,8 @@
             (this->*(event_data_list_[i]).event_parser)(data);
           }
           break;
+        default:
+          break;
       }
     }
   }
@@ -749,10 +794,11 @@
   vsync_handler_count_ = 0;  //  reset vsync handler count. lock not needed
   {
     std::lock_guard<std::mutex> lock(vsync_mutex_);
-    vsync_registered_ = false;
+    registered_hw_events_.reset(HWEvent::VSYNC);
     if (vsync_enabled_) {
       ret = RegisterVSync();
-      vsync_registered_ = (ret == kErrorNone);
+      if (ret == kErrorNone)
+        registered_hw_events_.set(HWEvent::VSYNC);
     }
   }
 
@@ -767,10 +813,11 @@
   if (vsync_handler_count_ > 1) {
     //  probable thread preemption caused > 1 vsync handling. Re-enable vsync before polling
     std::lock_guard<std::mutex> lock(vsync_mutex_);
-    vsync_registered_ = false;
+    registered_hw_events_.reset(HWEvent::VSYNC);
     if (vsync_enabled_) {
       ret = RegisterVSync();
-      vsync_registered_ = (ret == kErrorNone);
+      if (ret == kErrorNone)
+        registered_hw_events_.set(HWEvent::VSYNC);
     }
   }
 }
diff --git a/sdm/libs/dal/hw_events_drm.h b/sdm/libs/dal/hw_events_drm.h
index 8daf6eb..bf370ac 100644
--- a/sdm/libs/dal/hw_events_drm.h
+++ b/sdm/libs/dal/hw_events_drm.h
@@ -27,6 +27,42 @@
 * 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.
+*/
+
 #ifndef __HW_EVENTS_DRM_H__
 #define __HW_EVENTS_DRM_H__
 
@@ -41,6 +77,7 @@
 #include <utility>
 #include <vector>
 #include <climits>
+#include <bitset>
 
 #include "hw_device_drm.h"
 
@@ -108,9 +145,8 @@
   uint32_t vsync_index_ = UINT32_MAX;
   uint32_t histogram_index_ = UINT32_MAX;
   bool vsync_enabled_ = false;
-  bool vsync_registered_ = false;
   uint32_t vsync_handler_count_ = 0;
-  std::mutex vsync_mutex_;  // To protect vsync_enabled_ and vsync_registered_
+  std::mutex vsync_mutex_;  // To protect vsync_enabled_
   sde_drm::DRMDisplayToken token_ = {};
   bool is_primary_ = false;
   uint32_t panel_dead_index_ = UINT32_MAX;
@@ -126,6 +162,7 @@
   uint32_t mmrm_index_ = UINT32_MAX;
   uint32_t power_event_index_ = UINT32_MAX;
   uint32_t vm_release_event_index_ = UINT32_MAX;
+  std::bitset<HW_EVENT_MAX> registered_hw_events_ = {};
 };
 
 }  // namespace sdm