• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 
18 #pragma once
19 
20 #include <thread>
21 #include <vector>
22 #include <mutex>
23 #include <condition_variable>
24 
25 namespace swappy {
26 
27 class ChoreographerFilter {
28   public:
29     using Worker = std::function<std::chrono::nanoseconds()>;
30 
31     explicit ChoreographerFilter(std::chrono::nanoseconds refreshPeriod,
32                                  std::chrono::nanoseconds appToSfDelay,
33                                  Worker doWork);
34     ~ChoreographerFilter();
35 
36     void onChoreographer();
37 
38   private:
39     void launchThreadsLocked();
40     void terminateThreadsLocked();
41 
42     void onSettingsChanged();
43 
44     void threadMain(bool useAffinity, int32_t thread);
45 
46     std::mutex mThreadPoolMutex;
47     bool mUseAffinity = true;
48     std::vector<std::thread> mThreadPool;
49 
50     std::mutex mMutex;
51     std::condition_variable mCondition;
52     bool mIsRunning = true;
53     int64_t mSequenceNumber = 0;
54     std::chrono::steady_clock::time_point mLastTimestamp;
55 
56     std::mutex mWorkMutex;
57     std::chrono::steady_clock::time_point mLastWorkRun;
58     std::chrono::nanoseconds mWorkDuration;
59 
60     const std::chrono::nanoseconds mRefreshPeriod;
61     const std::chrono::nanoseconds mAppToSfDelay;
62     const Worker mDoWork;
63 };
64 
65 } // namespace swappy
66