| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
|  | 20 | namespace android { | 
|  | 21 |  | 
|  | 22 | EventControlThread::EventControlThread(const sp<SurfaceFlinger>& flinger): | 
|  | 23 | mFlinger(flinger), | 
|  | 24 | mVsyncEnabled(false) { | 
|  | 25 | } | 
|  | 26 |  | 
|  | 27 | void EventControlThread::setVsyncEnabled(bool enabled) { | 
|  | 28 | Mutex::Autolock lock(mMutex); | 
|  | 29 | mVsyncEnabled = enabled; | 
|  | 30 | mCond.signal(); | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | bool EventControlThread::threadLoop() { | 
| Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 34 | enum class VsyncState {Unset, On, Off}; | 
|  | 35 | auto currentVsyncState = VsyncState::Unset; | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | while (true) { | 
| Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 38 | auto requestedVsyncState = VsyncState::On; | 
|  | 39 | { | 
|  | 40 | Mutex::Autolock lock(mMutex); | 
|  | 41 | requestedVsyncState = | 
|  | 42 | mVsyncEnabled ? VsyncState::On : VsyncState::Off; | 
|  | 43 | while (currentVsyncState == requestedVsyncState) { | 
|  | 44 | status_t err = mCond.wait(mMutex); | 
|  | 45 | if (err != NO_ERROR) { | 
|  | 46 | ALOGE("error waiting for new events: %s (%d)", | 
|  | 47 | strerror(-err), err); | 
|  | 48 | return false; | 
|  | 49 | } | 
|  | 50 | requestedVsyncState = | 
|  | 51 | mVsyncEnabled ? VsyncState::On : VsyncState::Off; | 
|  | 52 | } | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
| Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 55 | bool enable = requestedVsyncState == VsyncState::On; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 56 | #ifdef USE_HWC2 | 
| Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 57 | mFlinger->setVsyncEnabled(HWC_DISPLAY_PRIMARY, enable); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 58 | #else | 
| Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 59 | mFlinger->eventControl(HWC_DISPLAY_PRIMARY, | 
|  | 60 | SurfaceFlinger::EVENT_VSYNC, enable); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 61 | #endif | 
| Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 62 | currentVsyncState = requestedVsyncState; | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 | return false; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | } // namespace android |