• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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_DRM_EVENT_LISTENER_H_
18 #define ANDROID_DRM_EVENT_LISTENER_H_
19 
20 #include <sys/epoll.h>
21 
22 #include <map>
23 
24 #include "autofd.h"
25 #include "worker.h"
26 
27 namespace android {
28 
29 constexpr uint32_t kDefaultVsyncPeriodNanoSecond = 16666666;
30 constexpr int32_t kDefaultRefreshRateFrequency = 60;
31 
32 class DrmDevice;
33 
34 class DrmEventHandler {
35  public:
DrmEventHandler()36   DrmEventHandler() {
37   }
~DrmEventHandler()38   virtual ~DrmEventHandler() {
39   }
40 
41   virtual void handleEvent(uint64_t timestamp_us) = 0;
42 };
43 
44 class DrmHistogramEventHandler {
45 public:
DrmHistogramEventHandler()46     DrmHistogramEventHandler() {}
~DrmHistogramEventHandler()47     virtual ~DrmHistogramEventHandler() {}
48 
49     virtual void handleHistogramEvent(uint32_t crtc_id, void *) = 0;
50 };
51 
52 class DrmHistogramChannelEventHandler {
53    public:
DrmHistogramChannelEventHandler()54     DrmHistogramChannelEventHandler() {}
~DrmHistogramChannelEventHandler()55     virtual ~DrmHistogramChannelEventHandler() {}
56 
57     virtual void handleHistogramChannelEvent(void *) = 0;
58 };
59 
60 class DrmContextHistogramEventHandler {
61  public:
DrmContextHistogramEventHandler()62   DrmContextHistogramEventHandler() {}
~DrmContextHistogramEventHandler()63   virtual ~DrmContextHistogramEventHandler() {}
64 
65   virtual void handleContextHistogramEvent(void *) = 0;
66 };
67 
68 class DrmTUIEventHandler {
69  public:
DrmTUIEventHandler()70   DrmTUIEventHandler() {
71   }
~DrmTUIEventHandler()72   virtual ~DrmTUIEventHandler() {
73   }
74 
75   virtual void handleTUIEvent() = 0;
76 };
77 
78 class DrmPanelIdleEventHandler {
79  public:
DrmPanelIdleEventHandler()80   DrmPanelIdleEventHandler() {}
~DrmPanelIdleEventHandler()81   virtual ~DrmPanelIdleEventHandler() {}
82 
83   virtual void handleIdleEnterEvent(char const *event) = 0;
84 };
85 
86 class DrmSysfsEventHandler {
87  public:
DrmSysfsEventHandler()88   DrmSysfsEventHandler() {}
~DrmSysfsEventHandler()89   virtual ~DrmSysfsEventHandler() {}
90 
91   virtual void handleSysfsEvent() = 0;
92   virtual int getFd() = 0;
93 };
94 
95 class DrmPropertyUpdateHandler {
96  public:
DrmPropertyUpdateHandler()97   DrmPropertyUpdateHandler() {}
~DrmPropertyUpdateHandler()98   virtual ~DrmPropertyUpdateHandler() {}
99 
100   virtual void handleDrmPropertyUpdate(unsigned connector_id, unsigned prop_id) = 0;
101 };
102 
103 class DrmEventListener : public Worker {
104   static constexpr const char kTUIStatusPath[] = "/sys/devices/platform/exynos-drm/tui_status";
105   static const uint32_t maxFds = 4;
106 
107  public:
108   DrmEventListener(DrmDevice *drm);
109   virtual ~DrmEventListener();
110 
111   int Init();
112 
113   void RegisterHotplugHandler(const std::shared_ptr<DrmEventHandler> &handler);
114   void UnRegisterHotplugHandler(const std::shared_ptr<DrmEventHandler> &handler);
115   void RegisterHistogramHandler(const std::shared_ptr<DrmHistogramEventHandler> &handler);
116   void UnRegisterHistogramHandler(const std::shared_ptr<DrmHistogramEventHandler> &handler);
117   void RegisterHistogramChannelHandler(
118       const std::shared_ptr<DrmHistogramChannelEventHandler> &handler);
119   void UnRegisterHistogramChannelHandler(
120       const std::shared_ptr<DrmHistogramChannelEventHandler> &handler);
121   void RegisterContextHistogramHandler(
122       const std::shared_ptr<DrmContextHistogramEventHandler> &handler);
123   void UnRegisterContextHistogramHandler(
124       const std::shared_ptr<DrmContextHistogramEventHandler> &handler);
125   void RegisterTUIHandler(const std::shared_ptr<DrmTUIEventHandler> &handler);
126   void UnRegisterTUIHandler(const std::shared_ptr<DrmTUIEventHandler> &handler);
127   void RegisterPanelIdleHandler(const std::shared_ptr<DrmPanelIdleEventHandler> &handler);
128   void UnRegisterPanelIdleHandler(const std::shared_ptr<DrmPanelIdleEventHandler> &handler);
129   int RegisterSysfsHandler(std::shared_ptr<DrmSysfsEventHandler> handler);
130   int UnRegisterSysfsHandler(int sysfs_fd);
131   void RegisterPropertyUpdateHandler(const std::shared_ptr<DrmPropertyUpdateHandler> &handler);
132   void UnRegisterPropertyUpdateHandler(const std::shared_ptr<DrmPropertyUpdateHandler> &handler);
133 
134   bool IsDrmInTUI();
135 
136   static void FlipHandler(int fd, unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec,
137                           void *user_data);
138 
139  protected:
140   virtual void Routine();
141 
142  private:
143   void UEventHandler();
144   void DRMEventHandler();
145   void TUIEventHandler();
146   void SysfsEventHandler(int fd);
147 
148   UniqueFd epoll_fd_;
149   UniqueFd uevent_fd_;
150   UniqueFd tuievent_fd_;
151 
152   DrmDevice *drm_;
153   std::shared_ptr<DrmEventHandler> hotplug_handler_;
154   std::shared_ptr<DrmHistogramEventHandler> histogram_handler_;
155   std::shared_ptr<DrmHistogramChannelEventHandler> histogram_channel_handler_;
156   std::shared_ptr<DrmContextHistogramEventHandler> context_histogram_handler_;
157   std::shared_ptr<DrmTUIEventHandler> tui_handler_;
158   std::shared_ptr<DrmPanelIdleEventHandler> panel_idle_handler_;
159   std::shared_ptr<DrmPropertyUpdateHandler> drm_prop_update_handler_;
160   std::mutex mutex_;
161   std::map<int, std::shared_ptr<DrmSysfsEventHandler>> sysfs_handlers_;
162 };
163 
164 }  // namespace android
165 
166 #endif
167