sdm: Add support for CPU hint

- Set the hint when there is only one updating layer, which helps
  in reducing CPU clock and thus power.
- This feature is enabled with the property sdm.perf_hint_window,
  which is set to a positive value, which will be the window of frames
  to skip before the hint is set.

CRs-fixed: 853602
Change-Id: I1af21551510f99752c0b6bbfec3e5c75880c3ab2
diff --git a/sdm/libs/hwc/Android.mk b/sdm/libs/hwc/Android.mk
index 13d832b..cb762e9 100644
--- a/sdm/libs/hwc/Android.mk
+++ b/sdm/libs/hwc/Android.mk
@@ -34,6 +34,7 @@
                                  hwc_buffer_allocator.cpp \
                                  hwc_buffer_sync_handler.cpp \
                                  hwc_color_manager.cpp \
-                                 blit_engine_c2d.cpp
+                                 blit_engine_c2d.cpp \
+                                 cpuhint.cpp
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/sdm/libs/hwc/cpuhint.cpp b/sdm/libs/hwc/cpuhint.cpp
new file mode 100644
index 0000000..dff25ac
--- /dev/null
+++ b/sdm/libs/hwc/cpuhint.cpp
@@ -0,0 +1,120 @@
+/* Copyright (c) 2015, The Linux Foundataion. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted 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 The Linux Foundation nor the names of its
+*       contributors may be used to endorse or promote products derived
+*       from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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 <cutils/properties.h>
+#include <dlfcn.h>
+
+#include "cpuhint.h"
+#include "hwc_debugger.h"
+
+#define __CLASS__ "CPUHint"
+
+namespace sdm {
+
+CPUHint::CPUHint() : enabled_(false), pre_enable_window_(0), frame_countdown_(0), lock_handle_(0),
+  lock_acquired_(false), lib_handle_(NULL), fn_lock_acquire_(NULL), fn_lock_release_(NULL) {
+}
+
+CPUHint::~CPUHint() {
+  if (lib_handle_) {
+    dlclose(lib_handle_);
+    lib_handle_ = NULL;
+  }
+}
+
+DisplayError CPUHint::Init(HWCDebugHandler *debug_handler) {
+  char path[PROPERTY_VALUE_MAX];
+  if (debug_handler->GetProperty("ro.vendor.extension_library", path) != kErrorNone) {
+    DLOGI("Vendor Extension Library not enabled");
+    return kErrorNotSupported;
+  }
+
+  int pre_enable_window = -1;
+  debug_handler->GetProperty("sdm.perf_hint_window", &pre_enable_window);
+  if (pre_enable_window <= 0) {
+    DLOGI("Invalid CPU Hint Pre-enable Window %d", pre_enable_window);
+    return kErrorNotSupported;
+  }
+
+  DLOGI("CPU Hint Pre-enable Window %d", pre_enable_window);
+  pre_enable_window_ = pre_enable_window;
+  lib_handle_ = dlopen(path, RTLD_NOW);
+
+  if (lib_handle_) {
+    *(reinterpret_cast<void **>(&fn_lock_acquire_)) = dlsym(lib_handle_, "perf_lock_acq");
+    *(reinterpret_cast<void **>(&fn_lock_release_)) = dlsym(lib_handle_, "perf_lock_rel");
+    if (!fn_lock_acquire_ || !fn_lock_release_) {
+      DLOGW("Failed to load symbols for Vendor Extension Library");
+      return kErrorNotSupported;
+    }
+    DLOGI("Successfully Loaded Vendor Extension Library symbols");
+    enabled_ = true;
+  } else {
+    DLOGW("Failed to open %s : %s", path, dlerror());
+  }
+
+  return kErrorNone;
+}
+
+void CPUHint::Set() {
+  if (!enabled_) {
+    return;
+  }
+  if (lock_acquired_) {
+    return;
+  }
+  if (frame_countdown_) {
+    --frame_countdown_;
+    return;
+  }
+
+  int hint = HINT;
+  lock_handle_ = fn_lock_acquire_(0 /*handle*/, 0/*duration*/,
+                                  &hint, sizeof(hint) / sizeof(int));
+  if (lock_handle_ >= 0) {
+    lock_acquired_ = true;
+  }
+}
+
+void CPUHint::Reset() {
+  if (!enabled_) {
+    return;
+  }
+
+  frame_countdown_ = pre_enable_window_;
+
+  if (!lock_acquired_) {
+    return;
+  }
+
+  fn_lock_release_(lock_handle_);
+  lock_acquired_ = false;
+}
+
+}  // namespace sdm
diff --git a/sdm/libs/hwc/cpuhint.h b/sdm/libs/hwc/cpuhint.h
new file mode 100644
index 0000000..2165c99
--- /dev/null
+++ b/sdm/libs/hwc/cpuhint.h
@@ -0,0 +1,62 @@
+/* Copyright (c) 2015, The Linux Foundataion. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted 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 The Linux Foundation nor the names of its
+*       contributors may be used to endorse or promote products derived
+*       from this software without specific prior written permission.
+*
+* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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 __CPUHINT_H__
+#define __CPUHINT_H__
+
+#include <core/sdm_types.h>
+
+namespace sdm {
+
+class HWCDebugHandler;
+
+class CPUHint {
+ public:
+  CPUHint();
+  ~CPUHint();
+  DisplayError Init(HWCDebugHandler *debug_handler);
+  void Set();
+  void Reset();
+
+ private:
+  enum { HINT =  0x4701 /* 47-display layer hint, 01-Enable */ };
+  bool enabled_;
+  // frames to wait before setting this hint
+  int pre_enable_window_;
+  int frame_countdown_;
+  int lock_handle_;
+  bool lock_acquired_;
+  void *lib_handle_;
+  int (*fn_lock_acquire_)(int handle, int duration, int *hints, int num_args);
+  int (*fn_lock_release_)(int value);
+};
+
+}  // namespace sdm
+
+#endif  // __CPUHINT_H__
diff --git a/sdm/libs/hwc/hwc_debugger.cpp b/sdm/libs/hwc/hwc_debugger.cpp
index f12eb10..6c58ba5 100644
--- a/sdm/libs/hwc/hwc_debugger.cpp
+++ b/sdm/libs/hwc/hwc_debugger.cpp
@@ -173,5 +173,13 @@
   return kErrorNotSupported;
 }
 
