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 #pragma once 18 19 #include <condition_variable> 20 #include <cstddef> 21 #include <functional> 22 #include <mutex> 23 #include <thread> 24 25 #include <android-base/thread_annotations.h> 26 27 namespace android { 28 29 class EventControlThread { 30 public: 31 virtual ~EventControlThread(); 32 33 virtual void setVsyncEnabled(bool enabled) = 0; 34 }; 35 36 namespace impl { 37 38 class EventControlThread final : public android::EventControlThread { 39 public: 40 using SetVSyncEnabledFunction = std::function<void(bool)>; 41 42 explicit EventControlThread(SetVSyncEnabledFunction function); 43 ~EventControlThread(); 44 45 // EventControlThread implementation 46 void setVsyncEnabled(bool enabled) override; 47 48 private: 49 void threadMain(); 50 51 std::mutex mMutex; 52 std::condition_variable mCondition; 53 54 const SetVSyncEnabledFunction mSetVSyncEnabled; 55 bool mVsyncEnabled GUARDED_BY(mMutex) = false; 56 bool mKeepRunning GUARDED_BY(mMutex) = true; 57 58 // Must be last so that everything is initialized before the thread starts. 59 std::thread mThread{&EventControlThread::threadMain, this}; 60 }; 61 62 } // namespace impl 63 } // namespace android 64