blob: 321fb79831013458966a79acd92611c0a7d7f9b1 [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
Lloyd Pique755e3192018-01-31 16:46:15 -080017#pragma once
Jamie Gennisd1700752013-10-14 12:22:52 -070018
Lloyd Pique755e3192018-01-31 16:46:15 -080019#include <cstddef>
20#include <condition_variable>
21#include <functional>
22#include <mutex>
23#include <thread>
Jamie Gennisd1700752013-10-14 12:22:52 -070024
Lloyd Pique755e3192018-01-31 16:46:15 -080025#include <android-base/thread_annotations.h>
Jamie Gennisd1700752013-10-14 12:22:52 -070026
27namespace android {
28
29class SurfaceFlinger;
30
Lloyd Pique755e3192018-01-31 16:46:15 -080031class EventControlThread {
Jamie Gennisd1700752013-10-14 12:22:52 -070032public:
Lloyd Pique755e3192018-01-31 16:46:15 -080033 using SetVSyncEnabledFunction = std::function<void(bool)>;
Jamie Gennisd1700752013-10-14 12:22:52 -070034
Lloyd Pique755e3192018-01-31 16:46:15 -080035 explicit EventControlThread(SetVSyncEnabledFunction function);
36 ~EventControlThread();
Jamie Gennisd1700752013-10-14 12:22:52 -070037
38 void setVsyncEnabled(bool enabled);
Jamie Gennisd1700752013-10-14 12:22:52 -070039
40private:
Lloyd Pique755e3192018-01-31 16:46:15 -080041 void threadMain();
Jamie Gennisd1700752013-10-14 12:22:52 -070042
Lloyd Pique755e3192018-01-31 16:46:15 -080043 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 Gennisd1700752013-10-14 12:22:52 -070052};
53
Lloyd Pique755e3192018-01-31 16:46:15 -080054} // namespace android