1#ifndef ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SERVICE_H_
2#define ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SERVICE_H_
3
4#include <pdx/service.h>
5#include <pdx/status.h>
6#include <private/dvr/buffer_hub_client.h>
7#include <private/dvr/bufferhub_rpc.h>
8#include <private/dvr/display_protocol.h>
9
10#include <functional>
11#include <iterator>
12#include <memory>
13#include <string>
14#include <vector>
15
16#include "acquired_buffer.h"
17#include "display_surface.h"
18#include "epoll_event_dispatcher.h"
19#include "hardware_composer.h"
20
21namespace android {
22namespace dvr {
23
24// DisplayService implements the display service component of VrFlinger.
25class DisplayService : public pdx::ServiceBase<DisplayService> {
26 public:
27  bool IsInitialized() const override;
28  std::string DumpState(size_t max_length) override;
29
30  void OnChannelClose(pdx::Message& message,
31                      const std::shared_ptr<pdx::Channel>& channel) override;
32  pdx::Status<void> HandleMessage(pdx::Message& message) override;
33
34  std::shared_ptr<DisplaySurface> GetDisplaySurface(int surface_id) const;
35  std::vector<std::shared_ptr<DisplaySurface>> GetDisplaySurfaces() const;
36  std::vector<std::shared_ptr<DirectDisplaySurface>> GetVisibleDisplaySurfaces()
37      const;
38
39  // Updates the list of actively displayed surfaces. This must be called after
40  // any change to client/manager attributes that affect visibility or z order.
41  void UpdateActiveDisplaySurfaces();
42
43  pdx::Status<BorrowedNativeBufferHandle> SetupNamedBuffer(
44      const std::string& name, size_t size, uint64_t usage);
45
46  template <class A>
47  void ForEachDisplaySurface(SurfaceType surface_type, A action) const {
48    ForEachChannel([surface_type,
49                    action](const ChannelIterator::value_type& pair) mutable {
50      auto surface = std::static_pointer_cast<DisplaySurface>(pair.second);
51      if (surface->surface_type() == surface_type)
52        action(surface);
53    });
54  }
55
56  using DisplayConfigurationUpdateNotifier = std::function<void(void)>;
57  void SetDisplayConfigurationUpdateNotifier(
58      DisplayConfigurationUpdateNotifier notifier);
59
60  using VSyncCallback = HardwareComposer::VSyncCallback;
61  void SetVSyncCallback(VSyncCallback callback) {
62    hardware_composer_.SetVSyncCallback(callback);
63  }
64
65  HWCDisplayMetrics GetDisplayMetrics() {
66    return hardware_composer_.display_metrics();
67  }
68
69  void GrantDisplayOwnership() { hardware_composer_.Enable(); }
70  void SeizeDisplayOwnership() { hardware_composer_.Disable(); }
71
72  void OnHardwareComposerRefresh();
73
74 private:
75  friend BASE;
76  friend DisplaySurface;
77
78  friend class VrDisplayStateService;
79
80  using RequestDisplayCallback = std::function<void(bool)>;
81
82  DisplayService(android::Hwc2::Composer* hidl,
83                 RequestDisplayCallback request_display_callback);
84
85  pdx::Status<BorrowedNativeBufferHandle> OnGetNamedBuffer(
86      pdx::Message& message, const std::string& name);
87  pdx::Status<display::Metrics> OnGetMetrics(pdx::Message& message);
88  pdx::Status<display::SurfaceInfo> OnCreateSurface(
89      pdx::Message& message, const display::SurfaceAttributes& attributes);
90
91  // Temporary query for current VR status. Will be removed later.
92  pdx::Status<bool> IsVrAppRunning(pdx::Message& message);
93
94  pdx::Status<void> AddEventHandler(int fd, int events,
95                                    EpollEventDispatcher::Handler handler) {
96    return dispatcher_.AddEventHandler(fd, events, handler);
97  }
98  pdx::Status<void> RemoveEventHandler(int fd) {
99    return dispatcher_.RemoveEventHandler(fd);
100  }
101
102  void SurfaceUpdated(SurfaceType surface_type,
103                      display::SurfaceUpdateFlags update_flags);
104
105  // Called by DisplaySurface to signal that a surface property has changed and
106  // the display manager should be notified.
107  void NotifyDisplayConfigurationUpdate();
108
109  pdx::Status<void> HandleSurfaceMessage(pdx::Message& message);
110
111  HardwareComposer hardware_composer_;
112  RequestDisplayCallback request_display_callback_;
113  EpollEventDispatcher dispatcher_;
114  DisplayConfigurationUpdateNotifier update_notifier_;
115
116  std::unordered_map<std::string, std::unique_ptr<IonBuffer>> named_buffers_;
117
118  DisplayService(const DisplayService&) = delete;
119  void operator=(const DisplayService&) = delete;
120};
121
122}  // namespace dvr
123}  // namespace android
124
125#endif  // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SERVICE_H_
126