blob: d31a3a9288f8027c7df50e3f171fb27cce474f67 [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
Jiwen 'Steve' Cai14553422017-03-21 15:43:31 -070063 uint32_t GetRenderBufferIndex(int buffer_id);
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -080064 bool IsBufferAvailable();
65 bool IsBufferPosted();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080066 AcquiredBuffer AcquireCurrentBuffer();
67
68 // Get the newest buffer. Up to one buffer will be skipped. If a buffer is
69 // skipped, it will be stored in skipped_buffer if non null.
70 AcquiredBuffer AcquireNewestAvailableBuffer(AcquiredBuffer* skipped_buffer);
71
72 // Display manager interface to control visibility and z order.
73 void ManagerSetVisible(bool visible);
74 void ManagerSetZOrder(int z_order);
75 void ManagerSetBlur(float blur);
76
77 // A surface must be set visible by both the client and the display manager to
78 // be visible on screen.
79 bool IsVisible() const { return client_visible_ && manager_visible_; }
80
81 // A surface is blurred if the display manager requests it.
82 bool IsBlurred() const { return manager_blur_ > 0.0f; }
83
84 // Set by HardwareComposer to the current logical layer order of this surface.
85 void SetLayerOrder(int layer_order) { layer_order_ = layer_order; }
86 // Gets the unique z-order index of this surface among other visible surfaces.
87 // This is not the same as the hardware layer index, as not all display
88 // surfaces map directly to hardware layers. Lower layer orders should be
89 // composited underneath higher layer orders.
90 int layer_order() const { return layer_order_; }
91
92 // Lock all video mesh surfaces so that VideoMeshCompositor can access them.
93 std::vector<std::shared_ptr<VideoMeshSurface>> GetVideoMeshSurfaces();
94
95 private:
96 friend class DisplayService;
97
98 // The capacity of the pending buffer queue. Should be enough to hold all the
99 // buffers of this DisplaySurface, although in practice only 1 or 2 frames
100 // will be pending at a time.
101 static constexpr int kMaxPostedBuffers =
102 kSurfaceBufferMaxCount * kSurfaceViewMaxCount;
103
104 // Returns whether a frame is available without locking the mutex.
105 bool IsFrameAvailableNoLock() const;
106
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800107 // Dispatches display surface messages to the appropriate handlers. This
108 // handler runs on the displayd message dispatch thread.
109 int HandleMessage(pdx::Message& message) override;
110
111 // Sets display surface's client-controlled attributes.
112 int OnClientSetAttributes(pdx::Message& message,
113 const DisplaySurfaceAttributes& attributes);
114
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800115 // Creates a BufferHubQueue associated with this surface and returns the PDX
116 // handle of its producer side to the client.
117 pdx::LocalChannelHandle OnCreateBufferQueue(pdx::Message& message);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800118
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800119 // Creates a video mesh surface associated with this surface and returns its
120 // PDX handle to the client.
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800121 pdx::RemoteChannelHandle OnCreateVideoMeshSurface(pdx::Message& message);
122
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800123 // Client interface (called through IPC) to set visibility and z order.
124 void ClientSetVisible(bool visible);
125 void ClientSetZOrder(int z_order);
126 void ClientSetExcludeFromBlur(bool exclude_from_blur);
127 void ClientSetBlurBehind(bool blur_behind);
128
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800129 // Dequeue all available buffers from the consumer queue.
130 void DequeueBuffersLocked();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800131
132 DisplaySurface(const DisplaySurface&) = delete;
133 void operator=(const DisplaySurface&) = delete;
134
135 int process_id_;
136
137 // Synchronizes access to mutable state below between message dispatch thread,
138 // epoll event thread, and frame post thread.
139 mutable std::mutex lock_;
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800140
141 // The consumer end of a BufferHubQueue. VrFlinger allocates and controls the
142 // buffer queue and pass producer end to the app and the consumer end to
143 // compositor.
144 // TODO(jwcai) Add support for multiple buffer queues per display surface.
145 std::shared_ptr<ConsumerQueue> consumer_queue_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800146
147 // In a triple-buffered surface, up to kMaxPostedBuffers buffers may be
148 // posted and pending.
Jiwen 'Steve' Caia3613612017-03-08 17:41:48 -0800149 RingBuffer<AcquiredBuffer> acquired_buffers_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800150
151 // Provides access to VideoMeshSurface. Here we don't want to increase
152 // the reference count immediately on allocation, will leave it into
153 // compositor's hand.
154 std::vector<std::weak_ptr<VideoMeshSurface>> pending_video_mesh_surfaces_;
155 volatile bool video_mesh_surfaces_updated_;
156
157 // Surface parameters.
158 int width_;
159 int height_;
160 int format_;
161 int usage_;
162 int flags_;
163 bool client_visible_;
164 int client_z_order_;
165 bool client_exclude_from_blur_;
166 bool client_blur_behind_;
167 bool manager_visible_;
168 int manager_z_order_;
169 float manager_blur_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800170 int layer_order_;
171
Jiwen 'Steve' Cai14553422017-03-21 15:43:31 -0700172 // The monotonically increasing index for allocated buffers in this surface.
173 uint32_t allocated_buffer_index_;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800174 // Maps from the buffer id to the corresponding allocated buffer index.
175 std::unordered_map<int, uint32_t> buffer_id_to_index_;
176};
177
178} // namespace dvr
179} // namespace android
180
181#endif // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_