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 | |
| 4 | #include <log/log.h> |
| 5 | #include <hardware/gralloc.h> |
| 6 | #include <hardware/hardware.h> |
| 7 | #include <hardware/hwcomposer2.h> |
| 8 | |
| 9 | #include <private/dvr/buffer_hub_client.h> |
| 10 | #include <private/dvr/sync_util.h> |
| 11 | |
| 12 | #include <array> |
| 13 | #include <condition_variable> |
| 14 | #include <memory> |
| 15 | #include <mutex> |
| 16 | #include <thread> |
| 17 | #include <tuple> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include <pdx/file_handle.h> |
| 21 | #include <private/dvr/buffer_hub_client.h> |
| 22 | #include <private/dvr/frame_time_history.h> |
| 23 | #include <private/dvr/sync_util.h> |
| 24 | |
| 25 | #include "acquired_buffer.h" |
| 26 | #include "compositor.h" |
| 27 | #include "display_surface.h" |
| 28 | |
| 29 | #include "DisplayHardware/ComposerHal.h" |
| 30 | |
| 31 | // Hardware composer HAL doesn't define HWC_TRANSFORM_NONE as of this writing. |
| 32 | #ifndef HWC_TRANSFORM_NONE |
| 33 | #define HWC_TRANSFORM_NONE static_cast<hwc_transform_t>(0) |
| 34 | #endif |
| 35 | |
| 36 | namespace android { |
| 37 | namespace dvr { |
| 38 | |
| 39 | // Basic display metrics for physical displays. Dimensions and densities are |
| 40 | // relative to the physical display orientation, which may be different from the |
| 41 | // logical display orientation exposed to applications. |
| 42 | struct HWCDisplayMetrics { |
| 43 | int width; |
| 44 | int height; |
| 45 | struct { |
| 46 | int x; |
| 47 | int y; |
| 48 | } dpi; |
| 49 | int vsync_period_ns; |
| 50 | }; |
| 51 | |
| 52 | // Layer represents the connection between a hardware composer layer and the |
| 53 | // source supplying buffers for the layer's contents. |
| 54 | class Layer { |
| 55 | public: |
| 56 | Layer(); |
| 57 | |
| 58 | // Sets the hardware composer layer and display metrics that this Layer should |
| 59 | // use each Prepare cycle. This class does not own either of these pointers, |
| 60 | // which MUST remain valid for its lifetime. This method MUST be called once |
| 61 | // in the life of the instance before any other method is valid to call. |
| 62 | void Initialize(Hwc2::Composer* hwc2_hidl, HWCDisplayMetrics* metrics); |
| 63 | |
| 64 | // Releases any shared pointers and fence handles held by this instance. |
| 65 | void Reset(); |
| 66 | |
| 67 | // Sets up the layer to use a display surface as its content source. The Layer |
| 68 | // will automatically handle ACQUIRE/RELEASE phases for the surface's buffer |
| 69 | // train every frame. |
| 70 | // |
| 71 | // |blending| receives HWC_BLENDING_* values. |
| 72 | // |transform| receives HWC_TRANSFORM_* values. |
| 73 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or |
| 74 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). |
| 75 | // |index| is the index of this surface in the DisplaySurface array. |
| 76 | void Setup(const std::shared_ptr<DisplaySurface>& surface, |
| 77 | hwc2_blend_mode_t blending, hwc_transform_t transform, |
| 78 | hwc2_composition_t composition_type, size_t index); |
| 79 | |
| 80 | // Sets up the layer to use a direct buffer as its content source. No special |
| 81 | // handling of the buffer is performed; responsibility for updating or |
| 82 | // changing the buffer each frame is on the caller. |
| 83 | // |
| 84 | // |blending| receives HWC_BLENDING_* values. |
| 85 | // |transform| receives HWC_TRANSFORM_* values. |
| 86 | // |composition_type| receives either HWC_FRAMEBUFFER for most layers or |
| 87 | // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing). |
| 88 | void Setup(const std::shared_ptr<IonBuffer>& buffer, |
| 89 | hwc2_blend_mode_t blending, hwc_transform_t transform, |
| 90 | hwc2_composition_t composition_type, size_t z_order); |
| 91 | |
| 92 | // Layers that use a direct IonBuffer should call this each frame to update |
| 93 | // which buffer will be used for the next PostLayers. |
| 94 | void UpdateDirectBuffer(const std::shared_ptr<IonBuffer>& buffer); |
| 95 | |
| 96 | // Sets up the hardware composer layer for the next frame. When the layer is |
| 97 | // associated with a display surface, this method automatically ACQUIRES a new |
| 98 | // buffer if one is available. |
| 99 | void Prepare(); |
| 100 | |
| 101 | // After calling prepare, if this frame is to be dropped instead of passing |
| 102 | // along to the HWC, call Drop to close the contained fence(s). |
| 103 | void Drop(); |
| 104 | |
| 105 | // Performs fence bookkeeping after the frame has been posted to hardware |
| 106 | // composer. |
| 107 | void Finish(int release_fence_fd); |
| 108 | |
| 109 | // Sets the blending for the layer. |blending| receives HWC_BLENDING_* values. |
| 110 | void SetBlending(hwc2_blend_mode_t blending); |
| 111 | |
| 112 | // Sets the Z-order of this layer |
| 113 | void SetZOrderIndex(int surface_index); |
| 114 | |
| 115 | // Gets the current IonBuffer associated with this layer. Ownership of the |
| 116 | // buffer DOES NOT pass to the caller and the pointer is not guaranteed to |
| 117 | // remain valid across calls to Layer::Setup(), Layer::Prepare(), or |
| 118 | // Layer::Reset(). YOU HAVE BEEN WARNED. |
| 119 | IonBuffer* GetBuffer(); |
| 120 | |
| 121 | hwc2_composition_t GetCompositionType() const { return composition_type_; } |
| 122 | |
| 123 | hwc2_layer_t GetLayerHandle() const { return hardware_composer_layer_; } |
| 124 | |
| 125 | bool UsesDirectBuffer() const { return direct_buffer_ != nullptr; } |
| 126 | |
| 127 | bool IsLayerSetup() const { |
| 128 | return direct_buffer_ != nullptr || surface_ != nullptr; |
| 129 | } |
| 130 | |
| 131 | // Applies all of the settings to this layer using the hwc functions |
| 132 | void UpdateLayerSettings(); |
| 133 | |
| 134 | int GetSurfaceId() const { |
| 135 | if (surface_ != nullptr) { |
| 136 | return surface_->surface_id(); |
| 137 | } else { |
| 138 | return -1; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | void CommonLayerSetup(); |
| 144 | |
| 145 | Hwc2::Composer* hwc2_hidl_; |
| 146 | |
| 147 | // Original display surface array index for tracking purposes. |
| 148 | size_t surface_index_; |
| 149 | |
| 150 | // The hardware composer layer and metrics to use during the prepare cycle. |
| 151 | hwc2_layer_t hardware_composer_layer_; |
| 152 | HWCDisplayMetrics* display_metrics_; |
| 153 | |
| 154 | // Layer properties used to setup the hardware composer layer during the |
| 155 | // Prepare phase. |
| 156 | hwc2_blend_mode_t blending_; |
| 157 | hwc_transform_t transform_; |
| 158 | hwc2_composition_t composition_type_; |
| 159 | |
| 160 | // These two members are mutually exclusive. When direct_buffer_ is set the |
| 161 | // Layer gets its contents directly from that buffer; when surface_ is set the |
| 162 | // Layer gets it contents from the surface's buffer train. |
| 163 | std::shared_ptr<IonBuffer> direct_buffer_; |
| 164 | std::shared_ptr<DisplaySurface> surface_; |
| 165 | |
| 166 | // State when associated with a display surface. |
| 167 | AcquiredBuffer acquired_buffer_; |
| 168 | pdx::LocalHandle release_fence_; |
| 169 | |
| 170 | pdx::LocalHandle acquire_fence_fd_; |
| 171 | bool surface_rect_functions_applied_; |
| 172 | |
| 173 | Layer(const Layer&) = delete; |
| 174 | void operator=(const Layer&) = delete; |
| 175 | }; |
| 176 | |
| 177 | // HardwareComposer encapsulates the hardware composer HAL, exposing a |
| 178 | // simplified API to post buffers to the display. |
| 179 | class HardwareComposer { |
| 180 | public: |
| 181 | // Type for vsync callback. |
| 182 | using VSyncCallback = std::function<void(int, int64_t, int64_t, uint32_t)>; |
| 183 | |
| 184 | // Since there is no universal way to query the number of hardware layers, |
| 185 | // just set it to 4 for now. |
| 186 | static constexpr int kMaxHardwareLayers = 4; |
| 187 | |
| 188 | HardwareComposer(); |
| 189 | HardwareComposer(Hwc2::Composer* hidl); |
| 190 | ~HardwareComposer(); |
| 191 | |
| 192 | bool Suspend(); |
| 193 | bool Resume(); |
| 194 | bool IsSuspended() const { return pause_post_thread_; } |
| 195 | |
| 196 | // Get the HMD display metrics for the current display. |
| 197 | DisplayMetrics GetHmdDisplayMetrics() const; |
| 198 | |
| 199 | int32_t GetDisplayAttribute(hwc2_display_t display, hwc2_config_t config, |
| 200 | hwc2_attribute_t attributes, |
| 201 | int32_t* out_value) const; |
| 202 | int32_t GetDisplayMetrics(hwc2_display_t display, hwc2_config_t config, |
| 203 | HWCDisplayMetrics* out_metrics) const; |
| 204 | void Dump(char* buffer, uint32_t* out_size); |
| 205 | |
| 206 | void SetVSyncCallback(VSyncCallback callback); |
| 207 | |
| 208 | // Metrics of the logical display, which is always landscape. |
| 209 | int DisplayWidth() const { return display_metrics_.width; } |
| 210 | int DisplayHeight() const { return display_metrics_.height; } |
| 211 | HWCDisplayMetrics display_metrics() const { return display_metrics_; } |
| 212 | |
| 213 | // Metrics of the native display, which depends on the specific hardware |
| 214 | // implementation of the display. |
| 215 | HWCDisplayMetrics native_display_metrics() const { |
| 216 | return native_display_metrics_; |
| 217 | } |
| 218 | |
| 219 | std::shared_ptr<IonBuffer> framebuffer_target() const { |
| 220 | return framebuffer_target_; |
| 221 | } |
| 222 | |
| 223 | // Set the display surface stack to compose to the display each frame. |
| 224 | int SetDisplaySurfaces(std::vector<std::shared_ptr<DisplaySurface>> surfaces); |
| 225 | |
| 226 | Compositor* GetCompositor() { return &compositor_; } |
| 227 | |
| 228 | private: |
| 229 | int32_t EnableVsync(bool enabled); |
| 230 | int32_t SetPowerMode(hwc2_display_t display, hwc2_power_mode_t mode); |
| 231 | |
| 232 | class ComposerCallback : public Hwc2::IComposerCallback { |
| 233 | public: |
| 234 | ComposerCallback() {} |
| 235 | |
| 236 | hardware::Return<void> onHotplug(Hwc2::Display /*display*/, |
| 237 | Connection /*connected*/) override { |
| 238 | // TODO(skiazyk): depending on how the server is implemented, we might |
| 239 | // have to set it up to synchronize with receiving this event, as it can |
| 240 | // potentially be a critical event for setting up state within the |
| 241 | // hwc2 module. That is, we (technically) should not call any other hwc |
| 242 | // methods until this method has been called after registering the |
| 243 | // callbacks. |
| 244 | return hardware::Void(); |
| 245 | } |
| 246 | |
| 247 | hardware::Return<void> onRefresh(Hwc2::Display /*display*/) override { |
| 248 | return hardware::Void(); |
| 249 | } |
| 250 | |
| 251 | hardware::Return<void> onVsync(Hwc2::Display /*display*/, |
| 252 | int64_t /*timestamp*/) override { |
| 253 | return hardware::Void(); |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | int32_t Validate(hwc2_display_t display); |
| 258 | int32_t Present(hwc2_display_t display); |
| 259 | |
| 260 | void SetBacklightBrightness(int brightness); |
| 261 | |
| 262 | void PostLayers(bool is_geometry_changed); |
| 263 | void PostThread(); |
| 264 | |
| 265 | int ReadWaitPPState(); |
| 266 | int BlockUntilVSync(); |
| 267 | int ReadVSyncTimestamp(int64_t* timestamp); |
| 268 | int WaitForVSync(int64_t* timestamp); |
| 269 | int SleepUntil(int64_t wakeup_timestamp); |
| 270 | |
| 271 | bool IsFramePendingInDriver() { return ReadWaitPPState() == 1; } |
| 272 | |
| 273 | // Returns true if the layer config changed, false otherwise |
| 274 | bool UpdateLayerConfig( |
| 275 | std::vector<std::shared_ptr<DisplaySurface>>* compositor_surfaces); |
| 276 | void PostCompositorBuffers( |
| 277 | const std::vector<std::shared_ptr<DisplaySurface>>& compositor_surfaces); |
| 278 | |
| 279 | void UpdateDisplayState(); |
| 280 | |
| 281 | struct FrameTimeMeasurementRecord { |
| 282 | int64_t start_time; |
| 283 | pdx::LocalHandle fence; |
| 284 | |
| 285 | FrameTimeMeasurementRecord(FrameTimeMeasurementRecord&&) = default; |
| 286 | FrameTimeMeasurementRecord& operator=(FrameTimeMeasurementRecord&&) = |
| 287 | default; |
| 288 | FrameTimeMeasurementRecord(const FrameTimeMeasurementRecord&) = delete; |
| 289 | FrameTimeMeasurementRecord& operator=(const FrameTimeMeasurementRecord&) = |
| 290 | delete; |
| 291 | }; |
| 292 | |
| 293 | void UpdateFrameTimeHistory(std::vector<FrameTimeMeasurementRecord>* backlog, |
| 294 | int backlog_max, |
| 295 | FenceInfoBuffer* fence_info_buffer, |
| 296 | FrameTimeHistory* history); |
| 297 | |
| 298 | // Returns true if the frame finished rendering, false otherwise. If the frame |
| 299 | // finished the frame end time is stored in timestamp. Doesn't block. |
| 300 | bool CheckFrameFinished(int frame_fence_fd, |
| 301 | FenceInfoBuffer* fence_info_buffer, |
| 302 | int64_t* timestamp); |
| 303 | |
| 304 | void HandlePendingScreenshots(); |
| 305 | |
| 306 | void PausePostThread(); |
| 307 | |
| 308 | // Hardware composer HAL device. |
| 309 | std::unique_ptr<Hwc2::Composer> hwc2_hidl_; |
| 310 | sp<ComposerCallback> callbacks_; |
| 311 | |
| 312 | // Display metrics of the physical display. |
| 313 | HWCDisplayMetrics native_display_metrics_; |
| 314 | // Display metrics of the logical display, adjusted so that orientation is |
| 315 | // landscape. |
| 316 | HWCDisplayMetrics display_metrics_; |
| 317 | // Transform required to get from native to logical display orientation. |
| 318 | hwc_transform_t display_transform_; |
| 319 | |
| 320 | // Buffer for the background layer required by hardware composer. |
| 321 | std::shared_ptr<IonBuffer> framebuffer_target_; |
| 322 | |
| 323 | // Protects access to the display surfaces and logical layers. |
| 324 | std::mutex layer_mutex_; |
| 325 | |
| 326 | // Active display surfaces configured by the display manager. |
| 327 | std::vector<std::shared_ptr<DisplaySurface>> display_surfaces_; |
| 328 | std::vector<std::shared_ptr<DisplaySurface>> added_display_surfaces_; |
| 329 | bool display_surfaces_updated_; |
| 330 | bool hardware_layers_need_update_; |
| 331 | |
| 332 | // Cache whether the display was turned on by us |
| 333 | bool display_on_; // TODO(hendrikw): The display is always on. Revisit. |
| 334 | |
| 335 | // Layer array for handling buffer flow into hardware composer layers. |
| 336 | // Note that the first array is the actual storage for the layer objects, |
| 337 | // and the latter is an array of pointers, which can be freely re-arranged |
| 338 | // without messing up the underlying objects. |
| 339 | std::array<Layer, kMaxHardwareLayers> layer_storage_; |
| 340 | std::array<Layer*, kMaxHardwareLayers> layers_; |
| 341 | size_t active_layer_count_; |
| 342 | |
| 343 | // Set by the Post thread to the index of the GPU compositing output |
| 344 | // buffer in the layers_ array. |
| 345 | Layer* gpu_layer_; |
| 346 | |
| 347 | // Handler to hook vsync events outside of this class. |
| 348 | VSyncCallback vsync_callback_; |
| 349 | |
| 350 | // Thread and condition for managing the layer posting thread. This thread |
| 351 | // wakes up a short time before vsync to hand buffers to post processing and |
| 352 | // the results to hardware composer. |
| 353 | std::thread post_thread_; |
| 354 | |
| 355 | // Control variables to control the state of the post thread |
| 356 | pdx::LocalHandle terminate_post_thread_event_fd_; |
| 357 | bool pause_post_thread_; |
| 358 | std::mutex thread_pause_mutex_; |
| 359 | std::condition_variable thread_pause_semaphore_; |
| 360 | |
| 361 | // Backlight LED brightness sysfs node. |
| 362 | pdx::LocalHandle backlight_brightness_fd_; |
| 363 | |
| 364 | // Primary display vsync event sysfs node. |
| 365 | pdx::LocalHandle primary_display_vsync_event_fd_; |
| 366 | |
| 367 | // Primary display wait_pingpong state sysfs node. |
| 368 | pdx::LocalHandle primary_display_wait_pp_fd_; |
| 369 | |
| 370 | // VSync sleep timerfd. |
| 371 | pdx::LocalHandle vsync_sleep_timer_fd_; |
| 372 | |
| 373 | // The timestamp of the last vsync. |
| 374 | int64_t last_vsync_timestamp_; |
| 375 | |
| 376 | // Vsync count since display on. |
| 377 | uint32_t vsync_count_; |
| 378 | |
| 379 | // Counter tracking the number of skipped frames. |
| 380 | int frame_skip_count_; |
| 381 | |
| 382 | // After construction, only accessed on post_thread_. |
| 383 | Compositor compositor_; |
| 384 | |
| 385 | // Fd array for tracking retire fences that are returned by hwc. This allows |
| 386 | // us to detect when the display driver begins queuing frames. |
| 387 | std::vector<pdx::LocalHandle> retire_fence_fds_; |
| 388 | |
| 389 | // Pose client for frame count notifications. Pose client predicts poses |
| 390 | // out to display frame boundaries, so we need to tell it about vsyncs. |
| 391 | DvrPose* pose_client_; |
| 392 | |
| 393 | static void HwcRefresh(hwc2_callback_data_t data, hwc2_display_t display); |
| 394 | static void HwcVSync(hwc2_callback_data_t data, hwc2_display_t display, |
| 395 | int64_t timestamp); |
| 396 | static void HwcHotplug(hwc2_callback_data_t callbackData, |
| 397 | hwc2_display_t display, hwc2_connection_t connected); |
| 398 | |
| 399 | HardwareComposer(const HardwareComposer&) = delete; |
| 400 | void operator=(const HardwareComposer&) = delete; |
| 401 | }; |
| 402 | |
| 403 | } // namespace dvr |
| 404 | } // namespace android |
| 405 | |
| 406 | #endif // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ |