SF: Cleanup EventControlThread

Primarily the goal was to eliminate the use of RefBase in various forms
from EventControlThread.

1) SurfaceFlinger only needs a std::unique_ptr<> and not an
android::sp<> to own the created instance.
2) Convert from android::Thread to std::thread, along with using
std::mutex and std::condition_variable to keep consistency.
3) The code only needs a reference to a function to call, rather than a
reference to all of SurfaceFlinger. This removes an unnecessary full
dependency.
4) Switch the header to #pragma once.
5) Added Clang thread annotations and enabled the corresponding warning.
6) Simplified the thread function to eliminate unnecessary locals and
indentation.
7) Added proper thread shutdown handling (invoked by dtor).

Bug: None
Test: Verified event control thread still works on Pixel XL

Change-Id: I2d5621b0cbbfb9e0f8c5831ccfc94704c95a4a55
diff --git a/services/surfaceflinger/EventControlThread.cpp b/services/surfaceflinger/EventControlThread.cpp
index 6fd4cdf..ac54059 100644
--- a/services/surfaceflinger/EventControlThread.cpp
+++ b/services/surfaceflinger/EventControlThread.cpp
@@ -14,45 +14,57 @@
  * limitations under the License.
  */
 
+#include <pthread.h>
+#include <sched.h>
+#include <sys/resource.h>
+
+#include <cutils/sched_policy.h>
+#include <log/log.h>
+#include <system/thread_defs.h>
+
 #include "EventControlThread.h"
-#include "SurfaceFlinger.h"
 
 namespace android {
 
-EventControlThread::EventControlThread(const sp<SurfaceFlinger>& flinger)
-      : mFlinger(flinger), mVsyncEnabled(false) {}
+EventControlThread::EventControlThread(EventControlThread::SetVSyncEnabledFunction function)
+      : mSetVSyncEnabled(function) {
+    pthread_setname_np(mThread.native_handle(), "EventControlThread");
 
-void EventControlThread::setVsyncEnabled(bool enabled) {
-    Mutex::Autolock lock(mMutex);
-    mVsyncEnabled = enabled;
-    mCond.signal();
+    pid_t tid = pthread_gettid_np(mThread.native_handle());
+    setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_URGENT_DISPLAY);
+    set_sched_policy(tid, SP_FOREGROUND);
 }
 
-bool EventControlThread::threadLoop() {
-    enum class VsyncState { Unset, On, Off };
-    auto currentVsyncState = VsyncState::Unset;
-
-    while (true) {
-        auto requestedVsyncState = VsyncState::On;
-        {
-            Mutex::Autolock lock(mMutex);
-            requestedVsyncState = mVsyncEnabled ? VsyncState::On : VsyncState::Off;
-            while (currentVsyncState == requestedVsyncState) {
-                status_t err = mCond.wait(mMutex);
-                if (err != NO_ERROR) {
-                    ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
-                    return false;
-                }
-                requestedVsyncState = mVsyncEnabled ? VsyncState::On : VsyncState::Off;
-            }
-        }
-
-        bool enable = requestedVsyncState == VsyncState::On;
-        mFlinger->setVsyncEnabled(HWC_DISPLAY_PRIMARY, enable);
-        currentVsyncState = requestedVsyncState;
+EventControlThread::~EventControlThread() {
+    {
+        std::lock_guard<std::mutex> lock(mMutex);
+        mKeepRunning = false;
+        mCondition.notify_all();
     }
+    mThread.join();
+}
 
-    return false;
+void EventControlThread::setVsyncEnabled(bool enabled) {
+    std::lock_guard<std::mutex> lock(mMutex);
+    mVsyncEnabled = enabled;
+    mCondition.notify_all();
+}
+
+// Unfortunately std::unique_lock gives warnings with -Wthread-safety
+void EventControlThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
+    auto keepRunning = true;
+    auto currentVsyncEnabled = false;
+
+    while (keepRunning) {
+        mSetVSyncEnabled(currentVsyncEnabled);
+
+        std::unique_lock<std::mutex> lock(mMutex);
+        mCondition.wait(lock, [this, currentVsyncEnabled, keepRunning]() NO_THREAD_SAFETY_ANALYSIS {
+            return currentVsyncEnabled != mVsyncEnabled || keepRunning != mKeepRunning;
+        });
+        currentVsyncEnabled = mVsyncEnabled;
+        keepRunning = mKeepRunning;
+    }
 }
 
 } // namespace android