blob: feb173ec9fc9d2e1b7c9867fb846897eebc58926 [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"
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080016#include "surface_channel.h"
17#include "video_mesh_surface.h"
18
19namespace android {
20namespace dvr {
21
22class DisplayService;
23
24// DisplaySurface is the service-side notion of a client display context. It is
25// responsible for managing display buffer format, geometry, and state, and
26// maintains the buffer consumers connected to the client.
27class DisplaySurface : public SurfaceChannel {
28 public:
29 DisplaySurface(DisplayService* service, int surface_id, int process_id,
30 int width, int height, int format, int usage, int flags);
31 ~DisplaySurface() override;
32
33 int process_id() const { return process_id_; }
34 int width() const { return width_; }
35 int height() const { return height_; }
36 int format() const { return format_; }
37 int usage() const { return usage_; }
38 int flags() const { return flags_; }
39
40 bool client_visible() const { return client_visible_; }
41 int client_z_order() const { return client_z_order_; }
42 bool client_exclude_from_blur() const { return client_exclude_from_blur_; }
43 bool client_blur_behind() const { return client_blur_behind_; }
44
45 bool manager_visible() const { return manager_visible_; }
46 int manager_z_order() const { return manager_z_order_; }
47 float manager_blur() const { return manager_blur_; }
48
49 bool video_mesh_surfaces_updated() const {
50 return video_mesh_surfaces_updated_;
51 }
52
53 volatile const DisplaySurfaceMetadata* GetMetadataBufferPtr() {
54 if (EnsureMetadataBuffer()) {
55 void* addr = nullptr;
56 metadata_buffer_->GetBlobReadWritePointer(metadata_size(), &addr);
57 return static_cast<const volatile DisplaySurfaceMetadata*>(addr);
58 } else {
59 return nullptr;
60 }
61 }
62
63 uint32_t GetRenderBufferIndex(int buffer_id) {
64 return buffer_id_to_index_[buffer_id];
65 }
66
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -080067 bool IsBufferAvailable();
68 bool IsBufferPosted();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080069 AcquiredBuffer AcquireCurrentBuffer();
70
71 // Get the newest buffer. Up to one buffer will be skipped. If a buffer is
72 // skipped, it will be stored in skipped_buffer if non null.
73 AcquiredBuffer AcquireNewestAvailableBuffer(AcquiredBuffer* skipped_buffer);
74
75 // Display manager interface to control visibility and z order.
76 void ManagerSetVisible(bool visible);
77 void ManagerSetZOrder(int z_order);
78 void ManagerSetBlur(float blur);
79
80 // A surface must be set visible by both the client and the display manager to
81 // be visible on screen.
82 bool IsVisible() const { return client_visible_ && manager_visible_; }
83
84 // A surface is blurred if the display manager requests it.
85 bool IsBlurred() const { return manager_blur_ > 0.0f; }
86
87 // Set by HardwareComposer to the current logical layer order of this surface.
88 void SetLayerOrder(int layer_order) { layer_order_ = layer_order; }
89 // Gets the unique z-order index of this surface among other visible surfaces.
90 // This is not the same as the hardware layer index, as not all display
91 // surfaces map directly to hardware layers. Lower layer orders should be
92 // composited underneath higher layer orders.
93 int layer_order() const { return layer_order_; }
94
95 // Lock all video mesh surfaces so that VideoMeshCompositor can access them.
96 std::vector<std::shared_ptr<VideoMeshSurface>> GetVideoMeshSurfaces();
97
98 private:
99 friend class DisplayService;
100
101 // The capacity of the pending buffer queue. Should be enough to hold all the
102 // buffers of this DisplaySurface, although in practice only 1 or 2 frames
103 // will be pending at a time.
104 static constexpr int kMaxPostedBuffers =
105 kSurfaceBufferMaxCount * kSurfaceViewMaxCount;
106
107 // Returns whether a frame is available without locking the mutex.
108 bool IsFrameAvailableNoLock() const;
109
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800110 // Dispatches display surface messages to the appropriate handlers. This
111 // handler runs on the displayd message dispatch thread.
112 int HandleMessage(pdx::Message& message) override;
113
114 // Sets display surface's client-controlled attributes.
115 int OnClientSetAttributes(pdx::Message& message,
116 const DisplaySurfaceAttributes& attributes);
117
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800118 // Creates a BufferHubQueue associated with this surface and returns the PDX
119 // handle of its producer side to the client.
120 pdx::LocalChannelHandle OnCreateBufferQueue(pdx::Message& message);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800121
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800122 // Creates a video mesh surface associated with this surface and returns its
123 // PDX handle to the client.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800124 pdx::RemoteChannelHandle OnCreateVideoMeshSurface(pdx::Message& message);
125
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800126 // Client interface (called through IPC) to set visibility and z order.
127 void ClientSetVisible(bool visible);
128 void ClientSetZOrder(int z_order);
129 void ClientSetExcludeFromBlur(bool exclude_from_blur);
130 void ClientSetBlurBehind(bool blur_behind);
131
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800132 // Dequeue all available buffers from the consumer queue.
133 void DequeueBuffersLocked();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800134
135 DisplaySurface(const DisplaySurface&) = delete;
136 void operator=(const DisplaySurface&) = delete;
137
138 int process_id_;
139
140 // Synchronizes access to mutable state below between message dispatch thread,
141 // epoll event thread, and frame post thread.
142 mutable std::mutex lock_;
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800143
144 // The consumer end of a BufferHubQueue. VrFlinger allocates and controls the
145 // buffer queue and pass producer end to the app and the consumer end to
146 // compositor.
147 // TODO(jwcai) Add support for multiple buffer queues per display surface.
148 std::shared_ptr<ConsumerQueue> consumer_queue_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800149
150 // In a triple-buffered surface, up to kMaxPostedBuffers buffers may be
151 // posted and pending.
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800152 RingBuffer<AcquiredBuffer> acquired_buffers_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800153
154 // Provides access to VideoMeshSurface. Here we don't want to increase
155 // the reference count immediately on allocation, will leave it into
156 // compositor's hand.
157 std::vector<std::weak_ptr<VideoMeshSurface>> pending_video_mesh_surfaces_;
158 volatile bool video_mesh_surfaces_updated_;
159
160 // Surface parameters.
161 int width_;
162 int height_;
163 int format_;
164 int usage_;
165 int flags_;
166 bool client_visible_;
167 int client_z_order_;
168 bool client_exclude_from_blur_;
169 bool client_blur_behind_;
170 bool manager_visible_;
171 int manager_z_order_;
172 float manager_blur_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800173 int layer_order_;
174
175 // Maps from the buffer id to the corresponding allocated buffer index.
176 std::unordered_map<int, uint32_t> buffer_id_to_index_;
177};
178
179} // namespace dvr
180} // namespace android
181
182#endif // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_