blob: b7bcd973dd7d17c38b70d8d324acba7f6f5c2089 [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>
6#include <private/dvr/display_rpc.h>
7#include <private/dvr/ring_buffer.h>
8
9#include <functional>
10#include <iterator>
11#include <memory>
12#include <string>
13#include <vector>
14
15#include "acquired_buffer.h"
16#include "epoll_event_dispatcher.h"
17#include "surface_channel.h"
18#include "video_mesh_surface.h"
19
20namespace android {
21namespace dvr {
22
23class DisplayService;
24
25// DisplaySurface is the service-side notion of a client display context. It is
26// responsible for managing display buffer format, geometry, and state, and
27// maintains the buffer consumers connected to the client.
28class DisplaySurface : public SurfaceChannel {
29 public:
30 DisplaySurface(DisplayService* service, int surface_id, int process_id,
31 int width, int height, int format, int usage, int flags);
32 ~DisplaySurface() override;
33
34 int process_id() const { return process_id_; }
35 int width() const { return width_; }
36 int height() const { return height_; }
37 int format() const { return format_; }
38 int usage() const { return usage_; }
39 int flags() const { return flags_; }
40
41 bool client_visible() const { return client_visible_; }
42 int client_z_order() const { return client_z_order_; }
43 bool client_exclude_from_blur() const { return client_exclude_from_blur_; }
44 bool client_blur_behind() const { return client_blur_behind_; }
45
46 bool manager_visible() const { return manager_visible_; }
47 int manager_z_order() const { return manager_z_order_; }
48 float manager_blur() const { return manager_blur_; }
49
50 bool video_mesh_surfaces_updated() const {
51 return video_mesh_surfaces_updated_;
52 }
53
54 volatile const DisplaySurfaceMetadata* GetMetadataBufferPtr() {
55 if (EnsureMetadataBuffer()) {
56 void* addr = nullptr;
57 metadata_buffer_->GetBlobReadWritePointer(metadata_size(), &addr);
58 return static_cast<const volatile DisplaySurfaceMetadata*>(addr);
59 } else {
60 return nullptr;
61 }
62 }
63
64 uint32_t GetRenderBufferIndex(int buffer_id) {
65 return buffer_id_to_index_[buffer_id];
66 }
67
68 size_t GetBufferCount() const;
69 std::vector<std::shared_ptr<BufferConsumer>> GetBuffers();
70
71 // Gets a new set of consumers for all of the surface's buffers. These
72 // consumers are independent from the consumers maintained internally to the
73 // surface and may be passed to other processes over IPC.
74 int GetConsumers(std::vector<pdx::LocalChannelHandle>* consumers);
75
76 template <class A>
77 void ForEachBuffer(A action) {
78 std::lock_guard<std::mutex> autolock(lock_);
79 std::for_each(buffers_.begin(), buffers_.end(), action);
80 }
81
82 bool IsBufferAvailable() const;
83 bool IsBufferPosted() const;
84 AcquiredBuffer AcquireCurrentBuffer();
85
86 // Get the newest buffer. Up to one buffer will be skipped. If a buffer is
87 // skipped, it will be stored in skipped_buffer if non null.
88 AcquiredBuffer AcquireNewestAvailableBuffer(AcquiredBuffer* skipped_buffer);
89
90 // Display manager interface to control visibility and z order.
91 void ManagerSetVisible(bool visible);
92 void ManagerSetZOrder(int z_order);
93 void ManagerSetBlur(float blur);
94
95 // A surface must be set visible by both the client and the display manager to
96 // be visible on screen.
97 bool IsVisible() const { return client_visible_ && manager_visible_; }
98
99 // A surface is blurred if the display manager requests it.
100 bool IsBlurred() const { return manager_blur_ > 0.0f; }
101
102 // Set by HardwareComposer to the current logical layer order of this surface.
103 void SetLayerOrder(int layer_order) { layer_order_ = layer_order; }
104 // Gets the unique z-order index of this surface among other visible surfaces.
105 // This is not the same as the hardware layer index, as not all display
106 // surfaces map directly to hardware layers. Lower layer orders should be
107 // composited underneath higher layer orders.
108 int layer_order() const { return layer_order_; }
109
110 // Lock all video mesh surfaces so that VideoMeshCompositor can access them.
111 std::vector<std::shared_ptr<VideoMeshSurface>> GetVideoMeshSurfaces();
112
113 private:
114 friend class DisplayService;
115
116 // The capacity of the pending buffer queue. Should be enough to hold all the
117 // buffers of this DisplaySurface, although in practice only 1 or 2 frames
118 // will be pending at a time.
119 static constexpr int kMaxPostedBuffers =
120 kSurfaceBufferMaxCount * kSurfaceViewMaxCount;
121
122 // Returns whether a frame is available without locking the mutex.
123 bool IsFrameAvailableNoLock() const;
124
125 // Handles epoll events for BufferHub consumers. Events are mainly generated
126 // by producers posting buffers ready for display. This handler runs on the
127 // epoll event thread.
128 void HandleConsumerEvents(const std::shared_ptr<BufferConsumer>& consumer,
129 int events);
130
131 // Dispatches display surface messages to the appropriate handlers. This
132 // handler runs on the displayd message dispatch thread.
133 int HandleMessage(pdx::Message& message) override;
134
135 // Sets display surface's client-controlled attributes.
136 int OnClientSetAttributes(pdx::Message& message,
137 const DisplaySurfaceAttributes& attributes);
138
139 // Allocates a buffer with the display surface geometry and settings and
140 // returns it to the client.
141 std::pair<uint32_t, pdx::LocalChannelHandle> OnAllocateBuffer(
142 pdx::Message& message);
143
144 // Creates a video mesh surface associated with this surface and returns it
145 // to the client.
146 pdx::RemoteChannelHandle OnCreateVideoMeshSurface(pdx::Message& message);
147
148 // Sets the current buffer for the display surface, discarding the previous
149 // buffer if it is not already claimed. Runs on the epoll event thread.
150 void OnPostConsumer(const std::shared_ptr<BufferConsumer>& consumer);
151
152 // Client interface (called through IPC) to set visibility and z order.
153 void ClientSetVisible(bool visible);
154 void ClientSetZOrder(int z_order);
155 void ClientSetExcludeFromBlur(bool exclude_from_blur);
156 void ClientSetBlurBehind(bool blur_behind);
157
158 // Runs on the displayd message dispatch thread.
159 int AddConsumer(const std::shared_ptr<BufferConsumer>& consumer);
160
161 // Runs on the epoll event thread.
162 void RemoveConsumer(const std::shared_ptr<BufferConsumer>& consumer);
163
164 // Runs on the epoll and display post thread.
165 void RemoveConsumerUnlocked(const std::shared_ptr<BufferConsumer>& consumer);
166
167 DisplaySurface(const DisplaySurface&) = delete;
168 void operator=(const DisplaySurface&) = delete;
169
170 int process_id_;
171
172 // Synchronizes access to mutable state below between message dispatch thread,
173 // epoll event thread, and frame post thread.
174 mutable std::mutex lock_;
175 std::unordered_map<int, std::shared_ptr<BufferConsumer>> buffers_;
176
177 // In a triple-buffered surface, up to kMaxPostedBuffers buffers may be
178 // posted and pending.
179 RingBuffer<AcquiredBuffer> posted_buffers_;
180
181 // Provides access to VideoMeshSurface. Here we don't want to increase
182 // the reference count immediately on allocation, will leave it into
183 // compositor's hand.
184 std::vector<std::weak_ptr<VideoMeshSurface>> pending_video_mesh_surfaces_;
185 volatile bool video_mesh_surfaces_updated_;
186
187 // Surface parameters.
188 int width_;
189 int height_;
190 int format_;
191 int usage_;
192 int flags_;
193 bool client_visible_;
194 int client_z_order_;
195 bool client_exclude_from_blur_;
196 bool client_blur_behind_;
197 bool manager_visible_;
198 int manager_z_order_;
199 float manager_blur_;
200 // The monotonically increasing index for allocated buffers in this surface.
201 uint32_t allocated_buffer_index_;
202 int layer_order_;
203
204 // Maps from the buffer id to the corresponding allocated buffer index.
205 std::unordered_map<int, uint32_t> buffer_id_to_index_;
206};
207
208} // namespace dvr
209} // namespace android
210
211#endif // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_