• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 <functional>
21 #include <map>
22 #include <mutex>
23 #include <thread>
24 
25 #include "DrmDevice.h"
26 #include "utils/thread_annotations.h"
27 
28 namespace android {
29 
30 class VSyncWorker {
31  public:
32   using VsyncTimestampCallback = std::function<void(int64_t /*timestamp*/,
33                                                     uint32_t /*period*/)>;
34 
35   ~VSyncWorker();
36 
37   auto static CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe)
38       -> std::unique_ptr<VSyncWorker>;
39 
40   // Set the expected vsync period. Resets internal timestamp tracking until the
41   // next vsync event is tracked.
42   void SetVsyncPeriodNs(uint32_t vsync_period_ns);
43 
44   // Set or clear a callback to be fired on vsync.
45   void SetTimestampCallback(std::optional<VsyncTimestampCallback> &&callback);
46 
47   // Enable vsync timestamp tracking. GetLastVsyncTimestamp will return 0 if
48   // vsync tracking is disabled, or if no vsync has happened since it was
49   // enabled.
50   void SetVsyncTimestampTracking(bool enabled);
51   uint32_t GetLastVsyncTimestamp();
52 
53   // Get the next predicted vsync timestamp after |time|, based on the last
54   // recorded vsync timestamp and the current vsync period.
55   int64_t GetNextVsyncTimestamp(int64_t time);
56 
57   void StopThread();
58 
59  private:
60   VSyncWorker() = default;
61 
62   void ThreadFn();
63 
64   int64_t GetPhasedVSync(int64_t frame_ns, int64_t current) const
65       REQUIRES(mutex_);
66   int SyntheticWaitVBlank(int64_t *timestamp);
67 
68   void UpdateVSyncControl();
69   bool ShouldEnable() const REQUIRES(mutex_);
70 
71   SharedFd drm_fd_;
72   uint32_t high_crtc_ = 0;
73 
74   bool enabled_ GUARDED_BY(mutex_) = false;
75   bool thread_exit_ GUARDED_BY(mutex_) = false;
76   std::optional<int64_t> last_timestamp_ GUARDED_BY(mutex_);
77 
78   // Default to 60Hz refresh rate
79   static constexpr uint32_t kDefaultVSPeriodNs = 16666666;
80   uint32_t vsync_period_ns_ GUARDED_BY(mutex_) = kDefaultVSPeriodNs;
81   bool enable_vsync_timestamps_ GUARDED_BY(mutex_) = false;
82   bool last_timestamp_is_fresh_ GUARDED_BY(mutex_) = false;
83   std::optional<VsyncTimestampCallback> callback_ GUARDED_BY(mutex_);
84 
85   std::condition_variable cv_;
86   std::thread vswt_;
87   std::mutex mutex_;
88 };
89 }  // namespace android
90