blob: 2f54f71770c30151a736f250464a3cf3cd736d0d [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 Tabaka3f82d312017-04-20 14:42:08 -070015#include <private/dvr/display_protocol.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080016#include <private/dvr/numeric.h>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080017#include <private/dvr/types.h>
18
Corey Tabaka2251d822017-04-20 16:04:07 -070019using android::dvr::display::DisplayProtocol;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080020using android::pdx::Channel;
Corey Tabaka2251d822017-04-20 16:04:07 -070021using android::pdx::ErrorStatus;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080022using android::pdx::Message;
Corey Tabaka2251d822017-04-20 16:04:07 -070023using android::pdx::Status;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080024using android::pdx::default_transport::Endpoint;
25using android::pdx::rpc::DispatchRemoteMethod;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080026
Hendrik Wagenaarbcb03d02017-05-23 14:59:08 -070027namespace {
28
29const char kDvrLensMetricsProperty[] = "ro.dvr.lens_metrics";
30const char kDvrDeviceMetricsProperty[] = "ro.dvr.device_metrics";
31const char kDvrDeviceConfigProperty[] = "ro.dvr.device_configuration";
32
33} // namespace
34
Alex Vakulenkoa8a92782017-01-27 14:41:57 -080035namespace android {
36namespace dvr {
37
Corey Tabaka2251d822017-04-20 16:04:07 -070038DisplayService::DisplayService(Hwc2::Composer* hidl,
39 RequestDisplayCallback request_display_callback)
40 : BASE("DisplayService",
41 Endpoint::Create(display::DisplayProtocol::kClientPath)),
42 hardware_composer_(hidl, request_display_callback),
43 request_display_callback_(request_display_callback) {
Stephen Kiazyk016e5e32017-02-21 17:09:22 -080044 hardware_composer_.Initialize();
45}
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());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800127 switch (message.GetOp()) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700128 case DisplayProtocol::GetMetrics::Opcode:
129 DispatchRemoteMethod<DisplayProtocol::GetMetrics>(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800130 *this, &DisplayService::OnGetMetrics, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700131 return {};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800132
Hendrik Wagenaarbcb03d02017-05-23 14:59:08 -0700133 case DisplayProtocol::GetConfigurationData::Opcode:
134 DispatchRemoteMethod<DisplayProtocol::GetConfigurationData>(
135 *this, &DisplayService::OnGetConfigurationData, message);
136 return {};
137
Corey Tabaka2251d822017-04-20 16:04:07 -0700138 case DisplayProtocol::CreateSurface::Opcode:
139 DispatchRemoteMethod<DisplayProtocol::CreateSurface>(
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800140 *this, &DisplayService::OnCreateSurface, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700141 return {};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800142
Okan Arikan36d23802017-05-15 15:20:39 -0700143 case DisplayProtocol::GetGlobalBuffer::Opcode:
144 DispatchRemoteMethod<DisplayProtocol::GetGlobalBuffer>(
145 *this, &DisplayService::OnGetGlobalBuffer, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700146 return {};
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700147
Corey Tabaka2251d822017-04-20 16:04:07 -0700148 case DisplayProtocol::IsVrAppRunning::Opcode:
149 DispatchRemoteMethod<DisplayProtocol::IsVrAppRunning>(
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400150 *this, &DisplayService::IsVrAppRunning, message);
Alex Vakulenkof0a7bd02017-03-31 18:06:19 -0700151 return {};
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400152
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800153 // Direct the surface specific messages to the surface instance.
Corey Tabaka2251d822017-04-20 16:04:07 -0700154 case DisplayProtocol::SetAttributes::Opcode:
155 case DisplayProtocol::CreateQueue::Opcode:
156 case DisplayProtocol::GetSurfaceInfo::Opcode:
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800157 return HandleSurfaceMessage(message);
158
159 default:
160 return Service::HandleMessage(message);
161 }
162}
163
Corey Tabaka2251d822017-04-20 16:04:07 -0700164Status<display::Metrics> DisplayService::OnGetMetrics(
165 pdx::Message& /*message*/) {
166 return {{static_cast<uint32_t>(GetDisplayMetrics().width),
167 static_cast<uint32_t>(GetDisplayMetrics().height),
168 static_cast<uint32_t>(GetDisplayMetrics().dpi.x),
169 static_cast<uint32_t>(GetDisplayMetrics().dpi.y),
170 static_cast<uint32_t>(
171 hardware_composer_.native_display_metrics().vsync_period_ns),
172 0,
173 0,
174 0,
175 0.0,
176 {},
177 {}}};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800178}
179
Hendrik Wagenaarbcb03d02017-05-23 14:59:08 -0700180pdx::Status<std::string> DisplayService::OnGetConfigurationData(
181 pdx::Message& /*message*/, display::ConfigFileType config_type) {
182 std::string property_name;
183 switch (config_type) {
184 case display::ConfigFileType::kLensMetrics:
185 property_name = kDvrLensMetricsProperty;
186 break;
187 case display::ConfigFileType::kDeviceMetrics:
188 property_name = kDvrDeviceMetricsProperty;
189 break;
190 case display::ConfigFileType::kDeviceConfiguration:
191 property_name = kDvrDeviceConfigProperty;
192 break;
193 default:
194 return ErrorStatus(EINVAL);
195 }
196 std::string file_path = base::GetProperty(property_name, "");
197 if (file_path.empty()) {
198 return ErrorStatus(ENOENT);
199 }
200
201 std::string data;
202 if (!base::ReadFileToString(file_path, &data)) {
203 return ErrorStatus(errno);
204 }
205
206 return std::move(data);
207}
208
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800209// Creates a new DisplaySurface and associates it with this channel. This may
210// only be done once per channel.
Corey Tabaka2251d822017-04-20 16:04:07 -0700211Status<display::SurfaceInfo> DisplayService::OnCreateSurface(
212 pdx::Message& message, const display::SurfaceAttributes& attributes) {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800213 // A surface may only be created once per channel.
214 if (message.GetChannel())
Corey Tabaka2251d822017-04-20 16:04:07 -0700215 return ErrorStatus(EINVAL);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800216
217 ALOGI_IF(TRACE, "DisplayService::OnCreateSurface: cid=%d",
218 message.GetChannelId());
219
220 // Use the channel id as the unique surface id.
221 const int surface_id = message.GetChannelId();
222 const int process_id = message.GetProcessId();
Corey Tabaka2251d822017-04-20 16:04:07 -0700223 const int user_id = message.GetEffectiveUserId();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800224
225 ALOGI_IF(TRACE,
Corey Tabaka2251d822017-04-20 16:04:07 -0700226 "DisplayService::OnCreateSurface: surface_id=%d process_id=%d",
227 surface_id, process_id);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800228
Corey Tabaka2251d822017-04-20 16:04:07 -0700229 auto surface_status =
230 DisplaySurface::Create(this, surface_id, process_id, user_id, attributes);
231 if (!surface_status) {
232 ALOGE("DisplayService::OnCreateSurface: Failed to create surface: %s",
233 surface_status.GetErrorMessage().c_str());
234 return ErrorStatus(surface_status.error());
235 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800236
Corey Tabaka2251d822017-04-20 16:04:07 -0700237 SurfaceType surface_type = surface_status.get()->surface_type();
238 display::SurfaceUpdateFlags update_flags =
239 surface_status.get()->update_flags();
240 display::SurfaceInfo surface_info{surface_status.get()->surface_id(),
241 surface_status.get()->visible(),
242 surface_status.get()->z_order()};
243
244 message.SetChannel(surface_status.take());
245
246 SurfaceUpdated(surface_type, update_flags);
247 return {surface_info};
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800248}
249
Corey Tabaka2251d822017-04-20 16:04:07 -0700250void DisplayService::SurfaceUpdated(SurfaceType surface_type,
251 display::SurfaceUpdateFlags update_flags) {
252 ALOGD_IF(TRACE, "DisplayService::SurfaceUpdated: update_flags=%x",
253 update_flags.value());
254 if (update_flags.value() != 0) {
255 if (surface_type == SurfaceType::Application)
256 NotifyDisplayConfigurationUpdate();
257 else
258 UpdateActiveDisplaySurfaces();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800259 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800260}
261
Okan Arikan36d23802017-05-15 15:20:39 -0700262pdx::Status<BorrowedNativeBufferHandle> DisplayService::OnGetGlobalBuffer(
263 pdx::Message& /* message */, DvrGlobalBufferKey key) {
264 ALOGD_IF(TRACE, "DisplayService::OnGetGlobalBuffer: key=%d", key);
265 auto global_buffer = global_buffers_.find(key);
266 if (global_buffer != global_buffers_.end())
267 return {BorrowedNativeBufferHandle(*global_buffer->second, 0)};
Corey Tabaka2251d822017-04-20 16:04:07 -0700268 else
269 return pdx::ErrorStatus(EINVAL);
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700270}
271
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800272// Calls the message handler for the DisplaySurface associated with this
273// channel.
Corey Tabaka2251d822017-04-20 16:04:07 -0700274Status<void> DisplayService::HandleSurfaceMessage(pdx::Message& message) {
275 auto surface = std::static_pointer_cast<DisplaySurface>(message.GetChannel());
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800276 ALOGW_IF(!surface,
277 "DisplayService::HandleSurfaceMessage: surface is nullptr!");
278
279 if (surface)
280 return surface->HandleMessage(message);
281 else
Corey Tabaka2251d822017-04-20 16:04:07 -0700282 return ErrorStatus(EINVAL);
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800283}
284
285std::shared_ptr<DisplaySurface> DisplayService::GetDisplaySurface(
286 int surface_id) const {
287 return std::static_pointer_cast<DisplaySurface>(GetChannel(surface_id));
288}
289
290std::vector<std::shared_ptr<DisplaySurface>>
291DisplayService::GetDisplaySurfaces() const {
292 return GetChannels<DisplaySurface>();
293}
294
Corey Tabaka2251d822017-04-20 16:04:07 -0700295std::vector<std::shared_ptr<DirectDisplaySurface>>
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800296DisplayService::GetVisibleDisplaySurfaces() const {
Corey Tabaka2251d822017-04-20 16:04:07 -0700297 std::vector<std::shared_ptr<DirectDisplaySurface>> visible_surfaces;
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800298
299 ForEachDisplaySurface(
Corey Tabaka2251d822017-04-20 16:04:07 -0700300 SurfaceType::Direct,
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800301 [&](const std::shared_ptr<DisplaySurface>& surface) mutable {
Corey Tabaka2251d822017-04-20 16:04:07 -0700302 if (surface->visible()) {
303 visible_surfaces.push_back(
304 std::static_pointer_cast<DirectDisplaySurface>(surface));
305 surface->ClearUpdate();
306 }
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800307 });
308
309 return visible_surfaces;
310}
311
Steven Thomas050b2c82017-03-06 11:45:16 -0800312void DisplayService::UpdateActiveDisplaySurfaces() {
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800313 auto visible_surfaces = GetVisibleDisplaySurfaces();
314
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800315 std::sort(visible_surfaces.begin(), visible_surfaces.end(),
316 [](const std::shared_ptr<DisplaySurface>& a,
317 const std::shared_ptr<DisplaySurface>& b) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700318 return a->z_order() < b->z_order();
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800319 });
320
321 ALOGD_IF(TRACE,
322 "DisplayService::UpdateActiveDisplaySurfaces: %zd visible surfaces",
323 visible_surfaces.size());
324
Steven Thomas050b2c82017-03-06 11:45:16 -0800325 hardware_composer_.SetDisplaySurfaces(std::move(visible_surfaces));
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800326}
327
Okan Arikan36d23802017-05-15 15:20:39 -0700328pdx::Status<BorrowedNativeBufferHandle> DisplayService::SetupGlobalBuffer(
329 DvrGlobalBufferKey key, size_t size, uint64_t usage) {
330 auto global_buffer = global_buffers_.find(key);
331 if (global_buffer == global_buffers_.end()) {
Jiwen 'Steve' Cai0057fdd2017-05-02 11:21:18 -0700332 auto ion_buffer = std::make_unique<IonBuffer>(static_cast<int>(size), 1,
333 HAL_PIXEL_FORMAT_BLOB, usage);
John Bates954796e2017-05-11 11:00:31 -0700334
335 // Some buffers are used internally. If they were configured with an
336 // invalid size or format, this will fail.
337 int result = hardware_composer_.OnNewGlobalBuffer(key, *ion_buffer.get());
338 if (result < 0)
339 return ErrorStatus(result);
Okan Arikan36d23802017-05-15 15:20:39 -0700340 global_buffer =
341 global_buffers_.insert(std::make_pair(key, std::move(ion_buffer)))
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700342 .first;
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700343 }
344
Okan Arikan36d23802017-05-15 15:20:39 -0700345 return {BorrowedNativeBufferHandle(*global_buffer->second, 0)};
Hendrik Wagenaar10e68eb2017-03-15 13:29:02 -0700346}
347
John Bates954796e2017-05-11 11:00:31 -0700348pdx::Status<void> DisplayService::DeleteGlobalBuffer(DvrGlobalBufferKey key) {
349 auto global_buffer = global_buffers_.find(key);
350 if (global_buffer != global_buffers_.end()) {
351 // Some buffers are used internally.
352 hardware_composer_.OnDeletedGlobalBuffer(key);
353 global_buffers_.erase(global_buffer);
354 }
355
356 return {0};
357}
358
Steven Thomas3cfac282017-02-06 12:29:30 -0800359void DisplayService::OnHardwareComposerRefresh() {
360 hardware_composer_.OnHardwareComposerRefresh();
361}
362
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800363void DisplayService::SetDisplayConfigurationUpdateNotifier(
364 DisplayConfigurationUpdateNotifier update_notifier) {
365 update_notifier_ = update_notifier;
366}
367
368void DisplayService::NotifyDisplayConfigurationUpdate() {
369 if (update_notifier_)
370 update_notifier_();
371}
372
Corey Tabaka2251d822017-04-20 16:04:07 -0700373Status<bool> DisplayService::IsVrAppRunning(pdx::Message& /*message*/) {
Albert Chaulk356bc372017-04-05 18:01:58 -0400374 bool visible = false;
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700375 ForEachDisplaySurface(
Corey Tabaka2251d822017-04-20 16:04:07 -0700376 SurfaceType::Application,
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700377 [&visible](const std::shared_ptr<DisplaySurface>& surface) {
Corey Tabaka2251d822017-04-20 16:04:07 -0700378 if (surface->visible())
Hendrik Wagenaareaa55222017-04-06 10:56:23 -0700379 visible = true;
380 });
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400381
Corey Tabaka2251d822017-04-20 16:04:07 -0700382 return {visible};
Albert Chaulkb7c8a4b2017-03-20 13:03:39 -0400383}
384
Alex Vakulenkoa8a92782017-01-27 14:41:57 -0800385} // namespace dvr
386} // namespace android