Snap for 6647388 from 6bd8a847b8e59aafdf09feda6f52afe55ee8dde2 to rvc-release

Change-Id: I7eeefe60f0a660c539eeeb25f76d83889a2c3f0d
diff --git a/power-libperfmgr/Android.bp b/power-libperfmgr/Android.bp
index f829a72..c4d0f87 100644
--- a/power-libperfmgr/Android.bp
+++ b/power-libperfmgr/Android.bp
@@ -17,7 +17,7 @@
     name: "libdisppower-pixel",
     proprietary: true,
     srcs: [
-        "disp-power/display-helper.cpp",
+        "disp-power/DisplayLowPower.cpp",
         "disp-power/InteractionHandler.cpp",
     ],
      shared_libs: [
diff --git a/power-libperfmgr/aidl/Power.cpp b/power-libperfmgr/aidl/Power.cpp
index eba695d..d944cf4 100644
--- a/power-libperfmgr/aidl/Power.cpp
+++ b/power-libperfmgr/aidl/Power.cpp
@@ -30,7 +30,7 @@
 #include <utils/Log.h>
 #include <utils/Trace.h>
 
-#include "disp-power/display-helper.h"
+#include "disp-power/DisplayLowPower.h"
 
 namespace aidl {
 namespace google {
@@ -43,8 +43,9 @@
 constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio";
 constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering";
 
-Power::Power(std::shared_ptr<HintManager> hm)
+Power::Power(std::shared_ptr<HintManager> hm, std::shared_ptr<DisplayLowPower> dlpw)
     : mHintManager(hm),
+      mDisplayLowPower(dlpw),
       mInteractionHandler(nullptr),
       mVRModeOn(false),
       mSustainedPerfModeOn(false) {
@@ -90,13 +91,10 @@
     ATRACE_INT(toString(type).c_str(), enabled);
     switch (type) {
         case Mode::LOW_POWER:
+            mDisplayLowPower->SetDisplayLowPower(enabled);
             if (enabled) {
-                // Device in battery saver mode, enable display low power mode
-                set_display_lpm(true);
                 mHintManager->DoHint(toString(type));
             } else {
-                // Device exiting battery saver mode, disable display low power mode
-                set_display_lpm(false);
                 mHintManager->EndHint(toString(type));
             }
             break;
diff --git a/power-libperfmgr/aidl/Power.h b/power-libperfmgr/aidl/Power.h
index 05b8780..8b90cb4 100644
--- a/power-libperfmgr/aidl/Power.h
+++ b/power-libperfmgr/aidl/Power.h
@@ -23,6 +23,7 @@
 #include <aidl/android/hardware/power/BnPower.h>
 #include <perfmgr/HintManager.h>
 
+#include "disp-power/DisplayLowPower.h"
 #include "disp-power/InteractionHandler.h"
 
 namespace aidl {
@@ -39,7 +40,7 @@
 
 class Power : public ::aidl::android::hardware::power::BnPower {
   public:
-    Power(std::shared_ptr<HintManager> hm);
+    Power(std::shared_ptr<HintManager> hm, std::shared_ptr<DisplayLowPower> dlpw);
     ndk::ScopedAStatus setMode(Mode type, bool enabled) override;
     ndk::ScopedAStatus isModeSupported(Mode type, bool *_aidl_return) override;
     ndk::ScopedAStatus setBoost(Boost type, int32_t durationMs) override;
@@ -48,6 +49,7 @@
 
   private:
     std::shared_ptr<HintManager> mHintManager;
+    std::shared_ptr<DisplayLowPower> mDisplayLowPower;
     std::unique_ptr<InteractionHandler> mInteractionHandler;
     std::atomic<bool> mVRModeOn;
     std::atomic<bool> mSustainedPerfModeOn;
diff --git a/power-libperfmgr/aidl/PowerExt.h b/power-libperfmgr/aidl/PowerExt.h
index 885c6a0..65cec2c 100644
--- a/power-libperfmgr/aidl/PowerExt.h
+++ b/power-libperfmgr/aidl/PowerExt.h
@@ -23,6 +23,8 @@
 #include <aidl/google/hardware/power/extension/pixel/BnPowerExt.h>
 #include <perfmgr/HintManager.h>
 
+#include "disp-power/DisplayLowPower.h"
+
 namespace aidl {
 namespace google {
 namespace hardware {
@@ -34,7 +36,8 @@
 
 class PowerExt : public ::aidl::google::hardware::power::extension::pixel::BnPowerExt {
   public:
-    PowerExt(std::shared_ptr<HintManager> hm) : mHintManager(hm) {}
+    PowerExt(std::shared_ptr<HintManager> hm, std::shared_ptr<DisplayLowPower> dlpw)
+        : mHintManager(hm), mDisplayLowPower(dlpw) {}
     ndk::ScopedAStatus setMode(const std::string &mode, bool enabled) override;
     ndk::ScopedAStatus isModeSupported(const std::string &mode, bool *_aidl_return) override;
     ndk::ScopedAStatus setBoost(const std::string &boost, int32_t durationMs) override;
@@ -42,6 +45,7 @@
 
   private:
     std::shared_ptr<HintManager> mHintManager;
+    std::shared_ptr<DisplayLowPower> mDisplayLowPower;
 };
 
 }  // namespace pixel
diff --git a/power-libperfmgr/aidl/service.cpp b/power-libperfmgr/aidl/service.cpp
index 4db1c30..aeb6356 100644
--- a/power-libperfmgr/aidl/service.cpp
+++ b/power-libperfmgr/aidl/service.cpp
@@ -25,6 +25,7 @@
 
 #include "Power.h"
 #include "PowerExt.h"
+#include "disp-power/DisplayLowPower.h"
 
 using aidl::google::hardware::power::impl::pixel::Power;
 using aidl::google::hardware::power::impl::pixel::PowerExt;
@@ -42,15 +43,17 @@
         LOG(FATAL) << "Invalid config: " << kPowerHalConfigPath;
     }
 
+    std::shared_ptr<DisplayLowPower> dlpw = std::make_shared<DisplayLowPower>();
+
     // single thread
     ABinderProcess_setThreadPoolMaxThreadCount(0);
 
     // core service
-    std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>(hm);
+    std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>(hm, dlpw);
     ndk::SpAIBinder pwBinder = pw->asBinder();
 
     // extension service
-    std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>(hm);
+    std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>(hm, dlpw);
 
     // attach the extension to the same binder we will be registering
     CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get()));
@@ -63,6 +66,7 @@
     std::thread initThread([&]() {
         ::android::base::WaitForProperty(kPowerHalInitProp, "1");
         hm->Start();
+        dlpw->Init();
     });
     initThread.detach();
 
diff --git a/power-libperfmgr/disp-power/DisplayLowPower.cpp b/power-libperfmgr/disp-power/DisplayLowPower.cpp
new file mode 100644
index 0000000..d38972a
--- /dev/null
+++ b/power-libperfmgr/disp-power/DisplayLowPower.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "android.hardware.power@-service.pixel-libperfmgr"
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <cutils/sockets.h>
+#include <log/log.h>
+
+#include "DisplayLowPower.h"
+
+DisplayLowPower::DisplayLowPower() : mFossStatus(false) {}
+
+void DisplayLowPower::Init() {
+    ConnectPpsDaemon();
+}
+
+void DisplayLowPower::SetDisplayLowPower(bool enable) {
+    SetFoss(enable);
+}
+
+void DisplayLowPower::ConnectPpsDaemon() {
+    constexpr const char kPpsDaemon[] = "pps";
+
+    mPpsSocket.reset(
+            socket_local_client(kPpsDaemon, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
+    if (mPpsSocket.get() < 0) {
+        ALOGW("Connecting to PPS daemon failed (%s)", strerror(errno));
+    }
+}
+
+int DisplayLowPower::SendPpsCommand(const std::string_view cmd) {
+    if (TEMP_FAILURE_RETRY(write(mPpsSocket.get(), cmd.data(), cmd.size())) < 0) {
+        ALOGE("Failed to send pps command '%s' over socket (%s)", cmd.data(), strerror(errno));
+        return -1;
+    }
+
+    return 0;
+}
+
+void DisplayLowPower::SetFoss(bool enable) {
+    if (mPpsSocket.get() < 0 || mFossStatus == enable) {
+        return;
+    }
+
+    ALOGI("%s foss", (enable) ? "Enable" : "Disable");
+
+    std::string_view foss_cmd;
+    if (enable) {
+        foss_cmd = "foss:on";
+    } else {
+        foss_cmd = "foss:off";
+    }
+
+    if (!SendPpsCommand(foss_cmd)) {
+        mFossStatus = enable;
+    }
+}
diff --git a/power-libperfmgr/disp-power/DisplayLowPower.h b/power-libperfmgr/disp-power/DisplayLowPower.h
new file mode 100644
index 0000000..c0d6c33
--- /dev/null
+++ b/power-libperfmgr/disp-power/DisplayLowPower.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <android-base/unique_fd.h>
+
+#include <string_view>
+
+class DisplayLowPower {
+  public:
+    DisplayLowPower();
+    ~DisplayLowPower() {}
+    void Init();
+    void SetDisplayLowPower(bool enable);
+
+  private:
+    void ConnectPpsDaemon();
+    int SendPpsCommand(const std::string_view cmd);
+    void SetFoss(bool enable);
+
+    android::base::unique_fd mPpsSocket;
+    bool mFossStatus;
+};
diff --git a/power-libperfmgr/disp-power/InteractionHandler.cpp b/power-libperfmgr/disp-power/InteractionHandler.cpp
index da6a917..1826958 100644
--- a/power-libperfmgr/disp-power/InteractionHandler.cpp
+++ b/power-libperfmgr/disp-power/InteractionHandler.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "android.hardware.power@1.3-service.pixel-libperfmgr"
+#define LOG_TAG "android.hardware.power@-service.pixel-libperfmgr"
 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
 
 #include <fcntl.h>
diff --git a/power-libperfmgr/disp-power/display-helper.cpp b/power-libperfmgr/disp-power/display-helper.cpp
deleted file mode 100644
index 2369c63..0000000
--- a/power-libperfmgr/disp-power/display-helper.cpp
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_NIDEBUG 0
-#define LOG_TAG "android.hardware.power@1.3-service.pixel-libperfmgr"
-
-#include <dlfcn.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <cutils/sockets.h>
-#include <log/log.h>
-
-#include "display-helper.h"
-
-#define DAEMON_SOCKET "pps"
-
-static int daemon_socket = -1;
-
-static int connectPPDaemon() {
-    // Setup socket connection, if not already done.
-    if (daemon_socket < 0)
-        daemon_socket =
-                socket_local_client(DAEMON_SOCKET, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
-
-    if (daemon_socket < 0) {
-        ALOGE("Connecting to socket failed: %s", strerror(errno));
-        return -1;
-    }
-    return 0;
-}
-
-static int ppdComm(const char *cmd) {
-    int ret = -1;
-
-    ret = connectPPDaemon();
-    if (ret < 0)
-        return ret;
-
-    ret = write(daemon_socket, cmd, strlen(cmd));
-    if (ret < 0) {
-        ALOGE("Failed to send data over socket, %s", strerror(errno));
-        return ret;
-    }
-    return 0;
-}
-
-void set_display_lpm(int enable) {
-    ALOGI("set_display_lpm state: %d", enable);
-    if (enable) {
-        ppdComm("foss:on");
-    } else {
-        ppdComm("foss:off");
-    }
-}
diff --git a/power-libperfmgr/disp-power/display-helper.h b/power-libperfmgr/disp-power/display-helper.h
deleted file mode 100644
index 70b9697..0000000
--- a/power-libperfmgr/disp-power/display-helper.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef POWER_LIBPERFMGR_DISPLAY_HELPER_H_
-#define POWER_LIBPERFMGR_DISPLAY_HELPER_H_
-
-enum display_lpm_state {
-    DISPLAY_LPM_OFF = 0,
-    DISPLAY_LPM_ON,
-    DISPLAY_LPM_UNKNOWN,
-};
-
-void set_display_lpm(int enable);
-
-#endif  // POWER_LIBPERFMGR_DISPLAY_HELPER_H_
diff --git a/power-libperfmgr/hidl/Power.cpp b/power-libperfmgr/hidl/Power.cpp
index 1092670..2b43b0f 100644
--- a/power-libperfmgr/hidl/Power.cpp
+++ b/power-libperfmgr/hidl/Power.cpp
@@ -31,7 +31,7 @@
 #include <utils/Trace.h>
 
 #include "AudioStreaming.h"
-#include "disp-power/display-helper.h"
+#include "disp-power/DisplayLowPower.h"
 
 namespace android {
 namespace hardware {
@@ -62,6 +62,7 @@
 Power::Power()
     : mHintManager(nullptr),
       mInteractionHandler(nullptr),
+      mDisplayLowPower(nullptr),
       mVRModeOn(false),
       mSustainedPerfModeOn(false),
       mCameraStreamingMode(CAMERA_STREAMING_OFF),
@@ -74,6 +75,8 @@
         }
         mInteractionHandler = std::make_unique<InteractionHandler>(mHintManager);
         mInteractionHandler->Init();
+        mDisplayLowPower = std::make_unique<DisplayLowPower>();
+        mDisplayLowPower->Init();
         std::string state = android::base::GetProperty(kPowerHalStateProp, "");
         if (state == "CAMERA_STREAMING") {
             ALOGI("Initialize with CAMERA_STREAMING on");
@@ -199,13 +202,7 @@
             }
             break;
         case PowerHint_1_0::LOW_POWER:
-            if (data) {
-                // Device in battery saver mode, enable display low power mode
-                set_display_lpm(true);
-            } else {
-                // Device exiting battery saver mode, disable display low power mode
-                set_display_lpm(false);
-            }
+            mDisplayLowPower->SetDisplayLowPower(static_cast<bool>(data));
             break;
         default:
             break;
diff --git a/power-libperfmgr/hidl/Power.h b/power-libperfmgr/hidl/Power.h
index a79f94f..9bac407 100644
--- a/power-libperfmgr/hidl/Power.h
+++ b/power-libperfmgr/hidl/Power.h
@@ -27,6 +27,7 @@
 #include <perfmgr/HintManager.h>
 
 #include "CameraMode.h"
+#include "disp-power/DisplayLowPower.h"
 #include "disp-power/InteractionHandler.h"
 
 namespace android {
@@ -72,6 +73,7 @@
   private:
     std::shared_ptr<HintManager> mHintManager;
     std::unique_ptr<InteractionHandler> mInteractionHandler;
+    std::unique_ptr<DisplayLowPower> mDisplayLowPower;
     std::atomic<bool> mVRModeOn;
     std::atomic<bool> mSustainedPerfModeOn;
     std::atomic<enum CameraStreamingMode> mCameraStreamingMode;