1/* 2* Copyright (c) 2014 - 2017, The Linux Foundation. All rights reserved. 3* 4* Redistribution and use in source and binary forms, with or without 5* modification, are permitted provided that the following conditions are 6* met: 7* * Redistributions of source code must retain the above copyright 8* notice, this list of conditions and the following disclaimer. 9* * Redistributions in binary form must reproduce the above 10* copyright notice, this list of conditions and the following 11* disclaimer in the documentation and/or other materials provided 12* with the distribution. 13* * Neither the name of The Linux Foundation nor the names of its 14* contributors may be used to endorse or promote products derived 15* from this software without specific prior written permission. 16* 17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*/ 29 30#include <cutils/properties.h> 31#include <sync/sync.h> 32#include <utils/constants.h> 33#include <utils/debug.h> 34#include <stdarg.h> 35#include <sys/mman.h> 36 37#include <gr.h> 38#include "hwc_display_primary.h" 39#include "hwc_debugger.h" 40 41#define __CLASS__ "HWCDisplayPrimary" 42 43namespace sdm { 44 45int HWCDisplayPrimary::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator, 46 hwc_procs_t const **hwc_procs, qService::QService *qservice, 47 HWCDisplay **hwc_display) { 48 int status = 0; 49 uint32_t primary_width = 0; 50 uint32_t primary_height = 0; 51 52 HWCDisplay *hwc_display_primary = new HWCDisplayPrimary(core_intf, buffer_allocator, 53 hwc_procs, qservice); 54 status = hwc_display_primary->Init(); 55 if (status) { 56 delete hwc_display_primary; 57 return status; 58 } 59 60 hwc_display_primary->GetMixerResolution(&primary_width, &primary_height); 61 int width = 0, height = 0; 62 HWCDebugHandler::Get()->GetProperty("sdm.fb_size_width", &width); 63 HWCDebugHandler::Get()->GetProperty("sdm.fb_size_height", &height); 64 if (width > 0 && height > 0) { 65 primary_width = UINT32(width); 66 primary_height = UINT32(height); 67 } 68 69 status = hwc_display_primary->SetFrameBufferResolution(primary_width, primary_height); 70 if (status) { 71 Destroy(hwc_display_primary); 72 return status; 73 } 74 75 *hwc_display = hwc_display_primary; 76 77 return status; 78} 79 80void HWCDisplayPrimary::Destroy(HWCDisplay *hwc_display) { 81 hwc_display->Deinit(); 82 delete hwc_display; 83} 84 85HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, 86 BufferAllocator *buffer_allocator, 87 hwc_procs_t const **hwc_procs, 88 qService::QService *qservice) 89 : HWCDisplay(core_intf, hwc_procs, kPrimary, HWC_DISPLAY_PRIMARY, true, qservice, 90 DISPLAY_CLASS_PRIMARY), buffer_allocator_(buffer_allocator) { 91} 92 93int HWCDisplayPrimary::Init() { 94 cpu_hint_.Init(static_cast<HWCDebugHandler*>(HWCDebugHandler::Get())); 95 96 use_metadata_refresh_rate_ = true; 97 int disable_metadata_dynfps = 0; 98 HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps); 99 if (disable_metadata_dynfps) { 100 use_metadata_refresh_rate_ = false; 101 } 102 103 int status = HWCDisplay::Init(); 104 if (status) { 105 return status; 106 } 107 color_mode_ = new HWCColorMode(display_intf_); 108 color_mode_->Init(); 109 110 return status; 111} 112 113int HWCDisplayPrimary::Deinit() { 114 color_mode_->DeInit(); 115 delete color_mode_; 116 color_mode_ = NULL; 117 118 return HWCDisplay::Deinit(); 119} 120 121 122void HWCDisplayPrimary::ProcessBootAnimCompleted(hwc_display_contents_1_t *list) { 123 uint32_t numBootUpLayers = 0; 124 125 numBootUpLayers = static_cast<uint32_t>(Debug::GetBootAnimLayerCount()); 126 127 if (numBootUpLayers == 0) { 128 numBootUpLayers = 2; 129 } 130 /* All other checks namely "init.svc.bootanim" or 131 * HWC_GEOMETRY_CHANGED fail in correctly identifying the 132 * exact bootup transition to homescreen 133 */ 134 char cryptoState[PROPERTY_VALUE_MAX]; 135 char voldDecryptState[PROPERTY_VALUE_MAX]; 136 bool isEncrypted = false; 137 bool main_class_services_started = false; 138 if (property_get("ro.crypto.state", cryptoState, "unencrypted")) { 139 if (!strcmp(cryptoState, "encrypted")) { 140 isEncrypted = true; 141 if (property_get("vold.decrypt", voldDecryptState, "") && 142 !strcmp(voldDecryptState, "trigger_restart_framework")) 143 main_class_services_started = true; 144 } 145 } 146 if ((!isEncrypted ||(isEncrypted && main_class_services_started)) && 147 (list->numHwLayers > numBootUpLayers)) { 148 boot_animation_completed_ = true; 149 // Applying default mode after bootanimation is finished And 150 // If Data is Encrypted, it is ready for access. 151 if (display_intf_) 152 display_intf_->ApplyDefaultDisplayMode(); 153 } 154} 155 156int HWCDisplayPrimary::Prepare(hwc_display_contents_1_t *content_list) { 157 int status = 0; 158 DisplayError error = kErrorNone; 159 160 if (!boot_animation_completed_) 161 ProcessBootAnimCompleted(content_list); 162 163 if (display_paused_) { 164 MarkLayersForGPUBypass(content_list); 165 return status; 166 } 167 168 status = AllocateLayerStack(content_list); 169 if (status) { 170 return status; 171 } 172 173 status = PrePrepareLayerStack(content_list); 174 if (status) { 175 return status; 176 } 177 178 bool pending_output_dump = dump_frame_count_ && dump_output_to_file_; 179 180 if (frame_capture_buffer_queued_ || pending_output_dump) { 181 // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up 182 // here in a subsequent draw round. 183 layer_stack_.output_buffer = &output_buffer_; 184 layer_stack_.flags.post_processed_output = post_processed_output_; 185 } 186 187 uint32_t num_updating_layers = GetUpdatingLayersCount(UINT32(content_list->numHwLayers - 1)); 188 bool one_updating_layer = (num_updating_layers == 1); 189 190 if (num_updating_layers != 0) { 191 ToggleCPUHint(one_updating_layer); 192 } 193 194 uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer); 195 // TODO(user): Need to read current refresh rate to avoid 196 // redundant calls to set refresh rate during idle fall back. 197 if ((current_refresh_rate_ != refresh_rate) || (handle_idle_timeout_)) { 198 error = display_intf_->SetRefreshRate(refresh_rate); 199 } 200 201 if (error == kErrorNone) { 202 // On success, set current refresh rate to new refresh rate 203 current_refresh_rate_ = refresh_rate; 204 } 205 206 if (handle_idle_timeout_) { 207 handle_idle_timeout_ = false; 208 } 209 210 if (content_list->numHwLayers <= 1) { 211 flush_ = true; 212 } 213 214 status = PrepareLayerStack(content_list); 215 if (status) { 216 return status; 217 } 218 219 return 0; 220} 221 222int HWCDisplayPrimary::Commit(hwc_display_contents_1_t *content_list) { 223 int status = 0; 224 225 DisplayConfigFixedInfo display_config; 226 display_intf_->GetConfig(&display_config); 227 if (content_list->numHwLayers <= 1 && display_config.is_cmdmode) { 228 DLOGV("Skipping null commit on cmd mode panel"); 229 flush_ = false; 230 return 0; 231 } 232 233 if (display_paused_) { 234 if (content_list->outbufAcquireFenceFd >= 0) { 235 // If we do not handle the frame set retireFenceFd to outbufAcquireFenceFd, 236 // which will make sure the framework waits on it and closes it. 237 content_list->retireFenceFd = dup(content_list->outbufAcquireFenceFd); 238 close(content_list->outbufAcquireFenceFd); 239 content_list->outbufAcquireFenceFd = -1; 240 } 241 242 DisplayError error = display_intf_->Flush(); 243 if (error != kErrorNone) { 244 DLOGE("Flush failed. Error = %d", error); 245 } 246 return status; 247 } 248 249 status = HWCDisplay::CommitLayerStack(content_list); 250 if (status) { 251 return status; 252 } 253 254 HandleFrameOutput(); 255 256 status = HWCDisplay::PostCommitLayerStack(content_list); 257 if (status) { 258 return status; 259 } 260 261 return 0; 262} 263 264int HWCDisplayPrimary::Perform(uint32_t operation, ...) { 265 va_list args; 266 va_start(args, operation); 267 int val = va_arg(args, int32_t); 268 va_end(args); 269 switch (operation) { 270 case SET_METADATA_DYN_REFRESH_RATE: 271 SetMetaDataRefreshRateFlag(val); 272 break; 273 case SET_BINDER_DYN_REFRESH_RATE: 274 ForceRefreshRate(UINT32(val)); 275 break; 276 case SET_DISPLAY_MODE: 277 SetDisplayMode(UINT32(val)); 278 break; 279 case SET_QDCM_SOLID_FILL_INFO: 280 SetQDCMSolidFillInfo(true, UINT32(val)); 281 break; 282 case UNSET_QDCM_SOLID_FILL_INFO: 283 SetQDCMSolidFillInfo(false, UINT32(val)); 284 break; 285 default: 286 DLOGW("Invalid operation %d", operation); 287 return -EINVAL; 288 } 289 290 return 0; 291} 292 293DisplayError HWCDisplayPrimary::SetDisplayMode(uint32_t mode) { 294 DisplayError error = kErrorNone; 295 296 if (display_intf_) { 297 error = display_intf_->SetDisplayMode(mode); 298 } 299 300 return error; 301} 302 303void HWCDisplayPrimary::SetMetaDataRefreshRateFlag(bool enable) { 304 int disable_metadata_dynfps = 0; 305 306 HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps); 307 if (disable_metadata_dynfps) { 308 return; 309 } 310 use_metadata_refresh_rate_ = enable; 311} 312 313void HWCDisplayPrimary::SetQDCMSolidFillInfo(bool enable, uint32_t color) { 314 solid_fill_enable_ = enable; 315 solid_fill_color_ = color; 316} 317 318void HWCDisplayPrimary::ToggleCPUHint(bool set) { 319 if (set) { 320 cpu_hint_.Set(); 321 } else { 322 cpu_hint_.Reset(); 323 } 324} 325 326void HWCDisplayPrimary::SetSecureDisplay(bool secure_display_active, bool force_flush) { 327 if (secure_display_active_ != secure_display_active) { 328 // Skip Prepare and call Flush for null commit 329 DLOGI("SecureDisplay state changed from %d to %d Needs Flush!!", secure_display_active_, 330 secure_display_active); 331 secure_display_active_ = secure_display_active; 332 skip_prepare_cnt = 1; 333 334 // Issue two null commits for command mode panels when external displays are connected. 335 // Two null commits are required to handle non secure to secure transitions at 30fps. 336 // TODO(user): Need two null commits on video mode also to handle transition cases of 337 // primary at higher fps (ex60) and external at lower fps. 338 339 // Avoid flush for command mode panels when no external displays are connected. 340 // This is to avoid flicker/blink on primary during transitions. 341 DisplayConfigFixedInfo display_config; 342 display_intf_->GetConfig(&display_config); 343 if (display_config.is_cmdmode) { 344 if (force_flush) { 345 DLOGI("Issue two null commits for command mode panels"); 346 skip_prepare_cnt = 2; 347 } else { 348 DLOGI("Avoid flush for command mode panel when no external displays are connected"); 349 skip_prepare_cnt = 0; 350 } 351 } 352 } 353} 354 355void HWCDisplayPrimary::ForceRefreshRate(uint32_t refresh_rate) { 356 if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) || 357 force_refresh_rate_ == refresh_rate) { 358 // Cannot honor force refresh rate, as its beyond the range or new request is same 359 return; 360 } 361 362 const hwc_procs_t *hwc_procs = *hwc_procs_; 363 force_refresh_rate_ = refresh_rate; 364 365 hwc_procs->invalidate(hwc_procs); 366 367 return; 368} 369 370uint32_t HWCDisplayPrimary::GetOptimalRefreshRate(bool one_updating_layer) { 371 if (force_refresh_rate_) { 372 return force_refresh_rate_; 373 } else if (handle_idle_timeout_) { 374 return min_refresh_rate_; 375 } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) { 376 return metadata_refresh_rate_; 377 } 378 379 return max_refresh_rate_; 380} 381 382DisplayError HWCDisplayPrimary::Refresh() { 383 const hwc_procs_t *hwc_procs = *hwc_procs_; 384 DisplayError error = kErrorNone; 385 386 if (!hwc_procs) { 387 return kErrorParameters; 388 } 389 390 hwc_procs->invalidate(hwc_procs); 391 handle_idle_timeout_ = true; 392 393 return error; 394} 395 396void HWCDisplayPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) { 397 display_intf_->SetIdleTimeoutMs(timeout_ms); 398} 399 400static void SetLayerBuffer(const BufferInfo& output_buffer_info, LayerBuffer *output_buffer) { 401 const BufferConfig& buffer_config = output_buffer_info.buffer_config; 402 const AllocatedBufferInfo &alloc_buffer_info = output_buffer_info.alloc_buffer_info; 403 404 output_buffer->width = alloc_buffer_info.aligned_width; 405 output_buffer->height = alloc_buffer_info.aligned_height; 406 output_buffer->unaligned_width = buffer_config.width; 407 output_buffer->unaligned_height = buffer_config.height; 408 output_buffer->format = buffer_config.format; 409 output_buffer->planes[0].fd = alloc_buffer_info.fd; 410 output_buffer->planes[0].stride = alloc_buffer_info.stride; 411} 412 413void HWCDisplayPrimary::HandleFrameOutput() { 414 if (frame_capture_buffer_queued_) { 415 HandleFrameCapture(); 416 } else if (dump_output_to_file_) { 417 HandleFrameDump(); 418 } 419} 420 421void HWCDisplayPrimary::HandleFrameCapture() { 422 if (output_buffer_.release_fence_fd >= 0) { 423 frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000); 424 ::close(output_buffer_.release_fence_fd); 425 output_buffer_.release_fence_fd = -1; 426 } 427 428 frame_capture_buffer_queued_ = false; 429 post_processed_output_ = false; 430 output_buffer_ = {}; 431} 432 433void HWCDisplayPrimary::HandleFrameDump() { 434 if (dump_frame_count_ && output_buffer_.release_fence_fd >= 0) { 435 int ret = sync_wait(output_buffer_.release_fence_fd, 1000); 436 ::close(output_buffer_.release_fence_fd); 437 output_buffer_.release_fence_fd = -1; 438 if (ret < 0) { 439 DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno)); 440 } else { 441 DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd); 442 } 443 } 444 445 if (0 == dump_frame_count_) { 446 dump_output_to_file_ = false; 447 // Unmap and Free buffer 448 if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) { 449 DLOGE("unmap failed with err %d", errno); 450 } 451 if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) { 452 DLOGE("FreeBuffer failed"); 453 } 454 455 post_processed_output_ = false; 456 output_buffer_ = {}; 457 output_buffer_info_ = {}; 458 output_buffer_base_ = nullptr; 459 } 460} 461 462void HWCDisplayPrimary::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) { 463 HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type); 464 dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP); 465 DLOGI("output_layer_dump_enable %d", dump_output_to_file_); 466 467 if (!count || !dump_output_to_file_) { 468 return; 469 } 470 471 // Allocate and map output buffer 472 output_buffer_info_ = {}; 473 // Since we dump DSPP output use Panel resolution. 474 GetPanelResolution(&output_buffer_info_.buffer_config.width, 475 &output_buffer_info_.buffer_config.height); 476 output_buffer_info_.buffer_config.format = kFormatRGB888; 477 output_buffer_info_.buffer_config.buffer_count = 1; 478 if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) { 479 DLOGE("Buffer allocation failed"); 480 output_buffer_info_ = {}; 481 return; 482 } 483 484 void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, 485 PROT_READ | PROT_WRITE, 486 MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0); 487 488 if (buffer == MAP_FAILED) { 489 DLOGE("mmap failed with err %d", errno); 490 buffer_allocator_->FreeBuffer(&output_buffer_info_); 491 output_buffer_info_ = {}; 492 return; 493 } 494 495 output_buffer_base_ = buffer; 496 post_processed_output_ = true; 497 DisablePartialUpdateOneFrame(); 498} 499 500int HWCDisplayPrimary::FrameCaptureAsync(const BufferInfo& output_buffer_info, 501 bool post_processed_output) { 502 // Note: This function is called in context of a binder thread and a lock is already held 503 if (output_buffer_info.alloc_buffer_info.fd < 0) { 504 DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd); 505 return -1; 506 } 507 508 auto panel_width = 0u; 509 auto panel_height = 0u; 510 auto fb_width = 0u; 511 auto fb_height = 0u; 512 513 GetPanelResolution(&panel_width, &panel_height); 514 GetFrameBufferResolution(&fb_width, &fb_height); 515 516 if (post_processed_output && (output_buffer_info.buffer_config.width < panel_width || 517 output_buffer_info.buffer_config.height < panel_height)) { 518 DLOGE("Buffer dimensions should not be less than panel resolution"); 519 return -1; 520 } else if (!post_processed_output && (output_buffer_info.buffer_config.width < fb_width || 521 output_buffer_info.buffer_config.height < fb_height)) { 522 DLOGE("Buffer dimensions should not be less than FB resolution"); 523 return -1; 524 } 525 526 SetLayerBuffer(output_buffer_info, &output_buffer_); 527 post_processed_output_ = post_processed_output; 528 frame_capture_buffer_queued_ = true; 529 // Status is only cleared on a new call to dump and remains valid otherwise 530 frame_capture_status_ = -EAGAIN; 531 DisablePartialUpdateOneFrame(); 532 533 return 0; 534} 535 536DisplayError HWCDisplayPrimary::SetDetailEnhancerConfig( 537 const DisplayDetailEnhancerData &de_data) { 538 DisplayError error = kErrorNotSupported; 539 if (display_intf_) { 540 error = display_intf_->SetDetailEnhancerData(de_data); 541 } 542 return error; 543} 544 545DisplayError HWCDisplayPrimary::ControlPartialUpdate(bool enable, uint32_t *pending) { 546 DisplayError error = kErrorNone; 547 548 if (display_intf_) { 549 error = display_intf_->ControlPartialUpdate(enable, pending); 550 } 551 552 return error; 553} 554 555DisplayError HWCDisplayPrimary::DisablePartialUpdateOneFrame() { 556 DisplayError error = kErrorNone; 557 558 if (display_intf_) { 559 error = display_intf_->DisablePartialUpdateOneFrame(); 560 } 561 562 return error; 563} 564 565DisplayError HWCDisplayPrimary::SetMixerResolution(uint32_t width, uint32_t height) { 566 return display_intf_->SetMixerResolution(width, height); 567} 568 569DisplayError HWCDisplayPrimary::GetMixerResolution(uint32_t *width, uint32_t *height) { 570 return display_intf_->GetMixerResolution(width, height); 571} 572 573} // namespace sdm 574 575