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 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 17 | #pragma once |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 18 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 19 | #include <cstddef> |
| 20 | #include <condition_variable> |
| 21 | #include <functional> |
| 22 | #include <mutex> |
| 23 | #include <thread> |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 24 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 25 | #include <android-base/thread_annotations.h> |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | class SurfaceFlinger; |
| 30 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 31 | class EventControlThread { |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 32 | public: |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 33 | using SetVSyncEnabledFunction = std::function<void(bool)>; |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 34 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 35 | explicit EventControlThread(SetVSyncEnabledFunction function); |
| 36 | ~EventControlThread(); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 37 | |
| 38 | void setVsyncEnabled(bool enabled); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 41 | void threadMain(); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 42 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 43 | std::mutex mMutex; |
| 44 | std::condition_variable mCondition; |
| 45 | |
| 46 | const SetVSyncEnabledFunction mSetVSyncEnabled; |
| 47 | bool mVsyncEnabled GUARDED_BY(mMutex) = false; |
| 48 | bool mKeepRunning GUARDED_BY(mMutex) = true; |
| 49 | |
| 50 | // Must be last so that everything is initialized before the thread starts. |
| 51 | std::thread mThread{&EventControlThread::threadMain, this}; |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 54 | } // namespace android |