Merge b17e9211394640b53fa820baf31ca79e6ac1b616 on remote branch

Change-Id: Ie8cf1d06ec7b84affca469c0a8690fd7c8e172d0
diff --git a/composer/hwc_layers.cpp b/composer/hwc_layers.cpp
index 842269f..b6bdb21 100644
--- a/composer/hwc_layers.cpp
+++ b/composer/hwc_layers.cpp
@@ -1079,6 +1079,9 @@
   if (gralloc::GetMetaDataValue(handle, qtigralloc::MetadataType_LinearFormat.value,
                                 &linear_format) == gralloc::Error::NONE) {
     layer_buffer->format = GetSDMFormat(INT32(linear_format), 0);
+    if (layer_buffer->format == kFormatInvalid) {
+      return kErrorNotSupported;
+    }
   }
 
   if ((interlace != layer_buffer->flags.interlace) || (frame_rate != layer->frame_rate)) {
@@ -1099,6 +1102,9 @@
       gralloc::Error::NONE) {
     // Only copy top layer for now as only top field for interlaced is used
     GetUBWCStatsFromMetaData(&cr_stats[0], &(layer_buffer->ubwc_crstats[0]));
+    if (cr_stats[0].version < 0 || cr_stats[0].version >= UBWC_MAX_VERSION) {
+      return kErrorNotSupported;
+    }
   }
 
   uint32_t single_buffer = 0;
diff --git a/composer/hwc_session.cpp b/composer/hwc_session.cpp
index d576bb9..f4e8c2b 100644
--- a/composer/hwc_session.cpp
+++ b/composer/hwc_session.cpp
@@ -4116,7 +4116,7 @@
                                       return session->WaitForCommitDone(display, client_id);
                                      }, this, display, client_id);
   auto ret = (commit_done_future_[display].wait_for(span) == std::future_status::timeout) ?
-             -EINVAL : commit_done_future_[display].get();
+             -ETIMEDOUT : commit_done_future_[display].get();
   return ret;
 }
 
@@ -4347,7 +4347,7 @@
         DLOGE("Device unassign failed with error %d", ret);
       }
       TUITransitionUnPrepare(disp_id);
-      return -EINVAL;
+      return 0;
     }
   }
 
@@ -4386,6 +4386,9 @@
   for (auto &info : map_info) {
     bool needs_refresh = false;
     {
+      if (info.client_id == target_display) {
+        continue;
+      }
       SEQUENCE_WAIT_SCOPE_LOCK(locker_[info.client_id]);
       if (hwc_display_[info.client_id]) {
         if (info.disp_type == kPluggable && pending_hotplug_event_ == kHotPlugEvent) {
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index fb843ce..54996f2 100644
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -1327,6 +1327,13 @@
     } else {
       DLOGE("Prepare failed: %d", error);
     }
+    // Clear fences
+    DLOGI("Clearing fences on input layers on display %d-%d", display_id_, display_type_);
+    for (auto &layer : layer_stack->layers) {
+      layer->input_buffer.release_fence = nullptr;
+    }
+    layer_stack->retire_fence = nullptr;
+
     return error;
   }
 
@@ -1415,15 +1422,9 @@
 
   disp_layer_stack_.info.retire_fence_offset = retire_fence_offset_;
   // Regiser for power events on first cycle in unified draw.
-  if (first_cycle_ && display_type_ != kVirtual) {
+  if (first_cycle_ && display_type_ == kBuiltIn) {
     // Register for panel dead since notification is sent at any time
     hw_events_intf_->SetEventState(HWEvent::PANEL_DEAD, true);
-
-    if (draw_method_ != kDrawDefault && !hw_panel_info_.is_primary_panel &&
-        display_type_ != kHDMI) {
-      DLOGI("Registering for power events");
-      hw_events_intf_->SetEventState(HWEvent::POWER_EVENT, true);
-    }
   }
 
   // Allow commit as pending doze/pending_power_on is handled as a part of draw cycle
@@ -1853,10 +1854,6 @@
     break;
 
   case kStateOn:
-    if (display_type_ == kHDMI && first_cycle_) {
-      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);
@@ -1924,7 +1921,7 @@
     return kErrorParameters;
   }
 
-  if ((pending_power_state_ == kPowerStateNone) && (!first_cycle_ || display_type_ == kHDMI)) {
+  if ((pending_power_state_ == kPowerStateNone) && !first_cycle_) {
     CacheRetireFence();
     SyncPoints sync = {};
     sync.retire_fence = retire_fence_;
@@ -3963,32 +3960,6 @@
 
 void DisplayBase::WaitForCompletion(SyncPoints *sync_points) {
   DTRACE_SCOPED();
-  // Wait on current retire fence.
-  if (draw_method_ == kDrawDefault || display_type_ == kVirtual) {
-    DLOGI("Wait for current retire fence");
-    Fence::Wait(sync_points->retire_fence);
-    DLOGI("Received retire fence");
-    return;
-  }
-
-  // Wait for CRTC power event on first cycle.
-  if (first_cycle_) {
-    if (hw_panel_info_.is_primary_panel) {
-      DLOGI("Sync commit on primary");
-      return;
-    }
-    DLOGI("Wait for CRTC power event on first cycle");
-    std::unique_lock<std::mutex> lck(power_mutex_);
-    while (!transition_done_) {
-      cv_.wait(lck);
-    }
-
-    DLOGI("Received CRTC power event on first cycle");
-    // Unregister power events.
-    hw_events_intf_->SetEventState(HWEvent::POWER_EVENT, false);
-    return;
-  }
-
   // For displays in unified draw, wait on cached retire fence in steady state.
   shared_ptr<Fence> retire_fence = sync_points->retire_fence;
   DLOGI("Wait for cached retire fence to be in steady state");
diff --git a/sdm/libs/dal/hw_device_drm.cpp b/sdm/libs/dal/hw_device_drm.cpp
index 5f04967..b5f9987 100644
--- a/sdm/libs/dal/hw_device_drm.cpp
+++ b/sdm/libs/dal/hw_device_drm.cpp
@@ -1158,7 +1158,10 @@
   drm_atomic_intf_->Perform(DRMOps::CRTC_GET_RELEASE_FENCE, token_.crtc_id, &release_fence_fd);
   drm_atomic_intf_->Perform(DRMOps::CONNECTOR_GET_RETIRE_FENCE, token_.conn_id, &retire_fence_fd);
 
-  int ret = NullCommit(false /* synchronous */, true /* retain_planes */);
+  // On the first boot up of the display, make the power call synchronous. This is only applicable
+  // to pluggable displays. Check HWPeripheralDRM::PowerOn. For builtin first power call defered
+  // and handled in commit(synchronous for first cycle).
+  int ret = NullCommit(first_cycle_ /* synchronous */, true /* retain_planes */);
   if (ret) {
     DLOGE("Failed with error: %d", ret);
     return kErrorHardware;