display_manager_service.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "display_manager_service.h"
  2. #include <pdx/channel_handle.h>
  3. #include <pdx/default_transport/service_endpoint.h>
  4. #include <private/android_filesystem_config.h>
  5. #include <private/dvr/display_protocol.h>
  6. #include <private/dvr/trusted_uids.h>
  7. #include <sys/poll.h>
  8. #include <array>
  9. using android::dvr::display::DisplayManagerProtocol;
  10. using android::pdx::Channel;
  11. using android::pdx::LocalChannelHandle;
  12. using android::pdx::Message;
  13. using android::pdx::default_transport::Endpoint;
  14. using android::pdx::ErrorStatus;
  15. using android::pdx::rpc::DispatchRemoteMethod;
  16. using android::pdx::rpc::IfAnyOf;
  17. using android::pdx::rpc::RemoteMethodError;
  18. namespace android {
  19. namespace dvr {
  20. void DisplayManager::SetNotificationsPending(bool pending) {
  21. auto status = service_->ModifyChannelEvents(channel_id_, pending ? 0 : POLLIN,
  22. pending ? POLLIN : 0);
  23. ALOGE_IF(!status,
  24. "DisplayManager::SetNotificationPending: Failed to modify channel "
  25. "events: %s",
  26. status.GetErrorMessage().c_str());
  27. }
  28. DisplayManagerService::DisplayManagerService(
  29. const std::shared_ptr<DisplayService>& display_service)
  30. : BASE("DisplayManagerService",
  31. Endpoint::Create(DisplayManagerProtocol::kClientPath)),
  32. display_service_(display_service) {
  33. display_service_->SetDisplayConfigurationUpdateNotifier(
  34. std::bind(&DisplayManagerService::OnDisplaySurfaceChange, this));
  35. }
  36. std::shared_ptr<pdx::Channel> DisplayManagerService::OnChannelOpen(
  37. pdx::Message& message) {
  38. const int user_id = message.GetEffectiveUserId();
  39. const bool trusted = user_id == AID_ROOT || IsTrustedUid(user_id);
  40. // Check if the display_manager_ has a defunct channel.
  41. if (display_manager_ && !HasChannelId(display_manager_->channel_id())) {
  42. ALOGE("DisplayManagerService::OnChannelOpen: Found defunct channel %d with "
  43. "no OnChannelClose, clearing prior display manager.",
  44. display_manager_->channel_id());
  45. display_manager_ = nullptr;
  46. }
  47. // Prevent more than one display manager from registering at a time or
  48. // untrusted UIDs from connecting.
  49. if (display_manager_ || !trusted) {
  50. RemoteMethodError(message, EPERM);
  51. return nullptr;
  52. }
  53. display_manager_ =
  54. std::make_shared<DisplayManager>(this, message.GetChannelId());
  55. return display_manager_;
  56. }
  57. void DisplayManagerService::OnChannelClose(
  58. pdx::Message& /*message*/, const std::shared_ptr<pdx::Channel>& channel) {
  59. // Unregister the display manager when the channel closes.
  60. if (display_manager_ == channel)
  61. display_manager_ = nullptr;
  62. }
  63. pdx::Status<void> DisplayManagerService::HandleMessage(pdx::Message& message) {
  64. ATRACE_NAME("DisplayManagerService::HandleMessage");
  65. auto channel = std::static_pointer_cast<DisplayManager>(message.GetChannel());
  66. switch (message.GetOp()) {
  67. case DisplayManagerProtocol::GetSurfaceState::Opcode:
  68. DispatchRemoteMethod<DisplayManagerProtocol::GetSurfaceState>(
  69. *this, &DisplayManagerService::OnGetSurfaceState, message);
  70. return {};
  71. case DisplayManagerProtocol::GetSurfaceQueue::Opcode:
  72. DispatchRemoteMethod<DisplayManagerProtocol::GetSurfaceQueue>(
  73. *this, &DisplayManagerService::OnGetSurfaceQueue, message);
  74. return {};
  75. default:
  76. return Service::DefaultHandleMessage(message);
  77. }
  78. }
  79. pdx::Status<std::vector<display::SurfaceState>>
  80. DisplayManagerService::OnGetSurfaceState(pdx::Message& /*message*/) {
  81. std::vector<display::SurfaceState> items;
  82. display_service_->ForEachDisplaySurface(
  83. SurfaceType::Application,
  84. [&items](const std::shared_ptr<DisplaySurface>& surface) mutable {
  85. items.push_back({surface->surface_id(), surface->process_id(),
  86. surface->user_id(), surface->attributes(),
  87. surface->update_flags(), surface->GetQueueIds()});
  88. surface->ClearUpdate();
  89. });
  90. // The fact that we're in the message handler implies that display_manager_ is
  91. // not nullptr. No check required, unless this service becomes multi-threaded.
  92. display_manager_->SetNotificationsPending(false);
  93. return items;
  94. }
  95. pdx::Status<pdx::LocalChannelHandle> DisplayManagerService::OnGetSurfaceQueue(
  96. pdx::Message& /*message*/, int surface_id, int queue_id) {
  97. auto surface = display_service_->GetDisplaySurface(surface_id);
  98. if (!surface || surface->surface_type() != SurfaceType::Application)
  99. return ErrorStatus(EINVAL);
  100. auto queue =
  101. std::static_pointer_cast<ApplicationDisplaySurface>(surface)->GetQueue(
  102. queue_id);
  103. if (!queue)
  104. return ErrorStatus(EINVAL);
  105. auto status = queue->CreateConsumerQueueHandle();
  106. ALOGE_IF(
  107. !status,
  108. "DisplayManagerService::OnGetSurfaceQueue: Failed to create consumer "
  109. "queue for queue_id=%d: %s",
  110. queue->id(), status.GetErrorMessage().c_str());
  111. return status;
  112. }
  113. void DisplayManagerService::OnDisplaySurfaceChange() {
  114. if (display_manager_)
  115. display_manager_->SetNotificationsPending(true);
  116. }
  117. } // namespace dvr
  118. } // namespace android