display_surface.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_
  2. #define ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_
  3. #include <pdx/file_handle.h>
  4. #include <pdx/service.h>
  5. #include <private/dvr/buffer_hub_queue_client.h>
  6. #include <private/dvr/display_protocol.h>
  7. #include <private/dvr/ring_buffer.h>
  8. #include <functional>
  9. #include <iterator>
  10. #include <memory>
  11. #include <string>
  12. #include <vector>
  13. #include "acquired_buffer.h"
  14. namespace android {
  15. namespace dvr {
  16. class DisplayService;
  17. enum class SurfaceType {
  18. Direct,
  19. Application,
  20. };
  21. class DisplaySurface : public pdx::Channel {
  22. public:
  23. static pdx::Status<std::shared_ptr<DisplaySurface>> Create(
  24. DisplayService* service, int surface_id, int process_id, int user_id,
  25. const display::SurfaceAttributes& attributes);
  26. ~DisplaySurface() override;
  27. DisplayService* service() const { return service_; }
  28. SurfaceType surface_type() const { return surface_type_; }
  29. int surface_id() const { return surface_id_; }
  30. int process_id() const { return process_id_; }
  31. int user_id() const { return user_id_; }
  32. bool visible() const { return visible_; }
  33. int z_order() const { return z_order_; }
  34. const display::SurfaceAttributes& attributes() const { return attributes_; }
  35. display::SurfaceUpdateFlags update_flags() const { return update_flags_; }
  36. virtual std::vector<int32_t> GetQueueIds() const { return {}; }
  37. bool IsUpdatePending() const {
  38. return update_flags_.value() != display::SurfaceUpdateFlags::None;
  39. }
  40. protected:
  41. DisplaySurface(DisplayService* service, SurfaceType surface_type,
  42. int surface_id, int process_id, int user_id);
  43. // Utility to retrieve a shared pointer to this channel as the desired derived
  44. // type.
  45. template <
  46. typename T = DisplaySurface,
  47. typename = std::enable_if_t<std::is_base_of<DisplaySurface, T>::value>>
  48. std::shared_ptr<T> Self() {
  49. return std::static_pointer_cast<T>(shared_from_this());
  50. }
  51. virtual pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
  52. pdx::Message& message, const ProducerQueueConfig& config) = 0;
  53. // Registers a consumer queue with the event dispatcher in DisplayService. The
  54. // OnQueueEvent callback below is called to handle queue events.
  55. pdx::Status<void> RegisterQueue(
  56. const std::shared_ptr<ConsumerQueue>& consumer_queue);
  57. pdx::Status<void> UnregisterQueue(
  58. const std::shared_ptr<ConsumerQueue>& consumer_queue);
  59. // Called by the event dispatcher in DisplayService when a registered queue
  60. // event triggers. Executes on the event dispatcher thread.
  61. virtual void OnQueueEvent(
  62. const std::shared_ptr<ConsumerQueue>& consumer_queue, int events);
  63. void SurfaceUpdated(display::SurfaceUpdateFlags update_flags);
  64. void ClearUpdate();
  65. // Synchronizes access to mutable state below between message dispatch thread
  66. // and frame post thread.
  67. mutable std::mutex lock_;
  68. private:
  69. friend class DisplayService;
  70. friend class DisplayManagerService;
  71. // Dispatches display surface messages to the appropriate handlers. This
  72. // handler runs on the VrFlinger message dispatch thread.
  73. pdx::Status<void> HandleMessage(pdx::Message& message);
  74. pdx::Status<void> OnSetAttributes(
  75. pdx::Message& message, const display::SurfaceAttributes& attributes);
  76. pdx::Status<display::SurfaceInfo> OnGetSurfaceInfo(pdx::Message& message);
  77. DisplayService* service_;
  78. SurfaceType surface_type_;
  79. int surface_id_;
  80. int process_id_;
  81. int user_id_;
  82. display::SurfaceAttributes attributes_;
  83. display::SurfaceUpdateFlags update_flags_ = display::SurfaceUpdateFlags::None;
  84. // Subset of attributes that may be interpreted by the display service.
  85. bool visible_ = false;
  86. int z_order_ = 0;
  87. DisplaySurface(const DisplaySurface&) = delete;
  88. void operator=(const DisplaySurface&) = delete;
  89. };
  90. class ApplicationDisplaySurface : public DisplaySurface {
  91. public:
  92. ApplicationDisplaySurface(DisplayService* service, int surface_id,
  93. int process_id, int user_id)
  94. : DisplaySurface(service, SurfaceType::Application, surface_id,
  95. process_id, user_id) {}
  96. std::shared_ptr<ConsumerQueue> GetQueue(int32_t queue_id);
  97. std::vector<int32_t> GetQueueIds() const override;
  98. private:
  99. pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
  100. pdx::Message& message, const ProducerQueueConfig& config) override;
  101. void OnQueueEvent(const std::shared_ptr<ConsumerQueue>& consumer_queue,
  102. int events) override;
  103. // Accessed by both message dispatch thread and epoll event thread.
  104. std::unordered_map<int32_t, std::shared_ptr<ConsumerQueue>> consumer_queues_;
  105. };
  106. class DirectDisplaySurface : public DisplaySurface {
  107. public:
  108. DirectDisplaySurface(DisplayService* service, int surface_id, int process_id,
  109. int user_id)
  110. : DisplaySurface(service, SurfaceType::Direct, surface_id, process_id,
  111. user_id),
  112. acquired_buffers_(kMaxPostedBuffers),
  113. metadata_(nullptr) {}
  114. std::vector<int32_t> GetQueueIds() const override;
  115. bool IsBufferAvailable();
  116. bool IsBufferPosted();
  117. AcquiredBuffer AcquireCurrentBuffer();
  118. // Get the newest buffer. Up to one buffer will be skipped. If a buffer is
  119. // skipped, it will be stored in skipped_buffer if non null.
  120. AcquiredBuffer AcquireNewestAvailableBuffer(AcquiredBuffer* skipped_buffer);
  121. private:
  122. pdx::Status<pdx::LocalChannelHandle> OnCreateQueue(
  123. pdx::Message& message, const ProducerQueueConfig& config) override;
  124. void OnQueueEvent(const std::shared_ptr<ConsumerQueue>& consumer_queue,
  125. int events) override;
  126. // The capacity of the pending buffer queue. Should be enough to hold all the
  127. // buffers of this DisplaySurface, although in practice only 1 or 2 frames
  128. // will be pending at a time.
  129. static constexpr int kSurfaceBufferMaxCount = 4;
  130. static constexpr int kSurfaceViewMaxCount = 4;
  131. static constexpr int kMaxPostedBuffers =
  132. kSurfaceBufferMaxCount * kSurfaceViewMaxCount;
  133. // Returns whether a frame is available without locking the mutex.
  134. bool IsFrameAvailableNoLock() const;
  135. // Dequeue all available buffers from the consumer queue.
  136. void DequeueBuffersLocked();
  137. // In a triple-buffered surface, up to kMaxPostedBuffers buffers may be
  138. // posted and pending.
  139. RingBuffer<AcquiredBuffer> acquired_buffers_;
  140. std::shared_ptr<ConsumerQueue> direct_queue_;
  141. // Stores metadata when it dequeue buffers from consumer queue.
  142. std::unique_ptr<uint8_t[]> metadata_;
  143. };
  144. } // namespace dvr
  145. } // namespace android
  146. #endif // ANDROID_DVR_SERVICES_DISPLAYD_DISPLAY_SURFACE_H_