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