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 modification, are permitted 5 * provided that the following conditions are met: 6 * * Redistributions of source code must retain the above copyright notice, this list of 7 * conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above copyright notice, this list of 9 * conditions and the following disclaimer in the documentation and/or other materials provided 10 * with the distribution. 11 * * Neither the name of The Linux Foundation nor the names of its contributors may be used to 12 * endorse or promote products derived from this software without specific prior written 13 * permission. 14 * 15 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 21 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <utils/constants.h> 26 #include <utils/debug.h> 27 #include <map> 28 #include <utility> 29 #include <vector> 30 31 #include "display_hdmi.h" 32 #include "hw_interface.h" 33 #include "hw_info_interface.h" 34 35 #define __CLASS__ "DisplayHDMI" 36 37 namespace sdm { 38 39 DisplayHDMI::DisplayHDMI(DisplayEventHandler *event_handler, HWInfoInterface *hw_info_intf, 40 BufferSyncHandler *buffer_sync_handler, CompManager *comp_manager) 41 : DisplayBase(kHDMI, event_handler, kDeviceHDMI, buffer_sync_handler, comp_manager, 42 hw_info_intf) { 43 } 44 45 DisplayError DisplayHDMI::Init() { 46 lock_guard<recursive_mutex> obj(recursive_mutex_); 47 48 DisplayError error = HWInterface::Create(kHDMI, hw_info_intf_, buffer_sync_handler_, 49 &hw_intf_); 50 if (error != kErrorNone) { 51 return error; 52 } 53 54 uint32_t active_mode_index; 55 char value[64] = "0"; 56 Debug::GetProperty("sdm.hdmi.s3d_mode", value); 57 HWS3DMode mode = (HWS3DMode)atoi(value); 58 if (mode > kS3DModeNone && mode < kS3DModeMax) { 59 active_mode_index = GetBestConfig(mode); 60 } else { 61 active_mode_index = GetBestConfig(kS3DModeNone); 62 } 63 64 error = hw_intf_->SetDisplayAttributes(active_mode_index); 65 if (error != kErrorNone) { 66 HWInterface::Destroy(hw_intf_); 67 } 68 69 error = DisplayBase::Init(); 70 if (error != kErrorNone) { 71 HWInterface::Destroy(hw_intf_); 72 return error; 73 } 74 75 GetScanSupport(); 76 underscan_supported_ = (scan_support_ == kScanAlwaysUnderscanned) || (scan_support_ == kScanBoth); 77 78 s3d_format_to_mode_.insert(std::pair<LayerBufferS3DFormat, HWS3DMode> 79 (kS3dFormatNone, kS3DModeNone)); 80 s3d_format_to_mode_.insert(std::pair<LayerBufferS3DFormat, HWS3DMode> 81 (kS3dFormatLeftRight, kS3DModeLR)); 82 s3d_format_to_mode_.insert(std::pair<LayerBufferS3DFormat, HWS3DMode> 83 (kS3dFormatRightLeft, kS3DModeRL)); 84 s3d_format_to_mode_.insert(std::pair<LayerBufferS3DFormat, HWS3DMode> 85 (kS3dFormatTopBottom, kS3DModeTB)); 86 s3d_format_to_mode_.insert(std::pair<LayerBufferS3DFormat, HWS3DMode> 87 (kS3dFormatFramePacking, kS3DModeFP)); 88 89 error = HWEventsInterface::Create(INT(display_type_), this, event_list_, &hw_events_intf_); 90 if (error != kErrorNone) { 91 DisplayBase::Deinit(); 92 HWInterface::Destroy(hw_intf_); 93 DLOGE("Failed to create hardware events interface. Error = %d", error); 94 } 95 96 return error; 97 } 98 99 DisplayError DisplayHDMI::Prepare(LayerStack *layer_stack) { 100 lock_guard<recursive_mutex> obj(recursive_mutex_); 101 DisplayError error = kErrorNone; 102 uint32_t new_mixer_width = 0; 103 uint32_t new_mixer_height = 0; 104 uint32_t display_width = display_attributes_.x_pixels; 105 uint32_t display_height = display_attributes_.y_pixels; 106 107 if (NeedsMixerReconfiguration(layer_stack, &new_mixer_width, &new_mixer_height)) { 108 error = ReconfigureMixer(new_mixer_width, new_mixer_height); 109 if (error != kErrorNone) { 110 ReconfigureMixer(display_width, display_height); 111 } 112 } 113 114 SetS3DMode(layer_stack); 115 116 // Clean hw layers for reuse. 117 hw_layers_ = HWLayers(); 118 119 return DisplayBase::Prepare(layer_stack); 120 } 121 122 DisplayError DisplayHDMI::GetRefreshRateRange(uint32_t *min_refresh_rate, 123 uint32_t *max_refresh_rate) { 124 lock_guard<recursive_mutex> obj(recursive_mutex_); 125 DisplayError error = kErrorNone; 126 127 if (hw_panel_info_.min_fps && hw_panel_info_.max_fps) { 128 *min_refresh_rate = hw_panel_info_.min_fps; 129 *max_refresh_rate = hw_panel_info_.max_fps; 130 } else { 131 error = DisplayBase::GetRefreshRateRange(min_refresh_rate, max_refresh_rate); 132 } 133 134 return error; 135 } 136 137 DisplayError DisplayHDMI::SetRefreshRate(uint32_t refresh_rate) { 138 lock_guard<recursive_mutex> obj(recursive_mutex_); 139 140 if (!active_) { 141 return kErrorPermission; 142 } 143 144 DisplayError error = hw_intf_->SetRefreshRate(refresh_rate); 145 if (error != kErrorNone) { 146 return error; 147 } 148 149 return DisplayBase::ReconfigureDisplay(); 150 } 151 152 bool DisplayHDMI::IsUnderscanSupported() { 153 lock_guard<recursive_mutex> obj(recursive_mutex_); 154 return underscan_supported_; 155 } 156 157 DisplayError DisplayHDMI::OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level) { 158 lock_guard<recursive_mutex> obj(recursive_mutex_); 159 return hw_intf_->OnMinHdcpEncryptionLevelChange(min_enc_level); 160 } 161 162 uint32_t DisplayHDMI::GetBestConfig(HWS3DMode s3d_mode) { 163 uint32_t best_index = 0, index; 164 uint32_t num_modes = 0; 165 HWDisplayAttributes best_attrib; 166 167 hw_intf_->GetNumDisplayAttributes(&num_modes); 168 169 // Get display attribute for each mode 170 std::vector<HWDisplayAttributes> attrib(num_modes); 171 for (index = 0; index < num_modes; index++) { 172 hw_intf_->GetDisplayAttributes(index, &attrib[index]); 173 } 174 175 // Select best config for s3d_mode. If s3d is not enabled, s3d_mode is kS3DModeNone 176 for (index = 0; index < num_modes; index ++) { 177 if (attrib[index].s3d_config[s3d_mode]) { 178 break; 179 } 180 } 181 if (index < num_modes) { 182 best_index = UINT32(index); 183 for (size_t index = best_index + 1; index < num_modes; index ++) { 184 if (!attrib[index].s3d_config[s3d_mode]) 185 continue; 186 187 // From the available configs, select the best 188 // Ex: 1920x1080@60Hz is better than 1920x1080@30 and 1920x1080@30 is better than 1280x720@60 189 if (attrib[index].y_pixels > attrib[best_index].y_pixels) { 190 best_index = UINT32(index); 191 } else if (attrib[index].y_pixels == attrib[best_index].y_pixels) { 192 if (attrib[index].x_pixels > attrib[best_index].x_pixels) { 193 best_index = UINT32(index); 194 } else if (attrib[index].x_pixels == attrib[best_index].x_pixels) { 195 if (attrib[index].vsync_period_ns < attrib[best_index].vsync_period_ns) { 196 best_index = UINT32(index); 197 } 198 } 199 } 200 } 201 } else { 202 DLOGW("%s, could not support S3D mode from EDID info. S3D mode is %d", 203 __FUNCTION__, s3d_mode); 204 } 205 206 // Used for changing HDMI Resolution - override the best with user set config 207 uint32_t user_config = UINT32(Debug::GetHDMIResolution()); 208 if (user_config) { 209 uint32_t config_index = 0; 210 // For the config, get the corresponding index 211 DisplayError error = hw_intf_->GetConfigIndex(user_config, &config_index); 212 if (error == kErrorNone) 213 return config_index; 214 } 215 216 return best_index; 217 } 218 219 void DisplayHDMI::GetScanSupport() { 220 DisplayError error = kErrorNone; 221 uint32_t video_format = 0; 222 uint32_t max_cea_format = 0; 223 HWScanInfo scan_info = HWScanInfo(); 224 hw_intf_->GetHWScanInfo(&scan_info); 225 226 uint32_t active_mode_index = 0; 227 hw_intf_->GetActiveConfig(&active_mode_index); 228 229 error = hw_intf_->GetVideoFormat(active_mode_index, &video_format); 230 if (error != kErrorNone) { 231 return; 232 } 233 234 error = hw_intf_->GetMaxCEAFormat(&max_cea_format); 235 if (error != kErrorNone) { 236 return; 237 } 238 239 // The scan support for a given HDMI TV must be read from scan info corresponding to 240 // Preferred Timing if the preferred timing of the display is currently active, and if it is 241 // valid. In all other cases, we must read the scan support from CEA scan info if 242 // the resolution is a CEA resolution, or from IT scan info for all other resolutions. 243 if (active_mode_index == 0 && scan_info.pt_scan_support != kScanNotSupported) { 244 scan_support_ = scan_info.pt_scan_support; 245 } else if (video_format < max_cea_format) { 246 scan_support_ = scan_info.cea_scan_support; 247 } else { 248 scan_support_ = scan_info.it_scan_support; 249 } 250 } 251 252 void DisplayHDMI::SetS3DMode(LayerStack *layer_stack) { 253 uint32_t s3d_layer_count = 0; 254 HWS3DMode s3d_mode = kS3DModeNone; 255 uint32_t layer_count = UINT32(layer_stack->layers.size()); 256 257 // S3D mode is supported for the following scenarios: 258 // 1. Layer stack containing only one s3d layer which is not skip 259 // 2. Layer stack containing only one secure layer along with one s3d layer 260 for (uint32_t i = 0; i < layer_count; i++) { 261 Layer *layer = layer_stack->layers.at(i); 262 LayerBuffer &layer_buffer = layer->input_buffer; 263 264 if (layer_buffer.s3d_format != kS3dFormatNone) { 265 s3d_layer_count++; 266 if (s3d_layer_count > 1 || layer->flags.skip) { 267 s3d_mode = kS3DModeNone; 268 break; 269 } 270 271 std::map<LayerBufferS3DFormat, HWS3DMode>::iterator it = 272 s3d_format_to_mode_.find(layer_buffer.s3d_format); 273 if (it != s3d_format_to_mode_.end()) { 274 s3d_mode = it->second; 275 } 276 } else if (layer_buffer.flags.secure && layer_count > 2) { 277 s3d_mode = kS3DModeNone; 278 break; 279 } 280 } 281 282 if (hw_intf_->SetS3DMode(s3d_mode) != kErrorNone) { 283 hw_intf_->SetS3DMode(kS3DModeNone); 284 layer_stack->flags.s3d_mode_present = false; 285 } else if (s3d_mode != kS3DModeNone) { 286 layer_stack->flags.s3d_mode_present = true; 287 } 288 289 DisplayBase::ReconfigureDisplay(); 290 } 291 292 void DisplayHDMI::CECMessage(char *message) { 293 event_handler_->CECMessage(message); 294 } 295 296 DisplayError DisplayHDMI::VSync(int64_t timestamp) { 297 if (vsync_enable_) { 298 DisplayEventVSync vsync; 299 vsync.timestamp = timestamp; 300 event_handler_->VSync(vsync); 301 } 302 303 return kErrorNone; 304 } 305 306 } // namespace sdm 307 308