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 | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 55 | Layer() {} |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 56 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 57 | // Releases any shared pointers and fence handles held by this instance. |
| 58 | void Reset(); |
| 59 | |
| 60 | // 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] | 61 | // automatically handles ACQUIRE/RELEASE phases for the surface's buffer train |
| 62 | // every frame. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 63 | // |
| 64 | // |blending| receives HWC_BLENDING_* values. |
| 65 | // |transform| receives HWC_TRANSFORM_* values. |
| 66 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or |
| 67 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 68 | // |index| is the index of this surface in the DirectDisplaySurface array. |
| 69 | void Setup(const std::shared_ptr<DirectDisplaySurface>& surface, |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 70 | const HWCDisplayMetrics& display_metrics, Hwc2::Composer* hidl, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 71 | HWC::BlendMode blending, HWC::Transform transform, |
| 72 | HWC::Composition composition_type, size_t z_roder); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 73 | |
| 74 | // Sets up the layer to use a direct buffer as its content source. No special |
| 75 | // handling of the buffer is performed; responsibility for updating or |
| 76 | // changing the buffer each frame is on the caller. |
| 77 | // |
| 78 | // |blending| receives HWC_BLENDING_* values. |
| 79 | // |transform| receives HWC_TRANSFORM_* values. |
| 80 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or |
| 81 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 82 | void Setup(const std::shared_ptr<IonBuffer>& buffer, |
| 83 | const HWCDisplayMetrics& display_metrics, Hwc2::Composer* hidl, |
| 84 | HWC::BlendMode blending, HWC::Transform transform, |
| 85 | HWC::Composition composition_type, size_t z_order); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 86 | |
| 87 | // Layers that use a direct IonBuffer should call this each frame to update |
| 88 | // which buffer will be used for the next PostLayers. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 89 | void UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 90 | |
| 91 | // Sets up the hardware composer layer for the next frame. When the layer is |
| 92 | // associated with a display surface, this method automatically ACQUIRES a new |
| 93 | // buffer if one is available. |
| 94 | void Prepare(); |
| 95 | |
| 96 | // After calling prepare, if this frame is to be dropped instead of passing |
| 97 | // along to the HWC, call Drop to close the contained fence(s). |
| 98 | void Drop(); |
| 99 | |
| 100 | // Performs fence bookkeeping after the frame has been posted to hardware |
| 101 | // composer. |
| 102 | void Finish(int release_fence_fd); |
| 103 | |
| 104 | // Sets the blending for the layer. |blending| receives HWC_BLENDING_* values. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 105 | void SetBlending(HWC::BlendMode blending); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 106 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 107 | // Sets the z-order of this layer |
| 108 | void SetZOrder(size_t z_order); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 109 | |
| 110 | // Gets the current IonBuffer associated with this layer. Ownership of the |
| 111 | // buffer DOES NOT pass to the caller and the pointer is not guaranteed to |
| 112 | // remain valid across calls to Layer::Setup(), Layer::Prepare(), or |
| 113 | // Layer::Reset(). YOU HAVE BEEN WARNED. |
| 114 | IonBuffer* GetBuffer(); |
| 115 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 116 | HWC::Composition GetCompositionType() const { return composition_type_; } |
| 117 | HWC::Layer GetLayerHandle() const { return hardware_composer_layer_; } |
| 118 | bool IsLayerSetup() const { return !source_.empty(); } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 119 | |
| 120 | // Applies all of the settings to this layer using the hwc functions |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 121 | void UpdateLayerSettings(const HWCDisplayMetrics& display_metrics); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 122 | |
| 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 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 141 | private: |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 142 | void CommonLayerSetup(const HWCDisplayMetrics& display_metrics); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 143 | |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 144 | Hwc2::Composer* hidl_ = nullptr; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 145 | |
| 146 | // The hardware composer layer and metrics to use during the prepare cycle. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 147 | hwc2_layer_t hardware_composer_layer_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 148 | |
| 149 | // Layer properties used to setup the hardware composer layer during the |
| 150 | // Prepare phase. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 151 | size_t z_order_ = 0; |
| 152 | HWC::BlendMode blending_ = HWC::BlendMode::None; |
| 153 | HWC::Transform transform_ = HWC::Transform::None; |
| 154 | HWC::Composition composition_type_ = HWC::Composition::Invalid; |
| 155 | HWC::Composition target_composition_type_ = HWC::Composition::Device; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 156 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 157 | // State when the layer is connected to a surface. Provides the same interface |
| 158 | // as SourceBuffer to simplify internal use by Layer. |
| 159 | struct SourceSurface { |
| 160 | std::shared_ptr<DirectDisplaySurface> surface; |
| 161 | AcquiredBuffer acquired_buffer; |
| 162 | pdx::LocalHandle release_fence; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 163 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 164 | SourceSurface(const std::shared_ptr<DirectDisplaySurface>& surface) |
| 165 | : surface(surface) {} |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 166 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 167 | // Attempts to acquire a new buffer from the surface and return a tuple with |
| 168 | // width, height, buffer handle, and fence. If a new buffer is not available |
| 169 | // the previous buffer is returned or an empty value if no buffer has ever |
| 170 | // been posted. When a new buffer is acquired the previous buffer's release |
| 171 | // fence is passed out automatically. |
| 172 | std::tuple<int, int, sp<GraphicBuffer>, pdx::LocalHandle> Acquire() { |
| 173 | if (surface->IsBufferAvailable()) { |
| 174 | acquired_buffer.Release(std::move(release_fence)); |
| 175 | acquired_buffer = surface->AcquireCurrentBuffer(); |
| 176 | ATRACE_ASYNC_END("BufferPost", acquired_buffer.buffer()->id()); |
| 177 | } |
| 178 | if (!acquired_buffer.IsEmpty()) { |
| 179 | return std::make_tuple(acquired_buffer.buffer()->width(), |
| 180 | acquired_buffer.buffer()->height(), |
| 181 | acquired_buffer.buffer()->buffer()->buffer(), |
| 182 | acquired_buffer.ClaimAcquireFence()); |
| 183 | } else { |
| 184 | return std::make_tuple(0, 0, nullptr, pdx::LocalHandle{}); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void Finish(pdx::LocalHandle fence) { release_fence = std::move(fence); } |
| 189 | |
| 190 | // Gets a pointer to the current acquired buffer or returns nullptr if there |
| 191 | // isn't one. |
| 192 | IonBuffer* GetBuffer() { |
| 193 | if (acquired_buffer.IsAvailable()) |
| 194 | return acquired_buffer.buffer()->buffer(); |
| 195 | else |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
| 199 | // Returns the surface id of the surface. |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 200 | int GetSurfaceId() const { return surface->surface_id(); } |
| 201 | |
| 202 | // Returns the buffer id for the current buffer. |
| 203 | int GetBufferId() const { |
| 204 | if (acquired_buffer.IsAvailable()) |
| 205 | return acquired_buffer.buffer()->id(); |
| 206 | else |
| 207 | return -1; |
| 208 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | // State when the layer is connected to a buffer. Provides the same interface |
| 212 | // as SourceSurface to simplify internal use by Layer. |
| 213 | struct SourceBuffer { |
| 214 | std::shared_ptr<IonBuffer> buffer; |
| 215 | |
| 216 | std::tuple<int, int, sp<GraphicBuffer>, pdx::LocalHandle> Acquire() { |
| 217 | if (buffer) |
| 218 | return std::make_tuple(buffer->width(), buffer->height(), |
| 219 | buffer->buffer(), pdx::LocalHandle{}); |
| 220 | else |
| 221 | return std::make_tuple(0, 0, nullptr, pdx::LocalHandle{}); |
| 222 | } |
| 223 | |
| 224 | void Finish(pdx::LocalHandle /*fence*/) {} |
| 225 | |
| 226 | IonBuffer* GetBuffer() { return buffer.get(); } |
| 227 | |
| 228 | int GetSurfaceId() const { return -1; } |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 229 | int GetBufferId() const { return -1; } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | // The underlying hardware composer layer is supplied buffers either from a |
| 233 | // surface buffer train or from a buffer directly. |
| 234 | pdx::rpc::Variant<SourceSurface, SourceBuffer> source_; |
| 235 | |
| 236 | pdx::LocalHandle acquire_fence_; |
| 237 | bool surface_rect_functions_applied_ = false; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 238 | |
| 239 | Layer(const Layer&) = delete; |
| 240 | void operator=(const Layer&) = delete; |
| 241 | }; |
| 242 | |
| 243 | // HardwareComposer encapsulates the hardware composer HAL, exposing a |
| 244 | // simplified API to post buffers to the display. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 245 | // |
| 246 | // HardwareComposer is accessed by both the vr flinger dispatcher thread and the |
| 247 | // surface flinger main thread, in addition to internally running a separate |
| 248 | // thread for compositing/EDS and posting layers to the HAL. When changing how |
| 249 | // variables are used or adding new state think carefully about which threads |
| 250 | // will access the state and whether it needs to be synchronized. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 251 | class HardwareComposer { |
| 252 | public: |
| 253 | // Type for vsync callback. |
| 254 | using VSyncCallback = std::function<void(int, int64_t, int64_t, uint32_t)>; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 255 | using RequestDisplayCallback = std::function<void(bool)>; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 256 | |
| 257 | // Since there is no universal way to query the number of hardware layers, |
| 258 | // just set it to 4 for now. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 259 | static constexpr size_t kMaxHardwareLayers = 4; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 260 | |
| 261 | HardwareComposer(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 262 | ~HardwareComposer(); |
| 263 | |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 264 | bool Initialize(Hwc2::Composer* hidl, |
| 265 | RequestDisplayCallback request_display_callback); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 266 | |
| 267 | bool IsInitialized() const { return initialized_; } |
| 268 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 269 | // Start the post thread if there's work to do (i.e. visible layers). This |
| 270 | // should only be called from surface flinger's main thread. |
| 271 | void Enable(); |
| 272 | // Pause the post thread, blocking until the post thread has signaled that |
| 273 | // it's paused. This should only be called from surface flinger's main thread. |
| 274 | void Disable(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 275 | |
| 276 | // Get the HMD display metrics for the current display. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 277 | display::Metrics GetHmdDisplayMetrics() const; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 278 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 279 | std::string Dump(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 280 | |
| 281 | void SetVSyncCallback(VSyncCallback callback); |
| 282 | |
| 283 | // Metrics of the logical display, which is always landscape. |
| 284 | int DisplayWidth() const { return display_metrics_.width; } |
| 285 | int DisplayHeight() const { return display_metrics_.height; } |
| 286 | HWCDisplayMetrics display_metrics() const { return display_metrics_; } |
| 287 | |
| 288 | // Metrics of the native display, which depends on the specific hardware |
| 289 | // implementation of the display. |
| 290 | HWCDisplayMetrics native_display_metrics() const { |
| 291 | return native_display_metrics_; |
| 292 | } |
| 293 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 294 | // Sets the display surfaces to compose the hardware layer stack. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 295 | void SetDisplaySurfaces( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 296 | std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 297 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 298 | int OnNewGlobalBuffer(DvrGlobalBufferKey key, IonBuffer& ion_buffer); |
| 299 | void OnDeletedGlobalBuffer(DvrGlobalBufferKey key); |
| 300 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 301 | private: |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 302 | HWC::Error GetDisplayAttribute(Hwc2::Composer* hidl, hwc2_display_t display, |
| 303 | hwc2_config_t config, |
| 304 | hwc2_attribute_t attributes, |
| 305 | int32_t* out_value) const; |
| 306 | HWC::Error GetDisplayMetrics(Hwc2::Composer* hidl, hwc2_display_t display, |
| 307 | hwc2_config_t config, |
| 308 | HWCDisplayMetrics* out_metrics) const; |
| 309 | |
| 310 | HWC::Error EnableVsync(bool enabled); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 311 | |
| 312 | class ComposerCallback : public Hwc2::IComposerCallback { |
| 313 | public: |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 314 | ComposerCallback(); |
| 315 | hardware::Return<void> onHotplug(Hwc2::Display display, |
| 316 | Connection conn) override; |
| 317 | hardware::Return<void> onRefresh(Hwc2::Display display) override; |
| 318 | hardware::Return<void> onVsync(Hwc2::Display display, |
| 319 | int64_t timestamp) override; |
| 320 | const pdx::LocalHandle& GetVsyncEventFd() const; |
| 321 | int64_t GetVsyncTime(); |
| 322 | private: |
| 323 | std::mutex vsync_mutex_; |
| 324 | pdx::LocalHandle vsync_event_fd_; |
| 325 | int64_t vsync_time_ = -1; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 326 | }; |
| 327 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 328 | HWC::Error Validate(hwc2_display_t display); |
| 329 | HWC::Error Present(hwc2_display_t display); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 330 | |
| 331 | void SetBacklightBrightness(int brightness); |
| 332 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 333 | void PostLayers(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 334 | void PostThread(); |
| 335 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 336 | // The post thread has two controlling states: |
| 337 | // 1. Idle: no work to do (no visible surfaces). |
| 338 | // 2. Suspended: explicitly halted (system is not in VR mode). |
| 339 | // When either #1 or #2 is true then the post thread is quiescent, otherwise |
| 340 | // it is active. |
| 341 | using PostThreadStateType = uint32_t; |
| 342 | struct PostThreadState { |
| 343 | enum : PostThreadStateType { |
| 344 | Active = 0, |
| 345 | Idle = (1 << 0), |
| 346 | Suspended = (1 << 1), |
| 347 | Quit = (1 << 2), |
| 348 | }; |
| 349 | }; |
| 350 | |
| 351 | void UpdatePostThreadState(uint32_t state, bool suspend); |
| 352 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 353 | // Blocks until either event_fd becomes readable, or we're interrupted by a |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 354 | // control thread, or timeout_ms is reached before any events occur. Any |
| 355 | // errors are returned as negative errno values, with -ETIMEDOUT returned in |
| 356 | // the case of a timeout. If we're interrupted, kPostThreadInterrupted will be |
| 357 | // returned. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 358 | int PostThreadPollInterruptible(const pdx::LocalHandle& event_fd, |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 359 | int requested_events, |
| 360 | int timeout_ms); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 361 | |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 362 | // WaitForVSync and SleepUntil are blocking calls made on the post thread that |
| 363 | // can be interrupted by a control thread. If interrupted, these calls return |
| 364 | // kPostThreadInterrupted. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 365 | int ReadWaitPPState(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 366 | int WaitForVSync(int64_t* timestamp); |
| 367 | int SleepUntil(int64_t wakeup_timestamp); |
| 368 | |
| 369 | bool IsFramePendingInDriver() { return ReadWaitPPState() == 1; } |
| 370 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 371 | // Reconfigures the layer stack if the display surfaces changed since the last |
| 372 | // frame. Called only from the post thread. |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 373 | bool UpdateLayerConfig(); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 374 | |
| 375 | // Called on the post thread when the post thread is resumed. |
| 376 | void OnPostThreadResumed(); |
| 377 | // Called on the post thread when the post thread is paused or quits. |
| 378 | void OnPostThreadPaused(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 379 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 380 | // Map the given shared memory buffer to our broadcast ring to track updates |
| 381 | // to the config parameters. |
| 382 | int MapConfigBuffer(IonBuffer& ion_buffer); |
| 383 | void ConfigBufferDeleted(); |
| 384 | // Poll for config udpates. |
| 385 | void UpdateConfigBuffer(); |
| 386 | |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 387 | bool initialized_; |
| 388 | |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 389 | std::unique_ptr<Hwc2::Composer> hidl_; |
| 390 | sp<ComposerCallback> hidl_callback_; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 391 | RequestDisplayCallback request_display_callback_; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 392 | |
| 393 | // Display metrics of the physical display. |
| 394 | HWCDisplayMetrics native_display_metrics_; |
| 395 | // Display metrics of the logical display, adjusted so that orientation is |
| 396 | // landscape. |
| 397 | HWCDisplayMetrics display_metrics_; |
| 398 | // Transform required to get from native to logical display orientation. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 399 | HWC::Transform display_transform_ = HWC::Transform::None; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 400 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 401 | // Pending surface list. Set by the display service when DirectSurfaces are |
| 402 | // added, removed, or change visibility. Written by the message dispatch |
| 403 | // thread and read by the post thread. |
| 404 | std::vector<std::shared_ptr<DirectDisplaySurface>> pending_surfaces_; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 405 | |
| 406 | // The surfaces displayed by the post thread. Used exclusively by the post |
| 407 | // thread. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 408 | std::vector<std::shared_ptr<DirectDisplaySurface>> display_surfaces_; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 409 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 410 | // Layer array for handling buffer flow into hardware composer layers. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 411 | std::array<Layer, kMaxHardwareLayers> layers_; |
| 412 | size_t active_layer_count_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 413 | |
| 414 | // Handler to hook vsync events outside of this class. |
| 415 | VSyncCallback vsync_callback_; |
| 416 | |
Steven Thomas | 282a5ed | 2017-02-07 18:07:01 -0800 | [diff] [blame] | 417 | // 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] | 418 | // hand buffers to hardware composer. |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 419 | std::thread post_thread_; |
| 420 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 421 | // Post thread state machine and synchronization primitives. |
Steven Thomas | 94e35b9 | 2017-07-26 18:48:28 -0700 | [diff] [blame^] | 422 | PostThreadStateType post_thread_state_{ |
| 423 | PostThreadState::Idle | PostThreadState::Suspended}; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 424 | std::atomic<bool> post_thread_quiescent_{true}; |
| 425 | bool post_thread_resumed_{false}; |
| 426 | pdx::LocalHandle post_thread_event_fd_; |
| 427 | std::mutex post_thread_mutex_; |
| 428 | std::condition_variable post_thread_wait_; |
| 429 | std::condition_variable post_thread_ready_; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 430 | |
| 431 | // Backlight LED brightness sysfs node. |
| 432 | pdx::LocalHandle backlight_brightness_fd_; |
| 433 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 434 | // Primary display wait_pingpong state sysfs node. |
| 435 | pdx::LocalHandle primary_display_wait_pp_fd_; |
| 436 | |
| 437 | // VSync sleep timerfd. |
| 438 | pdx::LocalHandle vsync_sleep_timer_fd_; |
| 439 | |
| 440 | // The timestamp of the last vsync. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 441 | int64_t last_vsync_timestamp_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 442 | |
| 443 | // Vsync count since display on. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 444 | uint32_t vsync_count_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 445 | |
| 446 | // Counter tracking the number of skipped frames. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 447 | int frame_skip_count_ = 0; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 448 | |
| 449 | // Fd array for tracking retire fences that are returned by hwc. This allows |
| 450 | // us to detect when the display driver begins queuing frames. |
| 451 | std::vector<pdx::LocalHandle> retire_fence_fds_; |
| 452 | |
Okan Arikan | 822b710 | 2017-05-08 13:31:34 -0700 | [diff] [blame] | 453 | // If we are publishing vsync data, we will put it here. |
| 454 | std::unique_ptr<CPUMappedBroadcastRing<DvrVsyncRing>> vsync_ring_; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 455 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 456 | // Broadcast ring for receiving config data from the DisplayManager. |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 457 | DvrConfigRing shared_config_ring_; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 458 | uint32_t shared_config_ring_sequence_{0}; |
| 459 | // Config buffer for reading from the post thread. |
Okan Arikan | 6f468c6 | 2017-05-31 14:48:30 -0700 | [diff] [blame] | 460 | DvrConfig post_thread_config_; |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 461 | std::mutex shared_config_mutex_; |
| 462 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 463 | static constexpr int kPostThreadInterrupted = 1; |
| 464 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 465 | HardwareComposer(const HardwareComposer&) = delete; |
| 466 | void operator=(const HardwareComposer&) = delete; |
| 467 | }; |
| 468 | |
| 469 | } // namespace dvr |
| 470 | } // namespace android |
| 471 | |
| 472 | #endif // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ |