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