Home | History | Annotate | Download | only in libhdmi
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  * Copyright (C) 2012-2014, 2016, The Linux Foundation. All rights reserved.
      4  *
      5  * Not a Contribution, Apache license notifications and license are
      6  * retained for attribution purposes only.
      7  *
      8  * Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *      http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 #define DEBUG 0
     22 #include <fcntl.h>
     23 #include <linux/msm_mdp.h>
     24 #include <video/msm_hdmi_modes.h>
     25 #include <linux/fb.h>
     26 #include <sys/ioctl.h>
     27 #include <cutils/properties.h>
     28 #include "hwc_utils.h"
     29 #include "hdmi.h"
     30 #include "overlayUtils.h"
     31 #include "overlay.h"
     32 #include "qd_utils.h"
     33 
     34 using namespace android;
     35 using namespace qdutils;
     36 
     37 namespace qhwc {
     38 #define UNKNOWN_STRING                  "unknown"
     39 #define SPD_NAME_LENGTH                 16
     40 
     41 /* The array gEDIDData contains a list of modes currently
     42  * supported by HDMI and display, and modes that are not
     43  * supported i.e. interlaced modes.
     44 
     45  * In order to add support for a new mode, the mode must be
     46  * appended to the end of the array.
     47  *
     48  * Each new entry must contain the following:
     49  * -Mode: a video format defined in msm_hdmi_modes.h
     50  * -Width: x resolution for the mode
     51  * -Height: y resolution for the mode
     52  * -FPS: the frame rate for the mode
     53  * -Mode Order: the priority for the new mode that is used when determining
     54  *  the best mode when the HDMI display is connected.
     55  */
     56 EDIDData gEDIDData [] = {
     57     EDIDData(HDMI_VFRMT_1440x480i60_4_3, 1440, 480, 60, 1),
     58     EDIDData(HDMI_VFRMT_1440x480i60_16_9, 1440, 480, 60, 2),
     59     EDIDData(HDMI_VFRMT_1440x576i50_4_3, 1440, 576, 50, 3),
     60     EDIDData(HDMI_VFRMT_1440x576i50_16_9, 1440, 576, 50, 4),
     61     EDIDData(HDMI_VFRMT_1920x1080i60_16_9, 1920, 1080, 60, 5),
     62     EDIDData(HDMI_VFRMT_640x480p60_4_3, 640, 480, 60, 6),
     63     EDIDData(HDMI_VFRMT_720x480p60_4_3, 720, 480, 60, 7),
     64     EDIDData(HDMI_VFRMT_720x480p60_16_9, 720, 480, 60, 8),
     65     EDIDData(HDMI_VFRMT_720x576p50_4_3, 720, 576, 50, 9),
     66     EDIDData(HDMI_VFRMT_720x576p50_16_9, 720, 576, 50, 10),
     67     EDIDData(HDMI_VFRMT_800x600p60_4_3, 800, 600, 60, 11),
     68     EDIDData(HDMI_VFRMT_848x480p60_16_9, 848, 480, 60, 12),
     69     EDIDData(HDMI_VFRMT_1024x768p60_4_3, 1024, 768, 60, 13),
     70     EDIDData(HDMI_VFRMT_1280x1024p60_5_4, 1280, 1024, 60, 14),
     71     EDIDData(HDMI_VFRMT_1280x720p50_16_9, 1280, 720, 50, 15),
     72     EDIDData(HDMI_VFRMT_1280x720p60_16_9, 1280, 720, 60, 16),
     73     EDIDData(HDMI_VFRMT_1280x800p60_16_10, 1280, 800, 60, 17),
     74     EDIDData(HDMI_VFRMT_1280x960p60_4_3, 1280, 960, 60, 18),
     75     EDIDData(HDMI_VFRMT_1360x768p60_16_9, 1360, 768, 60, 19),
     76     EDIDData(HDMI_VFRMT_1366x768p60_16_10, 1366, 768, 60, 20),
     77     EDIDData(HDMI_VFRMT_1440x900p60_16_10, 1440, 900, 60, 21),
     78     EDIDData(HDMI_VFRMT_1400x1050p60_4_3, 1400, 1050, 60, 22),
     79     EDIDData(HDMI_VFRMT_1680x1050p60_16_10, 1680, 1050, 60, 23),
     80     EDIDData(HDMI_VFRMT_1600x1200p60_4_3, 1600, 1200, 60, 24),
     81     EDIDData(HDMI_VFRMT_1920x1080p24_16_9, 1920, 1080, 24, 25),
     82     EDIDData(HDMI_VFRMT_1920x1080p25_16_9, 1920, 1080, 25, 26),
     83     EDIDData(HDMI_VFRMT_1920x1080p30_16_9, 1920, 1080, 30, 27),
     84     EDIDData(HDMI_VFRMT_1920x1080p50_16_9, 1920, 1080, 50, 28),
     85     EDIDData(HDMI_VFRMT_1920x1080p60_16_9, 1920, 1080, 60, 29),
     86     EDIDData(HDMI_VFRMT_1920x1200p60_16_10, 1920, 1200, 60, 30),
     87     EDIDData(HDMI_VFRMT_2560x1600p60_16_9, 2560, 1600, 60, 31),
     88     EDIDData(HDMI_VFRMT_3840x2160p24_16_9, 3840, 2160, 24, 32),
     89     EDIDData(HDMI_VFRMT_3840x2160p25_16_9, 3840, 2160, 25, 33),
     90     EDIDData(HDMI_VFRMT_3840x2160p30_16_9, 3840, 2160, 30, 34),
     91     EDIDData(HDMI_EVFRMT_4096x2160p24_16_9, 4096, 2160, 24, 35),
     92 };
     93 
     94 // Number of modes in gEDIDData
     95 const int gEDIDCount = (sizeof(gEDIDData)/sizeof(gEDIDData)[0]);
     96 
     97 int HDMIDisplay::configure() {
     98     if(!openFrameBuffer()) {
     99         ALOGE("%s: Failed to open FB: %d", __FUNCTION__, mFbNum);
    100         return -1;
    101     }
    102     readCEUnderscanInfo();
    103     readResolution();
    104     // TODO: Move this to activate
    105     /* Used for changing the resolution
    106      * getUserMode will get the preferred
    107      * mode set thru adb shell */
    108     mCurrentMode = getUserMode();
    109     if (mCurrentMode == -1) {
    110         //Get the best mode and set
    111         mCurrentMode = getBestMode();
    112     }
    113     setAttributes();
    114     // set system property
    115     property_set("hw.hdmiON", "1");
    116 
    117     // Read the system property to determine if downscale feature is enabled.
    118     char value[PROPERTY_VALUE_MAX];
    119     mMDPDownscaleEnabled = false;
    120     if(property_get("sys.hwc.mdp_downscale_enabled", value, "false")
    121             && !strcmp(value, "true")) {
    122         mMDPDownscaleEnabled = true;
    123     }
    124     return 0;
    125 }
    126 
    127 void HDMIDisplay::getAttributes(uint32_t& width, uint32_t& height) {
    128     uint32_t fps = 0;
    129     getAttrForMode(width, height, fps);
    130 }
    131 
    132 int HDMIDisplay::teardown() {
    133     closeFrameBuffer();
    134     resetInfo();
    135     // unset system property
    136     property_set("hw.hdmiON", "0");
    137     return 0;
    138 }
    139 
    140 HDMIDisplay::HDMIDisplay():mFd(-1),
    141     mCurrentMode(-1), mModeCount(0), mPrimaryWidth(0), mPrimaryHeight(0),
    142     mUnderscanSupported(false)
    143 {
    144     memset(&mVInfo, 0, sizeof(mVInfo));
    145 
    146     mDisplayId = HWC_DISPLAY_EXTERNAL;
    147     // Update the display if HDMI is connected as primary
    148     if (isHDMIPrimaryDisplay()) {
    149         mDisplayId = HWC_DISPLAY_PRIMARY;
    150     }
    151 
    152     mFbNum = overlay::Overlay::getInstance()->getFbForDpy(mDisplayId);
    153     // disable HPD at start, it will be enabled later
    154     // when the display powers on
    155     // This helps for framework reboot or adb shell stop/start
    156     writeHPDOption(0);
    157 
    158     // for HDMI - retreive all the modes supported by the driver
    159     if(mFbNum != -1) {
    160         supported_video_mode_lut =
    161                         new msm_hdmi_mode_timing_info[HDMI_VFRMT_MAX];
    162         // Populate the mode table for supported modes
    163         MSM_HDMI_MODES_INIT_TIMINGS(supported_video_mode_lut);
    164         MSM_HDMI_MODES_SET_SUPP_TIMINGS(supported_video_mode_lut,
    165                                         MSM_HDMI_MODES_ALL);
    166         // Update the Source Product Information
    167         // Vendor Name
    168         setSPDInfo("vendor_name", "ro.product.manufacturer");
    169         // Product Description
    170         setSPDInfo("product_description", "ro.product.name");
    171     }
    172 
    173     ALOGD_IF(DEBUG, "%s mDisplayId(%d) mFbNum(%d)",
    174             __FUNCTION__, mDisplayId, mFbNum);
    175 }
    176 /* gets the product manufacturer and product name and writes it
    177  * to the sysfs node, so that the driver can get that information
    178  * Used to show QCOM 8974 instead of Input 1 for example
    179  */
    180 void HDMIDisplay::setSPDInfo(const char* node, const char* property) {
    181     char info[PROPERTY_VALUE_MAX];
    182     ssize_t err = -1;
    183     int spdFile = openDeviceNode(node, O_RDWR);
    184     if (spdFile >= 0) {
    185         memset(info, 0, sizeof(info));
    186         property_get(property, info, UNKNOWN_STRING);
    187         ALOGD_IF(DEBUG, "In %s: %s = %s",
    188                 __FUNCTION__, property, info);
    189         if (strncmp(info, UNKNOWN_STRING, SPD_NAME_LENGTH)) {
    190             err = write(spdFile, info, strlen(info));
    191             if (err <= 0) {
    192                 ALOGE("%s: file write failed for '%s'"
    193                       "err no = %d", __FUNCTION__, node, errno);
    194             }
    195         } else {
    196             ALOGD_IF(DEBUG, "%s: property_get failed for SPD %s",
    197                          __FUNCTION__, node);
    198         }
    199         close(spdFile);
    200     }
    201 }
    202 
    203 void HDMIDisplay::setHPD(uint32_t value) {
    204     ALOGD_IF(DEBUG,"HPD enabled=%d", value);
    205     writeHPDOption(value);
    206 }
    207 
    208 void HDMIDisplay::setActionSafeDimension(int w, int h) {
    209     ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
    210     char actionsafeWidth[PROPERTY_VALUE_MAX];
    211     char actionsafeHeight[PROPERTY_VALUE_MAX];
    212     snprintf(actionsafeWidth, sizeof(actionsafeWidth), "%d", w);
    213     property_set("persist.sys.actionsafe.width", actionsafeWidth);
    214     snprintf(actionsafeHeight, sizeof(actionsafeHeight), "%d", h);
    215     property_set("persist.sys.actionsafe.height", actionsafeHeight);
    216 }
    217 
    218 int HDMIDisplay::getModeCount() const {
    219     ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
    220     return mModeCount;
    221 }
    222 
    223 void HDMIDisplay::readCEUnderscanInfo()
    224 {
    225     int hdmiScanInfoFile = -1;
    226     ssize_t len = -1;
    227     char scanInfo[17];
    228     char *ce_info_str = NULL;
    229     char *save_ptr;
    230     const char token[] = ", \n";
    231     int ce_info = -1;
    232 
    233     memset(scanInfo, 0, sizeof(scanInfo));
    234     hdmiScanInfoFile = openDeviceNode("scan_info", O_RDONLY);
    235     if (hdmiScanInfoFile < 0) {
    236         return;
    237     } else {
    238         len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
    239         ALOGD("%s: Scan Info string: %s length = %zu",
    240                  __FUNCTION__, scanInfo, len);
    241         if (len <= 0) {
    242             close(hdmiScanInfoFile);
    243             ALOGE("%s: Scan Info file empty", __FUNCTION__);
    244             return;
    245         }
    246         scanInfo[len] = '\0';  /* null terminate the string */
    247         close(hdmiScanInfoFile);
    248     }
    249 
    250     /*
    251      * The scan_info contains the three fields
    252      * PT - preferred video format
    253      * IT - video format
    254      * CE video format - containing the underscan support information
    255      */
    256 
    257     /* PT */
    258     ce_info_str = strtok_r(scanInfo, token, &save_ptr);
    259     if (ce_info_str) {
    260         /* IT */
    261         ce_info_str = strtok_r(NULL, token, &save_ptr);
    262         if (ce_info_str) {
    263             /* CE */
    264             ce_info_str = strtok_r(NULL, token, &save_ptr);
    265             if (ce_info_str)
    266                 ce_info = atoi(ce_info_str);
    267         }
    268     }
    269 
    270     if (ce_info_str) {
    271         // ce_info contains the underscan information
    272         if (ce_info == HDMI_SCAN_ALWAYS_UNDERSCANED ||
    273             ce_info == HDMI_SCAN_BOTH_SUPPORTED)
    274             // if TV supported underscan, then driver will always underscan
    275             // hence no need to apply action safe rectangle
    276             mUnderscanSupported = true;
    277     } else {
    278         ALOGE("%s: scan_info string error", __FUNCTION__);
    279     }
    280 
    281     // Store underscan support info in a system property
    282     const char* prop = (mUnderscanSupported) ? "1" : "0";
    283     property_set("hw.underscan_supported", prop);
    284     return;
    285 }
    286 
    287 HDMIDisplay::~HDMIDisplay()
    288 {
    289     delete [] supported_video_mode_lut;
    290     closeFrameBuffer();
    291 }
    292 
    293 /*
    294  * sets the fb_var_screeninfo from the hdmi_mode_timing_info
    295  */
    296 void setDisplayTiming(struct fb_var_screeninfo &info,
    297                                 const msm_hdmi_mode_timing_info* mode)
    298 {
    299     info.reserved[0] = 0;
    300     info.reserved[1] = 0;
    301     info.reserved[2] = 0;
    302 #ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
    303     info.reserved[3] = (info.reserved[3] & 0xFFFF) |
    304               (mode->video_format << 16);
    305 #endif
    306     info.xoffset = 0;
    307     info.yoffset = 0;
    308     info.xres = mode->active_h;
    309     info.yres = mode->active_v;
    310 
    311     info.pixclock = (mode->pixel_freq)*1000;
    312     info.vmode = mode->interlaced ?
    313                     FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
    314 
    315     info.right_margin = mode->front_porch_h;
    316     info.hsync_len = mode->pulse_width_h;
    317     info.left_margin = mode->back_porch_h;
    318     info.lower_margin = mode->front_porch_v;
    319     info.vsync_len = mode->pulse_width_v;
    320     info.upper_margin = mode->back_porch_v;
    321 }
    322 
    323 int HDMIDisplay::parseResolution(char* edidStr)
    324 {
    325     char delim = ',';
    326     int count = 0;
    327     char *start, *end;
    328     // EDIDs are string delimited by ','
    329     // Ex: 16,4,5,3,32,34,1
    330     // Parse this string to get mode(int)
    331     start = (char*) edidStr;
    332     end = &delim;
    333     while(*end == delim) {
    334         mEDIDModes[count] = (int) strtol(start, &end, 10);
    335         start = end+1;
    336         count++;
    337     }
    338     ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
    339     for (int i = 0; i < count; i++)
    340         ALOGD_IF(DEBUG, "Mode[%d] = %d", i, mEDIDModes[i]);
    341     return count;
    342 }
    343 
    344 bool HDMIDisplay::readResolution()
    345 {
    346     ssize_t len = -1;
    347     char edidStr[128] = {'\0'};
    348 
    349     int hdmiEDIDFile = openDeviceNode("edid_modes", O_RDONLY);
    350     if (hdmiEDIDFile < 0) {
    351         return false;
    352     } else {
    353         len = read(hdmiEDIDFile, edidStr, sizeof(edidStr)-1);
    354         ALOGD_IF(DEBUG, "%s: EDID string: %s length = %zu",
    355                  __FUNCTION__, edidStr, len);
    356         if (len <= 0) {
    357             ALOGE("%s: edid_modes file empty", __FUNCTION__);
    358             edidStr[0] = '\0';
    359         }
    360         else {
    361             while (len > 1 && isspace(edidStr[len-1])) {
    362                 --len;
    363             }
    364             edidStr[len] = '\0';
    365         }
    366         close(hdmiEDIDFile);
    367     }
    368     if(len > 0) {
    369         // Get EDID modes from the EDID strings
    370         mModeCount = parseResolution(edidStr);
    371         ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
    372                  mModeCount);
    373     }
    374 
    375     return (len > 0);
    376 }
    377 
    378 bool HDMIDisplay::openFrameBuffer()
    379 {
    380     if (mFd == -1) {
    381         char strDevPath[MAX_SYSFS_FILE_PATH];
    382         snprintf(strDevPath, MAX_SYSFS_FILE_PATH, "/dev/graphics/fb%d", mFbNum);
    383         mFd = open(strDevPath, O_RDWR);
    384         if (mFd < 0)
    385             ALOGE("%s: %s is not available", __FUNCTION__, strDevPath);
    386     }
    387     return (mFd > 0);
    388 }
    389 
    390 bool HDMIDisplay::closeFrameBuffer()
    391 {
    392     int ret = 0;
    393     if(mFd >= 0) {
    394         ret = close(mFd);
    395         mFd = -1;
    396     }
    397     return (ret == 0);
    398 }
    399 
    400 // clears the vinfo, edid, best modes
    401 void HDMIDisplay::resetInfo()
    402 {
    403     memset(&mVInfo, 0, sizeof(mVInfo));
    404     memset(mEDIDModes, 0, sizeof(mEDIDModes));
    405     mModeCount = 0;
    406     mCurrentMode = -1;
    407     mUnderscanSupported = false;
    408     mXres = 0;
    409     mYres = 0;
    410     mVsyncPeriod = 0;
    411     mMDPScalingMode = false;
    412     // Reset the underscan supported system property
    413     const char* prop = "0";
    414     property_set("hw.underscan_supported", prop);
    415 }
    416 
    417 int HDMIDisplay::getModeOrder(int mode)
    418 {
    419     for (int dataIndex = 0; dataIndex < gEDIDCount; dataIndex++) {
    420         if (gEDIDData[dataIndex].mMode == mode) {
    421             return gEDIDData[dataIndex].mModeOrder;
    422         }
    423     }
    424     ALOGE("%s Mode not found: %d", __FUNCTION__, mode);
    425     return -1;
    426 }
    427 
    428 /// Returns the user mode set(if any) using adb shell
    429 int HDMIDisplay::getUserMode() {
    430     /* Based on the property set the resolution */
    431     char property_value[PROPERTY_VALUE_MAX];
    432     property_get("hw.hdmi.resolution", property_value, "-1");
    433     int mode = atoi(property_value);
    434     // We dont support interlaced modes
    435     if(isValidMode(mode) && !isInterlacedMode(mode)) {
    436         ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
    437         return mode;
    438     }
    439     return -1;
    440 }
    441 
    442 // Get the best mode for the current HD TV
    443 int HDMIDisplay::getBestMode() {
    444     int bestOrder = 0;
    445     int bestMode = HDMI_VFRMT_640x480p60_4_3;
    446     // for all the edid read, get the best mode
    447     for(int i = 0; i < mModeCount; i++) {
    448         int mode = mEDIDModes[i];
    449         int order = getModeOrder(mode);
    450         if (order > bestOrder) {
    451             bestOrder = order;
    452             bestMode = mode;
    453         }
    454     }
    455     return bestMode;
    456 }
    457 
    458 inline bool HDMIDisplay::isValidMode(int ID)
    459 {
    460     bool valid = false;
    461     for (int i = 0; i < mModeCount; i++) {
    462         if(ID == mEDIDModes[i]) {
    463             valid = true;
    464             break;
    465         }
    466     }
    467     return valid;
    468 }
    469 
    470 // returns true if the mode(ID) is interlaced mode format
    471 bool HDMIDisplay::isInterlacedMode(int ID) {
    472     bool interlaced = false;
    473     switch(ID) {
    474         case HDMI_VFRMT_1440x480i60_4_3:
    475         case HDMI_VFRMT_1440x480i60_16_9:
    476         case HDMI_VFRMT_1440x576i50_4_3:
    477         case HDMI_VFRMT_1440x576i50_16_9:
    478         case HDMI_VFRMT_1920x1080i60_16_9:
    479             interlaced = true;
    480             break;
    481         default:
    482             interlaced = false;
    483             break;
    484     }
    485     return interlaced;
    486 }
    487 
    488 // Does a put_vscreen info on the HDMI interface which will update
    489 // the configuration (resolution, timing info) to match mCurrentMode
    490 void HDMIDisplay::activateDisplay()
    491 {
    492     int ret = 0;
    493     ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
    494     if(ret < 0) {
    495         ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
    496                                                             strerror(errno));
    497     }
    498     ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
    499             "(%d,%d,%d) %dMHz>", __FUNCTION__,
    500             mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
    501             mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
    502             mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
    503             mVInfo.pixclock/1000/1000);
    504 
    505     const struct msm_hdmi_mode_timing_info *mode =
    506             &supported_video_mode_lut[0];
    507     for (unsigned int i = 0; i < HDMI_VFRMT_MAX; ++i) {
    508         const struct msm_hdmi_mode_timing_info *cur =
    509                 &supported_video_mode_lut[i];
    510         if (cur->video_format == (uint32_t)mCurrentMode) {
    511             mode = cur;
    512             break;
    513         }
    514     }
    515     setDisplayTiming(mVInfo, mode);
    516     ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
    517             "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, mCurrentMode,
    518             mode->video_format, mVInfo.xres, mVInfo.yres,
    519             mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
    520             mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
    521             mVInfo.pixclock/1000/1000);
    522 #ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
    523     struct msmfb_metadata metadata;
    524     memset(&metadata, 0 , sizeof(metadata));
    525     metadata.op = metadata_op_vic;
    526     metadata.data.video_info_code = mode->video_format;
    527     if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
    528         ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
    529                 __FUNCTION__, strerror(errno));
    530     }
    531 #endif
    532     mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
    533     ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
    534     if(ret < 0) {
    535         ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
    536                 __FUNCTION__, strerror(errno));
    537     }
    538 }
    539 
    540 bool HDMIDisplay::writeHPDOption(int userOption) const
    541 {
    542     bool ret = true;
    543     if(mFbNum != -1) {
    544         int hdmiHPDFile = openDeviceNode("hpd", O_RDWR);
    545         if (hdmiHPDFile >= 0) {
    546             ssize_t err = -1;
    547             ALOGD_IF(DEBUG, "%s: option = %d",
    548                     __FUNCTION__, userOption);
    549             if(userOption)
    550                 err = write(hdmiHPDFile, "1", 2);
    551             else
    552                 err = write(hdmiHPDFile, "0" , 2);
    553             if (err <= 0) {
    554                 ALOGE("%s: file write failed 'hpd'", __FUNCTION__);
    555                 ret = false;
    556             }
    557             close(hdmiHPDFile);
    558         }
    559     }
    560     return ret;
    561 }
    562 
    563 
    564 void HDMIDisplay::setAttributes() {
    565     uint32_t fps = 0;
    566     // Always set dpyAttr res to mVInfo res
    567     getAttrForMode(mXres, mYres, fps);
    568     mMDPScalingMode = false;
    569 
    570     if(overlay::Overlay::getInstance()->isUIScalingOnExternalSupported()
    571         && mMDPDownscaleEnabled) {
    572         // if primary resolution is more than the hdmi resolution
    573         // configure dpy attr to primary resolution and set MDP
    574         // scaling mode
    575         // Restrict this upto 1080p resolution max, if target does not
    576         // support source split feature.
    577         uint32_t primaryArea = mPrimaryWidth * mPrimaryHeight;
    578         if(((primaryArea) > (mXres * mYres)) &&
    579             (((primaryArea) <= SUPPORTED_DOWNSCALE_AREA) ||
    580                 qdutils::MDPVersion::getInstance().isSrcSplit())) {
    581             // tmpW and tmpH will hold the primary dimensions before we
    582             // update the aspect ratio if necessary.
    583             int tmpW = mPrimaryWidth;
    584             int tmpH = mPrimaryHeight;
    585             // HDMI is always in landscape, so always assign the higher
    586             // dimension to hdmi's xres
    587             if(mPrimaryHeight > mPrimaryWidth) {
    588                 tmpW = mPrimaryHeight;
    589                 tmpH = mPrimaryWidth;
    590             }
    591             // The aspect ratios of the external and primary displays
    592             // can be different. As a result, directly assigning primary
    593             // resolution could lead to an incorrect final image.
    594             // We get around this by calculating a new resolution by
    595             // keeping aspect ratio intact.
    596             hwc_rect r = {0, 0, 0, 0};
    597             qdutils::getAspectRatioPosition(tmpW, tmpH, mXres, mYres, r);
    598             uint32_t newExtW = r.right - r.left;
    599             uint32_t newExtH = r.bottom - r.top;
    600             uint32_t alignedExtW;
    601             uint32_t alignedExtH;
    602             // On 8994 and below targets MDP supports only 4X downscaling,
    603             // Restricting selected external resolution to be exactly 4X
    604             // greater resolution than actual external resolution
    605             uint32_t maxMDPDownScale =
    606                     qdutils::MDPVersion::getInstance().getMaxMDPDownscale();
    607             if((mXres * mYres * maxMDPDownScale) < (newExtW * newExtH)) {
    608                 float upScaleFactor = (float)maxMDPDownScale / 2.0f;
    609                 newExtW = (int)((float)mXres * upScaleFactor);
    610                 newExtH = (int)((float)mYres * upScaleFactor);
    611             }
    612             // Align it down so that the new aligned resolution does not
    613             // exceed the maxMDPDownscale factor
    614             alignedExtW = overlay::utils::aligndown(newExtW, 4);
    615             alignedExtH = overlay::utils::aligndown(newExtH, 4);
    616             mXres = alignedExtW;
    617             mYres = alignedExtH;
    618             // Set External Display MDP Downscale mode indicator
    619             mMDPScalingMode = true;
    620         }
    621     }
    622     ALOGD_IF(DEBUG_MDPDOWNSCALE, "Selected external resolution [%d X %d] "
    623             "maxMDPDownScale %d mMDPScalingMode %d srcSplitEnabled %d "
    624             "MDPDownscale feature %d",
    625             mXres, mYres,
    626             qdutils::MDPVersion::getInstance().getMaxMDPDownscale(),
    627             mMDPScalingMode, qdutils::MDPVersion::getInstance().isSrcSplit(),
    628             mMDPDownscaleEnabled);
    629     mVsyncPeriod = (int) 1000000000l / fps;
    630     ALOGD_IF(DEBUG, "%s xres=%d, yres=%d", __FUNCTION__, mXres, mYres);
    631 }
    632 
    633 void HDMIDisplay::getAttrForMode(uint32_t& width, uint32_t& height,
    634         uint32_t& fps) {
    635     for (int dataIndex = 0; dataIndex < gEDIDCount; dataIndex++) {
    636         if (gEDIDData[dataIndex].mMode == mCurrentMode) {
    637             width = gEDIDData[dataIndex].mWidth;
    638             height = gEDIDData[dataIndex].mHeight;
    639             fps = gEDIDData[dataIndex].mFps;
    640             return;
    641         }
    642     }
    643     ALOGE("%s Unable to get attributes for %d", __FUNCTION__, mCurrentMode);
    644 }
    645 
    646 /* returns the fd related to the node specified*/
    647 int HDMIDisplay::openDeviceNode(const char* node, int fileMode) const {
    648     char sysFsFilePath[MAX_SYSFS_FILE_PATH];
    649     memset(sysFsFilePath, 0, sizeof(sysFsFilePath));
    650     snprintf(sysFsFilePath , sizeof(sysFsFilePath),
    651             "/sys/devices/virtual/graphics/fb%d/%s",
    652             mFbNum, node);
    653 
    654     int fd = open(sysFsFilePath, fileMode, 0);
    655 
    656     if (fd < 0) {
    657         ALOGE("%s: file '%s' not found : ret = %d err str: %s",
    658                 __FUNCTION__, sysFsFilePath, fd, strerror(errno));
    659     }
    660     return fd;
    661 }
    662 
    663 bool HDMIDisplay::isHDMIPrimaryDisplay() {
    664     int hdmiNode = qdutils::getHDMINode();
    665     return (hdmiNode == HWC_DISPLAY_PRIMARY);
    666 }
    667 
    668 int HDMIDisplay::getConnectedState() {
    669     int ret = -1;
    670     int mFbNum = qdutils::getHDMINode();
    671     int connectedNode = openDeviceNode("connected", O_RDONLY);
    672     if(connectedNode >= 0) {
    673         char opStr[4];
    674         ssize_t bytesRead = read(connectedNode, opStr, sizeof(opStr) - 1);
    675         if(bytesRead > 0) {
    676             opStr[bytesRead] = '\0';
    677             ret = atoi(opStr);
    678             ALOGD_IF(DEBUG, "%s: Read %d from connected", __FUNCTION__, ret);
    679         } else if(bytesRead == 0) {
    680             ALOGE("%s: HDMI connected node empty", __FUNCTION__);
    681         } else {
    682             ALOGE("%s: Read from HDMI connected node failed with error %s",
    683                     __FUNCTION__, strerror(errno));
    684         }
    685         close(connectedNode);
    686     } else {
    687         ALOGD("%s: /sys/class/graphics/fb%d/connected could not be opened : %s",
    688                 __FUNCTION__, mFbNum, strerror(errno));
    689     }
    690     return ret;
    691 }
    692 
    693 void HDMIDisplay::setPrimaryAttributes(uint32_t primaryWidth,
    694         uint32_t primaryHeight) {
    695     mPrimaryHeight = primaryHeight;
    696     mPrimaryWidth = primaryWidth;
    697 }
    698 
    699 };
    700