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