blob: fb6493a5dc5acbc997a332f8ac9cefef09ba1252 [file] [log] [blame]
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Tatenda Chipeperekwacbe1dfe2014-11-25 13:21:15 -08003 * Copyright (C) 2012-2015, The Linux Foundation. All rights reserved.
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -07004 *
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
Naseer Ahmed72cf9762012-07-21 12:17:13 -070021#define DEBUG 0
Naseer Ahmed72cf9762012-07-21 12:17:13 -070022#include <fcntl.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070023#include <linux/msm_mdp.h>
24#include <linux/fb.h>
25#include <sys/ioctl.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070026#include <cutils/properties.h>
27#include "hwc_utils.h"
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -070028#include "hdmi.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070029#include "overlayUtils.h"
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080030#include "overlay.h"
Tatenda Chipeperekwaf080b792014-03-05 19:32:33 -080031#include "qd_utils.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070032
33using namespace android;
Tatenda Chipeperekwace8d5c42014-08-13 10:53:49 -070034using namespace qdutils;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070035
36namespace qhwc {
Arun Kumar K.R26808f32013-02-11 19:17:05 -080037#define UNKNOWN_STRING "unknown"
38#define SPD_NAME_LENGTH 16
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070039
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -070040int HDMIDisplay::configure() {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070041 if(!openFrameBuffer()) {
42 ALOGE("%s: Failed to open FB: %d", __FUNCTION__, mFbNum);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080043 return -1;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070044 }
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -080045 readCEUnderscanInfo();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080046 readResolution();
Arun Kumar K.R37552c52012-12-10 12:47:18 -080047 /* Used for changing the resolution
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -070048 * getUserConfig will get the preferred
49 * config index set thru adb shell */
50 mActiveConfig = getUserConfig();
51 if (mActiveConfig == -1) {
Arun Kumar K.R37552c52012-12-10 12:47:18 -080052 //Get the best mode and set
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -070053 mActiveConfig = getBestConfig();
Arun Kumar K.R37552c52012-12-10 12:47:18 -080054 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -070055
56 // Read the system property to determine if downscale feature is enabled.
57 char value[PROPERTY_VALUE_MAX];
58 mMDPDownscaleEnabled = false;
59 if(property_get("sys.hwc.mdp_downscale_enabled", value, "false")
60 && !strcmp(value, "true")) {
61 mMDPDownscaleEnabled = true;
62 }
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -070063
Tatenda Chipeperekwacbe1dfe2014-11-25 13:21:15 -080064 // Set the mode corresponding to the active index
65 mCurrentMode = mEDIDModes[mActiveConfig];
66 setAttributes();
67 // set system property
68 property_set("hw.hdmiON", "1");
69
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -070070 // XXX: A debug property can be used to enable resolution change for
71 // testing purposes: debug.hwc.enable_resolution_change
72 mEnableResolutionChange = false;
73 if(property_get("debug.hwc.enable_resolution_change", value, "false")
74 && !strcmp(value, "true")) {
75 mEnableResolutionChange = true;
76 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080077 return 0;
78}
79
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -070080void HDMIDisplay::getAttributes(uint32_t& width, uint32_t& height) {
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -080081 uint32_t refresh = 0, fps = 0;
82 getAttrForConfig(mActiveConfig, width, height, refresh, fps);
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -070083}
84
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -070085int HDMIDisplay::teardown() {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070086 closeFrameBuffer();
87 resetInfo();
88 // unset system property
89 property_set("hw.hdmiON", "0");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080090 return 0;
91}
92
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -070093HDMIDisplay::HDMIDisplay():mFd(-1),
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -070094 mCurrentMode(-1), mModeCount(0), mPrimaryWidth(0), mPrimaryHeight(0),
Tatenda Chipeperekwacbe1dfe2014-11-25 13:21:15 -080095 mUnderscanSupported(false), mMDPDownscaleEnabled(false)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070096{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -070097 memset(&mVInfo, 0, sizeof(mVInfo));
Naseer Ahmed6bbd0a12015-01-23 11:57:10 -050098 mFbNum = qdutils::getHDMINode();
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -070099
100 mDisplayId = HWC_DISPLAY_EXTERNAL;
101 // Update the display if HDMI is connected as primary
102 if (isHDMIPrimaryDisplay()) {
103 mDisplayId = HWC_DISPLAY_PRIMARY;
104 }
105
Tatenda Chipeperekwacbe1dfe2014-11-25 13:21:15 -0800106 // Disable HPD at start if HDMI is external, it will be enabled later
Arun Kumar K.Re746ac52013-03-20 15:56:15 -0700107 // when the display powers on
108 // This helps for framework reboot or adb shell stop/start
Tatenda Chipeperekwacbe1dfe2014-11-25 13:21:15 -0800109 if (mDisplayId) {
110 writeHPDOption(0);
111 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800112
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700113 if(mFbNum != -1) {
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800114 // Update the Source Product Information
115 // Vendor Name
116 setSPDInfo("vendor_name", "ro.product.manufacturer");
117 // Product Description
118 setSPDInfo("product_description", "ro.product.name");
119 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700120
121 ALOGD_IF(DEBUG, "%s mDisplayId(%d) mFbNum(%d)",
122 __FUNCTION__, mDisplayId, mFbNum);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800123}
124/* gets the product manufacturer and product name and writes it
125 * to the sysfs node, so that the driver can get that information
126 * Used to show QCOM 8974 instead of Input 1 for example
127 */
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700128void HDMIDisplay::setSPDInfo(const char* node, const char* property) {
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800129 char info[PROPERTY_VALUE_MAX];
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700130 ssize_t err = -1;
131 int spdFile = openDeviceNode(node, O_RDWR);
132 if (spdFile >= 0) {
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800133 memset(info, 0, sizeof(info));
134 property_get(property, info, UNKNOWN_STRING);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700135 ALOGD_IF(DEBUG, "In %s: %s = %s",
136 __FUNCTION__, property, info);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800137 if (strncmp(info, UNKNOWN_STRING, SPD_NAME_LENGTH)) {
138 err = write(spdFile, info, strlen(info));
139 if (err <= 0) {
140 ALOGE("%s: file write failed for '%s'"
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700141 "err no = %d", __FUNCTION__, node, errno);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800142 }
143 } else {
144 ALOGD_IF(DEBUG, "%s: property_get failed for SPD %s",
145 __FUNCTION__, node);
146 }
147 close(spdFile);
Arun Kumar K.R25187572013-02-28 18:47:36 -0800148 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700149}
150
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700151void HDMIDisplay::setHPD(uint32_t value) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700152 ALOGD_IF(DEBUG,"HPD enabled=%d", value);
153 writeHPDOption(value);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700154}
155
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700156void HDMIDisplay::setActionSafeDimension(int w, int h) {
Saurabh Shah56f610d2012-08-07 15:27:06 -0700157 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800158 char actionsafeWidth[PROPERTY_VALUE_MAX];
159 char actionsafeHeight[PROPERTY_VALUE_MAX];
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800160 snprintf(actionsafeWidth, sizeof(actionsafeWidth), "%d", w);
Arun Kumar K.R4d73d642013-04-10 17:06:39 -0700161 property_set("persist.sys.actionsafe.width", actionsafeWidth);
Arun Kumar K.R26808f32013-02-11 19:17:05 -0800162 snprintf(actionsafeHeight, sizeof(actionsafeHeight), "%d", h);
Arun Kumar K.R4d73d642013-04-10 17:06:39 -0700163 property_set("persist.sys.actionsafe.height", actionsafeHeight);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700164}
165
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700166int HDMIDisplay::getModeCount() const {
Saurabh Shah56f610d2012-08-07 15:27:06 -0700167 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700168 return mModeCount;
169}
170
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700171void HDMIDisplay::readCEUnderscanInfo()
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800172{
173 int hdmiScanInfoFile = -1;
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530174 ssize_t len = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800175 char scanInfo[17];
176 char *ce_info_str = NULL;
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800177 char *save_ptr;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800178 const char token[] = ", \n";
179 int ce_info = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800180
181 memset(scanInfo, 0, sizeof(scanInfo));
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700182 hdmiScanInfoFile = openDeviceNode("scan_info", O_RDONLY);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800183 if (hdmiScanInfoFile < 0) {
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800184 return;
185 } else {
186 len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700187 ALOGD("%s: Scan Info string: %s length = %zu",
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800188 __FUNCTION__, scanInfo, len);
189 if (len <= 0) {
190 close(hdmiScanInfoFile);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700191 ALOGE("%s: Scan Info file empty", __FUNCTION__);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800192 return;
193 }
194 scanInfo[len] = '\0'; /* null terminate the string */
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530195 close(hdmiScanInfoFile);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800196 }
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800197
198 /*
199 * The scan_info contains the three fields
200 * PT - preferred video format
201 * IT - video format
202 * CE video format - containing the underscan support information
203 */
204
205 /* PT */
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800206 ce_info_str = strtok_r(scanInfo, token, &save_ptr);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800207 if (ce_info_str) {
208 /* IT */
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800209 ce_info_str = strtok_r(NULL, token, &save_ptr);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800210 if (ce_info_str) {
211 /* CE */
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800212 ce_info_str = strtok_r(NULL, token, &save_ptr);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800213 if (ce_info_str)
214 ce_info = atoi(ce_info_str);
215 }
216 }
217
218 if (ce_info_str) {
219 // ce_info contains the underscan information
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700220 if (ce_info == HDMI_SCAN_ALWAYS_UNDERSCANED ||
221 ce_info == HDMI_SCAN_BOTH_SUPPORTED)
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800222 // if TV supported underscan, then driver will always underscan
223 // hence no need to apply action safe rectangle
224 mUnderscanSupported = true;
225 } else {
226 ALOGE("%s: scan_info string error", __FUNCTION__);
227 }
228
229 // Store underscan support info in a system property
230 const char* prop = (mUnderscanSupported) ? "1" : "0";
231 property_set("hw.underscan_supported", prop);
232 return;
233}
234
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700235HDMIDisplay::~HDMIDisplay()
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700236{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700237 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700238}
239
Arun Kumar K.R25187572013-02-28 18:47:36 -0800240/*
241 * sets the fb_var_screeninfo from the hdmi_mode_timing_info
242 */
243void setDisplayTiming(struct fb_var_screeninfo &info,
244 const msm_hdmi_mode_timing_info* mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700245{
246 info.reserved[0] = 0;
247 info.reserved[1] = 0;
248 info.reserved[2] = 0;
Ken Zhang7b03a952013-01-16 13:23:48 -0500249#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Arun Kumar K.R25187572013-02-28 18:47:36 -0800250 info.reserved[3] = (info.reserved[3] & 0xFFFF) |
251 (mode->video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500252#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700253 info.xoffset = 0;
254 info.yoffset = 0;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800255 info.xres = mode->active_h;
256 info.yres = mode->active_v;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700257
Arun Kumar K.R25187572013-02-28 18:47:36 -0800258 info.pixclock = (mode->pixel_freq)*1000;
259 info.vmode = mode->interlaced ?
260 FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700261
Arun Kumar K.R25187572013-02-28 18:47:36 -0800262 info.right_margin = mode->front_porch_h;
263 info.hsync_len = mode->pulse_width_h;
264 info.left_margin = mode->back_porch_h;
265 info.lower_margin = mode->front_porch_v;
266 info.vsync_len = mode->pulse_width_v;
267 info.upper_margin = mode->back_porch_v;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700268}
269
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700270int HDMIDisplay::parseResolution(char* edidStr)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700271{
272 char delim = ',';
273 int count = 0;
274 char *start, *end;
275 // EDIDs are string delimited by ','
276 // Ex: 16,4,5,3,32,34,1
277 // Parse this string to get mode(int)
278 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700279 end = &delim;
280 while(*end == delim) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700281 mEDIDModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700282 start = end+1;
283 count++;
284 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700285 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
286 for (int i = 0; i < count; i++)
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700287 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, mEDIDModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700288 return count;
289}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700290
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700291bool HDMIDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700292{
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530293 ssize_t len = -1;
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800294 char edidStr[PAGE_SIZE] = {'\0'};
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700295
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700296 int hdmiEDIDFile = openDeviceNode("edid_modes", O_RDONLY);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700297 if (hdmiEDIDFile < 0) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700298 return false;
299 } else {
Arun Kumar K.R83812342013-09-24 17:30:36 -0700300 len = read(hdmiEDIDFile, edidStr, sizeof(edidStr)-1);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700301 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %zu",
Arun Kumar K.R83812342013-09-24 17:30:36 -0700302 __FUNCTION__, edidStr, len);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700303 if (len <= 0) {
304 ALOGE("%s: edid_modes file empty", __FUNCTION__);
Arun Kumar K.R83812342013-09-24 17:30:36 -0700305 edidStr[0] = '\0';
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700306 }
307 else {
Arun Kumar K.R83812342013-09-24 17:30:36 -0700308 while (len > 1 && isspace(edidStr[len-1])) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700309 --len;
Arun Kumar K.R83812342013-09-24 17:30:36 -0700310 }
311 edidStr[len] = '\0';
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700312 }
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530313 close(hdmiEDIDFile);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700314 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700315 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800316 // Get EDID modes from the EDID strings
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700317 mModeCount = parseResolution(edidStr);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700318 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
319 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700320 }
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800321 // Populate the internal data structure with the timing information
322 // for each edid mode read from the driver
323 if (mModeCount > 0) {
324 mDisplayConfigs = new msm_hdmi_mode_timing_info[mModeCount];
325 readConfigs();
326 } else {
327 // If we fail to read from EDID when HDMI is connected, then
328 // mModeCount will be 0 and bestConfigIndex will be invalid.
329 // In this case, we populate the mEDIDModes structure with
330 // a default mode at config index 0.
331 uint32_t defaultConfigIndex = 0;
332 mModeCount = 1;
333 mEDIDModes[defaultConfigIndex] = HDMI_VFRMT_640x480p60_4_3;
334 struct msm_hdmi_mode_timing_info defaultMode =
335 HDMI_VFRMT_640x480p60_4_3_TIMING;
336 mDisplayConfigs = new msm_hdmi_mode_timing_info[mModeCount];
337 mDisplayConfigs[defaultConfigIndex] = defaultMode;
338 ALOGD("%s Defaulting to HDMI_VFRMT_640x480p60_4_3", __FUNCTION__);
339 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700340
Arun Kumar K.R83812342013-09-24 17:30:36 -0700341 return (len > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700342}
343
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700344bool HDMIDisplay::openFrameBuffer()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700345{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700346 if (mFd == -1) {
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700347 char strDevPath[MAX_SYSFS_FILE_PATH];
348 snprintf(strDevPath, MAX_SYSFS_FILE_PATH, "/dev/graphics/fb%d", mFbNum);
349 mFd = open(strDevPath, O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700350 if (mFd < 0)
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700351 ALOGE("%s: %s is not available", __FUNCTION__, strDevPath);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700352 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700353 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700354}
355
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700356bool HDMIDisplay::closeFrameBuffer()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700357{
358 int ret = 0;
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500359 if(mFd >= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700360 ret = close(mFd);
361 mFd = -1;
362 }
363 return (ret == 0);
364}
365
366// clears the vinfo, edid, best modes
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700367void HDMIDisplay::resetInfo()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700368{
369 memset(&mVInfo, 0, sizeof(mVInfo));
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700370 memset(mEDIDModes, 0, sizeof(mEDIDModes));
371 mModeCount = 0;
372 mCurrentMode = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800373 mUnderscanSupported = false;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700374 mXres = 0;
375 mYres = 0;
376 mVsyncPeriod = 0;
377 mMDPScalingMode = false;
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800378 if (mDisplayConfigs) {
379 delete [] mDisplayConfigs;
380 mDisplayConfigs = 0;
381 }
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800382 // Reset the underscan supported system property
383 const char* prop = "0";
384 property_set("hw.underscan_supported", prop);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700385}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700386
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700387/// Returns the index of the user mode set(if any) using adb shell
388int HDMIDisplay::getUserConfig() {
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800389 /* Based on the property set the resolution */
390 char property_value[PROPERTY_VALUE_MAX];
Arun Kumar K.Rc31bdcb2013-02-25 17:47:42 -0800391 property_get("hw.hdmi.resolution", property_value, "-1");
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800392 int mode = atoi(property_value);
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800393 if(isValidMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500394 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700395 return getModeIndex(mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800396 }
397 return -1;
398}
399
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700400// Get the index of the best mode for the current HD TV
401int HDMIDisplay::getBestConfig() {
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800402 int bestConfigIndex = 0;
403 int edidMode = -1;
404 struct msm_hdmi_mode_timing_info currentModeInfo = {0};
405 struct msm_hdmi_mode_timing_info bestModeInfo = {0};
406 bestModeInfo.video_format = 0;
407 bestModeInfo.active_v = 0;
408 bestModeInfo.active_h = 0;
409 bestModeInfo.refresh_rate = 0;
410 bestModeInfo.ar = HDMI_RES_AR_INVALID;
411
412 // for all the timing info read, get the best config
413 for (int configIndex = 0; configIndex < mModeCount; configIndex++) {
414 currentModeInfo = mDisplayConfigs[configIndex];
Tatenda Chipeperekwaeaa79802015-03-02 19:57:17 -0800415 edidMode = mEDIDModes[configIndex];
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800416
Tatenda Chipeperekwaeaa79802015-03-02 19:57:17 -0800417 if (!isValidMode(edidMode)) {
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800418 ALOGD("%s EDID Mode %d is not supported", __FUNCTION__, edidMode);
419 continue;
420 }
421
422 ALOGD_IF(DEBUG, "%s Best (%d) : (%dx%d) @ %d;"
423 " Current (%d) (%dx%d) @ %d",
424 __FUNCTION__, bestConfigIndex, bestModeInfo.active_h,
425 bestModeInfo.active_v, bestModeInfo.refresh_rate, configIndex,
426 currentModeInfo.active_h, currentModeInfo.active_v,
427 currentModeInfo.refresh_rate);
428
429 // Compare two HDMI modes in order of height, width, refresh rate and
430 // aspect ratio.
431 if (currentModeInfo.active_v > bestModeInfo.active_v) {
432 bestConfigIndex = configIndex;
433 } else if (currentModeInfo.active_v == bestModeInfo.active_v) {
434 if (currentModeInfo.active_h > bestModeInfo.active_h) {
435 bestConfigIndex = configIndex;
436 } else if (currentModeInfo.active_h == bestModeInfo.active_h) {
437 if (currentModeInfo.refresh_rate > bestModeInfo.refresh_rate) {
438 bestConfigIndex = configIndex;
439 } else if (currentModeInfo.refresh_rate ==
440 bestModeInfo.refresh_rate) {
441 if (currentModeInfo.ar > bestModeInfo.ar) {
442 bestConfigIndex = configIndex;
443 }
444 }
445 }
446 }
447 if (bestConfigIndex == configIndex) {
448 bestModeInfo = mDisplayConfigs[bestConfigIndex];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700449 }
450 }
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800451 return bestConfigIndex;
452}
453
454// Utility function used to request HDMI driver to write a new page of timing
455// info into res_info node
456void HDMIDisplay::requestNewPage(int pageNumber) {
457 char pageString[PAGE_SIZE];
458 int fd = openDeviceNode("res_info", O_WRONLY);
459 if (fd >= 0) {
460 snprintf(pageString, sizeof(pageString), "%d", pageNumber);
461 ALOGD_IF(DEBUG, "%s: page=%s", __FUNCTION__, pageString);
462 ssize_t err = write(fd, pageString, sizeof(pageString));
463 if (err <= 0) {
464 ALOGE("%s: Write to res_info failed (%d)", __FUNCTION__, errno);
465 }
466 close(fd);
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700467 }
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800468}
469
470// Reads the contents of res_info node into a buffer if the file is not empty
471bool HDMIDisplay::readResFile(char * configBuffer) {
472 bool fileRead = false;
473 size_t bytesRead = 0;
474 int fd = openDeviceNode("res_info", O_RDONLY);
475 if (fd >= 0 && (bytesRead = read(fd, configBuffer, PAGE_SIZE)) != 0) {
476 fileRead = true;
477 }
478 close(fd);
479 ALOGD_IF(DEBUG, "%s: bytesRead=%d fileRead=%d",
480 __FUNCTION__, bytesRead, fileRead);
481 return fileRead;
482}
483
484// Populates the internal timing info structure with the timing info obtained
485// from the HDMI driver
486void HDMIDisplay::readConfigs() {
487 int configIndex = 0;
488 int pageNumber = MSM_HDMI_INIT_RES_PAGE;
489 long unsigned int size = sizeof(msm_hdmi_mode_timing_info);
490
491 while (true) {
492 char configBuffer[PAGE_SIZE] = {0};
493 msm_hdmi_mode_timing_info *info =
494 (msm_hdmi_mode_timing_info*) configBuffer;
495
496 if (!readResFile(configBuffer))
497 break;
498
499 while (info->video_format && size < PAGE_SIZE) {
500 mDisplayConfigs[configIndex] = *info;
501 size += sizeof(msm_hdmi_mode_timing_info);
502 info++;
503 ALOGD_IF(DEBUG, "%s: Config=%d Mode %d: (%dx%d) @ %d",
504 __FUNCTION__, configIndex,
505 mDisplayConfigs[configIndex].video_format,
506 mDisplayConfigs[configIndex].active_h,
507 mDisplayConfigs[configIndex].active_v,
508 mDisplayConfigs[configIndex].refresh_rate);
509 configIndex++;
510 }
511 size = sizeof(msm_hdmi_mode_timing_info);
512 // Request HDMI driver to populate res_info with more
513 // timing information
514 pageNumber++;
515 requestNewPage(pageNumber);
516 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700517}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700518
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700519inline bool HDMIDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700520{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800521 bool valid = false;
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800522 int modeIndex = getModeIndex(ID);
523 if (ID <= 0 || modeIndex < 0 || modeIndex > mModeCount) {
524 return false;
525 }
526 struct msm_hdmi_mode_timing_info* mode = &mDisplayConfigs[modeIndex];
527 // We dont support interlaced modes
528 if (mode->supported && !mode->interlaced) {
529 valid = true;
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800530 }
531 return valid;
532}
533
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700534// Does a put_vscreen info on the HDMI interface which will update
535// the configuration (resolution, timing info) to match mCurrentMode
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700536void HDMIDisplay::activateDisplay()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700537{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700538 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700539 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
540 if(ret < 0) {
541 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
542 strerror(errno));
543 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700544 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
545 "(%d,%d,%d) %dMHz>", __FUNCTION__,
546 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
547 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
548 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
549 mVInfo.pixclock/1000/1000);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700550
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800551 struct msm_hdmi_mode_timing_info *mode = &mDisplayConfigs[mActiveConfig];
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700552 setDisplayTiming(mVInfo, mode);
553 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
554 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, mCurrentMode,
555 mode->video_format, mVInfo.xres, mVInfo.yres,
556 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
557 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
558 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500559#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700560 struct msmfb_metadata metadata;
561 memset(&metadata, 0 , sizeof(metadata));
562 metadata.op = metadata_op_vic;
563 metadata.data.video_info_code = mode->video_format;
564 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
565 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
566 __FUNCTION__, strerror(errno));
567 }
Ken Zhang7b03a952013-01-16 13:23:48 -0500568#endif
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700569 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
570 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
571 if(ret < 0) {
572 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
573 __FUNCTION__, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700574 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700575}
576
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700577bool HDMIDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700578{
579 bool ret = true;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700580 if(mFbNum != -1) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700581 int hdmiHPDFile = openDeviceNode("hpd", O_RDWR);
582 if (hdmiHPDFile >= 0) {
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530583 ssize_t err = -1;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700584 ALOGD_IF(DEBUG, "%s: option = %d",
585 __FUNCTION__, userOption);
Arun Kumar K.R47d6b642013-06-26 16:20:30 -0700586 if(userOption)
587 err = write(hdmiHPDFile, "1", 2);
588 else
589 err = write(hdmiHPDFile, "0" , 2);
590 if (err <= 0) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700591 ALOGE("%s: file write failed 'hpd'", __FUNCTION__);
Arun Kumar K.R47d6b642013-06-26 16:20:30 -0700592 ret = false;
593 }
594 close(hdmiHPDFile);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700595 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700596 }
597 return ret;
598}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700599
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700600
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700601void HDMIDisplay::setAttributes() {
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800602 uint32_t refresh = 0, fps = 0;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700603 // Always set dpyAttr res to mVInfo res
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800604 getAttrForConfig(mActiveConfig, mXres, mYres, refresh, fps);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700605 mMDPScalingMode = false;
606
607 if(overlay::Overlay::getInstance()->isUIScalingOnExternalSupported()
608 && mMDPDownscaleEnabled) {
609 // if primary resolution is more than the hdmi resolution
610 // configure dpy attr to primary resolution and set MDP
611 // scaling mode
612 // Restrict this upto 1080p resolution max, if target does not
613 // support source split feature.
614 uint32_t primaryArea = mPrimaryWidth * mPrimaryHeight;
615 if(((primaryArea) > (mXres * mYres)) &&
616 (((primaryArea) <= SUPPORTED_DOWNSCALE_AREA) ||
Ramkumar Radhakrishnana5c72872014-08-28 19:04:18 -0700617 qdutils::MDPVersion::getInstance().isSrcSplit())) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700618 // tmpW and tmpH will hold the primary dimensions before we
619 // update the aspect ratio if necessary.
620 int tmpW = mPrimaryWidth;
621 int tmpH = mPrimaryHeight;
622 // HDMI is always in landscape, so always assign the higher
623 // dimension to hdmi's xres
624 if(mPrimaryHeight > mPrimaryWidth) {
625 tmpW = mPrimaryHeight;
626 tmpH = mPrimaryWidth;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700627 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700628 // The aspect ratios of the external and primary displays
629 // can be different. As a result, directly assigning primary
630 // resolution could lead to an incorrect final image.
631 // We get around this by calculating a new resolution by
632 // keeping aspect ratio intact.
633 hwc_rect r = {0, 0, 0, 0};
634 qdutils::getAspectRatioPosition(tmpW, tmpH, mXres, mYres, r);
635 uint32_t newExtW = r.right - r.left;
636 uint32_t newExtH = r.bottom - r.top;
637 uint32_t alignedExtW;
638 uint32_t alignedExtH;
639 // On 8994 and below targets MDP supports only 4X downscaling,
640 // Restricting selected external resolution to be exactly 4X
641 // greater resolution than actual external resolution
642 uint32_t maxMDPDownScale =
643 qdutils::MDPVersion::getInstance().getMaxMDPDownscale();
644 if((mXres * mYres * maxMDPDownScale) < (newExtW * newExtH)) {
645 float upScaleFactor = (float)maxMDPDownScale / 2.0f;
646 newExtW = (int)((float)mXres * upScaleFactor);
647 newExtH = (int)((float)mYres * upScaleFactor);
648 }
649 // Align it down so that the new aligned resolution does not
650 // exceed the maxMDPDownscale factor
651 alignedExtW = overlay::utils::aligndown(newExtW, 4);
652 alignedExtH = overlay::utils::aligndown(newExtH, 4);
653 mXres = alignedExtW;
654 mYres = alignedExtH;
655 // Set External Display MDP Downscale mode indicator
656 mMDPScalingMode = true;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700657 }
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700658 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700659 ALOGD_IF(DEBUG_MDPDOWNSCALE, "Selected external resolution [%d X %d] "
660 "maxMDPDownScale %d mMDPScalingMode %d srcSplitEnabled %d "
661 "MDPDownscale feature %d",
662 mXres, mYres,
663 qdutils::MDPVersion::getInstance().getMaxMDPDownscale(),
664 mMDPScalingMode, qdutils::MDPVersion::getInstance().isSrcSplit(),
665 mMDPDownscaleEnabled);
666 mVsyncPeriod = (int) 1000000000l / fps;
667 ALOGD_IF(DEBUG, "%s xres=%d, yres=%d", __FUNCTION__, mXres, mYres);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700668}
669
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700670/* returns the fd related to the node specified*/
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700671int HDMIDisplay::openDeviceNode(const char* node, int fileMode) const {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700672 char sysFsFilePath[MAX_SYSFS_FILE_PATH];
673 memset(sysFsFilePath, 0, sizeof(sysFsFilePath));
674 snprintf(sysFsFilePath , sizeof(sysFsFilePath),
675 "/sys/devices/virtual/graphics/fb%d/%s",
676 mFbNum, node);
677
678 int fd = open(sysFsFilePath, fileMode, 0);
679
680 if (fd < 0) {
681 ALOGE("%s: file '%s' not found : ret = %d err str: %s",
682 __FUNCTION__, sysFsFilePath, fd, strerror(errno));
683 }
684 return fd;
685}
686
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700687bool HDMIDisplay::isHDMIPrimaryDisplay() {
Naseer Ahmed6bbd0a12015-01-23 11:57:10 -0500688 return (mFbNum == HWC_DISPLAY_PRIMARY);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700689}
690
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700691int HDMIDisplay::getConnectedState() {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700692 int ret = -1;
693 int mFbNum = qdutils::getHDMINode();
694 int connectedNode = openDeviceNode("connected", O_RDONLY);
695 if(connectedNode >= 0) {
696 char opStr[4];
697 ssize_t bytesRead = read(connectedNode, opStr, sizeof(opStr) - 1);
698 if(bytesRead > 0) {
699 opStr[bytesRead] = '\0';
700 ret = atoi(opStr);
701 ALOGD_IF(DEBUG, "%s: Read %d from connected", __FUNCTION__, ret);
702 } else if(bytesRead == 0) {
703 ALOGE("%s: HDMI connected node empty", __FUNCTION__);
704 } else {
705 ALOGE("%s: Read from HDMI connected node failed with error %s",
706 __FUNCTION__, strerror(errno));
707 }
708 close(connectedNode);
709 } else {
710 ALOGD("%s: /sys/class/graphics/fb%d/connected could not be opened : %s",
711 __FUNCTION__, mFbNum, strerror(errno));
712 }
713 return ret;
714}
715
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700716void HDMIDisplay::setPrimaryAttributes(uint32_t primaryWidth,
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700717 uint32_t primaryHeight) {
718 mPrimaryHeight = primaryHeight;
719 mPrimaryWidth = primaryWidth;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700720}
721
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700722int HDMIDisplay::setActiveConfig(int newConfig) {
723 if(newConfig < 0 || newConfig > mModeCount) {
724 ALOGE("%s Invalid configuration %d", __FUNCTION__, newConfig);
725 return -EINVAL;
726 }
727
728 // XXX: Currently, we only support a change in frame rate.
729 // We need to validate the new config before proceeding.
730 if (!isValidConfigChange(newConfig)) {
731 ALOGE("%s Invalid configuration %d", __FUNCTION__, newConfig);
732 return -EINVAL;
733 }
734
735 mCurrentMode = mEDIDModes[newConfig];
736 mActiveConfig = newConfig;
737 activateDisplay();
738 ALOGD("%s config(%d) mode(%d)", __FUNCTION__, mActiveConfig, mCurrentMode);
739 return 0;
740}
741
Naseer Ahmed6bbd0a12015-01-23 11:57:10 -0500742static const char* getS3DStringFromMode(int s3dMode) {
743 const char* ret ;
744 switch(s3dMode) {
745 case HDMI_S3D_NONE:
746 ret = "None";
747 break;
748 case HDMI_S3D_SIDE_BY_SIDE:
749 ret = "SSH";
750 break;
751 case HDMI_S3D_TOP_AND_BOTTOM:
752 ret = "TAB";
753 break;
754 //FP (FramePacked) mode is not supported in the HAL
755 default:
756 ALOGD("%s: Unsupported s3d mode: %d", __FUNCTION__, s3dMode);
757 ret = NULL;
758 }
759 return ret;
760}
761
762bool HDMIDisplay::isS3DModeSupported(int s3dMode) {
763 if(s3dMode == HDMI_S3D_NONE)
764 return true;
765
766 char s3dEdidStr[PAGE_SIZE] = {'\0'};
767
768 const char *s3dModeString = getS3DStringFromMode(s3dMode);
769
770 if(s3dModeString == NULL)
771 return false;
772
773 int s3dEdidNode = openDeviceNode("edid_3d_modes", O_RDONLY);
774 if(s3dEdidNode >= 0) {
775 ssize_t len = read(s3dEdidNode, s3dEdidStr, sizeof(s3dEdidStr)-1);
776 if (len > 0) {
777 ALOGI("%s: s3dEdidStr: %s mCurrentMode:%d", __FUNCTION__,
778 s3dEdidStr, mCurrentMode);
779 //Three level inception!
780 //The string looks like 16=SSH,4=FP:TAB:SSH,5=FP:SSH,32=FP:TAB:SSH
781 char *saveptr_l1, *saveptr_l2, *saveptr_l3;
782 char *l1, *l2, *l3;
783 int mode = 0;
784 l1 = strtok_r(s3dEdidStr,",", &saveptr_l1);
785 while (l1 != NULL) {
786 l2 = strtok_r(l1, "=", &saveptr_l2);
787 if (l2 != NULL)
788 mode = atoi(l2);
789 while (l2 != NULL) {
790 if (mode != mCurrentMode) {
791 break;
792 }
793 l3 = strtok_r(l2, ":", &saveptr_l3);
794 while (l3 != NULL) {
795 if (strncmp(l3, s3dModeString,
796 strlen(s3dModeString)) == 0) {
797 close(s3dEdidNode);
798 return true;
799 }
800 l3 = strtok_r(NULL, ":", &saveptr_l3);
801 }
802 l2 = strtok_r(NULL, "=", &saveptr_l2);
803 }
804 l1 = strtok_r(NULL, ",", &saveptr_l1);
805 }
806
807 }
808 } else {
809 ALOGI("%s: /sys/class/graphics/fb%d/edid_3d_modes could not be opened : %s",
810 __FUNCTION__, mFbNum, strerror(errno));
811 }
812 close(s3dEdidNode);
813 return false;
814}
815
816bool HDMIDisplay::writeS3DMode(int s3dMode) {
817 bool ret = true;
818 if(mFbNum != -1) {
819 int hdmiS3DModeFile = openDeviceNode("s3d_mode", O_RDWR);
820 if(hdmiS3DModeFile >=0 ) {
821 char curModeStr[PROPERTY_VALUE_MAX];
822 int currentS3DMode = -1;
823 size_t len = read(hdmiS3DModeFile, curModeStr, sizeof(curModeStr) - 1);
824 if(len > 0) {
825 currentS3DMode = atoi(curModeStr);
826 } else {
827 ret = false;
828 ALOGE("%s: Failed to read s3d_mode", __FUNCTION__);
829 }
830
831 if (currentS3DMode >=0 && currentS3DMode != s3dMode) {
832 ssize_t err = -1;
833 ALOGD_IF(DEBUG, "%s: mode = %d",
834 __FUNCTION__, s3dMode);
835 char mode[PROPERTY_VALUE_MAX];
836 snprintf(mode,sizeof(mode),"%d",s3dMode);
837 err = write(hdmiS3DModeFile, mode, sizeof(mode));
838 if (err <= 0) {
839 ALOGE("%s: file write failed 's3d_mode'", __FUNCTION__);
840 ret = false;
841 }
842 }
843 close(hdmiS3DModeFile);
844 }
845 }
846 return ret;
847}
848
849bool HDMIDisplay::configure3D(int s3dMode) {
850 if(isS3DModeSupported(s3dMode)) {
851 if(!writeS3DMode(s3dMode))
852 return false;
853 } else {
854 ALOGE("%s: 3D mode: %d is not supported", __FUNCTION__, s3dMode);
855 return false;
856 }
857 return true;
858}
859
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700860// returns false if the xres or yres of the new config do
861// not match the current config
862bool HDMIDisplay::isValidConfigChange(int newConfig) {
863 int newMode = mEDIDModes[newConfig];
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800864 uint32_t width = 0, height = 0, refresh = 0, fps = 0;
865 getAttrForConfig(newConfig, width, height, refresh, fps);
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700866 return ((mXres == width) && (mYres == height)) || mEnableResolutionChange;
867}
868
869int HDMIDisplay::getModeIndex(int mode) {
870 int modeIndex = -1;
871 for(int i = 0; i < mModeCount; i++) {
872 if(mode == mEDIDModes[i]) {
873 modeIndex = i;
874 break;
875 }
876 }
877 return modeIndex;
878}
879
880int HDMIDisplay::getAttrForConfig(int config, uint32_t& xres,
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800881 uint32_t& yres, uint32_t& refresh, uint32_t& fps) const {
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700882 if(config < 0 || config > mModeCount) {
883 ALOGE("%s Invalid configuration %d", __FUNCTION__, config);
884 return -EINVAL;
885 }
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800886
887 xres = mDisplayConfigs[config].active_h;
888 yres = mDisplayConfigs[config].active_v;
889 fps = (mDisplayConfigs[config].refresh_rate / 1000);
890
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700891 refresh = (uint32_t) 1000000000l / fps;
892 ALOGD_IF(DEBUG, "%s xres(%d) yres(%d) fps(%d) refresh(%d)", __FUNCTION__,
893 xres, yres, fps, refresh);
894 return 0;
895}
896
897int HDMIDisplay::getDisplayConfigs(uint32_t* configs,
898 size_t* numConfigs) const {
899 if (*numConfigs <= 0) {
900 ALOGE("%s Invalid number of configs (%d)", __FUNCTION__, *numConfigs);
901 return -EINVAL;
902 }
903 *numConfigs = mModeCount;
904 for (int configIndex = 0; configIndex < mModeCount; configIndex++) {
905 configs[configIndex] = (uint32_t)configIndex;
906 }
907 return 0;
908}
909
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700910};