Home | History | Annotate | Download | only in hwc2
      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 <map>
     38 #include <string>
     39 #include <vector>
     40 
     41 #include "hwc_display_primary.h"
     42 #include "hwc_debugger.h"
     43 
     44 #define __CLASS__ "HWCDisplayPrimary"
     45 
     46 namespace sdm {
     47 
     48 int HWCDisplayPrimary::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
     49                               HWCCallbacks *callbacks, qService::QService *qservice,
     50                               HWCDisplay **hwc_display) {
     51   int status = 0;
     52   uint32_t primary_width = 0;
     53   uint32_t primary_height = 0;
     54 
     55   HWCDisplay *hwc_display_primary =
     56       new HWCDisplayPrimary(core_intf, buffer_allocator, callbacks, qservice);
     57   status = hwc_display_primary->Init();
     58   if (status) {
     59     delete hwc_display_primary;
     60     return status;
     61   }
     62 
     63   hwc_display_primary->GetMixerResolution(&primary_width, &primary_height);
     64   int width = 0, height = 0;
     65   HWCDebugHandler::Get()->GetProperty("sdm.fb_size_width", &width);
     66   HWCDebugHandler::Get()->GetProperty("sdm.fb_size_height", &height);
     67   if (width > 0 && height > 0) {
     68     primary_width = UINT32(width);
     69     primary_height = UINT32(height);
     70   }
     71 
     72   status = hwc_display_primary->SetFrameBufferResolution(primary_width, primary_height);
     73   if (status) {
     74     Destroy(hwc_display_primary);
     75     return status;
     76   }
     77 
     78   *hwc_display = hwc_display_primary;
     79 
     80   return status;
     81 }
     82 
     83 void HWCDisplayPrimary::Destroy(HWCDisplay *hwc_display) {
     84   hwc_display->Deinit();
     85   delete hwc_display;
     86 }
     87 
     88 HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
     89                                      HWCCallbacks *callbacks, qService::QService *qservice)
     90     : HWCDisplay(core_intf, callbacks, kPrimary, HWC_DISPLAY_PRIMARY, true, qservice,
     91                  DISPLAY_CLASS_PRIMARY, buffer_allocator),
     92       buffer_allocator_(buffer_allocator),
     93       cpu_hint_(NULL) {
     94 }
     95 
     96 int HWCDisplayPrimary::Init() {
     97   cpu_hint_ = new CPUHint();
     98   if (cpu_hint_->Init(static_cast<HWCDebugHandler *>(HWCDebugHandler::Get())) != kErrorNone) {
     99     delete cpu_hint_;
    100     cpu_hint_ = NULL;
    101   }
    102 
    103   use_metadata_refresh_rate_ = true;
    104   int disable_metadata_dynfps = 0;
    105   HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps);
    106   if (disable_metadata_dynfps) {
    107     use_metadata_refresh_rate_ = false;
    108   }
    109 
    110   int status = HWCDisplay::Init();
    111   if (status) {
    112     return status;
    113   }
    114   color_mode_ = new HWCColorMode(display_intf_);
    115   color_mode_->Init();
    116 
    117   return status;
    118 }
    119 
    120 void HWCDisplayPrimary::ProcessBootAnimCompleted() {
    121   uint32_t numBootUpLayers = 0;
    122   // TODO(user): Remove this hack
    123 
    124   numBootUpLayers = static_cast<uint32_t>(Debug::GetBootAnimLayerCount());
    125 
    126   if (numBootUpLayers == 0) {
    127     numBootUpLayers = 2;
    128   }
    129   /* All other checks namely "init.svc.bootanim" or
    130   * HWC_GEOMETRY_CHANGED fail in correctly identifying the
    131   * exact bootup transition to homescreen
    132   */
    133   char property[PROPERTY_VALUE_MAX];
    134   bool isEncrypted = false;
    135   bool main_class_services_started = false;
    136   property_get("ro.crypto.state", property, "unencrypted");
    137   if (!strcmp(property, "encrypted")) {
    138     property_get("ro.crypto.type", property, "block");
    139     if (!strcmp(property, "block")) {
    140       isEncrypted = true;
    141       property_get("vold.decrypt", property, "");
    142       if (!strcmp(property, "trigger_restart_framework")) {
    143         main_class_services_started = true;
    144       }
    145     }
    146   }
    147 
    148   if ((!isEncrypted || (isEncrypted && main_class_services_started)) &&
    149       (layer_set_.size() > numBootUpLayers)) {
    150     DLOGI("Applying default mode");
    151     boot_animation_completed_ = true;
    152     // Applying default mode after bootanimation is finished And
    153     // If Data is Encrypted, it is ready for access.
    154     if (display_intf_)
    155       display_intf_->ApplyDefaultDisplayMode();
    156   }
    157 }
    158 
    159 HWC2::Error HWCDisplayPrimary::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
    160   auto status = HWC2::Error::None;
    161   DisplayError error = kErrorNone;
    162 
    163   if (display_paused_) {
    164     MarkLayersForGPUBypass();
    165     return status;
    166   }
    167 
    168   if (color_tranform_failed_) {
    169     // Must fall back to client composition
    170     MarkLayersForClientComposition();
    171   }
    172 
    173   // Fill in the remaining blanks in the layers and add them to the SDM layerstack
    174   BuildLayerStack();
    175   // Checks and replaces layer stack for solid fill
    176   SolidFillPrepare();
    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   bool one_updating_layer = SingleLayerUpdating();
    188   ToggleCPUHint(one_updating_layer);
    189 
    190   uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer);
    191   bool final_rate = force_refresh_rate_ ? true : false;
    192   error = display_intf_->SetRefreshRate(refresh_rate, final_rate);
    193   if (error == kErrorNone) {
    194     // On success, set current refresh rate to new refresh rate
    195     current_refresh_rate_ = refresh_rate;
    196   }
    197 
    198   if (layer_set_.empty()) {
    199     flush_ = true;
    200     return status;
    201   }
    202 
    203   status = PrepareLayerStack(out_num_types, out_num_requests);
    204   return status;
    205 }
    206 
    207 HWC2::Error HWCDisplayPrimary::Present(int32_t *out_retire_fence) {
    208   auto status = HWC2::Error::None;
    209   if (display_paused_) {
    210     // TODO(user): From old HWC implementation
    211     // If we do not handle the frame set retireFenceFd to outbufAcquireFenceFd
    212     // Revisit this when validating display_paused
    213     DisplayError error = display_intf_->Flush();
    214     if (error != kErrorNone) {
    215       DLOGE("Flush failed. Error = %d", error);
    216     }
    217   } else {
    218     status = HWCDisplay::CommitLayerStack();
    219     if (status == HWC2::Error::None) {
    220       HandleFrameOutput();
    221       SolidFillCommit();
    222       status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
    223     }
    224   }
    225 
    226   CloseAcquireFds();
    227   return status;
    228 }
    229 
    230 HWC2::Error HWCDisplayPrimary::GetColorModes(uint32_t *out_num_modes,
    231                                              android_color_mode_t *out_modes) {
    232   if (out_modes == nullptr) {
    233     *out_num_modes = color_mode_->GetColorModeCount();
    234   } else {
    235     color_mode_->GetColorModes(out_num_modes, out_modes);
    236   }
    237 
    238   return HWC2::Error::None;
    239 }
    240 
    241 HWC2::Error HWCDisplayPrimary::SetColorMode(android_color_mode_t mode) {
    242   auto status = color_mode_->SetColorMode(mode);
    243   if (status != HWC2::Error::None) {
    244     DLOGE("failed for mode = %d", mode);
    245     return status;
    246   }
    247 
    248   callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
    249 
    250   return status;
    251 }
    252 
    253 HWC2::Error HWCDisplayPrimary::SetColorModeById(int32_t color_mode_id) {
    254   auto status = color_mode_->SetColorModeById(color_mode_id);
    255   if (status != HWC2::Error::None) {
    256     DLOGE("failed for mode = %d", color_mode_id);
    257     return status;
    258   }
    259 
    260   callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
    261 
    262   return status;
    263 }
    264 
    265 HWC2::Error HWCDisplayPrimary::SetColorTransform(const float *matrix,
    266                                                  android_color_transform_t hint) {
    267   if (!matrix) {
    268     return HWC2::Error::BadParameter;
    269   }
    270 
    271   auto status = color_mode_->SetColorTransform(matrix, hint);
    272   if (status != HWC2::Error::None) {
    273     DLOGE("failed for hint = %d", hint);
    274     color_tranform_failed_ = true;
    275     return status;
    276   }
    277 
    278   callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
    279   color_tranform_failed_ = false;
    280 
    281   return status;
    282 }
    283 
    284 int HWCDisplayPrimary::Perform(uint32_t operation, ...) {
    285   va_list args;
    286   va_start(args, operation);
    287   int val = 0;
    288   LayerRect *rect = NULL;
    289 
    290   switch (operation) {
    291     case SET_METADATA_DYN_REFRESH_RATE:
    292       val = va_arg(args, int32_t);
    293       SetMetaDataRefreshRateFlag(val);
    294       break;
    295     case SET_BINDER_DYN_REFRESH_RATE:
    296       val = va_arg(args, int32_t);
    297       ForceRefreshRate(UINT32(val));
    298       break;
    299     case SET_DISPLAY_MODE:
    300       val = va_arg(args, int32_t);
    301       SetDisplayMode(UINT32(val));
    302       break;
    303     case SET_QDCM_SOLID_FILL_INFO:
    304       val = va_arg(args, int32_t);
    305       SetQDCMSolidFillInfo(true, UINT32(val));
    306       break;
    307     case UNSET_QDCM_SOLID_FILL_INFO:
    308       val = va_arg(args, int32_t);
    309       SetQDCMSolidFillInfo(false, UINT32(val));
    310       break;
    311     case SET_QDCM_SOLID_FILL_RECT:
    312       rect = va_arg(args, LayerRect*);
    313       solid_fill_rect_ = *rect;
    314       break;
    315     default:
    316       DLOGW("Invalid operation %d", operation);
    317       va_end(args);
    318       return -EINVAL;
    319   }
    320   va_end(args);
    321 
    322   return 0;
    323 }
    324 
    325 DisplayError HWCDisplayPrimary::SetDisplayMode(uint32_t mode) {
    326   DisplayError error = kErrorNone;
    327 
    328   if (display_intf_) {
    329     error = display_intf_->SetDisplayMode(mode);
    330   }
    331 
    332   return error;
    333 }
    334 
    335 void HWCDisplayPrimary::SetMetaDataRefreshRateFlag(bool enable) {
    336   int disable_metadata_dynfps = 0;
    337 
    338   HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps);
    339   if (disable_metadata_dynfps) {
    340     return;
    341   }
    342   use_metadata_refresh_rate_ = enable;
    343 }
    344 
    345 void HWCDisplayPrimary::SetQDCMSolidFillInfo(bool enable, uint32_t color) {
    346   solid_fill_enable_ = enable;
    347   solid_fill_color_ = color;
    348 }
    349 
    350 void HWCDisplayPrimary::ToggleCPUHint(bool set) {
    351   if (!cpu_hint_) {
    352     return;
    353   }
    354 
    355   if (set) {
    356     cpu_hint_->Set();
    357   } else {
    358     cpu_hint_->Reset();
    359   }
    360 }
    361 
    362 void HWCDisplayPrimary::SetSecureDisplay(bool secure_display_active) {
    363   if (secure_display_active_ != secure_display_active) {
    364     // Skip Prepare and call Flush for null commit
    365     DLOGI("SecureDisplay state changed from %d to %d Needs Flush!!", secure_display_active_,
    366           secure_display_active);
    367     secure_display_active_ = secure_display_active;
    368     skip_prepare_ = true;
    369   }
    370   return;
    371 }
    372 
    373 void HWCDisplayPrimary::ForceRefreshRate(uint32_t refresh_rate) {
    374   if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) ||
    375       force_refresh_rate_ == refresh_rate) {
    376     // Cannot honor force refresh rate, as its beyond the range or new request is same
    377     return;
    378   }
    379 
    380   force_refresh_rate_ = refresh_rate;
    381 
    382   callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
    383 
    384   return;
    385 }
    386 
    387 uint32_t HWCDisplayPrimary::GetOptimalRefreshRate(bool one_updating_layer) {
    388   if (force_refresh_rate_) {
    389     return force_refresh_rate_;
    390   } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) {
    391     return metadata_refresh_rate_;
    392   }
    393 
    394   return max_refresh_rate_;
    395 }
    396 
    397 DisplayError HWCDisplayPrimary::Refresh() {
    398   DisplayError error = kErrorNone;
    399 
    400   callbacks_->Refresh(HWC_DISPLAY_PRIMARY);
    401 
    402   return error;
    403 }
    404 
    405 void HWCDisplayPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) {
    406   display_intf_->SetIdleTimeoutMs(timeout_ms);
    407 }
    408 
    409 static void SetLayerBuffer(const BufferInfo &output_buffer_info, LayerBuffer *output_buffer) {
    410   const BufferConfig& buffer_config = output_buffer_info.buffer_config;
    411   const AllocatedBufferInfo &alloc_buffer_info = output_buffer_info.alloc_buffer_info;
    412 
    413   output_buffer->width = alloc_buffer_info.aligned_width;
    414   output_buffer->height = alloc_buffer_info.aligned_height;
    415   output_buffer->unaligned_width = buffer_config.width;
    416   output_buffer->unaligned_height = buffer_config.height;
    417   output_buffer->format = buffer_config.format;
    418   output_buffer->planes[0].fd = alloc_buffer_info.fd;
    419   output_buffer->planes[0].stride = alloc_buffer_info.stride;
    420 }
    421 
    422 void HWCDisplayPrimary::HandleFrameOutput() {
    423   if (frame_capture_buffer_queued_) {
    424     HandleFrameCapture();
    425   } else if (dump_output_to_file_) {
    426     HandleFrameDump();
    427   }
    428 }
    429 
    430 void HWCDisplayPrimary::HandleFrameCapture() {
    431   if (output_buffer_.release_fence_fd >= 0) {
    432     frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000);
    433     ::close(output_buffer_.release_fence_fd);
    434     output_buffer_.release_fence_fd = -1;
    435   }
    436 
    437   frame_capture_buffer_queued_ = false;
    438   post_processed_output_ = false;
    439   output_buffer_ = {};
    440 }
    441 
    442 void HWCDisplayPrimary::HandleFrameDump() {
    443   if (dump_frame_count_ && output_buffer_.release_fence_fd >= 0) {
    444     int ret = sync_wait(output_buffer_.release_fence_fd, 1000);
    445     ::close(output_buffer_.release_fence_fd);
    446     output_buffer_.release_fence_fd = -1;
    447     if (ret < 0) {
    448       DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
    449     } else {
    450       DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd);
    451     }
    452   }
    453 
    454   if (0 == dump_frame_count_) {
    455     dump_output_to_file_ = false;
    456     // Unmap and Free buffer
    457     if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) {
    458       DLOGE("unmap failed with err %d", errno);
    459     }
    460     if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) {
    461       DLOGE("FreeBuffer failed");
    462     }
    463 
    464     post_processed_output_ = false;
    465     output_buffer_ = {};
    466     output_buffer_info_ = {};
    467     output_buffer_base_ = nullptr;
    468   }
    469 }
    470 
    471 void HWCDisplayPrimary::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) {
    472   HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type);
    473   dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
    474   DLOGI("output_layer_dump_enable %d", dump_output_to_file_);
    475 
    476   if (!count || !dump_output_to_file_) {
    477     return;
    478   }
    479 
    480   // Allocate and map output buffer
    481   output_buffer_info_ = {};
    482   // Since we dump DSPP output use Panel resolution.
    483   GetPanelResolution(&output_buffer_info_.buffer_config.width,
    484                      &output_buffer_info_.buffer_config.height);
    485   output_buffer_info_.buffer_config.format = kFormatRGB888;
    486   output_buffer_info_.buffer_config.buffer_count = 1;
    487   if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) {
    488     DLOGE("Buffer allocation failed");
    489     output_buffer_info_ = {};
    490     return;
    491   }
    492 
    493   void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, PROT_READ | PROT_WRITE,
    494                       MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0);
    495 
    496   if (buffer == MAP_FAILED) {
    497     DLOGE("mmap failed with err %d", errno);
    498     buffer_allocator_->FreeBuffer(&output_buffer_info_);
    499     output_buffer_info_ = {};
    500     return;
    501   }
    502 
    503   output_buffer_base_ = buffer;
    504   post_processed_output_ = true;
    505   DisablePartialUpdateOneFrame();
    506 }
    507 
    508 int HWCDisplayPrimary::FrameCaptureAsync(const BufferInfo &output_buffer_info,
    509                                          bool post_processed_output) {
    510   // Note: This function is called in context of a binder thread and a lock is already held
    511   if (output_buffer_info.alloc_buffer_info.fd < 0) {
    512     DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd);
    513     return -1;
    514   }
    515 
    516   auto panel_width = 0u;
    517   auto panel_height = 0u;
    518   auto fb_width = 0u;
    519   auto fb_height = 0u;
    520 
    521   GetPanelResolution(&panel_width, &panel_height);
    522   GetFrameBufferResolution(&fb_width, &fb_height);
    523 
    524   if (post_processed_output && (output_buffer_info_.buffer_config.width < panel_width ||
    525                                 output_buffer_info_.buffer_config.height < panel_height)) {
    526     DLOGE("Buffer dimensions should not be less than panel resolution");
    527     return -1;
    528   } else if (!post_processed_output && (output_buffer_info_.buffer_config.width < fb_width ||
    529                                         output_buffer_info_.buffer_config.height < fb_height)) {
    530     DLOGE("Buffer dimensions should not be less than FB resolution");
    531     return -1;
    532   }
    533 
    534   SetLayerBuffer(output_buffer_info, &output_buffer_);
    535   post_processed_output_ = post_processed_output;
    536   frame_capture_buffer_queued_ = true;
    537   // Status is only cleared on a new call to dump and remains valid otherwise
    538   frame_capture_status_ = -EAGAIN;
    539   DisablePartialUpdateOneFrame();
    540 
    541   return 0;
    542 }
    543 
    544 DisplayError HWCDisplayPrimary::SetDetailEnhancerConfig
    545                                    (const DisplayDetailEnhancerData &de_data) {
    546   DisplayError error = kErrorNotSupported;
    547 
    548   if (display_intf_) {
    549     error = display_intf_->SetDetailEnhancerData(de_data);
    550   }
    551   return error;
    552 }
    553 
    554 DisplayError HWCDisplayPrimary::ControlPartialUpdate(bool enable, uint32_t *pending) {
    555   DisplayError error = kErrorNone;
    556 
    557   if (display_intf_) {
    558     error = display_intf_->ControlPartialUpdate(enable, pending);
    559   }
    560 
    561   return error;
    562 }
    563 
    564 DisplayError HWCDisplayPrimary::DisablePartialUpdateOneFrame() {
    565   DisplayError error = kErrorNone;
    566 
    567   if (display_intf_) {
    568     error = display_intf_->DisablePartialUpdateOneFrame();
    569   }
    570 
    571   return error;
    572 }
    573 
    574 
    575 DisplayError HWCDisplayPrimary::SetMixerResolution(uint32_t width, uint32_t height) {
    576   return display_intf_->SetMixerResolution(width, height);
    577 }
    578 
    579 DisplayError HWCDisplayPrimary::GetMixerResolution(uint32_t *width, uint32_t *height) {
    580   return display_intf_->GetMixerResolution(width, height);
    581 }
    582 
    583 }  // namespace sdm
    584