blob: 6db3f556951f69e511c1da483a5c0249d29011f0 [file] [log] [blame]
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001#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>
Corey Tabaka2251d822017-04-20 16:04:07 -07006#include <private/dvr/buffer_hub_queue_client.h>
Corey Tabaka3f82d312017-04-20 14:42:08 -07007#include <private/dvr/display_protocol.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08008#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"
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080017
18namespace android {
19namespace dvr {
20
21class DisplayService;
22
Corey Tabaka2251d822017-04-20 16:04:07 -070023enum class SurfaceType {
24 Direct,
25 Application,
26};
27
28class DisplaySurface : public pdx::Channel {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080029 public:
Corey Tabaka2251d822017-04-20 16:04:07 -070030 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
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080034 ~DisplaySurface() override;
35
Corey Tabaka2251d822017-04-20 16:04:07 -070036 DisplayService* service() const { return service_; }
37 SurfaceType surface_type() const { return surface_type_; }
38 int surface_id() const { return surface_id_; }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080039 int process_id() const { return process_id_; }
Corey Tabaka2251d822017-04-20 16:04:07 -070040 int user_id() const { return user_id_; }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080041
Corey Tabaka2251d822017-04-20 16:04:07 -070042 bool visible() const { return visible_; }
43 int z_order() const { return z_order_; }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080044
Corey Tabaka2251d822017-04-20 16:04:07 -070045 const display::SurfaceAttributes& attributes() const { return attributes_; }
46 display::SurfaceUpdateFlags update_flags() const { return update_flags_; }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080047
Corey Tabaka2251d822017-04-20 16:04:07 -070048 virtual std::vector<int32_t> GetQueueIds() const { return {}; }
49
50 bool IsUpdatePending() const {
51 return update_flags_.value() != display::SurfaceUpdateFlags::None;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080052 }
53
Corey Tabaka2251d822017-04-20 16:04:07 -070054 protected:
55 DisplaySurface(DisplayService* service, SurfaceType surface_type,
56 int surface_id, int process_id, int user_id,
57 const display::SurfaceAttributes& attributes);
58
59 // Utility to retrieve a shared pointer to this channel as the desired derived
60 // type.
61 template <
62 typename T = DisplaySurface,
63 typename = std::enable_if_t<std::is_base_of<DisplaySurface, T>::value>>
64 std::shared_ptr<T> Self() {
65 return std::static_pointer_cast<T>(shared_from_this());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080066 }
67
Corey Tabaka2251d822017-04-20 16:04:07 -070068 virtual pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
Jiwen 'Steve' Cai656f4062017-05-22 13:02:33 -070069 pdx::Message& message, const ProducerQueueConfig& config) = 0;
Corey Tabaka2251d822017-04-20 16:04:07 -070070
71 // Registers a consumer queue with the event dispatcher in DisplayService. The
72 // OnQueueEvent callback below is called to handle queue events.
73 pdx::Status<void> RegisterQueue(
74 const std::shared_ptr<ConsumerQueue>& consumer_queue);
75 pdx::Status<void> UnregisterQueue(
76 const std::shared_ptr<ConsumerQueue>& consumer_queue);
77
78 // Called by the event dispatcher in DisplayService when a registered queue
79 // event triggers. Executes on the event dispatcher thread.
80 virtual void OnQueueEvent(
81 const std::shared_ptr<ConsumerQueue>& consumer_queue, int events);
82
83 void SurfaceUpdated(display::SurfaceUpdateFlags update_flags);
84 void ClearUpdate();
85
86 // Synchronizes access to mutable state below between message dispatch thread
87 // and frame post thread.
88 mutable std::mutex lock_;
89
90 private:
91 friend class DisplayService;
92 friend class DisplayManagerService;
93
94 // Dispatches display surface messages to the appropriate handlers. This
95 // handler runs on the VrFlinger message dispatch thread.
96 pdx::Status<void> HandleMessage(pdx::Message& message);
97
98 pdx::Status<void> OnSetAttributes(
99 pdx::Message& message, const display::SurfaceAttributes& attributes);
100 pdx::Status<display::SurfaceInfo> OnGetSurfaceInfo(pdx::Message& message);
101
102 DisplayService* service_;
103 SurfaceType surface_type_;
104 int surface_id_;
105 int process_id_;
106 int user_id_;
107
108 display::SurfaceAttributes attributes_;
109 display::SurfaceUpdateFlags update_flags_ = display::SurfaceUpdateFlags::None;
110
111 // Subset of attributes that may be interpreted by the display service.
112 bool visible_ = false;
113 int z_order_ = 0;
114
115 DisplaySurface(const DisplaySurface&) = delete;
116 void operator=(const DisplaySurface&) = delete;
117};
118
119class ApplicationDisplaySurface : public DisplaySurface {
120 public:
121 ApplicationDisplaySurface(DisplayService* service, int surface_id,
122 int process_id, int user_id,
123 const display::SurfaceAttributes& attributes)
124 : DisplaySurface(service, SurfaceType::Application, surface_id,
125 process_id, user_id, attributes) {}
126
127 std::shared_ptr<ConsumerQueue> GetQueue(int32_t queue_id);
128 std::vector<int32_t> GetQueueIds() const override;
129
130 private:
131 pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
Jiwen 'Steve' Cai656f4062017-05-22 13:02:33 -0700132 pdx::Message& message, const ProducerQueueConfig& config) override;
Corey Tabaka2251d822017-04-20 16:04:07 -0700133 void OnQueueEvent(const std::shared_ptr<ConsumerQueue>& consumer_queue,
134 int events) override;
135
136 std::unordered_map<int32_t, std::shared_ptr<ConsumerQueue>> consumer_queues_;
137};
138
139class DirectDisplaySurface : public DisplaySurface {
140 public:
141 DirectDisplaySurface(DisplayService* service, int surface_id, int process_id,
142 int user_id,
143 const display::SurfaceAttributes& attributes)
144 : DisplaySurface(service, SurfaceType::Direct, surface_id, process_id,
145 user_id, attributes),
rongliuf60f3072017-07-28 15:22:17 -0700146 acquired_buffers_(kMaxPostedBuffers),
147 metadata_(nullptr){}
Corey Tabaka0b485c92017-05-19 12:02:58 -0700148 std::vector<int32_t> GetQueueIds() const override;
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800149 bool IsBufferAvailable();
150 bool IsBufferPosted();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800151 AcquiredBuffer AcquireCurrentBuffer();
152
153 // Get the newest buffer. Up to one buffer will be skipped. If a buffer is
154 // skipped, it will be stored in skipped_buffer if non null.
155 AcquiredBuffer AcquireNewestAvailableBuffer(AcquiredBuffer* skipped_buffer);
156
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800157 private:
Corey Tabaka2251d822017-04-20 16:04:07 -0700158 pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
Jiwen 'Steve' Cai656f4062017-05-22 13:02:33 -0700159 pdx::Message& message, const ProducerQueueConfig& config) override;
Corey Tabaka2251d822017-04-20 16:04:07 -0700160 void OnQueueEvent(const std::shared_ptr<ConsumerQueue>& consumer_queue,
161 int events) override;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800162
163 // The capacity of the pending buffer queue. Should be enough to hold all the
164 // buffers of this DisplaySurface, although in practice only 1 or 2 frames
165 // will be pending at a time.
Hendrik Wagenaarcdb60532017-05-09 08:59:51 -0700166 static constexpr int kSurfaceBufferMaxCount = 4;
167 static constexpr int kSurfaceViewMaxCount = 4;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800168 static constexpr int kMaxPostedBuffers =
169 kSurfaceBufferMaxCount * kSurfaceViewMaxCount;
170
171 // Returns whether a frame is available without locking the mutex.
172 bool IsFrameAvailableNoLock() const;
173
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800174 // Dequeue all available buffers from the consumer queue.
175 void DequeueBuffersLocked();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800176
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800177 // In a triple-buffered surface, up to kMaxPostedBuffers buffers may be
178 // posted and pending.
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800179 RingBuffer<AcquiredBuffer> acquired_buffers_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800180
Corey Tabaka2251d822017-04-20 16:04:07 -0700181 std::shared_ptr<ConsumerQueue> direct_queue_;
rongliuf60f3072017-07-28 15:22:17 -0700182
183 // Stores metadata when it dequeue buffers from consumer queue.
184 std::unique_ptr<uint8_t[]> metadata_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800185};
186
187} // namespace dvr
188} // namespace android
189
190#endif // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_