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