blob: 822f02b266a5756697d89a31f0f92bd3a0885df8 [file] [log] [blame]
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001#ifndef ANDROID_DVR_SERVICES_DISPLAYD_VSYNC_SERVICE_H_
2#define ANDROID_DVR_SERVICES_DISPLAYD_VSYNC_SERVICE_H_
3
4#include <pdx/service.h>
5
6#include <list>
7#include <memory>
8#include <mutex>
9#include <thread>
10
11#include "display_service.h"
12
13namespace android {
14namespace dvr {
15
16// VSyncWaiter encapsulates a client blocked waiting for the next vsync.
17// It is used to enqueue the Message to reply to when the next vsync event
18// occurs.
19class VSyncWaiter {
20 public:
21 explicit VSyncWaiter(pdx::Message& message) : message_(std::move(message)) {}
22
23 void Notify(int64_t timestamp);
24
25 private:
Corey Tabaka2251d822017-04-20 16:04:07 -070026 pdx::Status<int64_t> OnWait(pdx::Message& message);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080027
28 pdx::Message message_;
29 int64_t timestamp_ = 0;
30
31 VSyncWaiter(const VSyncWaiter&) = delete;
32 void operator=(const VSyncWaiter&) = delete;
33};
34
35// VSyncChannel manages the service-side per-client context for each client
36// using the service.
37class VSyncChannel : public pdx::Channel {
38 public:
39 VSyncChannel(pdx::Service& service, int pid, int cid)
40 : service_(service), pid_(pid), cid_(cid) {}
41
42 void Ack();
43 void Signal();
44
45 private:
46 pdx::Service& service_;
47 pid_t pid_;
48 int cid_;
49
50 VSyncChannel(const VSyncChannel&) = delete;
51 void operator=(const VSyncChannel&) = delete;
52};
53
54// VSyncService implements the displayd vsync service over ServiceFS.
55class VSyncService : public pdx::ServiceBase<VSyncService> {
56 public:
57 ~VSyncService() override;
58
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -070059 pdx::Status<void> HandleMessage(pdx::Message& message) override;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080060
61 std::shared_ptr<pdx::Channel> OnChannelOpen(pdx::Message& message) override;
62 void OnChannelClose(pdx::Message& message,
63 const std::shared_ptr<pdx::Channel>& channel) override;
64
Corey Tabaka2251d822017-04-20 16:04:07 -070065 // Called by the hardware composer HAL, or similar, whenever a vsync event
Steven Thomas6e8f7062017-11-22 14:15:29 -080066 // occurs on the primary display. |compositor_time_ns| is the number of ns
67 // before the next vsync when the compositor will preempt the GPU to do EDS
68 // and lens warp.
69 void VSyncEvent(int64_t timestamp_ns, int64_t compositor_time_ns,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080070 uint32_t vsync_count);
71
72 private:
73 friend BASE;
74
75 VSyncService();
76
Corey Tabaka2251d822017-04-20 16:04:07 -070077 pdx::Status<int64_t> OnGetLastTimestamp(pdx::Message& message);
78 pdx::Status<display::VSyncSchedInfo> OnGetSchedInfo(pdx::Message& message);
79 pdx::Status<void> OnAcknowledge(pdx::Message& message);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080080
81 void NotifierThreadFunction();
82
83 void AddWaiter(pdx::Message& message);
84 void NotifyWaiters();
85 void UpdateClients();
86
87 void AddClient(const std::shared_ptr<VSyncChannel>& client);
88 void RemoveClient(const std::shared_ptr<VSyncChannel>& client);
89
90 int64_t last_vsync_;
91 int64_t current_vsync_;
92 int64_t compositor_time_ns_;
93 uint32_t current_vsync_count_;
94
95 std::mutex mutex_;
96
97 std::list<std::unique_ptr<VSyncWaiter>> waiters_;
98 std::list<std::shared_ptr<VSyncChannel>> clients_;
99
100 VSyncService(const VSyncService&) = delete;
101 void operator=(VSyncService&) = delete;
102};
103
104} // namespace dvr
105} // namespace android
106
107#endif // ANDROID_DVR_SERVICES_DISPLAYD_VSYNC_SERVICE_H_