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/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 
21 namespace android {
22 namespace dvr {
23 
24 // DisplayService implements the display service component of VrFlinger.
25 class 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> SetupGlobalBuffer(
44       DvrGlobalBufferKey key, size_t size, uint64_t usage);
45 
46   pdx::Status<void> DeleteGlobalBuffer(DvrGlobalBufferKey key);
47 
48   template <class A>
ForEachDisplaySurface(SurfaceType surface_type,A action)49   void ForEachDisplaySurface(SurfaceType surface_type, A action) const {
50     ForEachChannel([surface_type,
51                     action](const ChannelIterator::value_type& pair) mutable {
52       auto surface = std::static_pointer_cast<DisplaySurface>(pair.second);
53       if (surface->surface_type() == surface_type)
54         action(surface);
55     });
56   }
57 
58   using DisplayConfigurationUpdateNotifier = std::function<void(void)>;
59   void SetDisplayConfigurationUpdateNotifier(
60       DisplayConfigurationUpdateNotifier notifier);
61 
GrantDisplayOwnership()62   void GrantDisplayOwnership() { hardware_composer_.Enable(); }
SeizeDisplayOwnership()63   void SeizeDisplayOwnership() { hardware_composer_.Disable(); }
OnBootFinished()64   void OnBootFinished() { hardware_composer_.OnBootFinished(); }
65 
66  private:
67   friend BASE;
68   friend DisplaySurface;
69 
70   friend class VrDisplayStateService;
71 
72   using RequestDisplayCallback = std::function<void(bool)>;
73 
74   DisplayService(android::Hwc2::Composer* hidl,
75                  hwc2_display_t primary_display_id,
76                  RequestDisplayCallback request_display_callback);
77 
78   pdx::Status<BorrowedNativeBufferHandle> OnGetGlobalBuffer(
79       pdx::Message& message, DvrGlobalBufferKey key);
80   pdx::Status<display::Metrics> OnGetMetrics(pdx::Message& message);
81   pdx::Status<std::string> OnGetConfigurationData(
82       pdx::Message& message, display::ConfigFileType config_type);
83   pdx::Status<uint8_t> OnGetDisplayIdentificationPort(pdx::Message& message);
84   pdx::Status<display::SurfaceInfo> OnCreateSurface(
85       pdx::Message& message, const display::SurfaceAttributes& attributes);
86   pdx::Status<BorrowedNativeBufferHandle> OnSetupGlobalBuffer(
87       pdx::Message& message, DvrGlobalBufferKey key, size_t size,
88       uint64_t usage);
89   pdx::Status<void> OnDeleteGlobalBuffer(pdx::Message& message,
90                                          DvrGlobalBufferKey key);
91 
92   // Temporary query for current VR status. Will be removed later.
93   pdx::Status<bool> IsVrAppRunning(pdx::Message& message);
94 
AddEventHandler(int fd,int events,EpollEventDispatcher::Handler handler)95   pdx::Status<void> AddEventHandler(int fd, int events,
96                                     EpollEventDispatcher::Handler handler) {
97     return dispatcher_.AddEventHandler(fd, events, handler);
98   }
RemoveEventHandler(int fd)99   pdx::Status<void> RemoveEventHandler(int fd) {
100     return dispatcher_.RemoveEventHandler(fd);
101   }
102 
103   void SurfaceUpdated(SurfaceType surface_type,
104                       display::SurfaceUpdateFlags update_flags);
105 
106   // Called by DisplaySurface to signal that a surface property has changed and
107   // the display manager should be notified.
108   void NotifyDisplayConfigurationUpdate();
109 
110   pdx::Status<void> HandleSurfaceMessage(pdx::Message& message);
111 
112   HardwareComposer hardware_composer_;
113   EpollEventDispatcher dispatcher_;
114   DisplayConfigurationUpdateNotifier update_notifier_;
115 
116   std::unordered_map<DvrGlobalBufferKey, std::unique_ptr<IonBuffer>>
117       global_buffers_;
118 
119   DisplayService(const DisplayService&) = delete;
120   void operator=(const DisplayService&) = delete;
121 };
122 
123 }  // namespace dvr
124 }  // namespace android
125 
126 #endif  // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SERVICE_H_
127