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