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<display::SurfaceInfo> OnCreateSurface(
84       pdx::Message& message, const display::SurfaceAttributes& attributes);
85   pdx::Status<BorrowedNativeBufferHandle> OnSetupGlobalBuffer(
86       pdx::Message& message, DvrGlobalBufferKey key, size_t size,
87       uint64_t usage);
88   pdx::Status<void> OnDeleteGlobalBuffer(pdx::Message& message,
89                                          DvrGlobalBufferKey key);
90 
91   // Temporary query for current VR status. Will be removed later.
92   pdx::Status<bool> IsVrAppRunning(pdx::Message& message);
93 
AddEventHandler(int fd,int events,EpollEventDispatcher::Handler handler)94   pdx::Status<void> AddEventHandler(int fd, int events,
95                                     EpollEventDispatcher::Handler handler) {
96     return dispatcher_.AddEventHandler(fd, events, handler);
97   }
RemoveEventHandler(int fd)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   EpollEventDispatcher dispatcher_;
113   DisplayConfigurationUpdateNotifier update_notifier_;
114 
115   std::unordered_map<DvrGlobalBufferKey, std::unique_ptr<IonBuffer>>
116       global_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