| 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 |  | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 18 | #pragma clang diagnostic push | 
|  | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 20 |  | 
| Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 21 | #include <pthread.h> | 
|  | 22 | #include <sched.h> | 
|  | 23 | #include <sys/resource.h> | 
|  | 24 |  | 
|  | 25 | #include <cutils/sched_policy.h> | 
|  | 26 | #include <log/log.h> | 
|  | 27 | #include <system/thread_defs.h> | 
|  | 28 |  | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 29 | #include "EventControlThread.h" | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 |  | 
| Lloyd Pique | 379adc1 | 2018-01-22 17:31:47 -0800 | [diff] [blame] | 33 | EventControlThread::~EventControlThread() = default; | 
|  | 34 |  | 
|  | 35 | namespace impl { | 
|  | 36 |  | 
| Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 37 | EventControlThread::EventControlThread(EventControlThread::SetVSyncEnabledFunction function) | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 38 | : mSetVSyncEnabled(std::move(function)) { | 
| Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 39 | pthread_setname_np(mThread.native_handle(), "EventControlThread"); | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 40 |  | 
| Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 41 | pid_t tid = pthread_gettid_np(mThread.native_handle()); | 
|  | 42 | setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_URGENT_DISPLAY); | 
|  | 43 | set_sched_policy(tid, SP_FOREGROUND); | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
| Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 46 | EventControlThread::~EventControlThread() { | 
|  | 47 | { | 
|  | 48 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 49 | mKeepRunning = false; | 
|  | 50 | mCondition.notify_all(); | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 51 | } | 
| Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 52 | mThread.join(); | 
|  | 53 | } | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 54 |  | 
| Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 55 | void EventControlThread::setVsyncEnabled(bool enabled) { | 
|  | 56 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 57 | mVsyncEnabled = enabled; | 
|  | 58 | mCondition.notify_all(); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | // Unfortunately std::unique_lock gives warnings with -Wthread-safety | 
|  | 62 | void EventControlThread::threadMain() NO_THREAD_SAFETY_ANALYSIS { | 
|  | 63 | auto keepRunning = true; | 
|  | 64 | auto currentVsyncEnabled = false; | 
|  | 65 |  | 
|  | 66 | while (keepRunning) { | 
|  | 67 | mSetVSyncEnabled(currentVsyncEnabled); | 
|  | 68 |  | 
|  | 69 | std::unique_lock<std::mutex> lock(mMutex); | 
|  | 70 | mCondition.wait(lock, [this, currentVsyncEnabled, keepRunning]() NO_THREAD_SAFETY_ANALYSIS { | 
|  | 71 | return currentVsyncEnabled != mVsyncEnabled || keepRunning != mKeepRunning; | 
|  | 72 | }); | 
|  | 73 | currentVsyncEnabled = mVsyncEnabled; | 
|  | 74 | keepRunning = mKeepRunning; | 
|  | 75 | } | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Lloyd Pique | 379adc1 | 2018-01-22 17:31:47 -0800 | [diff] [blame] | 78 | } // namespace impl | 
| Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 79 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 80 |  | 
|  | 81 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 82 | #pragma clang diagnostic pop // ignored "-Wconversion" |