| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #include "display_surface.h" | 
|  | 2 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 3 | #include <private/android_filesystem_config.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 4 | #include <utils/Trace.h> | 
|  | 5 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 6 | #include <private/dvr/trusted_uids.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 7 |  | 
|  | 8 | #include "display_service.h" | 
|  | 9 | #include "hardware_composer.h" | 
|  | 10 |  | 
|  | 11 | #define LOCAL_TRACE 1 | 
|  | 12 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 13 | using android::dvr::display::DisplayProtocol; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 14 | using android::pdx::BorrowedChannelHandle; | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 15 | using android::pdx::ErrorStatus; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 16 | using android::pdx::LocalChannelHandle; | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 17 | using android::pdx::LocalHandle; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 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; | 
|  | 23 |  | 
|  | 24 | namespace android { | 
|  | 25 | namespace dvr { | 
|  | 26 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 27 | DisplaySurface::DisplaySurface(DisplayService* service, | 
|  | 28 | SurfaceType surface_type, int surface_id, | 
|  | 29 | int process_id, int user_id, | 
|  | 30 | const display::SurfaceAttributes& attributes) | 
|  | 31 | : service_(service), | 
|  | 32 | surface_type_(surface_type), | 
|  | 33 | surface_id_(surface_id), | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 34 | process_id_(process_id), | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 35 | user_id_(user_id), | 
|  | 36 | attributes_(attributes), | 
|  | 37 | update_flags_(display::SurfaceUpdateFlags::NewSurface) {} | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | DisplaySurface::~DisplaySurface() { | 
|  | 40 | ALOGD_IF(LOCAL_TRACE, | 
|  | 41 | "DisplaySurface::~DisplaySurface: surface_id=%d process_id=%d", | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 42 | surface_id(), process_id()); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 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); | 
|  | 50 | break; | 
|  | 51 |  | 
|  | 52 | case DisplayProtocol::GetSurfaceInfo::Opcode: | 
|  | 53 | DispatchRemoteMethod<DisplayProtocol::GetSurfaceInfo>( | 
|  | 54 | *this, &DisplaySurface::OnGetSurfaceInfo, message); | 
|  | 55 | break; | 
|  | 56 |  | 
|  | 57 | case DisplayProtocol::CreateQueue::Opcode: | 
|  | 58 | DispatchRemoteMethod<DisplayProtocol::CreateQueue>( | 
|  | 59 | *this, &DisplaySurface::OnCreateQueue, message); | 
|  | 60 | break; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | return {}; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | Status<void> DisplaySurface::OnSetAttributes( | 
|  | 67 | pdx::Message& /*message*/, const display::SurfaceAttributes& attributes) { | 
|  | 68 | display::SurfaceUpdateFlags update_flags; | 
|  | 69 |  | 
|  | 70 | for (const auto& attribute : attributes) { | 
| Corey Tabaka | 49a706d | 2017-06-08 14:29:58 -0700 | [diff] [blame] | 71 | const auto key = attribute.first; | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 72 | const auto* variant = &attribute.second; | 
|  | 73 | bool invalid_value = false; | 
|  | 74 | bool visibility_changed = false; | 
|  | 75 |  | 
|  | 76 | // Catch attributes that have significance to the display service. | 
|  | 77 | switch (key) { | 
|  | 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; | 
|  | 83 | z_order_ = value; | 
|  | 84 | } | 
|  | 85 | }); | 
|  | 86 | break; | 
|  | 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; | 
|  | 92 | visible_ = value; | 
|  | 93 | } | 
|  | 94 | }); | 
|  | 95 | break; | 
|  | 96 | } | 
|  | 97 |  | 
| Corey Tabaka | 49a706d | 2017-06-08 14:29:58 -0700 | [diff] [blame] | 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. | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 101 | if (invalid_value) { | 
|  | 102 | ALOGW( | 
|  | 103 | "DisplaySurface::OnClientSetAttributes: Failed to set display " | 
|  | 104 | "surface attribute '%d' because of incompatible type: %d", | 
|  | 105 | key, variant->index()); | 
|  | 106 | } else { | 
| Corey Tabaka | 49a706d | 2017-06-08 14:29:58 -0700 | [diff] [blame] | 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); | 
|  | 112 | } else { | 
|  | 113 | attributes_[key] = *variant; | 
|  | 114 | } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | // All attribute changes generate a notification, even if the value | 
|  | 117 | // doesn't change. Visibility attributes set a flag only if the value | 
|  | 118 | // changes. | 
|  | 119 | update_flags.Set(display::SurfaceUpdateFlags::AttributesChanged); | 
|  | 120 | if (visibility_changed) | 
|  | 121 | update_flags.Set(display::SurfaceUpdateFlags::VisibilityChanged); | 
|  | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | SurfaceUpdated(update_flags); | 
|  | 126 | return {}; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | void DisplaySurface::SurfaceUpdated(display::SurfaceUpdateFlags update_flags) { | 
|  | 130 | ALOGD_IF(TRACE, | 
|  | 131 | "DisplaySurface::SurfaceUpdated: surface_id=%d update_flags=0x%x", | 
|  | 132 | surface_id(), update_flags.value()); | 
|  | 133 |  | 
|  | 134 | update_flags_.Set(update_flags); | 
|  | 135 | service()->SurfaceUpdated(surface_type(), update_flags_); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | void DisplaySurface::ClearUpdate() { | 
| Corey Tabaka | 89bbefc | 2017-06-06 16:14:21 -0700 | [diff] [blame] | 139 | ALOGD_IF(TRACE > 1, "DisplaySurface::ClearUpdate: surface_id=%d", | 
|  | 140 | surface_id()); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 141 | update_flags_ = display::SurfaceUpdateFlags::None; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | Status<display::SurfaceInfo> DisplaySurface::OnGetSurfaceInfo( | 
|  | 145 | Message& /*message*/) { | 
|  | 146 | ALOGD_IF( | 
|  | 147 | TRACE, | 
|  | 148 | "DisplaySurface::OnGetSurfaceInfo: surface_id=%d visible=%d z_order=%d", | 
|  | 149 | surface_id(), visible(), z_order()); | 
|  | 150 | return {{surface_id(), visible(), z_order()}}; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 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); } | 
|  | 163 | // | 
|  | 164 | struct State { | 
|  | 165 | std::shared_ptr<DisplaySurface> surface; | 
|  | 166 | std::shared_ptr<ConsumerQueue> queue; | 
|  | 167 | }; | 
|  | 168 | State state{Self(), consumer_queue}; | 
|  | 169 |  | 
|  | 170 | return service()->AddEventHandler( | 
|  | 171 | consumer_queue->queue_fd(), EPOLLIN | EPOLLHUP | EPOLLET, | 
|  | 172 | [state](int events) { | 
|  | 173 | state.surface->OnQueueEvent(state.queue, events); | 
|  | 174 | }); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 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()); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | void DisplaySurface::OnQueueEvent( | 
|  | 185 | const std::shared_ptr<ConsumerQueue>& /*consumer_queue*/, int /*events*/) { | 
|  | 186 | ALOGE( | 
|  | 187 | "DisplaySurface::OnQueueEvent: ERROR base virtual method should not be " | 
|  | 188 | "called!!!"); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | std::shared_ptr<ConsumerQueue> ApplicationDisplaySurface::GetQueue( | 
|  | 192 | int32_t queue_id) { | 
|  | 193 | ALOGD_IF(TRACE, | 
|  | 194 | "ApplicationDisplaySurface::GetQueue: surface_id=%d queue_id=%d", | 
|  | 195 | surface_id(), queue_id); | 
|  | 196 |  | 
|  | 197 | auto search = consumer_queues_.find(queue_id); | 
|  | 198 | if (search != consumer_queues_.end()) | 
|  | 199 | return search->second; | 
|  | 200 | else | 
|  | 201 | return nullptr; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 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); | 
|  | 208 | return queue_ids; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | Status<LocalChannelHandle> ApplicationDisplaySurface::OnCreateQueue( | 
| Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 212 | Message& /*message*/, const ProducerQueueConfig& config) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 213 | ATRACE_NAME("ApplicationDisplaySurface::OnCreateQueue"); | 
|  | 214 | ALOGD_IF(TRACE, | 
|  | 215 | "ApplicationDisplaySurface::OnCreateQueue: surface_id=%d, " | 
|  | 216 | "meta_size_bytes=%zu", | 
| Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 217 | surface_id(), config.meta_size_bytes); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 218 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 219 | std::lock_guard<std::mutex> autolock(lock_); | 
| Jiwen 'Steve' Cai | 6bffc67 | 2017-05-18 23:05:05 -0700 | [diff] [blame] | 220 | auto producer = ProducerQueue::Create(config, UsagePolicy{}); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 221 | if (!producer) { | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 222 | ALOGE( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 223 | "ApplicationDisplaySurface::OnCreateQueue: Failed to create producer " | 
|  | 224 | "queue!"); | 
|  | 225 | return ErrorStatus(ENOMEM); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | std::shared_ptr<ConsumerQueue> consumer = | 
|  | 229 | producer->CreateSilentConsumerQueue(); | 
|  | 230 | auto status = RegisterQueue(consumer); | 
|  | 231 | if (!status) { | 
|  | 232 | ALOGE( | 
|  | 233 | "ApplicationDisplaySurface::OnCreateQueue: Failed to register consumer " | 
|  | 234 | "queue: %s", | 
|  | 235 | status.GetErrorMessage().c_str()); | 
|  | 236 | return status.error_status(); | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | consumer_queues_[consumer->id()] = std::move(consumer); | 
|  | 240 |  | 
|  | 241 | SurfaceUpdated(display::SurfaceUpdateFlags::BuffersChanged); | 
|  | 242 | return std::move(producer->GetChannelHandle()); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | void ApplicationDisplaySurface::OnQueueEvent( | 
|  | 246 | const std::shared_ptr<ConsumerQueue>& consumer_queue, int events) { | 
|  | 247 | ALOGD_IF(TRACE, | 
|  | 248 | "ApplicationDisplaySurface::OnQueueEvent: queue_id=%d events=%x", | 
|  | 249 | consumer_queue->id(), events); | 
|  | 250 |  | 
|  | 251 | // Always give the queue a chance to handle its internal bookkeeping. | 
|  | 252 | consumer_queue->HandleQueueEvents(); | 
|  | 253 |  | 
|  | 254 | // Check for hangup and remove a queue that is no longer needed. | 
|  | 255 | std::lock_guard<std::mutex> autolock(lock_); | 
|  | 256 | if (consumer_queue->hung_up()) { | 
|  | 257 | ALOGD_IF(TRACE, "ApplicationDisplaySurface::OnQueueEvent: Removing queue."); | 
|  | 258 | UnregisterQueue(consumer_queue); | 
|  | 259 | auto search = consumer_queues_.find(consumer_queue->id()); | 
|  | 260 | if (search != consumer_queues_.end()) { | 
|  | 261 | consumer_queues_.erase(search); | 
|  | 262 | } else { | 
|  | 263 | ALOGE( | 
|  | 264 | "ApplicationDisplaySurface::OnQueueEvent: Failed to find queue_id=%d", | 
|  | 265 | consumer_queue->id()); | 
|  | 266 | } | 
|  | 267 | SurfaceUpdated(display::SurfaceUpdateFlags::BuffersChanged); | 
|  | 268 | } | 
|  | 269 | } | 
|  | 270 |  | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 271 | std::vector<int32_t> DirectDisplaySurface::GetQueueIds() const { | 
|  | 272 | std::vector<int32_t> queue_ids; | 
|  | 273 | if (direct_queue_) | 
|  | 274 | queue_ids.push_back(direct_queue_->id()); | 
|  | 275 | return queue_ids; | 
|  | 276 | } | 
|  | 277 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 278 | Status<LocalChannelHandle> DirectDisplaySurface::OnCreateQueue( | 
| Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 279 | Message& /*message*/, const ProducerQueueConfig& config) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 280 | ATRACE_NAME("DirectDisplaySurface::OnCreateQueue"); | 
|  | 281 | ALOGD_IF( | 
|  | 282 | TRACE, | 
|  | 283 | "DirectDisplaySurface::OnCreateQueue: surface_id=%d meta_size_bytes=%zu", | 
| Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 284 | surface_id(), config.meta_size_bytes); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 285 |  | 
|  | 286 | std::lock_guard<std::mutex> autolock(lock_); | 
|  | 287 | if (!direct_queue_) { | 
| Corey Tabaka | a936f7a | 2017-05-18 17:28:07 -0700 | [diff] [blame] | 288 | // Inject the hw composer usage flag to enable the display to read the | 
|  | 289 | // buffers. | 
|  | 290 | auto producer = ProducerQueue::Create( | 
| Jiwen 'Steve' Cai | 6bffc67 | 2017-05-18 23:05:05 -0700 | [diff] [blame] | 291 | config, UsagePolicy{GraphicBuffer::USAGE_HW_COMPOSER, 0, 0, 0}); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 292 | if (!producer) { | 
|  | 293 | ALOGE( | 
|  | 294 | "DirectDisplaySurface::OnCreateQueue: Failed to create producer " | 
|  | 295 | "queue!"); | 
|  | 296 | return ErrorStatus(ENOMEM); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | direct_queue_ = producer->CreateConsumerQueue(); | 
|  | 300 | auto status = RegisterQueue(direct_queue_); | 
|  | 301 | if (!status) { | 
|  | 302 | ALOGE( | 
|  | 303 | "DirectDisplaySurface::OnCreateQueue: Failed to register consumer " | 
|  | 304 | "queue: %s", | 
|  | 305 | status.GetErrorMessage().c_str()); | 
|  | 306 | return status.error_status(); | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | return std::move(producer->GetChannelHandle()); | 
|  | 310 | } else { | 
|  | 311 | return ErrorStatus(EALREADY); | 
|  | 312 | } | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | void DirectDisplaySurface::OnQueueEvent( | 
|  | 316 | const std::shared_ptr<ConsumerQueue>& consumer_queue, int events) { | 
|  | 317 | ALOGD_IF(TRACE, "DirectDisplaySurface::OnQueueEvent: queue_id=%d events=%x", | 
|  | 318 | consumer_queue->id(), events); | 
|  | 319 |  | 
|  | 320 | // Always give the queue a chance to handle its internal bookkeeping. | 
|  | 321 | consumer_queue->HandleQueueEvents(); | 
|  | 322 |  | 
|  | 323 | // Check for hangup and remove a queue that is no longer needed. | 
|  | 324 | std::lock_guard<std::mutex> autolock(lock_); | 
|  | 325 | if (consumer_queue->hung_up()) { | 
|  | 326 | ALOGD_IF(TRACE, "DirectDisplaySurface::OnQueueEvent: Removing queue."); | 
|  | 327 | UnregisterQueue(consumer_queue); | 
|  | 328 | direct_queue_ = nullptr; | 
|  | 329 | } | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | void DirectDisplaySurface::DequeueBuffersLocked() { | 
|  | 333 | if (direct_queue_ == nullptr) { | 
|  | 334 | ALOGE( | 
|  | 335 | "DirectDisplaySurface::DequeueBuffersLocked: Consumer queue is not " | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 336 | "initialized."); | 
|  | 337 | return; | 
|  | 338 | } | 
|  | 339 |  | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 340 | while (true) { | 
|  | 341 | LocalHandle acquire_fence; | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 342 | size_t slot; | 
|  | 343 | auto buffer_status = direct_queue_->Dequeue(0, &slot, &acquire_fence); | 
|  | 344 | if (!buffer_status) { | 
|  | 345 | ALOGD_IF( | 
| Corey Tabaka | 89bbefc | 2017-06-06 16:14:21 -0700 | [diff] [blame] | 346 | TRACE > 1 && buffer_status.error() == ETIMEDOUT, | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 347 | "DirectDisplaySurface::DequeueBuffersLocked: All buffers dequeued."); | 
|  | 348 | ALOGE_IF(buffer_status.error() != ETIMEDOUT, | 
|  | 349 | "DirectDisplaySurface::DequeueBuffersLocked: Failed to dequeue " | 
|  | 350 | "buffer: %s", | 
|  | 351 | buffer_status.GetErrorMessage().c_str()); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 352 | return; | 
|  | 353 | } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 354 | auto buffer_consumer = buffer_status.take(); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 355 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 356 | if (!visible()) { | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 357 | ATRACE_NAME("DropFrameOnInvisibleSurface"); | 
|  | 358 | ALOGD_IF(TRACE, | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 359 | "DirectDisplaySurface::DequeueBuffersLocked: Discarding " | 
|  | 360 | "buffer_id=%d on invisible surface.", | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 361 | buffer_consumer->id()); | 
|  | 362 | buffer_consumer->Discard(); | 
|  | 363 | continue; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | if (acquired_buffers_.IsFull()) { | 
|  | 367 | ALOGE( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 368 | "DirectDisplaySurface::DequeueBuffersLocked: Posted buffers full, " | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 369 | "overwriting."); | 
|  | 370 | acquired_buffers_.PopBack(); | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | acquired_buffers_.Append( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 374 | AcquiredBuffer(buffer_consumer, std::move(acquire_fence))); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 375 | } | 
|  | 376 | } | 
|  | 377 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 378 | AcquiredBuffer DirectDisplaySurface::AcquireCurrentBuffer() { | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 379 | std::lock_guard<std::mutex> autolock(lock_); | 
|  | 380 | DequeueBuffersLocked(); | 
|  | 381 |  | 
|  | 382 | if (acquired_buffers_.IsEmpty()) { | 
|  | 383 | ALOGE( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 384 | "DirectDisplaySurface::AcquireCurrentBuffer: attempt to acquire buffer " | 
|  | 385 | "when none are posted."); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 386 | return AcquiredBuffer(); | 
|  | 387 | } | 
|  | 388 | AcquiredBuffer buffer = std::move(acquired_buffers_.Front()); | 
|  | 389 | acquired_buffers_.PopFront(); | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 390 | ALOGD_IF(TRACE, "DirectDisplaySurface::AcquireCurrentBuffer: buffer_id=%d", | 
|  | 391 | buffer.buffer()->id()); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 392 | return buffer; | 
|  | 393 | } | 
|  | 394 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 395 | AcquiredBuffer DirectDisplaySurface::AcquireNewestAvailableBuffer( | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 396 | AcquiredBuffer* skipped_buffer) { | 
|  | 397 | std::lock_guard<std::mutex> autolock(lock_); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 398 | DequeueBuffersLocked(); | 
|  | 399 |  | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 400 | AcquiredBuffer buffer; | 
|  | 401 | int frames = 0; | 
|  | 402 | // Basic latency stopgap for when the application misses a frame: | 
|  | 403 | // If the application recovers on the 2nd or 3rd (etc) frame after | 
|  | 404 | // missing, this code will skip frames to catch up by checking if | 
|  | 405 | // the next frame is also available. | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 406 | while (!acquired_buffers_.IsEmpty() && | 
|  | 407 | acquired_buffers_.Front().IsAvailable()) { | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 408 | // Capture the skipped buffer into the result parameter. | 
|  | 409 | // Note that this API only supports skipping one buffer per vsync. | 
|  | 410 | if (frames > 0 && skipped_buffer) | 
|  | 411 | *skipped_buffer = std::move(buffer); | 
|  | 412 | ++frames; | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 413 | buffer = std::move(acquired_buffers_.Front()); | 
|  | 414 | acquired_buffers_.PopFront(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 415 | if (frames == 2) | 
|  | 416 | break; | 
|  | 417 | } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 418 | ALOGD_IF(TRACE, | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 419 | "DirectDisplaySurface::AcquireNewestAvailableBuffer: buffer_id=%d", | 
|  | 420 | buffer.buffer()->id()); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 421 | return buffer; | 
|  | 422 | } | 
|  | 423 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 424 | bool DirectDisplaySurface::IsBufferAvailable() { | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 425 | std::lock_guard<std::mutex> autolock(lock_); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 426 | DequeueBuffersLocked(); | 
|  | 427 |  | 
|  | 428 | return !acquired_buffers_.IsEmpty() && | 
|  | 429 | acquired_buffers_.Front().IsAvailable(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 430 | } | 
|  | 431 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 432 | bool DirectDisplaySurface::IsBufferPosted() { | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 433 | std::lock_guard<std::mutex> autolock(lock_); | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 434 | DequeueBuffersLocked(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 435 |  | 
| Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 436 | return !acquired_buffers_.IsEmpty(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 439 | Status<std::shared_ptr<DisplaySurface>> DisplaySurface::Create( | 
|  | 440 | DisplayService* service, int surface_id, int process_id, int user_id, | 
|  | 441 | const display::SurfaceAttributes& attributes) { | 
|  | 442 | bool direct = false; | 
|  | 443 | auto search = attributes.find(display::SurfaceAttribute::Direct); | 
|  | 444 | if (search != attributes.end()) { | 
|  | 445 | if (!IfAnyOf<int32_t, int64_t, bool, float>::Get(&search->second, | 
|  | 446 | &direct)) { | 
|  | 447 | ALOGE( | 
|  | 448 | "DisplaySurface::Create: Invalid type for SurfaceAttribute::Direct!"); | 
|  | 449 | return ErrorStatus(EINVAL); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 450 | } | 
|  | 451 | } | 
|  | 452 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 453 | ALOGD_IF(TRACE, | 
|  | 454 | "DisplaySurface::Create: surface_id=%d process_id=%d user_id=%d " | 
|  | 455 | "direct=%d", | 
|  | 456 | surface_id, process_id, user_id, direct); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 457 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 458 | if (direct) { | 
|  | 459 | const bool trusted = user_id == AID_ROOT || IsTrustedUid(user_id); | 
|  | 460 | if (trusted) { | 
|  | 461 | return {std::shared_ptr<DisplaySurface>{new DirectDisplaySurface( | 
|  | 462 | service, surface_id, process_id, user_id, attributes)}}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 463 | } else { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 464 | ALOGE( | 
|  | 465 | "DisplaySurface::Create: Direct surfaces may only be created by " | 
|  | 466 | "trusted UIDs: user_id=%d", | 
|  | 467 | user_id); | 
|  | 468 | return ErrorStatus(EPERM); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 469 | } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 470 | } else { | 
|  | 471 | return {std::shared_ptr<DisplaySurface>{new ApplicationDisplaySurface( | 
|  | 472 | service, surface_id, process_id, user_id, attributes)}}; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 473 | } | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 474 | } | 
|  | 475 |  | 
|  | 476 | }  // namespace dvr | 
|  | 477 | }  // namespace android |