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