blob: fa34057191bc7837281fb0b8d6baf05e5acaadf1 [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
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080068 // Gets a new set of consumers for all of the surface's buffers. These
69 // consumers are independent from the consumers maintained internally to the
70 // surface and may be passed to other processes over IPC.
71 int GetConsumers(std::vector<pdx::LocalChannelHandle>* consumers);
72
73 template <class A>
74 void ForEachBuffer(A action) {
75 std::lock_guard<std::mutex> autolock(lock_);
76 std::for_each(buffers_.begin(), buffers_.end(), action);
77 }
78
79 bool IsBufferAvailable() const;
80 bool IsBufferPosted() const;
81 AcquiredBuffer AcquireCurrentBuffer();
82
83 // Get the newest buffer. Up to one buffer will be skipped. If a buffer is
84 // skipped, it will be stored in skipped_buffer if non null.
85 AcquiredBuffer AcquireNewestAvailableBuffer(AcquiredBuffer* skipped_buffer);
86
87 // Display manager interface to control visibility and z order.
88 void ManagerSetVisible(bool visible);
89 void ManagerSetZOrder(int z_order);
90 void ManagerSetBlur(float blur);
91
92 // A surface must be set visible by both the client and the display manager to
93 // be visible on screen.
94 bool IsVisible() const { return client_visible_ && manager_visible_; }
95
96 // A surface is blurred if the display manager requests it.
97 bool IsBlurred() const { return manager_blur_ > 0.0f; }
98
99 // Set by HardwareComposer to the current logical layer order of this surface.
100 void SetLayerOrder(int layer_order) { layer_order_ = layer_order; }
101 // Gets the unique z-order index of this surface among other visible surfaces.
102 // This is not the same as the hardware layer index, as not all display
103 // surfaces map directly to hardware layers. Lower layer orders should be
104 // composited underneath higher layer orders.
105 int layer_order() const { return layer_order_; }
106
107 // Lock all video mesh surfaces so that VideoMeshCompositor can access them.
108 std::vector<std::shared_ptr<VideoMeshSurface>> GetVideoMeshSurfaces();
109
110 private:
111 friend class DisplayService;
112
113 // The capacity of the pending buffer queue. Should be enough to hold all the
114 // buffers of this DisplaySurface, although in practice only 1 or 2 frames
115 // will be pending at a time.
116 static constexpr int kMaxPostedBuffers =
117 kSurfaceBufferMaxCount * kSurfaceViewMaxCount;
118
119 // Returns whether a frame is available without locking the mutex.
120 bool IsFrameAvailableNoLock() const;
121
122 // Handles epoll events for BufferHub consumers. Events are mainly generated
123 // by producers posting buffers ready for display. This handler runs on the
124 // epoll event thread.
125 void HandleConsumerEvents(const std::shared_ptr<BufferConsumer>& consumer,
126 int events);
127
128 // Dispatches display surface messages to the appropriate handlers. This
129 // handler runs on the displayd message dispatch thread.
130 int HandleMessage(pdx::Message& message) override;
131
132 // Sets display surface's client-controlled attributes.
133 int OnClientSetAttributes(pdx::Message& message,
134 const DisplaySurfaceAttributes& attributes);
135
136 // Allocates a buffer with the display surface geometry and settings and
137 // returns it to the client.
138 std::pair<uint32_t, pdx::LocalChannelHandle> OnAllocateBuffer(
139 pdx::Message& message);
140
141 // Creates a video mesh surface associated with this surface and returns it
142 // to the client.
143 pdx::RemoteChannelHandle OnCreateVideoMeshSurface(pdx::Message& message);
144
145 // Sets the current buffer for the display surface, discarding the previous
146 // buffer if it is not already claimed. Runs on the epoll event thread.
147 void OnPostConsumer(const std::shared_ptr<BufferConsumer>& consumer);
148
149 // Client interface (called through IPC) to set visibility and z order.
150 void ClientSetVisible(bool visible);
151 void ClientSetZOrder(int z_order);
152 void ClientSetExcludeFromBlur(bool exclude_from_blur);
153 void ClientSetBlurBehind(bool blur_behind);
154
155 // Runs on the displayd message dispatch thread.
156 int AddConsumer(const std::shared_ptr<BufferConsumer>& consumer);
157
158 // Runs on the epoll event thread.
159 void RemoveConsumer(const std::shared_ptr<BufferConsumer>& consumer);
160
161 // Runs on the epoll and display post thread.
162 void RemoveConsumerUnlocked(const std::shared_ptr<BufferConsumer>& consumer);
163
164 DisplaySurface(const DisplaySurface&) = delete;
165 void operator=(const DisplaySurface&) = delete;
166
167 int process_id_;
168
169 // Synchronizes access to mutable state below between message dispatch thread,
170 // epoll event thread, and frame post thread.
171 mutable std::mutex lock_;
172 std::unordered_map<int, std::shared_ptr<BufferConsumer>> buffers_;
173
174 // In a triple-buffered surface, up to kMaxPostedBuffers buffers may be
175 // posted and pending.
176 RingBuffer<AcquiredBuffer> posted_buffers_;
177
178 // Provides access to VideoMeshSurface. Here we don't want to increase
179 // the reference count immediately on allocation, will leave it into
180 // compositor's hand.
181 std::vector<std::weak_ptr<VideoMeshSurface>> pending_video_mesh_surfaces_;
182 volatile bool video_mesh_surfaces_updated_;
183
184 // Surface parameters.
185 int width_;
186 int height_;
187 int format_;
188 int usage_;
189 int flags_;
190 bool client_visible_;
191 int client_z_order_;
192 bool client_exclude_from_blur_;
193 bool client_blur_behind_;
194 bool manager_visible_;
195 int manager_z_order_;
196 float manager_blur_;
197 // The monotonically increasing index for allocated buffers in this surface.
198 uint32_t allocated_buffer_index_;
199 int layer_order_;
200
201 // Maps from the buffer id to the corresponding allocated buffer index.
202 std::unordered_map<int, uint32_t> buffer_id_to_index_;
203};
204
205} // namespace dvr
206} // namespace android
207
208#endif // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_