• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_
2 #define ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_
3 
4 #include <pdx/file_handle.h>
5 #include <pdx/service.h>
6 #include <private/dvr/buffer_hub_queue_client.h>
7 #include <private/dvr/display_protocol.h>
8 #include <private/dvr/ring_buffer.h>
9 
10 #include <functional>
11 #include <iterator>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "acquired_buffer.h"
17 
18 namespace android {
19 namespace dvr {
20 
21 class DisplayService;
22 
23 enum class SurfaceType {
24   Direct,
25   Application,
26 };
27 
28 class DisplaySurface : public pdx::Channel {
29  public:
30   static pdx::Status<std::shared_ptr<DisplaySurface>> Create(
31       DisplayService* service, int surface_id, int process_id, int user_id,
32       const display::SurfaceAttributes& attributes);
33 
34   ~DisplaySurface() override;
35 
service()36   DisplayService* service() const { return service_; }
surface_type()37   SurfaceType surface_type() const { return surface_type_; }
surface_id()38   int surface_id() const { return surface_id_; }
process_id()39   int process_id() const { return process_id_; }
user_id()40   int user_id() const { return user_id_; }
41 
visible()42   bool visible() const { return visible_; }
z_order()43   int z_order() const { return z_order_; }
44 
attributes()45   const display::SurfaceAttributes& attributes() const { return attributes_; }
update_flags()46   display::SurfaceUpdateFlags update_flags() const { return update_flags_; }
47 
GetQueueIds()48   virtual std::vector<int32_t> GetQueueIds() const { return {}; }
49 
IsUpdatePending()50   bool IsUpdatePending() const {
51     return update_flags_.value() != display::SurfaceUpdateFlags::None;
52   }
53 
54  protected:
55   DisplaySurface(DisplayService* service, SurfaceType surface_type,
56                  int surface_id, int process_id, int user_id);
57 
58   // Utility to retrieve a shared pointer to this channel as the desired derived
59   // type.
60   template <
61       typename T = DisplaySurface,
62       typename = std::enable_if_t<std::is_base_of<DisplaySurface, T>::value>>
Self()63   std::shared_ptr<T> Self() {
64     return std::static_pointer_cast<T>(shared_from_this());
65   }
66 
67   virtual pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
68       pdx::Message& message, const ProducerQueueConfig& config) = 0;
69 
70   // Registers a consumer queue with the event dispatcher in DisplayService. The
71   // OnQueueEvent callback below is called to handle queue events.
72   pdx::Status<void> RegisterQueue(
73       const std::shared_ptr<ConsumerQueue>& consumer_queue);
74   pdx::Status<void> UnregisterQueue(
75       const std::shared_ptr<ConsumerQueue>& consumer_queue);
76 
77   // Called by the event dispatcher in DisplayService when a registered queue
78   // event triggers. Executes on the event dispatcher thread.
79   virtual void OnQueueEvent(
80       const std::shared_ptr<ConsumerQueue>& consumer_queue, int events);
81 
82   void SurfaceUpdated(display::SurfaceUpdateFlags update_flags);
83   void ClearUpdate();
84 
85   // Synchronizes access to mutable state below between message dispatch thread
86   // and frame post thread.
87   mutable std::mutex lock_;
88 
89  private:
90   friend class DisplayService;
91   friend class DisplayManagerService;
92 
93   // Dispatches display surface messages to the appropriate handlers. This
94   // handler runs on the VrFlinger message dispatch thread.
95   pdx::Status<void> HandleMessage(pdx::Message& message);
96 
97   pdx::Status<void> OnSetAttributes(
98       pdx::Message& message, const display::SurfaceAttributes& attributes);
99   pdx::Status<display::SurfaceInfo> OnGetSurfaceInfo(pdx::Message& message);
100 
101   DisplayService* service_;
102   SurfaceType surface_type_;
103   int surface_id_;
104   int process_id_;
105   int user_id_;
106 
107   display::SurfaceAttributes attributes_;
108   display::SurfaceUpdateFlags update_flags_ = display::SurfaceUpdateFlags::None;
109 
110   // Subset of attributes that may be interpreted by the display service.
111   bool visible_ = false;
112   int z_order_ = 0;
113 
114   DisplaySurface(const DisplaySurface&) = delete;
115   void operator=(const DisplaySurface&) = delete;
116 };
117 
118 class ApplicationDisplaySurface : public DisplaySurface {
119  public:
ApplicationDisplaySurface(DisplayService * service,int surface_id,int process_id,int user_id)120   ApplicationDisplaySurface(DisplayService* service, int surface_id,
121                             int process_id, int user_id)
122       : DisplaySurface(service, SurfaceType::Application, surface_id,
123                        process_id, user_id) {}
124 
125   std::shared_ptr<ConsumerQueue> GetQueue(int32_t queue_id);
126   std::vector<int32_t> GetQueueIds() const override;
127 
128  private:
129   pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
130       pdx::Message& message, const ProducerQueueConfig& config) override;
131   void OnQueueEvent(const std::shared_ptr<ConsumerQueue>& consumer_queue,
132                     int events) override;
133 
134   // Accessed by both message dispatch thread and epoll event thread.
135   std::unordered_map<int32_t, std::shared_ptr<ConsumerQueue>> consumer_queues_;
136 };
137 
138 class DirectDisplaySurface : public DisplaySurface {
139  public:
DirectDisplaySurface(DisplayService * service,int surface_id,int process_id,int user_id)140   DirectDisplaySurface(DisplayService* service, int surface_id, int process_id,
141                        int user_id)
142       : DisplaySurface(service, SurfaceType::Direct, surface_id, process_id,
143                        user_id),
144         acquired_buffers_(kMaxPostedBuffers),
145         metadata_(nullptr) {}
146   std::vector<int32_t> GetQueueIds() const override;
147   bool IsBufferAvailable();
148   bool IsBufferPosted();
149   AcquiredBuffer AcquireCurrentBuffer();
150 
151   // Get the newest buffer. Up to one buffer will be skipped. If a buffer is
152   // skipped, it will be stored in skipped_buffer if non null.
153   AcquiredBuffer AcquireNewestAvailableBuffer(AcquiredBuffer* skipped_buffer);
154 
155  private:
156   pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
157       pdx::Message& message, const ProducerQueueConfig& config) override;
158   void OnQueueEvent(const std::shared_ptr<ConsumerQueue>& consumer_queue,
159                     int events) override;
160 
161   // The capacity of the pending buffer queue. Should be enough to hold all the
162   // buffers of this DisplaySurface, although in practice only 1 or 2 frames
163   // will be pending at a time.
164   static constexpr int kSurfaceBufferMaxCount = 4;
165   static constexpr int kSurfaceViewMaxCount = 4;
166   static constexpr int kMaxPostedBuffers =
167       kSurfaceBufferMaxCount * kSurfaceViewMaxCount;
168 
169   // Returns whether a frame is available without locking the mutex.
170   bool IsFrameAvailableNoLock() const;
171 
172   // Dequeue all available buffers from the consumer queue.
173   void DequeueBuffersLocked();
174 
175   // In a triple-buffered surface, up to kMaxPostedBuffers buffers may be
176   // posted and pending.
177   RingBuffer<AcquiredBuffer> acquired_buffers_;
178 
179   std::shared_ptr<ConsumerQueue> direct_queue_;
180 
181   // Stores metadata when it dequeue buffers from consumer queue.
182   std::unique_ptr<uint8_t[]> metadata_;
183 };
184 
185 }  // namespace dvr
186 }  // namespace android
187 
188 #endif  // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_
189