blob: ac68a5e3a4f52587cf5dfae1f383b92f3c26b2cf [file] [log] [blame]
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08001#include "display_service.h"
2
Hendrik Wagenaareaa55222017-04-06 10:56:23 -07003#include <unistd.h>
Corey Tabaka0b485c92017-05-19 12:02:58 -07004
5#include <algorithm>
6#include <sstream>
7#include <string>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -08008#include <vector>
9
Hendrik Wagenaarbcb03d02017-05-23 14:59:08 -070010#include <android-base/file.h>
11#include <android-base/properties.h>
Corey Tabaka2251d822017-04-20 16:04:07 -070012#include <dvr/dvr_display_types.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080013#include <pdx/default_transport/service_endpoint.h>
14#include <pdx/rpc/remote_method.h>
Corey Tabaka99c2d732017-06-07 17:54:33 -070015#include <private/android_filesystem_config.h>
Corey Tabaka3f82d312017-04-20 14:42:08 -070016#include <private/dvr/display_protocol.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080017#include <private/dvr/numeric.h>
Corey Tabaka99c2d732017-06-07 17:54:33 -070018#include <private/dvr/trusted_uids.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080019#include <private/dvr/types.h>
20
Corey Tabaka2251d822017-04-20 16:04:07 -070021using android::dvr::display::DisplayProtocol;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080022using android::pdx::Channel;
Corey Tabaka2251d822017-04-20 16:04:07 -070023using android::pdx::ErrorStatus;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080024using android::pdx::Message;
Corey Tabaka2251d822017-04-20 16:04:07 -070025using android::pdx::Status;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080026using android::pdx::default_transport::Endpoint;
27using android::pdx::rpc::DispatchRemoteMethod;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080028
Hendrik Wagenaarbcb03d02017-05-23 14:59:08 -070029namespace {
30
31const char kDvrLensMetricsProperty[] = "ro.dvr.lens_metrics";
32const char kDvrDeviceMetricsProperty[] = "ro.dvr.device_metrics";
33const char kDvrDeviceConfigProperty[] = "ro.dvr.device_configuration";
34
35} // namespace
36
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080037namespace android {
38namespace dvr {
39
Corey Tabaka2251d822017-04-20 16:04:07 -070040DisplayService::DisplayService(Hwc2::Composer* hidl,
41 RequestDisplayCallback request_display_callback)
42 : BASE("DisplayService",
Steven Thomasd7f49c52017-07-26 18:48:28 -070043 Endpoint::Create(display::DisplayProtocol::kClientPath)) {
44 hardware_composer_.Initialize(hidl, request_display_callback);
Stephen Kiazyk016e5e32017-02-21 17:09:22 -080045}
46
47bool DisplayService::IsInitialized() const {
48 return BASE::IsInitialized() && hardware_composer_.IsInitialized();
49}
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080050
Corey Tabaka2251d822017-04-20 16:04:07 -070051std::string DisplayService::DumpState(size_t /*max_length*/) {
Corey Tabaka0b485c92017-05-19 12:02:58 -070052 std::ostringstream stream;
53
54 auto surfaces = GetDisplaySurfaces();
55 std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) {
56 return a->surface_id() < b->surface_id();
57 });
58
59 stream << "Application Surfaces:" << std::endl;
60
61 size_t count = 0;
62 for (const auto& surface : surfaces) {
63 if (surface->surface_type() == SurfaceType::Application) {
64 stream << "Surface " << count++ << ":";
65 stream << " surface_id=" << surface->surface_id()
66 << " process_id=" << surface->process_id()
67 << " user_id=" << surface->user_id()
68 << " visible=" << surface->visible()
69 << " z_order=" << surface->z_order();
70
71 stream << " queue_ids=";
72 auto queue_ids = surface->GetQueueIds();
73 std::sort(queue_ids.begin(), queue_ids.end());
74 for (int32_t id : queue_ids) {
75 if (id != queue_ids[0])
76 stream << ",";
77 stream << id;
78 }
79 stream << std::endl;
80 }
81 }
82 stream << std::endl;
83
84 stream << "Direct Surfaces:" << std::endl;
85
86 count = 0;
87 for (const auto& surface : surfaces) {
88 if (surface->surface_type() == SurfaceType::Direct) {
89 stream << "Surface " << count++ << ":";
90 stream << " surface_id=" << surface->surface_id()
91 << " process_id=" << surface->process_id()
92 << " user_id=" << surface->user_id()
93 << " visible=" << surface->visible()
94 << " z_order=" << surface->z_order();
95
96 stream << " queue_ids=";
97 auto queue_ids = surface->GetQueueIds();
98 std::sort(queue_ids.begin(), queue_ids.end());
99 for (int32_t id : queue_ids) {
100 if (id != queue_ids[0])
101 stream << ",";
102 stream << id;
103 }
104 stream << std::endl;
105 }
106 }
107 stream << std::endl;
108
109 stream << hardware_composer_.Dump();
110 return stream.str();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800111}
112
Corey Tabaka2251d822017-04-20 16:04:07 -0700113void DisplayService::OnChannelClose(pdx::Message& message,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800114 const std::shared_ptr<Channel>& channel) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700115 if (auto surface = std::static_pointer_cast<DisplaySurface>(channel)) {
116 surface->OnSetAttributes(message,
117 {{display::SurfaceAttribute::Visible,
118 display::SurfaceAttributeValue{false}}});
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800119 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800120}
121
122// First-level dispatch for display service messages. Directly handles messages
123// that are independent of the display surface (metrics, creation) and routes
124// surface-specific messages to the per-instance handlers.
Corey Tabaka2251d822017-04-20 16:04:07 -0700125Status<void> DisplayService::HandleMessage(pdx::Message& message) {
126 ALOGD_IF(TRACE, "DisplayService::HandleMessage: opcode=%d", message.GetOp());
Corey Tabakab3732f02017-09-16 00:58:54 -0700127 ATRACE_NAME("DisplayService::HandleMessage");
128
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800129 switch (message.GetOp()) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700130 case DisplayProtocol::GetMetrics::Opcode:
131 DispatchRemoteMethod<DisplayProtocol::GetMetrics>(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800132 *this, &DisplayService::OnGetMetrics, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700133 return {};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800134
Hendrik Wagenaarbcb03d02017-05-23 14:59:08 -0700135 case DisplayProtocol::GetConfigurationData::Opcode:
136 DispatchRemoteMethod<DisplayProtocol::GetConfigurationData>(
137 *this, &DisplayService::OnGetConfigurationData, message);
138 return {};
139
Corey Tabaka2251d822017-04-20 16:04:07 -0700140 case DisplayProtocol::CreateSurface::Opcode:
141 DispatchRemoteMethod<DisplayProtocol::CreateSurface>(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800142 *this, &DisplayService::OnCreateSurface, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700143 return {};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800144
Corey Tabaka99c2d732017-06-07 17:54:33 -0700145 case DisplayProtocol::SetupGlobalBuffer::Opcode:
146 DispatchRemoteMethod<DisplayProtocol::SetupGlobalBuffer>(
147 *this, &DisplayService::OnSetupGlobalBuffer, message);
148 return {};
149
150 case DisplayProtocol::DeleteGlobalBuffer::Opcode:
151 DispatchRemoteMethod<DisplayProtocol::DeleteGlobalBuffer>(
152 *this, &DisplayService::OnDeleteGlobalBuffer, message);
153 return {};
154
Okan Arikan36d23802017-05-15 15:20:39 -0700155 case DisplayProtocol::GetGlobalBuffer::Opcode:
156 DispatchRemoteMethod<DisplayProtocol::GetGlobalBuffer>(
157 *this, &DisplayService::OnGetGlobalBuffer, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700158 return {};
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700159
Corey Tabaka2251d822017-04-20 16:04:07 -0700160 case DisplayProtocol::IsVrAppRunning::Opcode:
161 DispatchRemoteMethod<DisplayProtocol::IsVrAppRunning>(
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400162 *this, &DisplayService::IsVrAppRunning, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700163 return {};
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400164
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800165 // Direct the surface specific messages to the surface instance.
Corey Tabaka2251d822017-04-20 16:04:07 -0700166 case DisplayProtocol::SetAttributes::Opcode:
167 case DisplayProtocol::CreateQueue::Opcode:
168 case DisplayProtocol::GetSurfaceInfo::Opcode:
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800169 return HandleSurfaceMessage(message);
170
171 default:
172 return Service::HandleMessage(message);
173 }
174}
175
Corey Tabaka2251d822017-04-20 16:04:07 -0700176Status<display::Metrics> DisplayService::OnGetMetrics(
177 pdx::Message& /*message*/) {
178 return {{static_cast<uint32_t>(GetDisplayMetrics().width),
179 static_cast<uint32_t>(GetDisplayMetrics().height),
180 static_cast<uint32_t>(GetDisplayMetrics().dpi.x),
181 static_cast<uint32_t>(GetDisplayMetrics().dpi.y),
182 static_cast<uint32_t>(
183 hardware_composer_.native_display_metrics().vsync_period_ns),
184 0,
185 0,
186 0,
187 0.0,
188 {},
189 {}}};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800190}
191
Hendrik Wagenaarbcb03d02017-05-23 14:59:08 -0700192pdx::Status<std::string> DisplayService::OnGetConfigurationData(
193 pdx::Message& /*message*/, display::ConfigFileType config_type) {
194 std::string property_name;
195 switch (config_type) {
196 case display::ConfigFileType::kLensMetrics:
197 property_name = kDvrLensMetricsProperty;
198 break;
199 case display::ConfigFileType::kDeviceMetrics:
200 property_name = kDvrDeviceMetricsProperty;
201 break;
202 case display::ConfigFileType::kDeviceConfiguration:
203 property_name = kDvrDeviceConfigProperty;
204 break;
205 default:
206 return ErrorStatus(EINVAL);
207 }
208 std::string file_path = base::GetProperty(property_name, "");
209 if (file_path.empty()) {
210 return ErrorStatus(ENOENT);
211 }
212
213 std::string data;
214 if (!base::ReadFileToString(file_path, &data)) {
215 return ErrorStatus(errno);
216 }
217
218 return std::move(data);
219}
220
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800221// Creates a new DisplaySurface and associates it with this channel. This may
222// only be done once per channel.
Corey Tabaka2251d822017-04-20 16:04:07 -0700223Status<display::SurfaceInfo> DisplayService::OnCreateSurface(
224 pdx::Message& message, const display::SurfaceAttributes& attributes) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800225 // A surface may only be created once per channel.
226 if (message.GetChannel())
Corey Tabaka2251d822017-04-20 16:04:07 -0700227 return ErrorStatus(EINVAL);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800228
229 ALOGI_IF(TRACE, "DisplayService::OnCreateSurface: cid=%d",
230 message.GetChannelId());
231
232 // Use the channel id as the unique surface id.
233 const int surface_id = message.GetChannelId();
234 const int process_id = message.GetProcessId();
Corey Tabaka2251d822017-04-20 16:04:07 -0700235 const int user_id = message.GetEffectiveUserId();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800236
237 ALOGI_IF(TRACE,
Corey Tabaka2251d822017-04-20 16:04:07 -0700238 "DisplayService::OnCreateSurface: surface_id=%d process_id=%d",
239 surface_id, process_id);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800240
Corey Tabaka2251d822017-04-20 16:04:07 -0700241 auto surface_status =
242 DisplaySurface::Create(this, surface_id, process_id, user_id, attributes);
243 if (!surface_status) {
244 ALOGE("DisplayService::OnCreateSurface: Failed to create surface: %s",
245 surface_status.GetErrorMessage().c_str());
246 return ErrorStatus(surface_status.error());
247 }
Corey Tabaka00d9bb32017-08-16 19:59:48 -0700248 auto surface = surface_status.take();
249 message.SetChannel(surface);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800250
Corey Tabaka00d9bb32017-08-16 19:59:48 -0700251 // Update the surface with the attributes supplied with the create call. For
252 // application surfaces this has the side effect of notifying the display
253 // manager of the new surface. For direct surfaces, this may trigger a mode
254 // change, depending on the value of the visible attribute.
255 surface->OnSetAttributes(message, attributes);
Corey Tabaka2251d822017-04-20 16:04:07 -0700256
Corey Tabaka00d9bb32017-08-16 19:59:48 -0700257 return {{surface->surface_id(), surface->visible(), surface->z_order()}};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800258}
259
Corey Tabaka2251d822017-04-20 16:04:07 -0700260void DisplayService::SurfaceUpdated(SurfaceType surface_type,
261 display::SurfaceUpdateFlags update_flags) {
262 ALOGD_IF(TRACE, "DisplayService::SurfaceUpdated: update_flags=%x",
263 update_flags.value());
264 if (update_flags.value() != 0) {
265 if (surface_type == SurfaceType::Application)
266 NotifyDisplayConfigurationUpdate();
267 else
268 UpdateActiveDisplaySurfaces();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800269 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800270}
271
Corey Tabaka99c2d732017-06-07 17:54:33 -0700272pdx::Status<BorrowedNativeBufferHandle> DisplayService::OnSetupGlobalBuffer(
273 pdx::Message& message, DvrGlobalBufferKey key, size_t size,
274 uint64_t usage) {
275 const int user_id = message.GetEffectiveUserId();
276 const bool trusted = user_id == AID_ROOT || IsTrustedUid(user_id);
277
278 if (!trusted) {
279 ALOGE(
280 "DisplayService::OnSetupGlobalBuffer: Permission denied for user_id=%d",
281 user_id);
282 return ErrorStatus(EPERM);
283 }
284 return SetupGlobalBuffer(key, size, usage);
285}
286
287pdx::Status<void> DisplayService::OnDeleteGlobalBuffer(pdx::Message& message,
288 DvrGlobalBufferKey key) {
289 const int user_id = message.GetEffectiveUserId();
290 const bool trusted = (user_id == AID_ROOT) || IsTrustedUid(user_id);
291
292 if (!trusted) {
293 ALOGE(
294 "DisplayService::OnDeleteGlobalBuffer: Permission denied for "
295 "user_id=%d",
296 user_id);
297 return ErrorStatus(EPERM);
298 }
299 return DeleteGlobalBuffer(key);
300}
301
Okan Arikan36d23802017-05-15 15:20:39 -0700302pdx::Status<BorrowedNativeBufferHandle> DisplayService::OnGetGlobalBuffer(
303 pdx::Message& /* message */, DvrGlobalBufferKey key) {
304 ALOGD_IF(TRACE, "DisplayService::OnGetGlobalBuffer: key=%d", key);
305 auto global_buffer = global_buffers_.find(key);
306 if (global_buffer != global_buffers_.end())
307 return {BorrowedNativeBufferHandle(*global_buffer->second, 0)};
Corey Tabaka2251d822017-04-20 16:04:07 -0700308 else
309 return pdx::ErrorStatus(EINVAL);
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700310}
311
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800312// Calls the message handler for the DisplaySurface associated with this
313// channel.
Corey Tabaka2251d822017-04-20 16:04:07 -0700314Status<void> DisplayService::HandleSurfaceMessage(pdx::Message& message) {
315 auto surface = std::static_pointer_cast<DisplaySurface>(message.GetChannel());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800316 ALOGW_IF(!surface,
317 "DisplayService::HandleSurfaceMessage: surface is nullptr!");
318
319 if (surface)
320 return surface->HandleMessage(message);
321 else
Corey Tabaka2251d822017-04-20 16:04:07 -0700322 return ErrorStatus(EINVAL);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800323}
324
325std::shared_ptr<DisplaySurface> DisplayService::GetDisplaySurface(
326 int surface_id) const {
327 return std::static_pointer_cast<DisplaySurface>(GetChannel(surface_id));
328}
329
330std::vector<std::shared_ptr<DisplaySurface>>
331DisplayService::GetDisplaySurfaces() const {
332 return GetChannels<DisplaySurface>();
333}
334
Corey Tabaka2251d822017-04-20 16:04:07 -0700335std::vector<std::shared_ptr<DirectDisplaySurface>>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800336DisplayService::GetVisibleDisplaySurfaces() const {
Corey Tabaka2251d822017-04-20 16:04:07 -0700337 std::vector<std::shared_ptr<DirectDisplaySurface>> visible_surfaces;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800338
339 ForEachDisplaySurface(
Corey Tabaka2251d822017-04-20 16:04:07 -0700340 SurfaceType::Direct,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800341 [&](const std::shared_ptr<DisplaySurface>& surface) mutable {
Corey Tabaka2251d822017-04-20 16:04:07 -0700342 if (surface->visible()) {
343 visible_surfaces.push_back(
344 std::static_pointer_cast<DirectDisplaySurface>(surface));
345 surface->ClearUpdate();
346 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800347 });
348
349 return visible_surfaces;
350}
351
Steven Thomas050b2c82017-03-06 11:45:16 -0800352void DisplayService::UpdateActiveDisplaySurfaces() {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800353 auto visible_surfaces = GetVisibleDisplaySurfaces();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800354 ALOGD_IF(TRACE,
355 "DisplayService::UpdateActiveDisplaySurfaces: %zd visible surfaces",
356 visible_surfaces.size());
Steven Thomas050b2c82017-03-06 11:45:16 -0800357 hardware_composer_.SetDisplaySurfaces(std::move(visible_surfaces));
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800358}
359
Okan Arikan36d23802017-05-15 15:20:39 -0700360pdx::Status<BorrowedNativeBufferHandle> DisplayService::SetupGlobalBuffer(
361 DvrGlobalBufferKey key, size_t size, uint64_t usage) {
362 auto global_buffer = global_buffers_.find(key);
363 if (global_buffer == global_buffers_.end()) {
Jiwen 'Steve' Cai0057fdd2017-05-02 11:21:18 -0700364 auto ion_buffer = std::make_unique<IonBuffer>(static_cast<int>(size), 1,
365 HAL_PIXEL_FORMAT_BLOB, usage);
John Bates954796e2017-05-11 11:00:31 -0700366
367 // Some buffers are used internally. If they were configured with an
368 // invalid size or format, this will fail.
369 int result = hardware_composer_.OnNewGlobalBuffer(key, *ion_buffer.get());
370 if (result < 0)
371 return ErrorStatus(result);
Okan Arikan36d23802017-05-15 15:20:39 -0700372 global_buffer =
373 global_buffers_.insert(std::make_pair(key, std::move(ion_buffer)))
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700374 .first;
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700375 }
376
Okan Arikan36d23802017-05-15 15:20:39 -0700377 return {BorrowedNativeBufferHandle(*global_buffer->second, 0)};
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700378}
379
John Bates954796e2017-05-11 11:00:31 -0700380pdx::Status<void> DisplayService::DeleteGlobalBuffer(DvrGlobalBufferKey key) {
381 auto global_buffer = global_buffers_.find(key);
382 if (global_buffer != global_buffers_.end()) {
383 // Some buffers are used internally.
384 hardware_composer_.OnDeletedGlobalBuffer(key);
385 global_buffers_.erase(global_buffer);
386 }
387
388 return {0};
389}
390
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800391void DisplayService::SetDisplayConfigurationUpdateNotifier(
392 DisplayConfigurationUpdateNotifier update_notifier) {
393 update_notifier_ = update_notifier;
394}
395
396void DisplayService::NotifyDisplayConfigurationUpdate() {
397 if (update_notifier_)
398 update_notifier_();
399}
400
Corey Tabaka2251d822017-04-20 16:04:07 -0700401Status<bool> DisplayService::IsVrAppRunning(pdx::Message& /*message*/) {
Albert Chaulk356bc372017-04-05 18:01:58 -0400402 bool visible = false;
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700403 ForEachDisplaySurface(
Corey Tabaka2251d822017-04-20 16:04:07 -0700404 SurfaceType::Application,
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700405 [&visible](const std::shared_ptr<DisplaySurface>& surface) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700406 if (surface->visible())
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700407 visible = true;
408 });
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400409
Corey Tabaka2251d822017-04-20 16:04:07 -0700410 return {visible};
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400411}
412
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800413} // namespace dvr
414} // namespace android