1 #include "display_surface.h"
3 #include <private/android_filesystem_config.h>
4 #include <utils/Trace.h>
6 #include <private/dvr/trusted_uids.h>
8 #include "display_service.h"
9 #include "hardware_composer.h"
13 using android::dvr::display::DisplayProtocol;
14 using android::pdx::BorrowedChannelHandle;
15 using android::pdx::ErrorStatus;
16 using android::pdx::LocalChannelHandle;
17 using android::pdx::LocalHandle;
18 using android::pdx::Message;
19 using android::pdx::RemoteChannelHandle;
20 using android::pdx::Status;
21 using android::pdx::rpc::DispatchRemoteMethod;
22 using android::pdx::rpc::IfAnyOf;
27 DisplaySurface::DisplaySurface(DisplayService* service,
28 SurfaceType surface_type, int surface_id,
29 int process_id, int user_id,
30 const display::SurfaceAttributes& attributes)
32 surface_type_(surface_type),
33 surface_id_(surface_id),
34 process_id_(process_id),
36 attributes_(attributes),
37 update_flags_(display::SurfaceUpdateFlags::NewSurface) {}
39 DisplaySurface::~DisplaySurface() {
41 "DisplaySurface::~DisplaySurface: surface_id=%d process_id=%d",
42 surface_id(), process_id());
45 Status<void> DisplaySurface::HandleMessage(pdx::Message& message) {
46 switch (message.GetOp()) {
47 case DisplayProtocol::SetAttributes::Opcode:
48 DispatchRemoteMethod<DisplayProtocol::SetAttributes>(
49 *this, &DisplaySurface::OnSetAttributes, message);
52 case DisplayProtocol::GetSurfaceInfo::Opcode:
53 DispatchRemoteMethod<DisplayProtocol::GetSurfaceInfo>(
54 *this, &DisplaySurface::OnGetSurfaceInfo, message);
57 case DisplayProtocol::CreateQueue::Opcode:
58 DispatchRemoteMethod<DisplayProtocol::CreateQueue>(
59 *this, &DisplaySurface::OnCreateQueue, message);
66 Status<void> DisplaySurface::OnSetAttributes(
67 pdx::Message& /*message*/, const display::SurfaceAttributes& attributes) {
68 display::SurfaceUpdateFlags update_flags;
70 for (const auto& attribute : attributes) {
71 const auto key = attribute.first;
72 const auto* variant = &attribute.second;
73 bool invalid_value = false;
74 bool visibility_changed = false;
76 // Catch attributes that have significance to the display service.
78 case display::SurfaceAttribute::ZOrder:
79 invalid_value = !IfAnyOf<int32_t, int64_t, float>::Call(
80 variant, [&](const auto& value) {
81 if (z_order_ != value) {
82 visibility_changed = true;
87 case display::SurfaceAttribute::Visible:
88 invalid_value = !IfAnyOf<int32_t, int64_t, bool>::Call(
89 variant, [&](const auto& value) {
90 if (visible_ != value) {
91 visibility_changed = true;
98 // Only update the attribute map with valid values. This check also has the
99 // effect of preventing special attributes handled above from being deleted
100 // by an empty value.
103 "DisplaySurface::OnClientSetAttributes: Failed to set display "
104 "surface attribute '%d' because of incompatible type: %d",
105 key, variant->index());
107 // An empty value indicates the attribute should be deleted.
108 if (variant->empty()) {
109 auto search = attributes_.find(key);
110 if (search != attributes_.end())
111 attributes_.erase(search);
113 attributes_[key] = *variant;
116 // All attribute changes generate a notification, even if the value
117 // doesn't change. Visibility attributes set a flag only if the value
119 update_flags.Set(display::SurfaceUpdateFlags::AttributesChanged);
120 if (visibility_changed)
121 update_flags.Set(display::SurfaceUpdateFlags::VisibilityChanged);
125 SurfaceUpdated(update_flags);
129 void DisplaySurface::SurfaceUpdated(display::SurfaceUpdateFlags update_flags) {
131 "DisplaySurface::SurfaceUpdated: surface_id=%d update_flags=0x%x",
132 surface_id(), update_flags.value());
134 update_flags_.Set(update_flags);
135 service()->SurfaceUpdated(surface_type(), update_flags_);
138 void DisplaySurface::ClearUpdate() {
139 ALOGD_IF(TRACE > 1, "DisplaySurface::ClearUpdate: surface_id=%d",
141 update_flags_ = display::SurfaceUpdateFlags::None;
144 Status<display::SurfaceInfo> DisplaySurface::OnGetSurfaceInfo(
145 Message& /*message*/) {
148 "DisplaySurface::OnGetSurfaceInfo: surface_id=%d visible=%d z_order=%d",
149 surface_id(), visible(), z_order());
150 return {{surface_id(), visible(), z_order()}};
153 Status<void> DisplaySurface::RegisterQueue(
154 const std::shared_ptr<ConsumerQueue>& consumer_queue) {
155 ALOGD_IF(TRACE, "DisplaySurface::RegisterQueue: surface_id=%d queue_id=%d",
156 surface_id(), consumer_queue->id());
157 // Capture references for the lambda to work around apparent clang bug.
158 // TODO(eieio): Figure out if there is a clang bug or C++11 ambiguity when
159 // capturing self and consumer_queue by copy in the following case:
160 // auto self = Self();
161 // [self, consumer_queue](int events) {
162 // self->OnQueueEvent(consuemr_queue, events); }
165 std::shared_ptr<DisplaySurface> surface;
166 std::shared_ptr<ConsumerQueue> queue;
168 State state{Self(), consumer_queue};
170 return service()->AddEventHandler(
171 consumer_queue->queue_fd(), EPOLLIN | EPOLLHUP | EPOLLET,
172 [state](int events) {
173 state.surface->OnQueueEvent(state.queue, events);
177 Status<void> DisplaySurface::UnregisterQueue(
178 const std::shared_ptr<ConsumerQueue>& consumer_queue) {
179 ALOGD_IF(TRACE, "DisplaySurface::UnregisterQueue: surface_id=%d queue_id=%d",
180 surface_id(), consumer_queue->id());
181 return service()->RemoveEventHandler(consumer_queue->queue_fd());
184 void DisplaySurface::OnQueueEvent(
185 const std::shared_ptr<ConsumerQueue>& /*consumer_queue*/, int /*events*/) {
187 "DisplaySurface::OnQueueEvent: ERROR base virtual method should not be "
191 std::shared_ptr<ConsumerQueue> ApplicationDisplaySurface::GetQueue(
194 "ApplicationDisplaySurface::GetQueue: surface_id=%d queue_id=%d",
195 surface_id(), queue_id);
197 auto search = consumer_queues_.find(queue_id);
198 if (search != consumer_queues_.end())
199 return search->second;
204 std::vector<int32_t> ApplicationDisplaySurface::GetQueueIds() const {
205 std::vector<int32_t> queue_ids;
206 for (const auto& entry : consumer_queues_)
207 queue_ids.push_back(entry.first);
211 Status<LocalChannelHandle> ApplicationDisplaySurface::OnCreateQueue(
212 Message& /*message*/, const ProducerQueueConfig& config) {
213 ATRACE_NAME("ApplicationDisplaySurface::OnCreateQueue");
215 "ApplicationDisplaySurface::OnCreateQueue: surface_id=%d, "
216 "meta_size_bytes=%zu",
217 surface_id(), config.meta_size_bytes);
219 std::lock_guard<std::mutex> autolock(lock_);
220 auto producer = ProducerQueue::Create(config, UsagePolicy{});
223 "ApplicationDisplaySurface::OnCreateQueue: Failed to create producer "
225 return ErrorStatus(ENOMEM);
228 std::shared_ptr<ConsumerQueue> consumer =
229 producer->CreateSilentConsumerQueue();
230 auto status = RegisterQueue(consumer);
233 "ApplicationDisplaySurface::OnCreateQueue: Failed to register consumer "
235 status.GetErrorMessage().c_str());
236 return status.error_status();
239 consumer_queues_[consumer->id()] = std::move(consumer);
241 SurfaceUpdated(display::SurfaceUpdateFlags::BuffersChanged);
242 return std::move(producer->GetChannelHandle());
245 void ApplicationDisplaySurface::OnQueueEvent(
246 const std::shared_ptr<ConsumerQueue>& consumer_queue, int events) {
248 "ApplicationDisplaySurface::OnQueueEvent: queue_id=%d events=%x",
249 consumer_queue->id(), events);
251 std::lock_guard<std::mutex> autolock(lock_);
253 // Always give the queue a chance to handle its internal bookkeeping.
254 consumer_queue->HandleQueueEvents();
256 // Check for hangup and remove a queue that is no longer needed.
257 if (consumer_queue->hung_up()) {
258 ALOGD_IF(TRACE, "ApplicationDisplaySurface::OnQueueEvent: Removing queue.");
259 UnregisterQueue(consumer_queue);
260 auto search = consumer_queues_.find(consumer_queue->id());
261 if (search != consumer_queues_.end()) {
262 consumer_queues_.erase(search);
265 "ApplicationDisplaySurface::OnQueueEvent: Failed to find queue_id=%d",
266 consumer_queue->id());
268 SurfaceUpdated(display::SurfaceUpdateFlags::BuffersChanged);
272 std::vector<int32_t> DirectDisplaySurface::GetQueueIds() const {
273 std::vector<int32_t> queue_ids;
275 queue_ids.push_back(direct_queue_->id());
279 Status<LocalChannelHandle> DirectDisplaySurface::OnCreateQueue(
280 Message& /*message*/, const ProducerQueueConfig& config) {
281 ATRACE_NAME("DirectDisplaySurface::OnCreateQueue");
284 "DirectDisplaySurface::OnCreateQueue: surface_id=%d meta_size_bytes=%zu",
285 surface_id(), config.meta_size_bytes);
287 std::lock_guard<std::mutex> autolock(lock_);
288 if (!direct_queue_) {
289 // Inject the hw composer usage flag to enable the display to read the
291 auto producer = ProducerQueue::Create(
292 config, UsagePolicy{GraphicBuffer::USAGE_HW_COMPOSER, 0, 0, 0});
295 "DirectDisplaySurface::OnCreateQueue: Failed to create producer "
297 return ErrorStatus(ENOMEM);
300 direct_queue_ = producer->CreateConsumerQueue();
301 if (direct_queue_->metadata_size() > 0) {
302 metadata_.reset(new uint8_t[direct_queue_->metadata_size()]);
304 auto status = RegisterQueue(direct_queue_);
307 "DirectDisplaySurface::OnCreateQueue: Failed to register consumer "
309 status.GetErrorMessage().c_str());
310 return status.error_status();
313 return std::move(producer->GetChannelHandle());
315 return ErrorStatus(EALREADY);
319 void DirectDisplaySurface::OnQueueEvent(
320 const std::shared_ptr<ConsumerQueue>& consumer_queue, int events) {
321 ALOGD_IF(TRACE, "DirectDisplaySurface::OnQueueEvent: queue_id=%d events=%x",
322 consumer_queue->id(), events);
324 std::lock_guard<std::mutex> autolock(lock_);
326 // Always give the queue a chance to handle its internal bookkeeping.
327 consumer_queue->HandleQueueEvents();
329 // Check for hangup and remove a queue that is no longer needed.
330 if (consumer_queue->hung_up()) {
331 ALOGD_IF(TRACE, "DirectDisplaySurface::OnQueueEvent: Removing queue.");
332 UnregisterQueue(consumer_queue);
333 direct_queue_ = nullptr;
337 void DirectDisplaySurface::DequeueBuffersLocked() {
338 if (direct_queue_ == nullptr) {
340 "DirectDisplaySurface::DequeueBuffersLocked: Consumer queue is not "
346 LocalHandle acquire_fence;
348 auto buffer_status = direct_queue_->Dequeue(
349 0, &slot, metadata_.get(),
350 direct_queue_->metadata_size(), &acquire_fence);
352 "DirectDisplaySurface::DequeueBuffersLocked: Dequeue with metadata_size: %zu",
353 direct_queue_->metadata_size());
354 if (!buffer_status) {
356 TRACE > 1 && buffer_status.error() == ETIMEDOUT,
357 "DirectDisplaySurface::DequeueBuffersLocked: All buffers dequeued.");
358 ALOGE_IF(buffer_status.error() != ETIMEDOUT,
359 "DirectDisplaySurface::DequeueBuffersLocked: Failed to dequeue "
361 buffer_status.GetErrorMessage().c_str());
364 auto buffer_consumer = buffer_status.take();
367 ATRACE_NAME("DropFrameOnInvisibleSurface");
369 "DirectDisplaySurface::DequeueBuffersLocked: Discarding "
370 "buffer_id=%d on invisible surface.",
371 buffer_consumer->id());
372 buffer_consumer->Discard();
376 if (acquired_buffers_.IsFull()) {
378 "DirectDisplaySurface::DequeueBuffersLocked: Posted buffers full, "
380 acquired_buffers_.PopBack();
383 acquired_buffers_.Append(
384 AcquiredBuffer(buffer_consumer, std::move(acquire_fence)));
388 AcquiredBuffer DirectDisplaySurface::AcquireCurrentBuffer() {
389 std::lock_guard<std::mutex> autolock(lock_);
390 DequeueBuffersLocked();
392 if (acquired_buffers_.IsEmpty()) {
394 "DirectDisplaySurface::AcquireCurrentBuffer: attempt to acquire buffer "
395 "when none are posted.");
396 return AcquiredBuffer();
398 AcquiredBuffer buffer = std::move(acquired_buffers_.Front());
399 acquired_buffers_.PopFront();
400 ALOGD_IF(TRACE, "DirectDisplaySurface::AcquireCurrentBuffer: buffer_id=%d",
401 buffer.buffer()->id());
405 AcquiredBuffer DirectDisplaySurface::AcquireNewestAvailableBuffer(
406 AcquiredBuffer* skipped_buffer) {
407 std::lock_guard<std::mutex> autolock(lock_);
408 DequeueBuffersLocked();
410 AcquiredBuffer buffer;
412 // Basic latency stopgap for when the application misses a frame:
413 // If the application recovers on the 2nd or 3rd (etc) frame after
414 // missing, this code will skip frames to catch up by checking if
415 // the next frame is also available.
416 while (!acquired_buffers_.IsEmpty() &&
417 acquired_buffers_.Front().IsAvailable()) {
418 // Capture the skipped buffer into the result parameter.
419 // Note that this API only supports skipping one buffer per vsync.
420 if (frames > 0 && skipped_buffer)
421 *skipped_buffer = std::move(buffer);
423 buffer = std::move(acquired_buffers_.Front());
424 acquired_buffers_.PopFront();
429 "DirectDisplaySurface::AcquireNewestAvailableBuffer: buffer_id=%d",
430 buffer.buffer()->id());
434 bool DirectDisplaySurface::IsBufferAvailable() {
435 std::lock_guard<std::mutex> autolock(lock_);
436 DequeueBuffersLocked();
438 return !acquired_buffers_.IsEmpty() &&
439 acquired_buffers_.Front().IsAvailable();
442 bool DirectDisplaySurface::IsBufferPosted() {
443 std::lock_guard<std::mutex> autolock(lock_);
444 DequeueBuffersLocked();
446 return !acquired_buffers_.IsEmpty();
449 Status<std::shared_ptr<DisplaySurface>> DisplaySurface::Create(
450 DisplayService* service, int surface_id, int process_id, int user_id,
451 const display::SurfaceAttributes& attributes) {
453 auto search = attributes.find(display::SurfaceAttribute::Direct);
454 if (search != attributes.end()) {
455 if (!IfAnyOf<int32_t, int64_t, bool, float>::Get(&search->second,
458 "DisplaySurface::Create: Invalid type for SurfaceAttribute::Direct!");
459 return ErrorStatus(EINVAL);
464 "DisplaySurface::Create: surface_id=%d process_id=%d user_id=%d "
466 surface_id, process_id, user_id, direct);
469 const bool trusted = user_id == AID_ROOT || IsTrustedUid(user_id);
471 return {std::shared_ptr<DisplaySurface>{new DirectDisplaySurface(
472 service, surface_id, process_id, user_id, attributes)}};
475 "DisplaySurface::Create: Direct surfaces may only be created by "
476 "trusted UIDs: user_id=%d",
478 return ErrorStatus(EPERM);
481 return {std::shared_ptr<DisplaySurface>{new ApplicationDisplaySurface(
482 service, surface_id, process_id, user_id, attributes)}};
487 } // namespace android