• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #ifndef ANDROID_HWC_VSYNCTHREAD_H
18 #define ANDROID_HWC_VSYNCTHREAD_H
19 
20 #include <android/hardware/graphics/common/1.0/types.h>
21 #include <utils/Thread.h>
22 
23 #include <chrono>
24 #include <mutex>
25 #include <optional>
26 
27 #include "Common.h"
28 
29 namespace android {
30 
31 // Generates Vsync signals in software.
32 class VsyncThread : public Thread {
33  public:
34   VsyncThread(hwc2_display_t id);
~VsyncThread()35   virtual ~VsyncThread() {}
36 
37   VsyncThread(const VsyncThread&) = default;
38   VsyncThread& operator=(const VsyncThread&) = default;
39 
40   VsyncThread(VsyncThread&&) = default;
41   VsyncThread& operator=(VsyncThread&&) = default;
42 
43   HWC2::Error start(hwc2_vsync_period_t period);
44 
45   HWC2::Error setVsyncCallback(HWC2_PFN_VSYNC callback,
46                                hwc2_callback_data_t callbackData);
47   HWC2::Error setVsync24Callback(HWC2_PFN_VSYNC_2_4 callback,
48                                  hwc2_callback_data_t callbackData);
49 
50   HWC2::Error setVsyncEnabled(bool enabled);
51 
52   HWC2::Error scheduleVsyncUpdate(
53       hwc2_vsync_period_t newVsyncPeriod,
54       hwc_vsync_period_change_constraints_t* newVsyncPeriodChangeConstraints,
55       hwc_vsync_period_change_timeline_t* timeline);
56 
57  private:
58   bool threadLoop() final;
59 
60   std::chrono::nanoseconds updateVsyncPeriodLocked(
61       std::chrono::time_point<std::chrono::steady_clock> now);
62 
63   const hwc2_display_t mDisplayId;
64 
65   std::mutex mStateMutex;
66 
67   HWC2_PFN_VSYNC mVsyncCallback;
68   hwc2_callback_data_t mVsyncCallbackData = nullptr;
69 
70   HWC2_PFN_VSYNC_2_4 mVsync24Callback;
71   hwc2_callback_data_t mVsync24CallbackData = nullptr;
72 
73   bool mVsyncEnabled = false;
74   std::chrono::nanoseconds mVsyncPeriod;
75   std::chrono::time_point<std::chrono::steady_clock> mPreviousVsync;
76 
77   struct PendingUpdate {
78     std::chrono::nanoseconds period;
79     std::chrono::time_point<std::chrono::steady_clock> updateAfter;
80   };
81   std::optional<PendingUpdate> mPendingUpdate;
82 };
83 
84 }  // namespace android
85 
86 #endif