+DisplayError HWCDebugHandler::GetProperty(const char *property_name, char *value) {
+  if (property_get(property_name, value, NULL) > 0) {
+    return kErrorNone;
+  }
+
+  return kErrorNotSupported;
+}
+
 }  // namespace sdm
 
diff --git a/sdm/libs/hwc/hwc_debugger.h b/sdm/libs/hwc/hwc_debugger.h
index 46f6a25..474e1c5 100644
--- a/sdm/libs/hwc/hwc_debugger.h
+++ b/sdm/libs/hwc/hwc_debugger.h
@@ -74,6 +74,7 @@
                           const char *custom_string);
   virtual void EndTrace();
   virtual DisplayError GetProperty(const char *property_name, int *value);
+  virtual DisplayError GetProperty(const char *property_name, char *value);
 
  private:
   static HWCDebugHandler debug_handler_;
diff --git a/sdm/libs/hwc/hwc_display_primary.cpp b/sdm/libs/hwc/hwc_display_primary.cpp
index c53f4eb..10cdde7 100644
--- a/sdm/libs/hwc/hwc_display_primary.cpp
+++ b/sdm/libs/hwc/hwc_display_primary.cpp
@@ -82,7 +82,17 @@
 }
 
 HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, hwc_procs_t const **hwc_procs)
-  : HWCDisplay(core_intf, hwc_procs, kPrimary, HWC_DISPLAY_PRIMARY, true) {
+  : HWCDisplay(core_intf, hwc_procs, kPrimary, HWC_DISPLAY_PRIMARY, true), cpu_hint_(NULL) {
+}
+
+int HWCDisplayPrimary::Init() {
+  cpu_hint_ = new CPUHint();
+  if (cpu_hint_->Init(static_cast<HWCDebugHandler*>(HWCDebugHandler::Get())) != kErrorNone) {
+    delete cpu_hint_;
+    cpu_hint_ = NULL;
+  }
+
+  return HWCDisplay::Init();
 }
 
 void HWCDisplayPrimary::ProcessBootAnimCompleted() {
@@ -129,6 +139,8 @@
     SetRefreshRate(metadata_refresh_rate_);
   }
 
+  ToggleCPUHint(app_layer_count);
+
   return 0;
 }
 
@@ -216,5 +228,26 @@
   solid_fill_color_  = color;
 }
 
+void HWCDisplayPrimary::ToggleCPUHint(int app_layer_count) {
+  int updating_count = 0;
+
+  if (!cpu_hint_) {
+    return;
+  }
+
+  for (int i = 0; i < app_layer_count; i++) {
+    Layer &layer = layer_stack_.layers[i];
+    if (layer.flags.updating) {
+      updating_count++;
+    }
+  }
+
+  if (updating_count == 1) {
+    cpu_hint_->Set();
+  } else {
+    cpu_hint_->Reset();
+  }
+}
+
 }  // namespace sdm
 
diff --git a/sdm/libs/hwc/hwc_display_primary.h b/sdm/libs/hwc/hwc_display_primary.h
index 161dab7..c92a35c 100644
--- a/sdm/libs/hwc/hwc_display_primary.h
+++ b/sdm/libs/hwc/hwc_display_primary.h
@@ -25,6 +25,7 @@
 #ifndef __HWC_DISPLAY_PRIMARY_H__
 #define __HWC_DISPLAY_PRIMARY_H__
 
+#include "cpuhint.h"
 #include "hwc_display.h"
 
 namespace sdm {
@@ -42,6 +43,7 @@
   static int Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
                     HWCDisplay **hwc_display);
   static void Destroy(HWCDisplay *hwc_display);
+  virtual int Init();
   virtual int Prepare(hwc_display_contents_1_t *content_list);
   virtual int Commit(hwc_display_contents_1_t *content_list);
   virtual int SetActiveConfig(int index);
@@ -54,6 +56,9 @@
   virtual DisplayError SetDisplayMode(uint32_t mode);
   void ProcessBootAnimCompleted();
   void SetQDCMSolidFillInfo(bool enable, uint32_t color);
+  void ToggleCPUHint(int app_layer_count);
+
+  CPUHint *cpu_hint_;
 };
 
 }  // namespace sdm