summaryrefslogtreecommitdiff
path: root/services/surfaceflinger/EventControlThread.cpp
diff options
context:
space:
mode:
author Xin Li <delphij@google.com> 2018-06-08 15:11:57 -0700
committer Xin Li <delphij@google.com> 2018-06-08 15:11:57 -0700
commitf11e2bd016d886a333345dea853ebda23a408d5c (patch)
tree905bd4d25d6ab2b046620ff459777d8dcfa5d7de /services/surfaceflinger/EventControlThread.cpp
parentaabd6b7fa343654cd85b3b2da392e424d037d15a (diff)
parent5c947cdf72270fd1f766b2248d526ebc8c7227f6 (diff)
Merge pi-dev-plus-aosp-without-vendor into stage-aosp-master
Bug: 79597307 Change-Id: I6d6bee71b9424eb478780bbfc06b830eb8ded342
Diffstat (limited to 'services/surfaceflinger/EventControlThread.cpp')
-rw-r--r--services/surfaceflinger/EventControlThread.cpp79
1 files changed, 43 insertions, 36 deletions
diff --git a/services/surfaceflinger/EventControlThread.cpp b/services/surfaceflinger/EventControlThread.cpp
index 052a959724..fb6cff5705 100644
--- a/services/surfaceflinger/EventControlThread.cpp
+++ b/services/surfaceflinger/EventControlThread.cpp
@@ -14,55 +14,62 @@
* 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() = default;
+
+namespace impl {
+
+EventControlThread::EventControlThread(EventControlThread::SetVSyncEnabledFunction function)
+ : mSetVSyncEnabled(function) {
+ pthread_setname_np(mThread.native_handle(), "EventControlThread");
+
+ pid_t tid = pthread_gettid_np(mThread.native_handle());
+ setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_URGENT_DISPLAY);
+ set_sched_policy(tid, SP_FOREGROUND);
+}
+
+EventControlThread::~EventControlThread() {
+ {
+ std::lock_guard<std::mutex> lock(mMutex);
+ mKeepRunning = false;
+ mCondition.notify_all();
+ }
+ mThread.join();
}
void EventControlThread::setVsyncEnabled(bool enabled) {
- Mutex::Autolock lock(mMutex);
+ std::lock_guard<std::mutex> lock(mMutex);
mVsyncEnabled = enabled;
- mCond.signal();
+ mCondition.notify_all();
}
-bool EventControlThread::threadLoop() {
- enum class VsyncState {Unset, On, Off};
- auto currentVsyncState = VsyncState::Unset;
+// Unfortunately std::unique_lock gives warnings with -Wthread-safety
+void EventControlThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
+ auto keepRunning = true;
+ auto currentVsyncEnabled = false;
- 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;
- }
- }
+ while (keepRunning) {
+ mSetVSyncEnabled(currentVsyncEnabled);
- bool enable = requestedVsyncState == VsyncState::On;
-#ifdef USE_HWC2
- mFlinger->setVsyncEnabled(HWC_DISPLAY_PRIMARY, enable);
-#else
- mFlinger->eventControl(HWC_DISPLAY_PRIMARY,
- SurfaceFlinger::EVENT_VSYNC, enable);
-#endif
- currentVsyncState = requestedVsyncState;
+ 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;
}
-
- return false;
}
+} // namespace impl
} // namespace android