Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #ifndef ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ |
| 2 | #define ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ |
| 3 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 4 | #include <ui/GraphicBuffer.h> |
| 5 | #include "DisplayHardware/ComposerHal.h" |
| 6 | #include "hwc_types.h" |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 7 | |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 8 | #include <dvr/dvr_shared_buffers.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 9 | #include <hardware/gralloc.h> |
| 10 | #include <log/log.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 11 | |
| 12 | #include <array> |
| 13 | #include <condition_variable> |
| 14 | #include <memory> |
| 15 | #include <mutex> |
| 16 | #include <thread> |
| 17 | #include <tuple> |
| 18 | #include <vector> |
| 19 | |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 20 | #include <dvr/dvr_config.h> |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 21 | #include <dvr/dvr_vsync.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 22 | #include <pdx/file_handle.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 23 | #include <pdx/rpc/variant.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 24 | #include <private/dvr/buffer_hub_client.h> |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 25 | #include <private/dvr/shared_buffer_helpers.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 26 | |
| 27 | #include "acquired_buffer.h" |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 28 | #include "display_surface.h" |
| 29 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 30 | // Hardware composer HAL doesn't define HWC_TRANSFORM_NONE as of this writing. |
| 31 | #ifndef HWC_TRANSFORM_NONE |
| 32 | #define HWC_TRANSFORM_NONE static_cast<hwc_transform_t>(0) |
| 33 | #endif |
| 34 | |
| 35 | namespace android { |
| 36 | namespace dvr { |
| 37 | |
| 38 | // Basic display metrics for physical displays. Dimensions and densities are |
| 39 | // relative to the physical display orientation, which may be different from the |
| 40 | // logical display orientation exposed to applications. |
| 41 | struct HWCDisplayMetrics { |
| 42 | int width; |
| 43 | int height; |
| 44 | struct { |
| 45 | int x; |
| 46 | int y; |
| 47 | } dpi; |
| 48 | int vsync_period_ns; |
| 49 | }; |
| 50 | |
| 51 | // Layer represents the connection between a hardware composer layer and the |
| 52 | // source supplying buffers for the layer's contents. |
| 53 | class Layer { |
| 54 | public: |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 55 | Layer() = default; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 56 | |
| 57 | // Sets up the layer to use a display surface as its content source. The Layer |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 58 | // automatically handles ACQUIRE/RELEASE phases for the surface's buffer train |
| 59 | // every frame. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 60 | // |
| 61 | // |blending| receives HWC_BLENDING_* values. |
| 62 | // |transform| receives HWC_TRANSFORM_* values. |
| 63 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or |
| 64 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 65 | // |index| is the index of this surface in the DirectDisplaySurface array. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 66 | Layer(const std::shared_ptr<DirectDisplaySurface>& surface, |
| 67 | HWC::BlendMode blending, HWC::Transform transform, |
| 68 | HWC::Composition composition_type, size_t z_roder); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 69 | |
| 70 | // Sets up the layer to use a direct buffer as its content source. No special |
| 71 | // handling of the buffer is performed; responsibility for updating or |
| 72 | // changing the buffer each frame is on the caller. |
| 73 | // |
| 74 | // |blending| receives HWC_BLENDING_* values. |
| 75 | // |transform| receives HWC_TRANSFORM_* values. |
| 76 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or |
| 77 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 78 | Layer(const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending, |
| 79 | HWC::Transform transform, HWC::Composition composition_type, |
| 80 | size_t z_order); |
| 81 | |
| 82 | Layer(Layer&&); |
| 83 | Layer& operator=(Layer&&); |
| 84 | |
| 85 | ~Layer(); |
| 86 | |
| 87 | // Releases any shared pointers and fence handles held by this instance. |
| 88 | void Reset(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 89 | |
| 90 | // Layers that use a direct IonBuffer should call this each frame to update |
| 91 | // which buffer will be used for the next PostLayers. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 92 | void UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 93 | |
| 94 | // Sets up the hardware composer layer for the next frame. When the layer is |
| 95 | // associated with a display surface, this method automatically ACQUIRES a new |
| 96 | // buffer if one is available. |
| 97 | void Prepare(); |
| 98 | |
| 99 | // After calling prepare, if this frame is to be dropped instead of passing |
| 100 | // along to the HWC, call Drop to close the contained fence(s). |
| 101 | void Drop(); |
| 102 | |
| 103 | // Performs fence bookkeeping after the frame has been posted to hardware |
| 104 | // composer. |
| 105 | void Finish(int release_fence_fd); |
| 106 | |
| 107 | // Sets the blending for the layer. |blending| receives HWC_BLENDING_* values. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 108 | void SetBlending(HWC::BlendMode blending); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 109 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 110 | // Sets the z-order of this layer |
| 111 | void SetZOrder(size_t z_order); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 112 | |
| 113 | // Gets the current IonBuffer associated with this layer. Ownership of the |
| 114 | // buffer DOES NOT pass to the caller and the pointer is not guaranteed to |
| 115 | // remain valid across calls to Layer::Setup(), Layer::Prepare(), or |
| 116 | // Layer::Reset(). YOU HAVE BEEN WARNED. |
| 117 | IonBuffer* GetBuffer(); |
| 118 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 119 | HWC::Composition GetCompositionType() const { return composition_type_; } |
| 120 | HWC::Layer GetLayerHandle() const { return hardware_composer_layer_; } |
| 121 | bool IsLayerSetup() const { return !source_.empty(); } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 122 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 123 | int GetSurfaceId() const { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 124 | int surface_id = -1; |
| 125 | pdx::rpc::IfAnyOf<SourceSurface>::Call( |
| 126 | &source_, [&surface_id](const SourceSurface& surface_source) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 127 | surface_id = surface_source.GetSurfaceId(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 128 | }); |
| 129 | return surface_id; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 132 | int GetBufferId() const { |
| 133 | int buffer_id = -1; |
| 134 | pdx::rpc::IfAnyOf<SourceSurface>::Call( |
| 135 | &source_, [&buffer_id](const SourceSurface& surface_source) { |
| 136 | buffer_id = surface_source.GetBufferId(); |
| 137 | }); |
| 138 | return buffer_id; |
| 139 | } |
| 140 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 141 | // Compares Layers by surface id. |
| 142 | bool operator<(const Layer& other) const { |
| 143 | return GetSurfaceId() < other.GetSurfaceId(); |
| 144 | } |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 145 | bool operator<(int surface_id) const { return GetSurfaceId() < surface_id; } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 146 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 147 | // Sets the composer instance used by all Layer instances. |
| 148 | static void SetComposer(Hwc2::Composer* composer) { composer_ = composer; } |
| 149 | |
| 150 | // Sets the display metrics used by all Layer instances. |
| 151 | static void SetDisplayMetrics(HWCDisplayMetrics display_metrics) { |
| 152 | display_metrics_ = display_metrics; |
| 153 | } |
| 154 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 155 | // Sets the display id used by all Layer instances. |
| 156 | static void SetDisplayId(hwc2_display_t display_id) { |
| 157 | display_id_ = display_id; |
| 158 | } |
| 159 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 160 | private: |
| 161 | void CommonLayerSetup(); |
| 162 | |
| 163 | // Applies all of the settings to this layer using the hwc functions |
| 164 | void UpdateLayerSettings(); |
| 165 | |
| 166 | // Applies visibility settings that may have changed. |
| 167 | void UpdateVisibilitySettings(); |
| 168 | |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 169 | // Checks whether the buffer, given by id, is associated with the given slot |
| 170 | // in the HWC buffer cache. If the slot is not associated with the given |
| 171 | // buffer the cache is updated to establish the association and the buffer |
| 172 | // should be sent to HWC using setLayerBuffer. Returns true if the association |
| 173 | // was already established, false if not. A buffer_id of -1 is never |
| 174 | // associated and always returns false. |
| 175 | bool CheckAndUpdateCachedBuffer(std::size_t slot, int buffer_id); |
| 176 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 177 | // Composer instance shared by all instances of Layer. This must be set |
| 178 | // whenever a new instance of the Composer is created. This may be set to |
| 179 | // nullptr as long as there are no instances of Layer that might need to use |
| 180 | // it. |
| 181 | static Hwc2::Composer* composer_; |
| 182 | |
| 183 | // Display metrics shared by all instances of Layer. This must be set at least |
| 184 | // once during VrFlinger initialization and is expected to remain constant |
| 185 | // thereafter. |
| 186 | static HWCDisplayMetrics display_metrics_; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 187 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 188 | // Id of the primary display. Shared by all instances of Layer. This must be |
| 189 | // set whenever the primary display id changes. This can be left unset as long |
| 190 | // as there are no instances of Layer that might need to use it. |
| 191 | static hwc2_display_t display_id_; |
| 192 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 193 | // The hardware composer layer and metrics to use during the prepare cycle. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 194 | hwc2_layer_t hardware_composer_layer_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 195 | |
| 196 | // Layer properties used to setup the hardware composer layer during the |
| 197 | // Prepare phase. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 198 | size_t z_order_ = 0; |
| 199 | HWC::BlendMode blending_ = HWC::BlendMode::None; |
| 200 | HWC::Transform transform_ = HWC::Transform::None; |
| 201 | HWC::Composition composition_type_ = HWC::Composition::Invalid; |
| 202 | HWC::Composition target_composition_type_ = HWC::Composition::Device; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 203 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 204 | // State when the layer is connected to a surface. Provides the same interface |
| 205 | // as SourceBuffer to simplify internal use by Layer. |
| 206 | struct SourceSurface { |
| 207 | std::shared_ptr<DirectDisplaySurface> surface; |
| 208 | AcquiredBuffer acquired_buffer; |
| 209 | pdx::LocalHandle release_fence; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 210 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 211 | SourceSurface(const std::shared_ptr<DirectDisplaySurface>& surface) |
| 212 | : surface(surface) {} |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 213 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 214 | // Attempts to acquire a new buffer from the surface and return a tuple with |
| 215 | // width, height, buffer handle, and fence. If a new buffer is not available |
| 216 | // the previous buffer is returned or an empty value if no buffer has ever |
| 217 | // been posted. When a new buffer is acquired the previous buffer's release |
| 218 | // fence is passed out automatically. |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 219 | std::tuple<int, int, int, sp<GraphicBuffer>, pdx::LocalHandle, std::size_t> |
| 220 | Acquire() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 221 | if (surface->IsBufferAvailable()) { |
| 222 | acquired_buffer.Release(std::move(release_fence)); |
| 223 | acquired_buffer = surface->AcquireCurrentBuffer(); |
| 224 | ATRACE_ASYNC_END("BufferPost", acquired_buffer.buffer()->id()); |
| 225 | } |
| 226 | if (!acquired_buffer.IsEmpty()) { |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 227 | return std::make_tuple( |
| 228 | acquired_buffer.buffer()->width(), |
| 229 | acquired_buffer.buffer()->height(), acquired_buffer.buffer()->id(), |
| 230 | acquired_buffer.buffer()->buffer()->buffer(), |
| 231 | acquired_buffer.ClaimAcquireFence(), acquired_buffer.slot()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 232 | } else { |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 233 | return std::make_tuple(0, 0, -1, nullptr, pdx::LocalHandle{}, 0); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | void Finish(pdx::LocalHandle fence) { release_fence = std::move(fence); } |
| 238 | |
| 239 | // Gets a pointer to the current acquired buffer or returns nullptr if there |
| 240 | // isn't one. |
| 241 | IonBuffer* GetBuffer() { |
| 242 | if (acquired_buffer.IsAvailable()) |
| 243 | return acquired_buffer.buffer()->buffer(); |
| 244 | else |
| 245 | return nullptr; |
| 246 | } |
| 247 | |
| 248 | // Returns the surface id of the surface. |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 249 | int GetSurfaceId() const { return surface->surface_id(); } |
| 250 | |
| 251 | // Returns the buffer id for the current buffer. |
| 252 | int GetBufferId() const { |
| 253 | if (acquired_buffer.IsAvailable()) |
| 254 | return acquired_buffer.buffer()->id(); |
| 255 | else |
| 256 | return -1; |
| 257 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | // State when the layer is connected to a buffer. Provides the same interface |
| 261 | // as SourceSurface to simplify internal use by Layer. |
| 262 | struct SourceBuffer { |
| 263 | std::shared_ptr<IonBuffer> buffer; |
| 264 | |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 265 | std::tuple<int, int, int, sp<GraphicBuffer>, pdx::LocalHandle, std::size_t> |
| 266 | Acquire() { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 267 | if (buffer) |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 268 | return std::make_tuple(buffer->width(), buffer->height(), -1, |
| 269 | buffer->buffer(), pdx::LocalHandle{}, 0); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 270 | else |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 271 | return std::make_tuple(0, 0, -1, nullptr, pdx::LocalHandle{}, 0); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void Finish(pdx::LocalHandle /*fence*/) {} |
| 275 | |
| 276 | IonBuffer* GetBuffer() { return buffer.get(); } |
| 277 | |
| 278 | int GetSurfaceId() const { return -1; } |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 279 | int GetBufferId() const { return -1; } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | // The underlying hardware composer layer is supplied buffers either from a |
| 283 | // surface buffer train or from a buffer directly. |
| 284 | pdx::rpc::Variant<SourceSurface, SourceBuffer> source_; |
| 285 | |
| 286 | pdx::LocalHandle acquire_fence_; |
| 287 | bool surface_rect_functions_applied_ = false; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 288 | bool pending_visibility_settings_ = true; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 289 | |
Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 290 | // Map of buffer slot assignments that have already been established with HWC: |
| 291 | // slot -> buffer_id. When this map contains a matching slot and buffer_id the |
| 292 | // buffer argument to setLayerBuffer may be nullptr to avoid the cost of |
| 293 | // importing a buffer HWC already knows about. |
| 294 | std::map<std::size_t, int> cached_buffer_map_; |
| 295 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 296 | Layer(const Layer&) = delete; |
| 297 | void operator=(const Layer&) = delete; |
| 298 | }; |
| 299 | |
| 300 | // HardwareComposer encapsulates the hardware composer HAL, exposing a |
| 301 | // simplified API to post buffers to the display. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 302 | // |
| 303 | // HardwareComposer is accessed by both the vr flinger dispatcher thread and the |
| 304 | // surface flinger main thread, in addition to internally running a separate |
| 305 | // thread for compositing/EDS and posting layers to the HAL. When changing how |
| 306 | // variables are used or adding new state think carefully about which threads |
| 307 | // will access the state and whether it needs to be synchronized. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 308 | class HardwareComposer { |
| 309 | public: |
| 310 | // Type for vsync callback. |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 311 | using VSyncCallback = std::function<void(int64_t, int64_t, uint32_t)>; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 312 | using RequestDisplayCallback = std::function<void(bool)>; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 313 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 314 | HardwareComposer(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 315 | ~HardwareComposer(); |
| 316 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 317 | bool Initialize(Hwc2::Composer* composer, |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 318 | hwc2_display_t primary_display_id, |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 319 | RequestDisplayCallback request_display_callback); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 320 | |
| 321 | bool IsInitialized() const { return initialized_; } |
| 322 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 323 | // Start the post thread if there's work to do (i.e. visible layers). This |
| 324 | // should only be called from surface flinger's main thread. |
| 325 | void Enable(); |
| 326 | // Pause the post thread, blocking until the post thread has signaled that |
| 327 | // it's paused. This should only be called from surface flinger's main thread. |
| 328 | void Disable(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 329 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 330 | // Called on a binder thread. |
| 331 | void OnBootFinished(); |
| 332 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 333 | // Get the HMD display metrics for the current display. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 334 | display::Metrics GetHmdDisplayMetrics() const; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 335 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 336 | std::string Dump(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 337 | |
| 338 | void SetVSyncCallback(VSyncCallback callback); |
| 339 | |
| 340 | // Metrics of the logical display, which is always landscape. |
| 341 | int DisplayWidth() const { return display_metrics_.width; } |
| 342 | int DisplayHeight() const { return display_metrics_.height; } |
| 343 | HWCDisplayMetrics display_metrics() const { return display_metrics_; } |
| 344 | |
| 345 | // Metrics of the native display, which depends on the specific hardware |
| 346 | // implementation of the display. |
| 347 | HWCDisplayMetrics native_display_metrics() const { |
| 348 | return native_display_metrics_; |
| 349 | } |
| 350 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 351 | // Sets the display surfaces to compose the hardware layer stack. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 352 | void SetDisplaySurfaces( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 353 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 354 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 355 | int OnNewGlobalBuffer(DvrGlobalBufferKey key, IonBuffer& ion_buffer); |
| 356 | void OnDeletedGlobalBuffer(DvrGlobalBufferKey key); |
| 357 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 358 | private: |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 359 | HWC::Error GetDisplayAttribute(Hwc2::Composer* composer, |
| 360 | hwc2_display_t display, hwc2_config_t config, |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 361 | hwc2_attribute_t attributes, |
| 362 | int32_t* out_value) const; |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 363 | HWC::Error GetDisplayMetrics(Hwc2::Composer* composer, hwc2_display_t display, |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 364 | hwc2_config_t config, |
| 365 | HWCDisplayMetrics* out_metrics) const; |
| 366 | |
| 367 | HWC::Error EnableVsync(bool enabled); |
Corey Tabaka | 7024b8f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 368 | HWC::Error SetPowerMode(bool active); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 369 | |
| 370 | class ComposerCallback : public Hwc2::IComposerCallback { |
| 371 | public: |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 372 | ComposerCallback() = default; |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 373 | hardware::Return<void> onHotplug(Hwc2::Display display, |
| 374 | Connection conn) override; |
| 375 | hardware::Return<void> onRefresh(Hwc2::Display display) override; |
| 376 | hardware::Return<void> onVsync(Hwc2::Display display, |
| 377 | int64_t timestamp) override; |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 378 | |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 379 | bool HasDisplayId() { return has_display_id_; } |
| 380 | hwc2_display_t GetDisplayId() { return display_id_; } |
| 381 | pdx::Status<int64_t> GetVsyncTime(); |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 382 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 383 | private: |
| 384 | std::mutex vsync_mutex_; |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 385 | bool has_display_id_ = false; |
| 386 | hwc2_display_t display_id_; |
| 387 | pdx::LocalHandle driver_vsync_event_fd_; |
| 388 | int64_t callback_vsync_timestamp_{0}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 389 | }; |
| 390 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 391 | HWC::Error Validate(hwc2_display_t display); |
| 392 | HWC::Error Present(hwc2_display_t display); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 393 | |
| 394 | void SetBacklightBrightness(int brightness); |
| 395 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 396 | void PostLayers(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 397 | void PostThread(); |
| 398 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 399 | // The post thread has two controlling states: |
| 400 | // 1. Idle: no work to do (no visible surfaces). |
| 401 | // 2. Suspended: explicitly halted (system is not in VR mode). |
| 402 | // When either #1 or #2 is true then the post thread is quiescent, otherwise |
| 403 | // it is active. |
| 404 | using PostThreadStateType = uint32_t; |
| 405 | struct PostThreadState { |
| 406 | enum : PostThreadStateType { |
| 407 | Active = 0, |
| 408 | Idle = (1 << 0), |
| 409 | Suspended = (1 << 1), |
| 410 | Quit = (1 << 2), |
| 411 | }; |
| 412 | }; |
| 413 | |
| 414 | void UpdatePostThreadState(uint32_t state, bool suspend); |
| 415 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 416 | // Blocks until either event_fd becomes readable, or we're interrupted by a |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 417 | // control thread, or timeout_ms is reached before any events occur. Any |
| 418 | // errors are returned as negative errno values, with -ETIMEDOUT returned in |
| 419 | // the case of a timeout. If we're interrupted, kPostThreadInterrupted will be |
| 420 | // returned. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 421 | int PostThreadPollInterruptible(const pdx::LocalHandle& event_fd, |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 422 | int requested_events, int timeout_ms); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 423 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 424 | // WaitForVSync and SleepUntil are blocking calls made on the post thread that |
| 425 | // can be interrupted by a control thread. If interrupted, these calls return |
| 426 | // kPostThreadInterrupted. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 427 | int ReadWaitPPState(); |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 428 | pdx::Status<int64_t> WaitForVSync(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 429 | int SleepUntil(int64_t wakeup_timestamp); |
| 430 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 431 | // Reconfigures the layer stack if the display surfaces changed since the last |
| 432 | // frame. Called only from the post thread. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 433 | bool UpdateLayerConfig(); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 434 | |
| 435 | // Called on the post thread when the post thread is resumed. |
| 436 | void OnPostThreadResumed(); |
| 437 | // Called on the post thread when the post thread is paused or quits. |
| 438 | void OnPostThreadPaused(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 439 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 440 | // Use post_thread_wait_ to wait for a specific condition, specified by pred. |
| 441 | // timeout_sec < 0 means wait indefinitely, otherwise it specifies the timeout |
| 442 | // in seconds. |
| 443 | // The lock must be held when this function is called. |
| 444 | // Returns true if the wait was interrupted because the post thread was asked |
| 445 | // to quit. |
| 446 | bool PostThreadCondWait(std::unique_lock<std::mutex>& lock, |
| 447 | int timeout_sec, |
| 448 | const std::function<bool()>& pred); |
| 449 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 450 | // Map the given shared memory buffer to our broadcast ring to track updates |
| 451 | // to the config parameters. |
| 452 | int MapConfigBuffer(IonBuffer& ion_buffer); |
| 453 | void ConfigBufferDeleted(); |
| 454 | // Poll for config udpates. |
| 455 | void UpdateConfigBuffer(); |
| 456 | |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 457 | bool initialized_; |
Corey Tabaka | 7024b8f | 2017-08-22 11:59:15 -0700 | [diff] [blame] | 458 | bool is_standalone_device_; |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 459 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 460 | std::unique_ptr<Hwc2::Composer> composer_; |
| 461 | sp<ComposerCallback> composer_callback_; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 462 | RequestDisplayCallback request_display_callback_; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 463 | |
| 464 | // Display metrics of the physical display. |
| 465 | HWCDisplayMetrics native_display_metrics_; |
| 466 | // Display metrics of the logical display, adjusted so that orientation is |
| 467 | // landscape. |
| 468 | HWCDisplayMetrics display_metrics_; |
| 469 | // Transform required to get from native to logical display orientation. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 470 | HWC::Transform display_transform_ = HWC::Transform::None; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 471 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 472 | // Pending surface list. Set by the display service when DirectSurfaces are |
| 473 | // added, removed, or change visibility. Written by the message dispatch |
| 474 | // thread and read by the post thread. |
| 475 | std::vector<std::shared_ptr<DirectDisplaySurface>> pending_surfaces_; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 476 | |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 477 | // Layer set for handling buffer flow into hardware composer layers. This |
| 478 | // vector must be sorted by surface_id in ascending order. |
| 479 | std::vector<Layer> layers_; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 480 | |
| 481 | // Handler to hook vsync events outside of this class. |
| 482 | VSyncCallback vsync_callback_; |
| 483 | |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 484 | // The layer posting thread. This thread wakes up a short time before vsync to |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 485 | // hand buffers to hardware composer. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 486 | std::thread post_thread_; |
| 487 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 488 | // Post thread state machine and synchronization primitives. |
Corey Tabaka | 2c4aea3 | 2017-08-31 20:01:15 -0700 | [diff] [blame] | 489 | PostThreadStateType post_thread_state_{PostThreadState::Idle | |
| 490 | PostThreadState::Suspended}; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 491 | std::atomic<bool> post_thread_quiescent_{true}; |
| 492 | bool post_thread_resumed_{false}; |
| 493 | pdx::LocalHandle post_thread_event_fd_; |
| 494 | std::mutex post_thread_mutex_; |
| 495 | std::condition_variable post_thread_wait_; |
| 496 | std::condition_variable post_thread_ready_; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 497 | |
Steven Thomas | af33627 | 2018-01-04 17:36:47 -0800 | [diff] [blame^] | 498 | // When boot is finished this will be set to true and the post thread will be |
| 499 | // notified via post_thread_wait_. |
| 500 | bool boot_finished_ = false; |
| 501 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 502 | // Backlight LED brightness sysfs node. |
| 503 | pdx::LocalHandle backlight_brightness_fd_; |
| 504 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 505 | // VSync sleep timerfd. |
| 506 | pdx::LocalHandle vsync_sleep_timer_fd_; |
| 507 | |
| 508 | // The timestamp of the last vsync. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 509 | int64_t last_vsync_timestamp_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 510 | |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 511 | // The number of vsync intervals to predict since the last vsync. |
| 512 | int vsync_prediction_interval_ = 1; |
| 513 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 514 | // Vsync count since display on. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 515 | uint32_t vsync_count_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 516 | |
| 517 | // Counter tracking the number of skipped frames. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 518 | int frame_skip_count_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 519 | |
| 520 | // Fd array for tracking retire fences that are returned by hwc. This allows |
| 521 | // us to detect when the display driver begins queuing frames. |
| 522 | std::vector<pdx::LocalHandle> retire_fence_fds_; |
| 523 | |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 524 | // If we are publishing vsync data, we will put it here. |
| 525 | std::unique_ptr<CPUMappedBroadcastRing<DvrVsyncRing>> vsync_ring_; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 526 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 527 | // Broadcast ring for receiving config data from the DisplayManager. |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 528 | DvrConfigRing shared_config_ring_; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 529 | uint32_t shared_config_ring_sequence_{0}; |
| 530 | // Config buffer for reading from the post thread. |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 531 | DvrConfig post_thread_config_; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 532 | std::mutex shared_config_mutex_; |
| 533 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 534 | static constexpr int kPostThreadInterrupted = 1; |
| 535 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 536 | HardwareComposer(const HardwareComposer&) = delete; |
| 537 | void operator=(const HardwareComposer&) = delete; |
| 538 | }; |
| 539 | |
| 540 | } // namespace dvr |
| 541 | } // namespace android |
| 542 | |
| 543 | #endif // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ |