hardware_composer.h revision 3cfac28462910d3f976aebac54ac7301aca7e434
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 36namespace android { 37namespace 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. 42struct 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. 54class 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. 179class 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 195 // Get the HMD display metrics for the current display. 196 DisplayMetrics GetHmdDisplayMetrics() const; 197 198 int32_t GetDisplayAttribute(hwc2_display_t display, hwc2_config_t config, 199 hwc2_attribute_t attributes, 200 int32_t* out_value) const; 201 int32_t GetDisplayMetrics(hwc2_display_t display, hwc2_config_t config, 202 HWCDisplayMetrics* out_metrics) const; 203 void Dump(char* buffer, uint32_t* out_size); 204 205 void SetVSyncCallback(VSyncCallback callback); 206 207 // Metrics of the logical display, which is always landscape. 208 int DisplayWidth() const { return display_metrics_.width; } 209 int DisplayHeight() const { return display_metrics_.height; } 210 HWCDisplayMetrics display_metrics() const { return display_metrics_; } 211 212 // Metrics of the native display, which depends on the specific hardware 213 // implementation of the display. 214 HWCDisplayMetrics native_display_metrics() const { 215 return native_display_metrics_; 216 } 217 218 std::shared_ptr<IonBuffer> framebuffer_target() const { 219 return framebuffer_target_; 220 } 221 222 // Set the display surface stack to compose to the display each frame. 223 int SetDisplaySurfaces(std::vector<std::shared_ptr<DisplaySurface>> surfaces); 224 225 Compositor* GetCompositor() { return &compositor_; } 226 227 void OnHardwareComposerRefresh(); 228 229 private: 230 int32_t EnableVsync(bool enabled); 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(/*out*/ bool* suspend_requested); 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 // Hardware composer HAL device. 307 std::unique_ptr<Hwc2::Composer> hwc2_hidl_; 308 sp<ComposerCallback> callbacks_; 309 310 // Display metrics of the physical display. 311 HWCDisplayMetrics native_display_metrics_; 312 // Display metrics of the logical display, adjusted so that orientation is 313 // landscape. 314 HWCDisplayMetrics display_metrics_; 315 // Transform required to get from native to logical display orientation. 316 hwc_transform_t display_transform_; 317 318 // Buffer for the background layer required by hardware composer. 319 std::shared_ptr<IonBuffer> framebuffer_target_; 320 321 // Protects access to the display surfaces and logical layers. 322 std::mutex layer_mutex_; 323 324 // Active display surfaces configured by the display manager. 325 std::vector<std::shared_ptr<DisplaySurface>> display_surfaces_; 326 std::vector<std::shared_ptr<DisplaySurface>> added_display_surfaces_; 327 bool display_surfaces_updated_; 328 bool hardware_layers_need_update_; 329 330 // Layer array for handling buffer flow into hardware composer layers. 331 // Note that the first array is the actual storage for the layer objects, 332 // and the latter is an array of pointers, which can be freely re-arranged 333 // without messing up the underlying objects. 334 std::array<Layer, kMaxHardwareLayers> layer_storage_; 335 std::array<Layer*, kMaxHardwareLayers> layers_; 336 size_t active_layer_count_; 337 338 // Set by the Post thread to the index of the GPU compositing output 339 // buffer in the layers_ array. 340 Layer* gpu_layer_; 341 342 // Handler to hook vsync events outside of this class. 343 VSyncCallback vsync_callback_; 344 345 // The layer posting thread. This thread wakes up a short time before vsync to 346 // hand buffers to post processing and the results to hardware composer. 347 std::thread post_thread_; 348 349 enum class PostThreadState { 350 // post_thread_state_ starts off paused. When suspending, the control thread 351 // will block until post_thread_state_ == kPaused, indicating the post 352 // thread has completed the transition to paused (most importantly: no more 353 // hardware composer calls). 354 kPaused, 355 // post_thread_state_ is set to kRunning by the control thread (either 356 // surface flinger's main thread or the vr flinger dispatcher thread). The 357 // post thread blocks until post_thread_state_ == kRunning. 358 kRunning, 359 // Set by the control thread to indicate the post thread should pause. The 360 // post thread will change post_thread_state_ from kPauseRequested to 361 // kPaused when it stops. 362 kPauseRequested 363 }; 364 // Control variables to control the state of the post thread 365 PostThreadState post_thread_state_; 366 // Used to wake the post thread up while it's waiting for vsync, for faster 367 // transition to the paused state. 368 pdx::LocalHandle terminate_post_thread_event_fd_; 369 // post_thread_state_mutex_ should be held before checking or modifying 370 // post_thread_state_. 371 std::mutex post_thread_state_mutex_; 372 // Used to communicate between the control thread and the post thread. 373 std::condition_variable post_thread_state_cond_var_; 374 375 // Backlight LED brightness sysfs node. 376 pdx::LocalHandle backlight_brightness_fd_; 377 378 // Primary display vsync event sysfs node. 379 pdx::LocalHandle primary_display_vsync_event_fd_; 380 381 // Primary display wait_pingpong state sysfs node. 382 pdx::LocalHandle primary_display_wait_pp_fd_; 383 384 // VSync sleep timerfd. 385 pdx::LocalHandle vsync_sleep_timer_fd_; 386 387 // The timestamp of the last vsync. 388 int64_t last_vsync_timestamp_; 389 390 // Vsync count since display on. 391 uint32_t vsync_count_; 392 393 // Counter tracking the number of skipped frames. 394 int frame_skip_count_; 395 396 // After construction, only accessed on post_thread_. 397 Compositor compositor_; 398 399 // Fd array for tracking retire fences that are returned by hwc. This allows 400 // us to detect when the display driver begins queuing frames. 401 std::vector<pdx::LocalHandle> retire_fence_fds_; 402 403 // Pose client for frame count notifications. Pose client predicts poses 404 // out to display frame boundaries, so we need to tell it about vsyncs. 405 DvrPose* pose_client_; 406 407 static void HwcRefresh(hwc2_callback_data_t data, hwc2_display_t display); 408 static void HwcVSync(hwc2_callback_data_t data, hwc2_display_t display, 409 int64_t timestamp); 410 static void HwcHotplug(hwc2_callback_data_t callbackData, 411 hwc2_display_t display, hwc2_connection_t connected); 412 413 HardwareComposer(const HardwareComposer&) = delete; 414 void operator=(const HardwareComposer&) = delete; 415}; 416 417} // namespace dvr 418} // namespace android 419 420#endif // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_ 421