blob: 6fd4cdf28e50a64bf9d8c7689657b53829bf54b2 [file] [log] [blame]
Jamie Gennisd1700752013-10-14 12:22:52 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "EventControlThread.h"
18#include "SurfaceFlinger.h"
19
20namespace android {
21
Lloyd Pique78ce4182018-01-31 16:39:51 -080022EventControlThread::EventControlThread(const sp<SurfaceFlinger>& flinger)
23 : mFlinger(flinger), mVsyncEnabled(false) {}
Jamie Gennisd1700752013-10-14 12:22:52 -070024
25void EventControlThread::setVsyncEnabled(bool enabled) {
26 Mutex::Autolock lock(mMutex);
27 mVsyncEnabled = enabled;
28 mCond.signal();
29}
30
31bool EventControlThread::threadLoop() {
Lloyd Pique78ce4182018-01-31 16:39:51 -080032 enum class VsyncState { Unset, On, Off };
Steven Thomas94e35b92017-07-26 18:48:28 -070033 auto currentVsyncState = VsyncState::Unset;
Jamie Gennisd1700752013-10-14 12:22:52 -070034
35 while (true) {
Steven Thomas94e35b92017-07-26 18:48:28 -070036 auto requestedVsyncState = VsyncState::On;
37 {
38 Mutex::Autolock lock(mMutex);
Lloyd Pique78ce4182018-01-31 16:39:51 -080039 requestedVsyncState = mVsyncEnabled ? VsyncState::On : VsyncState::Off;
Steven Thomas94e35b92017-07-26 18:48:28 -070040 while (currentVsyncState == requestedVsyncState) {
41 status_t err = mCond.wait(mMutex);
42 if (err != NO_ERROR) {
Lloyd Pique78ce4182018-01-31 16:39:51 -080043 ALOGE("error waiting for new events: %s (%d)", strerror(-err), err);
Steven Thomas94e35b92017-07-26 18:48:28 -070044 return false;
45 }
Lloyd Pique78ce4182018-01-31 16:39:51 -080046 requestedVsyncState = mVsyncEnabled ? VsyncState::On : VsyncState::Off;
Steven Thomas94e35b92017-07-26 18:48:28 -070047 }
Jamie Gennisd1700752013-10-14 12:22:52 -070048 }
49
Steven Thomas94e35b92017-07-26 18:48:28 -070050 bool enable = requestedVsyncState == VsyncState::On;
Steven Thomas94e35b92017-07-26 18:48:28 -070051 mFlinger->setVsyncEnabled(HWC_DISPLAY_PRIMARY, enable);
Steven Thomas94e35b92017-07-26 18:48:28 -070052 currentVsyncState = requestedVsyncState;
Jamie Gennisd1700752013-10-14 12:22:52 -070053 }
54
55 return false;
56}
57
58} // namespace android