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 <dvr/dvr_shared_buffers.h>
9 #include <hardware/gralloc.h>
10 #include <log/log.h>
11 
12 #include <array>
13 #include <condition_variable>
14 #include <memory>
15 #include <mutex>
16 #include <optional>
17 #include <thread>
18 #include <tuple>
19 #include <vector>
20 
21 #include <dvr/dvr_config.h>
22 #include <dvr/dvr_vsync.h>
23 #include <pdx/file_handle.h>
24 #include <pdx/rpc/variant.h>
25 #include <private/dvr/shared_buffer_helpers.h>
26 #include <private/dvr/vsync_service.h>
27 
28 #include "DisplayHardware/DisplayIdentification.h"
29 #include "acquired_buffer.h"
30 #include "display_surface.h"
31 
32 // Hardware composer HAL doesn't define HWC_TRANSFORM_NONE as of this writing.
33 #ifndef HWC_TRANSFORM_NONE
34 #define HWC_TRANSFORM_NONE static_cast<hwc_transform_t>(0)
35 #endif
36 
37 namespace android {
38 namespace dvr {
39 
40 // Basic display metrics for physical displays.
41 struct DisplayParams {
42   hwc2_display_t id;
43   bool is_primary;
44 
45   int width;
46   int height;
47 
48   struct {
49     int x;
50     int y;
51   } dpi;
52 
53   int vsync_period_ns;
54 };
55 
56 // Layer represents the connection between a hardware composer layer and the
57 // source supplying buffers for the layer's contents.
58 class Layer {
59  public:
60   Layer() = default;
61 
62   // Sets up the layer to use a display surface as its content source. The Layer
63   // automatically handles ACQUIRE/RELEASE phases for the surface's buffer train
64   // every frame.
65   //
66   // |composer| The composer instance.
67   // |display_params| Info about the display to use.
68   // |blending| receives HWC_BLENDING_* values.
69   // |composition_type| receives either HWC_FRAMEBUFFER for most layers or
70   // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing).
71   // |index| is the index of this surface in the DirectDisplaySurface array.
72   Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
73         const std::shared_ptr<DirectDisplaySurface>& surface,
74         HWC::BlendMode blending, HWC::Composition composition_type,
75         size_t z_order);
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   // |composer| The composer instance.
82   // |display_params| Info about the display to use.
83   // |blending| receives HWC_BLENDING_* values.
84   // |composition_type| receives either HWC_FRAMEBUFFER for most layers or
85   // HWC_FRAMEBUFFER_TARGET (unless you know what you are doing).
86   Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
87         const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending,
88         HWC::Composition composition_type, size_t z_order);
89 
90   Layer(Layer&&) noexcept;
91   Layer& operator=(Layer&&) noexcept;
92 
93   ~Layer();
94 
95   // Releases any shared pointers and fence handles held by this instance.
96   void Reset();
97 
98   // Layers that use a direct IonBuffer should call this each frame to update
99   // which buffer will be used for the next PostLayers.
100   void UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer);
101 
102   // Sets up the hardware composer layer for the next frame. When the layer is
103   // associated with a display surface, this method automatically ACQUIRES a new
104   // buffer if one is available.
105   void Prepare();
106 
107   // After calling prepare, if this frame is to be dropped instead of passing
108   // along to the HWC, call Drop to close the contained fence(s).
109   void Drop();
110 
111   // Performs fence bookkeeping after the frame has been posted to hardware
112   // composer.
113   void Finish(int release_fence_fd);
114 
115   // Sets the blending for the layer. |blending| receives HWC_BLENDING_* values.
116   void SetBlending(HWC::BlendMode blending);
117 
118   // Sets the z-order of this layer
119   void SetZOrder(size_t z_order);
120 
121   // Gets the current IonBuffer associated with this layer. Ownership of the
122   // buffer DOES NOT pass to the caller and the pointer is not guaranteed to
123   // remain valid across calls to Layer::Setup(), Layer::Prepare(), or
124   // Layer::Reset(). YOU HAVE BEEN WARNED.
125   IonBuffer* GetBuffer();
126 
GetCompositionType()127   HWC::Composition GetCompositionType() const { return composition_type_; }
GetLayerHandle()128   HWC::Layer GetLayerHandle() const { return hardware_composer_layer_; }
IsLayerSetup()129   bool IsLayerSetup() const { return !source_.empty(); }
130 
GetSurfaceId()131   int GetSurfaceId() const {
132     int surface_id = -1;
133     pdx::rpc::IfAnyOf<SourceSurface>::Call(
134         &source_, [&surface_id](const SourceSurface& surface_source) {
135           surface_id = surface_source.GetSurfaceId();
136         });
137     return surface_id;
138   }
139 
GetBufferId()140   int GetBufferId() const {
141     int buffer_id = -1;
142     pdx::rpc::IfAnyOf<SourceSurface>::Call(
143         &source_, [&buffer_id](const SourceSurface& surface_source) {
144           buffer_id = surface_source.GetBufferId();
145         });
146     return buffer_id;
147   }
148 
149   // Compares Layers by surface id.
150   bool operator<(const Layer& other) const {
151     return GetSurfaceId() < other.GetSurfaceId();
152   }
153   bool operator<(int surface_id) const { return GetSurfaceId() < surface_id; }
154 
IgnoreBadDisplayErrorsOnDestroy(bool ignore)155   void IgnoreBadDisplayErrorsOnDestroy(bool ignore) {
156     ignore_bad_display_errors_on_destroy_ = ignore;
157   }
158 
159  private:
160   void CommonLayerSetup();
161 
162   // Applies all of the settings to this layer using the hwc functions
163   void UpdateLayerSettings();
164 
165   // Applies visibility settings that may have changed.
166   void UpdateVisibilitySettings();
167 
168   // Checks whether the buffer, given by id, is associated with the given slot
169   // in the HWC buffer cache. If the slot is not associated with the given
170   // buffer the cache is updated to establish the association and the buffer
171   // should be sent to HWC using setLayerBuffer. Returns true if the association
172   // was already established, false if not. A buffer_id of -1 is never
173   // associated and always returns false.
174   bool CheckAndUpdateCachedBuffer(std::size_t slot, int buffer_id);
175 
176   // Composer instance.
177   Hwc2::Composer* composer_ = nullptr;
178 
179   // Parameters of the display to use for this layer.
180   DisplayParams display_params_;
181 
182   // The hardware composer layer and metrics to use during the prepare cycle.
183   hwc2_layer_t hardware_composer_layer_ = 0;
184 
185   // Layer properties used to setup the hardware composer layer during the
186   // Prepare phase.
187   size_t z_order_ = 0;
188   HWC::BlendMode blending_ = HWC::BlendMode::None;
189   HWC::Composition composition_type_ = HWC::Composition::Invalid;
190   HWC::Composition target_composition_type_ = HWC::Composition::Device;
191 
192   // State when the layer is connected to a surface. Provides the same interface
193   // as SourceBuffer to simplify internal use by Layer.
194   struct SourceSurface {
195     std::shared_ptr<DirectDisplaySurface> surface;
196     AcquiredBuffer acquired_buffer;
197     pdx::LocalHandle release_fence;
198 
SourceSurfaceSourceSurface199     explicit SourceSurface(const std::shared_ptr<DirectDisplaySurface>& surface)
200         : surface(surface) {}
201 
202     // Attempts to acquire a new buffer from the surface and return a tuple with
203     // width, height, buffer handle, and fence. If a new buffer is not available
204     // the previous buffer is returned or an empty value if no buffer has ever
205     // been posted. When a new buffer is acquired the previous buffer's release
206     // fence is passed out automatically.
207     std::tuple<int, int, int, sp<GraphicBuffer>, pdx::LocalHandle, std::size_t>
AcquireSourceSurface208     Acquire() {
209       if (surface->IsBufferAvailable()) {
210         acquired_buffer.Release(std::move(release_fence));
211         acquired_buffer = surface->AcquireCurrentBuffer();
212         ATRACE_ASYNC_END("BufferPost", acquired_buffer.buffer()->id());
213       }
214       if (!acquired_buffer.IsEmpty()) {
215         return std::make_tuple(
216             acquired_buffer.buffer()->width(),
217             acquired_buffer.buffer()->height(), acquired_buffer.buffer()->id(),
218             acquired_buffer.buffer()->buffer()->buffer(),
219             acquired_buffer.ClaimAcquireFence(), acquired_buffer.slot());
220       } else {
221         return std::make_tuple(0, 0, -1, nullptr, pdx::LocalHandle{}, 0);
222       }
223     }
224 
FinishSourceSurface225     void Finish(pdx::LocalHandle fence) { release_fence = std::move(fence); }
226 
227     // Gets a pointer to the current acquired buffer or returns nullptr if there
228     // isn't one.
GetBufferSourceSurface229     IonBuffer* GetBuffer() {
230       if (acquired_buffer.IsAvailable())
231         return acquired_buffer.buffer()->buffer();
232       else
233         return nullptr;
234     }
235 
236     // Returns the surface id of the surface.
GetSurfaceIdSourceSurface237     int GetSurfaceId() const { return surface->surface_id(); }
238 
239     // Returns the buffer id for the current buffer.
GetBufferIdSourceSurface240     int GetBufferId() const {
241       if (acquired_buffer.IsAvailable())
242         return acquired_buffer.buffer()->id();
243       else
244         return -1;
245     }
246   };
247 
248   // State when the layer is connected to a buffer. Provides the same interface
249   // as SourceSurface to simplify internal use by Layer.
250   struct SourceBuffer {
251     std::shared_ptr<IonBuffer> buffer;
252 
253     std::tuple<int, int, int, sp<GraphicBuffer>, pdx::LocalHandle, std::size_t>
AcquireSourceBuffer254     Acquire() {
255       if (buffer)
256         return std::make_tuple(buffer->width(), buffer->height(), -1,
257                                buffer->buffer(), pdx::LocalHandle{}, 0);
258       else
259         return std::make_tuple(0, 0, -1, nullptr, pdx::LocalHandle{}, 0);
260     }
261 
FinishSourceBuffer262     void Finish(pdx::LocalHandle /*fence*/) {}
263 
GetBufferSourceBuffer264     IonBuffer* GetBuffer() { return buffer.get(); }
265 
GetSurfaceIdSourceBuffer266     int GetSurfaceId() const { return -1; }
GetBufferIdSourceBuffer267     int GetBufferId() const { return -1; }
268   };
269 
270   // The underlying hardware composer layer is supplied buffers either from a
271   // surface buffer train or from a buffer directly.
272   pdx::rpc::Variant<SourceSurface, SourceBuffer> source_;
273 
274   pdx::LocalHandle acquire_fence_;
275   bool surface_rect_functions_applied_ = false;
276   bool pending_visibility_settings_ = true;
277 
278   // Map of buffer slot assignments that have already been established with HWC:
279   // slot -> buffer_id. When this map contains a matching slot and buffer_id the
280   // buffer argument to setLayerBuffer may be nullptr to avoid the cost of
281   // importing a buffer HWC already knows about.
282   std::map<std::size_t, int> cached_buffer_map_;
283 
284   // When calling destroyLayer() on an external display that's been removed we
285   // typically get HWC2_ERROR_BAD_DISPLAY errors. If
286   // ignore_bad_display_errors_on_destroy_ is true, don't log the bad display
287   // errors, since they're expected.
288   bool ignore_bad_display_errors_on_destroy_ = false;
289 
290   Layer(const Layer&) = delete;
291   void operator=(const Layer&) = delete;
292 };
293 
294 // HardwareComposer encapsulates the hardware composer HAL, exposing a
295 // simplified API to post buffers to the display.
296 //
297 // HardwareComposer is accessed by both the vr flinger dispatcher thread and the
298 // surface flinger main thread, in addition to internally running a separate
299 // thread for compositing/EDS and posting layers to the HAL. When changing how
300 // variables are used or adding new state think carefully about which threads
301 // will access the state and whether it needs to be synchronized.
302 class HardwareComposer {
303  public:
304   using RequestDisplayCallback = std::function<void(bool)>;
305 
306   HardwareComposer();
307   ~HardwareComposer();
308 
309   bool Initialize(Hwc2::Composer* composer,
310                   hwc2_display_t primary_display_id,
311                   RequestDisplayCallback request_display_callback);
312 
IsInitialized()313   bool IsInitialized() const { return initialized_; }
314 
315   // Start the post thread if there's work to do (i.e. visible layers). This
316   // should only be called from surface flinger's main thread.
317   void Enable();
318   // Pause the post thread, blocking until the post thread has signaled that
319   // it's paused. This should only be called from surface flinger's main thread.
320   void Disable();
321 
322   // Called on a binder thread.
323   void OnBootFinished();
324 
325   std::string Dump();
326 
GetPrimaryDisplayParams()327   const DisplayParams& GetPrimaryDisplayParams() const {
328     return primary_display_;
329   }
330 
331   // Sets the display surfaces to compose the hardware layer stack.
332   void SetDisplaySurfaces(
333       std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces);
334 
335   int OnNewGlobalBuffer(DvrGlobalBufferKey key, IonBuffer& ion_buffer);
336   void OnDeletedGlobalBuffer(DvrGlobalBufferKey key);
337 
338   // Gets the edid data for the current active display (internal or external)
GetCurrentDisplayIdentificationData()339   DisplayIdentificationData GetCurrentDisplayIdentificationData() {
340     return display_identification_data_;
341   }
342 
343   // Gets the edid port for the current active display (internal or external)
GetCurrentDisplayPort()344   uint8_t GetCurrentDisplayPort() { return display_port_; }
345 
346  private:
347   DisplayParams GetDisplayParams(Hwc2::Composer* composer,
348       hwc2_display_t display, bool is_primary);
349 
350   // Turn display vsync on/off. Returns true on success, false on failure.
351   bool EnableVsync(const DisplayParams& display, bool enabled);
352   // Turn display power on/off. Returns true on success, false on failure.
353   bool SetPowerMode(const DisplayParams& display, bool active);
354   // Convenience function to turn a display on/off. Turns both power and vsync
355   // on/off. Returns true on success, false on failure.
356   bool EnableDisplay(const DisplayParams& display, bool enabled);
357 
358   class VsyncService : public BnVsyncService {
359    public:
360     status_t registerCallback(const sp<IVsyncCallback> callback) override;
361     status_t unregisterCallback(const sp<IVsyncCallback> callback) override;
362     void OnVsync(int64_t vsync_timestamp);
363    private:
364     std::vector<sp<IVsyncCallback>>::const_iterator FindCallback(
365         const sp<IVsyncCallback>& callback) const;
366     std::mutex mutex_;
367     std::vector<sp<IVsyncCallback>> callbacks_;
368   };
369 
370   class ComposerCallback : public Hwc2::IComposerCallback {
371    public:
372     ComposerCallback() = default;
373     hardware::Return<void> onHotplug(Hwc2::Display display,
374                                      Connection conn) override;
375     hardware::Return<void> onRefresh(Hwc2::Display display) override;
376     hardware::Return<void> onVsync(Hwc2::Display display,
377                                    int64_t timestamp) override;
378     hardware::Return<void> onVsync_2_4(
379         Hwc2::Display display, int64_t timestamp,
380         Hwc2::VsyncPeriodNanos vsyncPeriodNanos) override;
381     hardware::Return<void> onVsyncPeriodTimingChanged(
382         Hwc2::Display display,
383         const Hwc2::VsyncPeriodChangeTimeline& updatedTimeline) override;
384     hardware::Return<void> onSeamlessPossible(Hwc2::Display display) override;
385 
GotFirstHotplug()386     bool GotFirstHotplug() { return got_first_hotplug_; }
387     void SetVsyncService(const sp<VsyncService>& vsync_service);
388 
389     struct Displays {
390       hwc2_display_t primary_display = 0;
391       std::optional<hwc2_display_t> external_display;
392       bool external_display_was_hotplugged = false;
393     };
394 
395     Displays GetDisplays();
396     pdx::Status<int64_t> GetVsyncTime(hwc2_display_t display);
397 
398    private:
399     struct DisplayInfo {
400       hwc2_display_t id = 0;
401       pdx::LocalHandle driver_vsync_event_fd;
402       int64_t callback_vsync_timestamp{0};
403     };
404 
405     DisplayInfo* GetDisplayInfo(hwc2_display_t display);
406 
407     std::mutex mutex_;
408 
409     bool got_first_hotplug_ = false;
410     DisplayInfo primary_display_;
411     std::optional<DisplayInfo> external_display_;
412     bool external_display_was_hotplugged_ = false;
413     sp<VsyncService> vsync_service_;
414   };
415 
416   HWC::Error Validate(hwc2_display_t display);
417   HWC::Error Present(hwc2_display_t display);
418 
419   void PostLayers(hwc2_display_t display);
420   void PostThread();
421 
422   // The post thread has two controlling states:
423   // 1. Idle: no work to do (no visible surfaces).
424   // 2. Suspended: explicitly halted (system is not in VR mode).
425   // When either #1 or #2 is true then the post thread is quiescent, otherwise
426   // it is active.
427   using PostThreadStateType = uint32_t;
428   struct PostThreadState {
429     enum : PostThreadStateType {
430       Active = 0,
431       Idle = (1 << 0),
432       Suspended = (1 << 1),
433       Quit = (1 << 2),
434     };
435   };
436 
437   void UpdatePostThreadState(uint32_t state, bool suspend);
438 
439   // Blocks until either event_fd becomes readable, or we're interrupted by a
440   // control thread, or timeout_ms is reached before any events occur. Any
441   // errors are returned as negative errno values, with -ETIMEDOUT returned in
442   // the case of a timeout. If we're interrupted, kPostThreadInterrupted will be
443   // returned.
444   int PostThreadPollInterruptible(const pdx::LocalHandle& event_fd,
445                                   int requested_events, int timeout_ms);
446 
447   // WaitForPredictedVSync and SleepUntil are blocking calls made on the post
448   // thread that can be interrupted by a control thread. If interrupted, these
449   // calls return kPostThreadInterrupted.
450   int ReadWaitPPState();
451   pdx::Status<int64_t> WaitForPredictedVSync();
452   int SleepUntil(int64_t wakeup_timestamp);
453 
454   // Initialize any newly connected displays, and set target_display_ to the
455   // display we should render to. Returns true if target_display_
456   // changed. Called only from the post thread.
457   bool UpdateTargetDisplay();
458 
459   // Reconfigures the layer stack if the display surfaces changed since the last
460   // frame. Called only from the post thread.
461   void UpdateLayerConfig();
462 
463   // Called on the post thread to create the Composer instance.
464   void CreateComposer();
465 
466   // Called on the post thread when the post thread is resumed.
467   void OnPostThreadResumed();
468   // Called on the post thread when the post thread is paused or quits.
469   void OnPostThreadPaused();
470 
471   // Use post_thread_wait_ to wait for a specific condition, specified by pred.
472   // timeout_sec < 0 means wait indefinitely, otherwise it specifies the timeout
473   // in seconds.
474   // The lock must be held when this function is called.
475   // Returns true if the wait was interrupted because the post thread was asked
476   // to quit.
477   bool PostThreadCondWait(std::unique_lock<std::mutex>& lock,
478                           int timeout_sec,
479                           const std::function<bool()>& pred);
480 
481   // Map the given shared memory buffer to our broadcast ring to track updates
482   // to the config parameters.
483   int MapConfigBuffer(IonBuffer& ion_buffer);
484   void ConfigBufferDeleted();
485   // Poll for config udpates.
486   void UpdateConfigBuffer();
487 
488   bool initialized_;
489   bool is_standalone_device_;
490 
491   std::unique_ptr<Hwc2::Composer> composer_;
492   sp<ComposerCallback> composer_callback_;
493   RequestDisplayCallback request_display_callback_;
494 
495   DisplayParams primary_display_;
496   std::optional<DisplayParams> external_display_;
497   DisplayParams* target_display_ = &primary_display_;
498 
499   // The list of surfaces we should draw. Set by the display service when
500   // DirectSurfaces are added, removed, or change visibility. Written by the
501   // message dispatch thread and read by the post thread.
502   std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces_;
503   // Set to true by the dispatch thread whenever surfaces_ changes. Set to false
504   // by the post thread when the new list of surfaces is processed.
505   bool surfaces_changed_ = false;
506 
507   std::vector<std::shared_ptr<DirectDisplaySurface>> current_surfaces_;
508 
509   // Layer set for handling buffer flow into hardware composer layers. This
510   // vector must be sorted by surface_id in ascending order.
511   std::vector<Layer> layers_;
512 
513   // The layer posting thread. This thread wakes up a short time before vsync to
514   // hand buffers to hardware composer.
515   std::thread post_thread_;
516 
517   // Post thread state machine and synchronization primitives.
518   PostThreadStateType post_thread_state_{PostThreadState::Idle |
519                                          PostThreadState::Suspended};
520   std::atomic<bool> post_thread_quiescent_{true};
521   bool post_thread_resumed_{false};
522   pdx::LocalHandle post_thread_event_fd_;
523   std::mutex post_thread_mutex_;
524   std::condition_variable post_thread_wait_;
525   std::condition_variable post_thread_ready_;
526 
527   // When boot is finished this will be set to true and the post thread will be
528   // notified via post_thread_wait_.
529   bool boot_finished_ = false;
530 
531   // VSync sleep timerfd.
532   pdx::LocalHandle vsync_sleep_timer_fd_;
533 
534   // The timestamp of the last vsync.
535   int64_t last_vsync_timestamp_ = 0;
536 
537   // The number of vsync intervals to predict since the last vsync.
538   int vsync_prediction_interval_ = 1;
539 
540   // Vsync count since display on.
541   uint32_t vsync_count_ = 0;
542 
543   // Counter tracking the number of skipped frames.
544   int frame_skip_count_ = 0;
545 
546   // Fd array for tracking retire fences that are returned by hwc. This allows
547   // us to detect when the display driver begins queuing frames.
548   std::vector<pdx::LocalHandle> retire_fence_fds_;
549 
550   // If we are publishing vsync data, we will put it here.
551   std::unique_ptr<CPUMappedBroadcastRing<DvrVsyncRing>> vsync_ring_;
552 
553   // Broadcast ring for receiving config data from the DisplayManager.
554   DvrConfigRing shared_config_ring_;
555   uint32_t shared_config_ring_sequence_{0};
556   // Config buffer for reading from the post thread.
557   DvrConfig post_thread_config_;
558   std::mutex shared_config_mutex_;
559 
560   bool vsync_trace_parity_ = false;
561   sp<VsyncService> vsync_service_;
562 
563   // Edid section.
564   void UpdateEdidData(Hwc2::Composer* composer, hwc2_display_t hw_id);
565   DisplayIdentificationData display_identification_data_;
566   uint8_t display_port_;
567 
568   static constexpr int kPostThreadInterrupted = 1;
569 
570   HardwareComposer(const HardwareComposer&) = delete;
571   void operator=(const HardwareComposer&) = delete;
572 };
573 
574 }  // namespace dvr
575 }  // namespace android
576 
577 #endif  // ANDROID_DVR_SERVICES_DISPLAYD_HARDWARE_COMPOSER_H_
578