Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #include "display_service.h" |
| 2 | |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 3 | #include <unistd.h> |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 4 | |
| 5 | #include <algorithm> |
| 6 | #include <sstream> |
| 7 | #include <string> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
Hendrik Wagenaar | bcb03d0 | 2017-05-23 14:59:08 -0700 | [diff] [blame] | 10 | #include <android-base/file.h> |
| 11 | #include <android-base/properties.h> |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 12 | #include <dvr/dvr_display_types.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 13 | #include <pdx/default_transport/service_endpoint.h> |
| 14 | #include <pdx/rpc/remote_method.h> |
Corey Tabaka | 99c2d73 | 2017-06-07 17:54:33 -0700 | [diff] [blame] | 15 | #include <private/android_filesystem_config.h> |
Corey Tabaka | 3f82d31 | 2017-04-20 14:42:08 -0700 | [diff] [blame] | 16 | #include <private/dvr/display_protocol.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 17 | #include <private/dvr/numeric.h> |
Corey Tabaka | 99c2d73 | 2017-06-07 17:54:33 -0700 | [diff] [blame] | 18 | #include <private/dvr/trusted_uids.h> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 19 | #include <private/dvr/types.h> |
| 20 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 21 | using android::dvr::display::DisplayProtocol; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 22 | using android::pdx::Channel; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 23 | using android::pdx::ErrorStatus; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 24 | using android::pdx::Message; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 25 | using android::pdx::Status; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 26 | using android::pdx::default_transport::Endpoint; |
| 27 | using android::pdx::rpc::DispatchRemoteMethod; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 28 | |
Hendrik Wagenaar | bcb03d0 | 2017-05-23 14:59:08 -0700 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | const char kDvrLensMetricsProperty[] = "ro.dvr.lens_metrics"; |
| 32 | const char kDvrDeviceMetricsProperty[] = "ro.dvr.device_metrics"; |
| 33 | const char kDvrDeviceConfigProperty[] = "ro.dvr.device_configuration"; |
| 34 | |
| 35 | } // namespace |
| 36 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace dvr { |
| 39 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 40 | DisplayService::DisplayService(Hwc2::Composer* hidl, |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 41 | hwc2_display_t primary_display_id, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 42 | RequestDisplayCallback request_display_callback) |
| 43 | : BASE("DisplayService", |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 44 | Endpoint::Create(display::DisplayProtocol::kClientPath)) { |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 45 | hardware_composer_.Initialize( |
| 46 | hidl, primary_display_id, request_display_callback); |
Stephen Kiazyk | 016e5e3 | 2017-02-21 17:09:22 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool DisplayService::IsInitialized() const { |
| 50 | return BASE::IsInitialized() && hardware_composer_.IsInitialized(); |
| 51 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 52 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 53 | std::string DisplayService::DumpState(size_t /*max_length*/) { |
Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 54 | std::ostringstream stream; |
| 55 | |
| 56 | auto surfaces = GetDisplaySurfaces(); |
| 57 | std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) { |
| 58 | return a->surface_id() < b->surface_id(); |
| 59 | }); |
| 60 | |
| 61 | stream << "Application Surfaces:" << std::endl; |
| 62 | |
| 63 | size_t count = 0; |
| 64 | for (const auto& surface : surfaces) { |
| 65 | if (surface->surface_type() == SurfaceType::Application) { |
| 66 | stream << "Surface " << count++ << ":"; |
| 67 | stream << " surface_id=" << surface->surface_id() |
| 68 | << " process_id=" << surface->process_id() |
| 69 | << " user_id=" << surface->user_id() |
| 70 | << " visible=" << surface->visible() |
| 71 | << " z_order=" << surface->z_order(); |
| 72 | |
| 73 | stream << " queue_ids="; |
| 74 | auto queue_ids = surface->GetQueueIds(); |
| 75 | std::sort(queue_ids.begin(), queue_ids.end()); |
| 76 | for (int32_t id : queue_ids) { |
| 77 | if (id != queue_ids[0]) |
| 78 | stream << ","; |
| 79 | stream << id; |
| 80 | } |
| 81 | stream << std::endl; |
| 82 | } |
| 83 | } |
| 84 | stream << std::endl; |
| 85 | |
| 86 | stream << "Direct Surfaces:" << std::endl; |
| 87 | |
| 88 | count = 0; |
| 89 | for (const auto& surface : surfaces) { |
| 90 | if (surface->surface_type() == SurfaceType::Direct) { |
| 91 | stream << "Surface " << count++ << ":"; |
| 92 | stream << " surface_id=" << surface->surface_id() |
| 93 | << " process_id=" << surface->process_id() |
| 94 | << " user_id=" << surface->user_id() |
| 95 | << " visible=" << surface->visible() |
| 96 | << " z_order=" << surface->z_order(); |
| 97 | |
| 98 | stream << " queue_ids="; |
| 99 | auto queue_ids = surface->GetQueueIds(); |
| 100 | std::sort(queue_ids.begin(), queue_ids.end()); |
| 101 | for (int32_t id : queue_ids) { |
| 102 | if (id != queue_ids[0]) |
| 103 | stream << ","; |
| 104 | stream << id; |
| 105 | } |
| 106 | stream << std::endl; |
| 107 | } |
| 108 | } |
| 109 | stream << std::endl; |
| 110 | |
| 111 | stream << hardware_composer_.Dump(); |
| 112 | return stream.str(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 115 | void DisplayService::OnChannelClose(pdx::Message& message, |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 116 | const std::shared_ptr<Channel>& channel) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 117 | if (auto surface = std::static_pointer_cast<DisplaySurface>(channel)) { |
| 118 | surface->OnSetAttributes(message, |
| 119 | {{display::SurfaceAttribute::Visible, |
| 120 | display::SurfaceAttributeValue{false}}}); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 121 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // First-level dispatch for display service messages. Directly handles messages |
| 125 | // that are independent of the display surface (metrics, creation) and routes |
| 126 | // surface-specific messages to the per-instance handlers. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 127 | Status<void> DisplayService::HandleMessage(pdx::Message& message) { |
| 128 | ALOGD_IF(TRACE, "DisplayService::HandleMessage: opcode=%d", message.GetOp()); |
Corey Tabaka | b3732f0 | 2017-09-16 00:58:54 -0700 | [diff] [blame] | 129 | ATRACE_NAME("DisplayService::HandleMessage"); |
| 130 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 131 | switch (message.GetOp()) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 132 | case DisplayProtocol::GetMetrics::Opcode: |
| 133 | DispatchRemoteMethod<DisplayProtocol::GetMetrics>( |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 134 | *this, &DisplayService::OnGetMetrics, message); |
Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 135 | return {}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 136 | |
Hendrik Wagenaar | bcb03d0 | 2017-05-23 14:59:08 -0700 | [diff] [blame] | 137 | case DisplayProtocol::GetConfigurationData::Opcode: |
| 138 | DispatchRemoteMethod<DisplayProtocol::GetConfigurationData>( |
| 139 | *this, &DisplayService::OnGetConfigurationData, message); |
| 140 | return {}; |
| 141 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 142 | case DisplayProtocol::CreateSurface::Opcode: |
| 143 | DispatchRemoteMethod<DisplayProtocol::CreateSurface>( |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 144 | *this, &DisplayService::OnCreateSurface, message); |
Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 145 | return {}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 146 | |
Corey Tabaka | 99c2d73 | 2017-06-07 17:54:33 -0700 | [diff] [blame] | 147 | case DisplayProtocol::SetupGlobalBuffer::Opcode: |
| 148 | DispatchRemoteMethod<DisplayProtocol::SetupGlobalBuffer>( |
| 149 | *this, &DisplayService::OnSetupGlobalBuffer, message); |
| 150 | return {}; |
| 151 | |
| 152 | case DisplayProtocol::DeleteGlobalBuffer::Opcode: |
| 153 | DispatchRemoteMethod<DisplayProtocol::DeleteGlobalBuffer>( |
| 154 | *this, &DisplayService::OnDeleteGlobalBuffer, message); |
| 155 | return {}; |
| 156 | |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 157 | case DisplayProtocol::GetGlobalBuffer::Opcode: |
| 158 | DispatchRemoteMethod<DisplayProtocol::GetGlobalBuffer>( |
| 159 | *this, &DisplayService::OnGetGlobalBuffer, message); |
Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 160 | return {}; |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 161 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 162 | case DisplayProtocol::IsVrAppRunning::Opcode: |
| 163 | DispatchRemoteMethod<DisplayProtocol::IsVrAppRunning>( |
Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 164 | *this, &DisplayService::IsVrAppRunning, message); |
Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 165 | return {}; |
Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 166 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 167 | // Direct the surface specific messages to the surface instance. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 168 | case DisplayProtocol::SetAttributes::Opcode: |
| 169 | case DisplayProtocol::CreateQueue::Opcode: |
| 170 | case DisplayProtocol::GetSurfaceInfo::Opcode: |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 171 | return HandleSurfaceMessage(message); |
| 172 | |
| 173 | default: |
| 174 | return Service::HandleMessage(message); |
| 175 | } |
| 176 | } |
| 177 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 178 | Status<display::Metrics> DisplayService::OnGetMetrics( |
| 179 | pdx::Message& /*message*/) { |
Steven Thomas | bfe46a0 | 2018-02-16 14:27:35 -0800 | [diff] [blame] | 180 | const auto& params = hardware_composer_.GetPrimaryDisplayParams(); |
| 181 | return {{static_cast<uint32_t>(params.width), |
| 182 | static_cast<uint32_t>(params.height), |
| 183 | static_cast<uint32_t>(params.dpi.x), |
| 184 | static_cast<uint32_t>(params.dpi.y), |
| 185 | static_cast<uint32_t>(params.vsync_period_ns), |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 186 | 0, |
| 187 | 0, |
| 188 | 0, |
| 189 | 0.0, |
| 190 | {}, |
| 191 | {}}}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Hendrik Wagenaar | bcb03d0 | 2017-05-23 14:59:08 -0700 | [diff] [blame] | 194 | pdx::Status<std::string> DisplayService::OnGetConfigurationData( |
| 195 | pdx::Message& /*message*/, display::ConfigFileType config_type) { |
| 196 | std::string property_name; |
| 197 | switch (config_type) { |
| 198 | case display::ConfigFileType::kLensMetrics: |
| 199 | property_name = kDvrLensMetricsProperty; |
| 200 | break; |
| 201 | case display::ConfigFileType::kDeviceMetrics: |
| 202 | property_name = kDvrDeviceMetricsProperty; |
| 203 | break; |
| 204 | case display::ConfigFileType::kDeviceConfiguration: |
| 205 | property_name = kDvrDeviceConfigProperty; |
| 206 | break; |
| 207 | default: |
| 208 | return ErrorStatus(EINVAL); |
| 209 | } |
| 210 | std::string file_path = base::GetProperty(property_name, ""); |
| 211 | if (file_path.empty()) { |
| 212 | return ErrorStatus(ENOENT); |
| 213 | } |
| 214 | |
| 215 | std::string data; |
| 216 | if (!base::ReadFileToString(file_path, &data)) { |
| 217 | return ErrorStatus(errno); |
| 218 | } |
| 219 | |
| 220 | return std::move(data); |
| 221 | } |
| 222 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 223 | // Creates a new DisplaySurface and associates it with this channel. This may |
| 224 | // only be done once per channel. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 225 | Status<display::SurfaceInfo> DisplayService::OnCreateSurface( |
| 226 | pdx::Message& message, const display::SurfaceAttributes& attributes) { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 227 | // A surface may only be created once per channel. |
| 228 | if (message.GetChannel()) |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 229 | return ErrorStatus(EINVAL); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 230 | |
| 231 | ALOGI_IF(TRACE, "DisplayService::OnCreateSurface: cid=%d", |
| 232 | message.GetChannelId()); |
| 233 | |
| 234 | // Use the channel id as the unique surface id. |
| 235 | const int surface_id = message.GetChannelId(); |
| 236 | const int process_id = message.GetProcessId(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 237 | const int user_id = message.GetEffectiveUserId(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 238 | |
| 239 | ALOGI_IF(TRACE, |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 240 | "DisplayService::OnCreateSurface: surface_id=%d process_id=%d", |
| 241 | surface_id, process_id); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 242 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 243 | auto surface_status = |
| 244 | DisplaySurface::Create(this, surface_id, process_id, user_id, attributes); |
| 245 | if (!surface_status) { |
| 246 | ALOGE("DisplayService::OnCreateSurface: Failed to create surface: %s", |
| 247 | surface_status.GetErrorMessage().c_str()); |
| 248 | return ErrorStatus(surface_status.error()); |
| 249 | } |
Corey Tabaka | 00d9bb3 | 2017-08-16 19:59:48 -0700 | [diff] [blame] | 250 | auto surface = surface_status.take(); |
| 251 | message.SetChannel(surface); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 252 | |
Corey Tabaka | 00d9bb3 | 2017-08-16 19:59:48 -0700 | [diff] [blame] | 253 | // Update the surface with the attributes supplied with the create call. For |
| 254 | // application surfaces this has the side effect of notifying the display |
| 255 | // manager of the new surface. For direct surfaces, this may trigger a mode |
| 256 | // change, depending on the value of the visible attribute. |
| 257 | surface->OnSetAttributes(message, attributes); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 258 | |
Corey Tabaka | 00d9bb3 | 2017-08-16 19:59:48 -0700 | [diff] [blame] | 259 | return {{surface->surface_id(), surface->visible(), surface->z_order()}}; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 262 | void DisplayService::SurfaceUpdated(SurfaceType surface_type, |
| 263 | display::SurfaceUpdateFlags update_flags) { |
| 264 | ALOGD_IF(TRACE, "DisplayService::SurfaceUpdated: update_flags=%x", |
| 265 | update_flags.value()); |
| 266 | if (update_flags.value() != 0) { |
| 267 | if (surface_type == SurfaceType::Application) |
| 268 | NotifyDisplayConfigurationUpdate(); |
| 269 | else |
| 270 | UpdateActiveDisplaySurfaces(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 271 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Corey Tabaka | 99c2d73 | 2017-06-07 17:54:33 -0700 | [diff] [blame] | 274 | pdx::Status<BorrowedNativeBufferHandle> DisplayService::OnSetupGlobalBuffer( |
| 275 | pdx::Message& message, DvrGlobalBufferKey key, size_t size, |
| 276 | uint64_t usage) { |
| 277 | const int user_id = message.GetEffectiveUserId(); |
| 278 | const bool trusted = user_id == AID_ROOT || IsTrustedUid(user_id); |
| 279 | |
| 280 | if (!trusted) { |
| 281 | ALOGE( |
| 282 | "DisplayService::OnSetupGlobalBuffer: Permission denied for user_id=%d", |
| 283 | user_id); |
| 284 | return ErrorStatus(EPERM); |
| 285 | } |
| 286 | return SetupGlobalBuffer(key, size, usage); |
| 287 | } |
| 288 | |
| 289 | pdx::Status<void> DisplayService::OnDeleteGlobalBuffer(pdx::Message& message, |
| 290 | DvrGlobalBufferKey key) { |
| 291 | const int user_id = message.GetEffectiveUserId(); |
| 292 | const bool trusted = (user_id == AID_ROOT) || IsTrustedUid(user_id); |
| 293 | |
| 294 | if (!trusted) { |
| 295 | ALOGE( |
| 296 | "DisplayService::OnDeleteGlobalBuffer: Permission denied for " |
| 297 | "user_id=%d", |
| 298 | user_id); |
| 299 | return ErrorStatus(EPERM); |
| 300 | } |
| 301 | return DeleteGlobalBuffer(key); |
| 302 | } |
| 303 | |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 304 | pdx::Status<BorrowedNativeBufferHandle> DisplayService::OnGetGlobalBuffer( |
| 305 | pdx::Message& /* message */, DvrGlobalBufferKey key) { |
| 306 | ALOGD_IF(TRACE, "DisplayService::OnGetGlobalBuffer: key=%d", key); |
| 307 | auto global_buffer = global_buffers_.find(key); |
| 308 | if (global_buffer != global_buffers_.end()) |
| 309 | return {BorrowedNativeBufferHandle(*global_buffer->second, 0)}; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 310 | else |
| 311 | return pdx::ErrorStatus(EINVAL); |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 314 | // Calls the message handler for the DisplaySurface associated with this |
| 315 | // channel. |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 316 | Status<void> DisplayService::HandleSurfaceMessage(pdx::Message& message) { |
| 317 | auto surface = std::static_pointer_cast<DisplaySurface>(message.GetChannel()); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 318 | ALOGW_IF(!surface, |
| 319 | "DisplayService::HandleSurfaceMessage: surface is nullptr!"); |
| 320 | |
| 321 | if (surface) |
| 322 | return surface->HandleMessage(message); |
| 323 | else |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 324 | return ErrorStatus(EINVAL); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | std::shared_ptr<DisplaySurface> DisplayService::GetDisplaySurface( |
| 328 | int surface_id) const { |
| 329 | return std::static_pointer_cast<DisplaySurface>(GetChannel(surface_id)); |
| 330 | } |
| 331 | |
| 332 | std::vector<std::shared_ptr<DisplaySurface>> |
| 333 | DisplayService::GetDisplaySurfaces() const { |
| 334 | return GetChannels<DisplaySurface>(); |
| 335 | } |
| 336 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 337 | std::vector<std::shared_ptr<DirectDisplaySurface>> |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 338 | DisplayService::GetVisibleDisplaySurfaces() const { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 339 | std::vector<std::shared_ptr<DirectDisplaySurface>> visible_surfaces; |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 340 | |
| 341 | ForEachDisplaySurface( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 342 | SurfaceType::Direct, |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 343 | [&](const std::shared_ptr<DisplaySurface>& surface) mutable { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 344 | if (surface->visible()) { |
| 345 | visible_surfaces.push_back( |
| 346 | std::static_pointer_cast<DirectDisplaySurface>(surface)); |
| 347 | surface->ClearUpdate(); |
| 348 | } |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 349 | }); |
| 350 | |
| 351 | return visible_surfaces; |
| 352 | } |
| 353 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 354 | void DisplayService::UpdateActiveDisplaySurfaces() { |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 355 | auto visible_surfaces = GetVisibleDisplaySurfaces(); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 356 | ALOGD_IF(TRACE, |
| 357 | "DisplayService::UpdateActiveDisplaySurfaces: %zd visible surfaces", |
| 358 | visible_surfaces.size()); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 359 | hardware_composer_.SetDisplaySurfaces(std::move(visible_surfaces)); |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 362 | pdx::Status<BorrowedNativeBufferHandle> DisplayService::SetupGlobalBuffer( |
| 363 | DvrGlobalBufferKey key, size_t size, uint64_t usage) { |
| 364 | auto global_buffer = global_buffers_.find(key); |
| 365 | if (global_buffer == global_buffers_.end()) { |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 366 | auto ion_buffer = std::make_unique<IonBuffer>(static_cast<int>(size), 1, |
| 367 | HAL_PIXEL_FORMAT_BLOB, usage); |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 368 | |
| 369 | // Some buffers are used internally. If they were configured with an |
| 370 | // invalid size or format, this will fail. |
| 371 | int result = hardware_composer_.OnNewGlobalBuffer(key, *ion_buffer.get()); |
| 372 | if (result < 0) |
| 373 | return ErrorStatus(result); |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 374 | global_buffer = |
| 375 | global_buffers_.insert(std::make_pair(key, std::move(ion_buffer))) |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 376 | .first; |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 379 | return {BorrowedNativeBufferHandle(*global_buffer->second, 0)}; |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 380 | } |
| 381 | |
John Bates | 954796e | 2017-05-11 11:00:31 -0700 | [diff] [blame] | 382 | pdx::Status<void> DisplayService::DeleteGlobalBuffer(DvrGlobalBufferKey key) { |
| 383 | auto global_buffer = global_buffers_.find(key); |
| 384 | if (global_buffer != global_buffers_.end()) { |
| 385 | // Some buffers are used internally. |
| 386 | hardware_composer_.OnDeletedGlobalBuffer(key); |
| 387 | global_buffers_.erase(global_buffer); |
| 388 | } |
| 389 | |
| 390 | return {0}; |
| 391 | } |
| 392 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 393 | void DisplayService::SetDisplayConfigurationUpdateNotifier( |
| 394 | DisplayConfigurationUpdateNotifier update_notifier) { |
| 395 | update_notifier_ = update_notifier; |
| 396 | } |
| 397 | |
| 398 | void DisplayService::NotifyDisplayConfigurationUpdate() { |
| 399 | if (update_notifier_) |
| 400 | update_notifier_(); |
| 401 | } |
| 402 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 403 | Status<bool> DisplayService::IsVrAppRunning(pdx::Message& /*message*/) { |
Albert Chaulk | 356bc37 | 2017-04-05 18:01:58 -0400 | [diff] [blame] | 404 | bool visible = false; |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 405 | ForEachDisplaySurface( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 406 | SurfaceType::Application, |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 407 | [&visible](const std::shared_ptr<DisplaySurface>& surface) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 408 | if (surface->visible()) |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 409 | visible = true; |
| 410 | }); |
Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 411 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 412 | return {visible}; |
Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 415 | } // namespace dvr |
| 416 | } // namespace android |