blob: cafae53400c5aa0ef2d319e24f20cffad3c1cba9 [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 <condition_variable>
Lloyd Pique0c3a8832018-01-22 17:31:47 -080020#include <cstddef>
Lloyd Pique755e3192018-01-31 16:46:15 -080021#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
Lloyd Pique755e3192018-01-31 16:46:15 -080029class EventControlThread {
Jamie Gennisd1700752013-10-14 12:22:52 -070030public:
Lloyd Pique0c3a8832018-01-22 17:31:47 -080031 virtual ~EventControlThread();
32
33 virtual void setVsyncEnabled(bool enabled) = 0;
34};
35
36namespace impl {
37
38class EventControlThread final : public android::EventControlThread {
39public:
Lloyd Pique755e3192018-01-31 16:46:15 -080040 using SetVSyncEnabledFunction = std::function<void(bool)>;
Jamie Gennisd1700752013-10-14 12:22:52 -070041
Lloyd Pique755e3192018-01-31 16:46:15 -080042 explicit EventControlThread(SetVSyncEnabledFunction function);
43 ~EventControlThread();
Jamie Gennisd1700752013-10-14 12:22:52 -070044
Lloyd Pique0c3a8832018-01-22 17:31:47 -080045 // EventControlThread implementation
46 void setVsyncEnabled(bool enabled) override;
Jamie Gennisd1700752013-10-14 12:22:52 -070047
48private:
Lloyd Pique755e3192018-01-31 16:46:15 -080049 void threadMain();
Jamie Gennisd1700752013-10-14 12:22:52 -070050
Lloyd Pique755e3192018-01-31 16:46:15 -080051 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};
Jamie Gennisd1700752013-10-14 12:22:52 -070060};
61
Lloyd Pique0c3a8832018-01-22 17:31:47 -080062} // namespace impl
Lloyd Pique755e3192018-01-31 16:46:15 -080063} // namespace android