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.h b/services/surfaceflinger/EventControlThread.h
index 1b1ef75..321fb79 100644
--- a/services/surfaceflinger/EventControlThread.h
+++ b/services/surfaceflinger/EventControlThread.h
@@ -14,35 +14,41 @@
* limitations under the License.
*/
-#ifndef ANDROID_EVENTCONTROLTHREAD_H
-#define ANDROID_EVENTCONTROLTHREAD_H
+#pragma once
-#include <stddef.h>
+#include <cstddef>
+#include <condition_variable>
+#include <functional>
+#include <mutex>
+#include <thread>
-#include <utils/Mutex.h>
-#include <utils/Thread.h>
+#include <android-base/thread_annotations.h>
namespace android {
class SurfaceFlinger;
-class EventControlThread: public Thread {
+class EventControlThread {
public:
+ using SetVSyncEnabledFunction = std::function<void(bool)>;
- explicit EventControlThread(const sp<SurfaceFlinger>& flinger);
- virtual ~EventControlThread() {}
+ explicit EventControlThread(SetVSyncEnabledFunction function);
+ ~EventControlThread();
void setVsyncEnabled(bool enabled);
- virtual bool threadLoop();
private:
- sp<SurfaceFlinger> mFlinger;
- bool mVsyncEnabled;
+ void threadMain();
- Mutex mMutex;
- Condition mCond;
+ std::mutex mMutex;
+ std::condition_variable mCondition;
+
+ const SetVSyncEnabledFunction mSetVSyncEnabled;
+ bool mVsyncEnabled GUARDED_BY(mMutex) = false;
+ bool mKeepRunning GUARDED_BY(mMutex) = true;
+
+ // Must be last so that everything is initialized before the thread starts.
+ std::thread mThread{&EventControlThread::threadMain, this};
};
-}
-
-#endif // ANDROID_EVENTCONTROLTHREAD_H
+} // namespace android