sdm: Update BufferSyncHandler

* Add SyncWait() member function with timeout.
* Enhance logging on libsync sync_wait() failure.
* Add sync_wait() timeouts specific to each power mode transition.

CRs-Fixed: 2508524
Change-Id: I0f652543261b2fdbb68065219c7aaa19bfaaa735
diff --git a/composer/hwc_buffer_sync_handler.cpp b/composer/hwc_buffer_sync_handler.cpp
index f2dacb2..7cab4c0 100644
--- a/composer/hwc_buffer_sync_handler.cpp
+++ b/composer/hwc_buffer_sync_handler.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -40,12 +40,17 @@
 namespace sdm {
 
 DisplayError HWCBufferSyncHandler::SyncWait(int fd) {
+  return SyncWait(fd, 1000);
+}
+
+DisplayError HWCBufferSyncHandler::SyncWait(int fd, int timeout) {
   int error = 0;
 
   if (fd >= 0) {
-    error = sync_wait(fd, 1000);
+    error = sync_wait(fd, timeout);
     if (error < 0) {
-      DLOGE("sync_wait error errno = %d, desc = %s", errno,  strerror(errno));
+      DLOGE("sync_wait() error on fd = %d, timeout = %dms. (errno = %d \"%s\")", fd, timeout, errno,
+            strerror(errno));
       return kErrorTimeOut;
     }
   }
diff --git a/composer/hwc_buffer_sync_handler.h b/composer/hwc_buffer_sync_handler.h
index 81479d8..7f076b6 100644
--- a/composer/hwc_buffer_sync_handler.h
+++ b/composer/hwc_buffer_sync_handler.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -43,6 +43,7 @@
   HWCBufferSyncHandler() { }
 
   virtual DisplayError SyncWait(int fd);
+  virtual DisplayError SyncWait(int fd, int timeout);
   virtual DisplayError SyncMerge(int fd1, int fd2, int *merged_fd);
   virtual bool IsSyncSignaled(int fd);
 };
diff --git a/sdm/include/core/buffer_sync_handler.h b/sdm/include/core/buffer_sync_handler.h
index 1dc5413..2aa0da7 100644
--- a/sdm/include/core/buffer_sync_handler.h
+++ b/sdm/include/core/buffer_sync_handler.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+* Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -53,8 +53,8 @@
  public:
   /*! @brief Method to wait for ouput buffer to be released.
 
-    @details This method waits for fd to be signaled by the producer/consumer.
-    It is responsibility of the caller to close file descriptor.
+    @details This method waits for fd to be signaled by the producer/consumer with a default
+    timeout. It is the responsibility of the caller to close the file descriptor.
 
     @param[in] fd
 
@@ -63,6 +63,18 @@
 
   virtual DisplayError SyncWait(int fd) = 0;
 
+  /*! @brief Method to wait specified time for ouput buffer to be released.
+
+    @details This method waits for fd to be signaled by the producer/consumer with a specified
+    timeout in milliseconds. It is the responsibility of the caller to close the file descriptor.
+
+    @param[in] fd
+
+    @return \link DisplayError \endlink
+  */
+
+  virtual DisplayError SyncWait(int fd, int timeout) = 0;
+
   /*! @brief Method to merge two sync fds into one sync fd
 
     @details This method merges two buffer sync fds into one sync fd, if a producer/consumer
diff --git a/sdm/libs/core/drm/hw_device_drm.cpp b/sdm/libs/core/drm/hw_device_drm.cpp
index c4b3403..9119f0e 100644
--- a/sdm/libs/core/drm/hw_device_drm.cpp
+++ b/sdm/libs/core/drm/hw_device_drm.cpp
@@ -907,7 +907,7 @@
   }
   pending_doze_ = false;
 
-  buffer_sync_handler_->SyncWait(INT(retire_fence_t));
+  buffer_sync_handler_->SyncWait(INT(retire_fence_t), kTimeoutMsPowerOn);
   Sys::close_(INT(retire_fence_t));
 
   return kErrorNone;
@@ -940,7 +940,7 @@
   pending_doze_ = false;
 
   int release_fence = static_cast<int>(release_fence_t);
-  buffer_sync_handler_->SyncWait(release_fence);
+  buffer_sync_handler_->SyncWait(release_fence, kTimeoutMsPowerOff);
   Sys::close_(release_fence);
 
   return kErrorNone;
@@ -982,7 +982,7 @@
   }
 
   int retire_fence = static_cast<int>(retire_fence_t);
-  buffer_sync_handler_->SyncWait(retire_fence);
+  buffer_sync_handler_->SyncWait(retire_fence, kTimeoutMsDoze);
   Sys::close_(retire_fence);
 
   return kErrorNone;
@@ -1022,7 +1022,7 @@
   pending_doze_ = false;
 
   int retire_fence = static_cast<int>(retire_fence_t);
-  buffer_sync_handler_->SyncWait(retire_fence);
+  buffer_sync_handler_->SyncWait(retire_fence, kTimeoutMsDozeSuspend);
   Sys::close_(retire_fence);
 
   return kErrorNone;
diff --git a/sdm/libs/core/drm/hw_device_drm.h b/sdm/libs/core/drm/hw_device_drm.h
index f2c8def..e5bf849 100644
--- a/sdm/libs/core/drm/hw_device_drm.h
+++ b/sdm/libs/core/drm/hw_device_drm.h
@@ -137,6 +137,12 @@
   static const int kNumPhysicalDisplays = 2;
   static const int kMaxSysfsCommandLength = 12;
 
+  // Max tolerable power-state-change wait-times in milliseconds.
+  static const int kTimeoutMsPowerOn = 5000;
+  static const int kTimeoutMsPowerOff = 3000;
+  static const int kTimeoutMsDoze = kTimeoutMsPowerOff;
+  static const int kTimeoutMsDozeSuspend = kTimeoutMsPowerOff;
+
   DisplayError SetFormat(const LayerBufferFormat &source, uint32_t *target);
   DisplayError SetStride(HWDeviceType device_type, LayerBufferFormat format, uint32_t width,
                          uint32_t *target);