Home | History | Annotate | Download | only in hwc
      1 /*
      2 * Copyright (c) 2014 - 2016, 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 <utils/constants.h>
     32 #include <utils/debug.h>
     33 #include <algorithm>
     34 
     35 #include "hwc_display_external.h"
     36 #include "hwc_debugger.h"
     37 
     38 #define __CLASS__ "HWCDisplayExternal"
     39 
     40 namespace sdm {
     41 
     42 int HWCDisplayExternal::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
     43                                qService::QService *qservice, HWCDisplay **hwc_display) {
     44   return Create(core_intf, hwc_procs, 0, 0, qservice, false, hwc_display);
     45 }
     46 
     47 int HWCDisplayExternal::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
     48                                uint32_t primary_width, uint32_t primary_height,
     49                                qService::QService *qservice, bool use_primary_res,
     50                                HWCDisplay **hwc_display) {
     51   uint32_t external_width = 0;
     52   uint32_t external_height = 0;
     53   int drc_enabled = 0;
     54   int drc_reset_fps_enabled = 0;
     55   DisplayError error = kErrorNone;
     56 
     57   HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, hwc_procs, qservice);
     58   int status = hwc_display_external->Init();
     59   if (status) {
     60     delete hwc_display_external;
     61     return status;
     62   }
     63 
     64   error = hwc_display_external->GetMixerResolution(&external_width, &external_height);
     65   if (error != kErrorNone) {
     66     return -EINVAL;
     67   }
     68 
     69   if (primary_width && primary_height) {
     70     // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the
     71     // provided primary_width and primary_height
     72     if (use_primary_res) {
     73       external_width = primary_width;
     74       external_height = primary_height;
     75     } else {
     76       int downscale_enabled = 0;
     77       HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled);
     78       if (downscale_enabled) {
     79         GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
     80       }
     81     }
     82   }
     83 
     84   status = hwc_display_external->SetFrameBufferResolution(external_width, external_height);
     85   if (status) {
     86     Destroy(hwc_display_external);
     87     return status;
     88   }
     89 
     90   HWCDebugHandler::Get()->GetProperty("sdm.hdmi.drc_enabled", &(drc_enabled));
     91   reinterpret_cast<HWCDisplayExternal *>(hwc_display_external)->drc_enabled_ = drc_enabled;
     92 
     93   HWCDebugHandler::Get()->GetProperty("sdm.hdmi.drc_reset_fps", &(drc_reset_fps_enabled));
     94   reinterpret_cast<HWCDisplayExternal *>(hwc_display_external)->drc_reset_fps_enabled_ =
     95                                                                 drc_reset_fps_enabled;
     96 
     97   *hwc_display = hwc_display_external;
     98 
     99   return status;
    100 }
    101 
    102 void HWCDisplayExternal::Destroy(HWCDisplay *hwc_display) {
    103   hwc_display->Deinit();
    104   delete hwc_display;
    105 }
    106 
    107 HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
    108                                        qService::QService *qservice)
    109   : HWCDisplay(core_intf, hwc_procs, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice,
    110                DISPLAY_CLASS_EXTERNAL) {
    111 }
    112 
    113 int HWCDisplayExternal::Prepare(hwc_display_contents_1_t *content_list) {
    114   int status = 0;
    115   DisplayError error = kErrorNone;
    116 
    117   if (secure_display_active_) {
    118     MarkLayersForGPUBypass(content_list);
    119     return status;
    120   }
    121 
    122   status = AllocateLayerStack(content_list);
    123   if (status) {
    124     return status;
    125   }
    126 
    127   status = PrePrepareLayerStack(content_list);
    128   if (status) {
    129     return status;
    130   }
    131 
    132   if (content_list->numHwLayers <= 1) {
    133     flush_ = true;
    134     return 0;
    135   }
    136 
    137   bool one_video_updating_layer = SingleVideoLayerUpdating(UINT32(content_list->numHwLayers - 1));
    138 
    139   uint32_t refresh_rate = GetOptimalRefreshRate(one_video_updating_layer);
    140   bool final_rate = force_refresh_rate_ ? true : false;
    141   error = display_intf_->SetRefreshRate(refresh_rate, final_rate);
    142   if (error == kErrorNone) {
    143     // On success, set current refresh rate to new refresh rate
    144     current_refresh_rate_ = refresh_rate;
    145   }
    146 
    147   status = PrepareLayerStack(content_list);
    148   if (status) {
    149     return status;
    150   }
    151 
    152   return 0;
    153 }
    154 
    155 int HWCDisplayExternal::Commit(hwc_display_contents_1_t *content_list) {
    156   int status = 0;
    157 
    158   if (secure_display_active_) {
    159     return status;
    160   }
    161 
    162   status = HWCDisplay::CommitLayerStack(content_list);
    163   if (status) {
    164     return status;
    165   }
    166 
    167   status = HWCDisplay::PostCommitLayerStack(content_list);
    168   if (status) {
    169     return status;
    170   }
    171 
    172   return 0;
    173 }
    174 
    175 void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) {
    176   if (display_intf_->IsUnderscanSupported()) {
    177     return;
    178   }
    179 
    180   // Read user defined width and height ratio
    181   int width = 0, height = 0;
    182   HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_width", &width);
    183   float width_ratio = FLOAT(width) / 100.0f;
    184   HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_height", &height);
    185   float height_ratio = FLOAT(height) / 100.0f;
    186 
    187   if (width_ratio == 0.0f ||  height_ratio == 0.0f) {
    188     return;
    189   }
    190 
    191   uint32_t mixer_width = 0;
    192   uint32_t mixer_height = 0;
    193   GetMixerResolution(&mixer_width, &mixer_height);
    194 
    195   if (mixer_width == 0 || mixer_height == 0) {
    196     DLOGV("Invalid mixer dimensions (%d, %d)", mixer_width, mixer_height);
    197     return;
    198   }
    199 
    200   uint32_t new_mixer_width = UINT32(mixer_width * FLOAT(1.0f - width_ratio));
    201   uint32_t new_mixer_height = UINT32(mixer_height * FLOAT(1.0f - height_ratio));
    202 
    203   int x_offset = INT((FLOAT(mixer_width) * width_ratio) / 2.0f);
    204   int y_offset = INT((FLOAT(mixer_height) * height_ratio) / 2.0f);
    205 
    206   display_frame->left = (display_frame->left * INT32(new_mixer_width) / INT32(mixer_width))
    207                         + x_offset;
    208   display_frame->top = (display_frame->top * INT32(new_mixer_height) / INT32(mixer_height)) +
    209                        y_offset;
    210   display_frame->right = ((display_frame->right * INT32(new_mixer_width)) / INT32(mixer_width)) +
    211                          x_offset;
    212   display_frame->bottom = ((display_frame->bottom * INT32(new_mixer_height)) / INT32(mixer_height))
    213                           + y_offset;
    214 }
    215 
    216 void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active, bool force_flush) {
    217   if (secure_display_active_ != secure_display_active) {
    218     secure_display_active_ = secure_display_active;
    219 
    220     if (secure_display_active_) {
    221       DisplayError error = display_intf_->Flush();
    222       if (error != kErrorNone) {
    223         DLOGE("Flush failed. Error = %d", error);
    224       }
    225     }
    226   }
    227   return;
    228 }
    229 
    230 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
    231                                    uint32_t *src_height) {
    232   *src_height = (dst_width * (*src_height)) / (*src_width);
    233   *src_width = dst_width;
    234 }
    235 
    236 void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
    237                                         uint32_t *non_primary_width, uint32_t *non_primary_height) {
    238   uint32_t primary_area = primary_width * primary_height;
    239   uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height);
    240 
    241   if (primary_area > non_primary_area) {
    242     if (primary_height > primary_width) {
    243       std::swap(primary_height, primary_width);
    244     }
    245     AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
    246   }
    247 }
    248 
    249 uint32_t HWCDisplayExternal::RoundToStandardFPS(float fps) {
    250   static const uint32_t standard_fps[] = {23976, 24000, 25000, 29970, 30000, 50000, 59940, 60000};
    251   static const uint32_t mapping_fps[] = {59940, 60000, 60000, 59940, 60000, 50000, 59940, 60000};
    252   uint32_t frame_rate = (uint32_t)(fps * 1000);
    253 
    254   // process non valid
    255   if (frame_rate == 0) {
    256     return current_refresh_rate_;
    257   }
    258 
    259   int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
    260   for (int i = 0; i < count; i++) {
    261     // Most likely used for video, the fps for frames should be stable from video side.
    262     if (standard_fps[i] > frame_rate) {
    263       if (i > 0) {
    264         if ((standard_fps[i] - frame_rate) > (frame_rate - standard_fps[i-1])) {
    265           return mapping_fps[i-1];
    266         } else {
    267           return mapping_fps[i];
    268         }
    269       } else {
    270         return mapping_fps[i];
    271       }
    272     }
    273   }
    274 
    275   return standard_fps[count - 1];
    276 }
    277 
    278 void HWCDisplayExternal::PrepareDynamicRefreshRate(Layer *layer) {
    279   if (layer->input_buffer.flags.video) {
    280     if (layer->frame_rate != 0) {
    281       metadata_refresh_rate_ = SanitizeRefreshRate(layer->frame_rate);
    282     } else {
    283       metadata_refresh_rate_ = current_refresh_rate_;
    284     }
    285     layer->frame_rate = current_refresh_rate_;
    286   } else if (!layer->frame_rate) {
    287     layer->frame_rate = current_refresh_rate_;
    288   }
    289 }
    290 
    291 void HWCDisplayExternal::ForceRefreshRate(uint32_t refresh_rate) {
    292   if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) ||
    293        force_refresh_rate_ == refresh_rate) {
    294     // Cannot honor force refresh rate, as its beyond the range or new request is same
    295     return;
    296   }
    297 
    298   force_refresh_rate_ = refresh_rate;
    299 }
    300 
    301 uint32_t HWCDisplayExternal::GetOptimalRefreshRate(bool one_updating_layer) {
    302   if (force_refresh_rate_) {
    303     return force_refresh_rate_;
    304   } else if (one_updating_layer && drc_enabled_) {
    305     return metadata_refresh_rate_;
    306   }
    307 
    308   if (drc_reset_fps_enabled_) {
    309     DisplayConfigVariableInfo fb_config;
    310     display_intf_->GetFrameBufferConfig(&fb_config);
    311     return (fb_config.fps * 1000);
    312   }
    313 
    314   return current_refresh_rate_;
    315 }
    316 
    317 int HWCDisplayExternal::Perform(uint32_t operation, ...) {
    318   va_list args;
    319   va_start(args, operation);
    320   int val = va_arg(args, int32_t);
    321   va_end(args);
    322   switch (operation) {
    323     case SET_BINDER_DYN_REFRESH_RATE:
    324       ForceRefreshRate(UINT32(val));
    325       break;
    326     default:
    327       DLOGW("Invalid operation %d", operation);
    328       return -EINVAL;
    329   }
    330 
    331   return 0;
    332 }
    333 
    334 }  // namespace sdm
    335 
    336