blob: 1b2ffe0f2ba6b573bafce7516afec1b7c6013011 [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];
415
416 if (!currentModeInfo.supported) {
417 ALOGD("%s EDID Mode %d is not supported", __FUNCTION__, edidMode);
418 continue;
419 }
420
421 ALOGD_IF(DEBUG, "%s Best (%d) : (%dx%d) @ %d;"
422 " Current (%d) (%dx%d) @ %d",
423 __FUNCTION__, bestConfigIndex, bestModeInfo.active_h,
424 bestModeInfo.active_v, bestModeInfo.refresh_rate, configIndex,
425 currentModeInfo.active_h, currentModeInfo.active_v,
426 currentModeInfo.refresh_rate);
427
428 // Compare two HDMI modes in order of height, width, refresh rate and
429 // aspect ratio.
430 if (currentModeInfo.active_v > bestModeInfo.active_v) {
431 bestConfigIndex = configIndex;
432 } else if (currentModeInfo.active_v == bestModeInfo.active_v) {
433 if (currentModeInfo.active_h > bestModeInfo.active_h) {
434 bestConfigIndex = configIndex;
435 } else if (currentModeInfo.active_h == bestModeInfo.active_h) {
436 if (currentModeInfo.refresh_rate > bestModeInfo.refresh_rate) {
437 bestConfigIndex = configIndex;
438 } else if (currentModeInfo.refresh_rate ==
439 bestModeInfo.refresh_rate) {
440 if (currentModeInfo.ar > bestModeInfo.ar) {
441 bestConfigIndex = configIndex;
442 }
443 }
444 }
445 }
446 if (bestConfigIndex == configIndex) {
447 bestModeInfo = mDisplayConfigs[bestConfigIndex];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700448 }
449 }
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800450 return bestConfigIndex;
451}
452
453// Utility function used to request HDMI driver to write a new page of timing
454// info into res_info node
455void HDMIDisplay::requestNewPage(int pageNumber) {
456 char pageString[PAGE_SIZE];
457 int fd = openDeviceNode("res_info", O_WRONLY);
458 if (fd >= 0) {
459 snprintf(pageString, sizeof(pageString), "%d", pageNumber);
460 ALOGD_IF(DEBUG, "%s: page=%s", __FUNCTION__, pageString);
461 ssize_t err = write(fd, pageString, sizeof(pageString));
462 if (err <= 0) {
463 ALOGE("%s: Write to res_info failed (%d)", __FUNCTION__, errno);
464 }
465 close(fd);
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700466 }
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800467}
468
469// Reads the contents of res_info node into a buffer if the file is not empty
470bool HDMIDisplay::readResFile(char * configBuffer) {
471 bool fileRead = false;
472 size_t bytesRead = 0;
473 int fd = openDeviceNode("res_info", O_RDONLY);
474 if (fd >= 0 && (bytesRead = read(fd, configBuffer, PAGE_SIZE)) != 0) {
475 fileRead = true;
476 }
477 close(fd);
478 ALOGD_IF(DEBUG, "%s: bytesRead=%d fileRead=%d",
479 __FUNCTION__, bytesRead, fileRead);
480 return fileRead;
481}
482
483// Populates the internal timing info structure with the timing info obtained
484// from the HDMI driver
485void HDMIDisplay::readConfigs() {
486 int configIndex = 0;
487 int pageNumber = MSM_HDMI_INIT_RES_PAGE;
488 long unsigned int size = sizeof(msm_hdmi_mode_timing_info);
489
490 while (true) {
491 char configBuffer[PAGE_SIZE] = {0};
492 msm_hdmi_mode_timing_info *info =
493 (msm_hdmi_mode_timing_info*) configBuffer;
494
495 if (!readResFile(configBuffer))
496 break;
497
498 while (info->video_format && size < PAGE_SIZE) {
499 mDisplayConfigs[configIndex] = *info;
500 size += sizeof(msm_hdmi_mode_timing_info);
501 info++;
502 ALOGD_IF(DEBUG, "%s: Config=%d Mode %d: (%dx%d) @ %d",
503 __FUNCTION__, configIndex,
504 mDisplayConfigs[configIndex].video_format,
505 mDisplayConfigs[configIndex].active_h,
506 mDisplayConfigs[configIndex].active_v,
507 mDisplayConfigs[configIndex].refresh_rate);
508 configIndex++;
509 }
510 size = sizeof(msm_hdmi_mode_timing_info);
511 // Request HDMI driver to populate res_info with more
512 // timing information
513 pageNumber++;
514 requestNewPage(pageNumber);
515 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700516}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700517
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700518inline bool HDMIDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700519{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800520 bool valid = false;
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800521 int modeIndex = getModeIndex(ID);
522 if (ID <= 0 || modeIndex < 0 || modeIndex > mModeCount) {
523 return false;
524 }
525 struct msm_hdmi_mode_timing_info* mode = &mDisplayConfigs[modeIndex];
526 // We dont support interlaced modes
527 if (mode->supported && !mode->interlaced) {
528 valid = true;
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800529 }
530 return valid;
531}
532
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700533// Does a put_vscreen info on the HDMI interface which will update
534// the configuration (resolution, timing info) to match mCurrentMode
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700535void HDMIDisplay::activateDisplay()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700536{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700537 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700538 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
539 if(ret < 0) {
540 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
541 strerror(errno));
542 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700543 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
544 "(%d,%d,%d) %dMHz>", __FUNCTION__,
545 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
546 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
547 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
548 mVInfo.pixclock/1000/1000);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700549
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800550 struct msm_hdmi_mode_timing_info *mode = &mDisplayConfigs[mActiveConfig];
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700551 setDisplayTiming(mVInfo, mode);
552 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
553 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, mCurrentMode,
554 mode->video_format, mVInfo.xres, mVInfo.yres,
555 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
556 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
557 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500558#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700559 struct msmfb_metadata metadata;
560 memset(&metadata, 0 , sizeof(metadata));
561 metadata.op = metadata_op_vic;
562 metadata.data.video_info_code = mode->video_format;
563 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
564 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
565 __FUNCTION__, strerror(errno));
566 }
Ken Zhang7b03a952013-01-16 13:23:48 -0500567#endif
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700568 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
569 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
570 if(ret < 0) {
571 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
572 __FUNCTION__, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700573 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700574}
575
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700576bool HDMIDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700577{
578 bool ret = true;
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700579 if(mFbNum != -1) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700580 int hdmiHPDFile = openDeviceNode("hpd", O_RDWR);
581 if (hdmiHPDFile >= 0) {
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530582 ssize_t err = -1;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700583 ALOGD_IF(DEBUG, "%s: option = %d",
584 __FUNCTION__, userOption);
Arun Kumar K.R47d6b642013-06-26 16:20:30 -0700585 if(userOption)
586 err = write(hdmiHPDFile, "1", 2);
587 else
588 err = write(hdmiHPDFile, "0" , 2);
589 if (err <= 0) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700590 ALOGE("%s: file write failed 'hpd'", __FUNCTION__);
Arun Kumar K.R47d6b642013-06-26 16:20:30 -0700591 ret = false;
592 }
593 close(hdmiHPDFile);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700594 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700595 }
596 return ret;
597}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700598
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700599
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700600void HDMIDisplay::setAttributes() {
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800601 uint32_t refresh = 0, fps = 0;
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700602 // Always set dpyAttr res to mVInfo res
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800603 getAttrForConfig(mActiveConfig, mXres, mYres, refresh, fps);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700604 mMDPScalingMode = false;
605
606 if(overlay::Overlay::getInstance()->isUIScalingOnExternalSupported()
607 && mMDPDownscaleEnabled) {
608 // if primary resolution is more than the hdmi resolution
609 // configure dpy attr to primary resolution and set MDP
610 // scaling mode
611 // Restrict this upto 1080p resolution max, if target does not
612 // support source split feature.
613 uint32_t primaryArea = mPrimaryWidth * mPrimaryHeight;
614 if(((primaryArea) > (mXres * mYres)) &&
615 (((primaryArea) <= SUPPORTED_DOWNSCALE_AREA) ||
Ramkumar Radhakrishnana5c72872014-08-28 19:04:18 -0700616 qdutils::MDPVersion::getInstance().isSrcSplit())) {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700617 // tmpW and tmpH will hold the primary dimensions before we
618 // update the aspect ratio if necessary.
619 int tmpW = mPrimaryWidth;
620 int tmpH = mPrimaryHeight;
621 // HDMI is always in landscape, so always assign the higher
622 // dimension to hdmi's xres
623 if(mPrimaryHeight > mPrimaryWidth) {
624 tmpW = mPrimaryHeight;
625 tmpH = mPrimaryWidth;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700626 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700627 // The aspect ratios of the external and primary displays
628 // can be different. As a result, directly assigning primary
629 // resolution could lead to an incorrect final image.
630 // We get around this by calculating a new resolution by
631 // keeping aspect ratio intact.
632 hwc_rect r = {0, 0, 0, 0};
633 qdutils::getAspectRatioPosition(tmpW, tmpH, mXres, mYres, r);
634 uint32_t newExtW = r.right - r.left;
635 uint32_t newExtH = r.bottom - r.top;
636 uint32_t alignedExtW;
637 uint32_t alignedExtH;
638 // On 8994 and below targets MDP supports only 4X downscaling,
639 // Restricting selected external resolution to be exactly 4X
640 // greater resolution than actual external resolution
641 uint32_t maxMDPDownScale =
642 qdutils::MDPVersion::getInstance().getMaxMDPDownscale();
643 if((mXres * mYres * maxMDPDownScale) < (newExtW * newExtH)) {
644 float upScaleFactor = (float)maxMDPDownScale / 2.0f;
645 newExtW = (int)((float)mXres * upScaleFactor);
646 newExtH = (int)((float)mYres * upScaleFactor);
647 }
648 // Align it down so that the new aligned resolution does not
649 // exceed the maxMDPDownscale factor
650 alignedExtW = overlay::utils::aligndown(newExtW, 4);
651 alignedExtH = overlay::utils::aligndown(newExtH, 4);
652 mXres = alignedExtW;
653 mYres = alignedExtH;
654 // Set External Display MDP Downscale mode indicator
655 mMDPScalingMode = true;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700656 }
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700657 }
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700658 ALOGD_IF(DEBUG_MDPDOWNSCALE, "Selected external resolution [%d X %d] "
659 "maxMDPDownScale %d mMDPScalingMode %d srcSplitEnabled %d "
660 "MDPDownscale feature %d",
661 mXres, mYres,
662 qdutils::MDPVersion::getInstance().getMaxMDPDownscale(),
663 mMDPScalingMode, qdutils::MDPVersion::getInstance().isSrcSplit(),
664 mMDPDownscaleEnabled);
665 mVsyncPeriod = (int) 1000000000l / fps;
666 ALOGD_IF(DEBUG, "%s xres=%d, yres=%d", __FUNCTION__, mXres, mYres);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700667}
668
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700669/* returns the fd related to the node specified*/
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700670int HDMIDisplay::openDeviceNode(const char* node, int fileMode) const {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700671 char sysFsFilePath[MAX_SYSFS_FILE_PATH];
672 memset(sysFsFilePath, 0, sizeof(sysFsFilePath));
673 snprintf(sysFsFilePath , sizeof(sysFsFilePath),
674 "/sys/devices/virtual/graphics/fb%d/%s",
675 mFbNum, node);
676
677 int fd = open(sysFsFilePath, fileMode, 0);
678
679 if (fd < 0) {
680 ALOGE("%s: file '%s' not found : ret = %d err str: %s",
681 __FUNCTION__, sysFsFilePath, fd, strerror(errno));
682 }
683 return fd;
684}
685
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700686bool HDMIDisplay::isHDMIPrimaryDisplay() {
Naseer Ahmed6bbd0a12015-01-23 11:57:10 -0500687 return (mFbNum == HWC_DISPLAY_PRIMARY);
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700688}
689
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700690int HDMIDisplay::getConnectedState() {
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700691 int ret = -1;
692 int mFbNum = qdutils::getHDMINode();
693 int connectedNode = openDeviceNode("connected", O_RDONLY);
694 if(connectedNode >= 0) {
695 char opStr[4];
696 ssize_t bytesRead = read(connectedNode, opStr, sizeof(opStr) - 1);
697 if(bytesRead > 0) {
698 opStr[bytesRead] = '\0';
699 ret = atoi(opStr);
700 ALOGD_IF(DEBUG, "%s: Read %d from connected", __FUNCTION__, ret);
701 } else if(bytesRead == 0) {
702 ALOGE("%s: HDMI connected node empty", __FUNCTION__);
703 } else {
704 ALOGE("%s: Read from HDMI connected node failed with error %s",
705 __FUNCTION__, strerror(errno));
706 }
707 close(connectedNode);
708 } else {
709 ALOGD("%s: /sys/class/graphics/fb%d/connected could not be opened : %s",
710 __FUNCTION__, mFbNum, strerror(errno));
711 }
712 return ret;
713}
714
Tatenda Chipeperekwaaf2c0042014-09-17 12:55:01 -0700715void HDMIDisplay::setPrimaryAttributes(uint32_t primaryWidth,
Tatenda Chipeperekwa03d5df22014-09-16 18:09:18 -0700716 uint32_t primaryHeight) {
717 mPrimaryHeight = primaryHeight;
718 mPrimaryWidth = primaryWidth;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700719}
720
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700721int HDMIDisplay::setActiveConfig(int newConfig) {
722 if(newConfig < 0 || newConfig > mModeCount) {
723 ALOGE("%s Invalid configuration %d", __FUNCTION__, newConfig);
724 return -EINVAL;
725 }
726
727 // XXX: Currently, we only support a change in frame rate.
728 // We need to validate the new config before proceeding.
729 if (!isValidConfigChange(newConfig)) {
730 ALOGE("%s Invalid configuration %d", __FUNCTION__, newConfig);
731 return -EINVAL;
732 }
733
734 mCurrentMode = mEDIDModes[newConfig];
735 mActiveConfig = newConfig;
736 activateDisplay();
737 ALOGD("%s config(%d) mode(%d)", __FUNCTION__, mActiveConfig, mCurrentMode);
738 return 0;
739}
740
Naseer Ahmed6bbd0a12015-01-23 11:57:10 -0500741static const char* getS3DStringFromMode(int s3dMode) {
742 const char* ret ;
743 switch(s3dMode) {
744 case HDMI_S3D_NONE:
745 ret = "None";
746 break;
747 case HDMI_S3D_SIDE_BY_SIDE:
748 ret = "SSH";
749 break;
750 case HDMI_S3D_TOP_AND_BOTTOM:
751 ret = "TAB";
752 break;
753 //FP (FramePacked) mode is not supported in the HAL
754 default:
755 ALOGD("%s: Unsupported s3d mode: %d", __FUNCTION__, s3dMode);
756 ret = NULL;
757 }
758 return ret;
759}
760
761bool HDMIDisplay::isS3DModeSupported(int s3dMode) {
762 if(s3dMode == HDMI_S3D_NONE)
763 return true;
764
765 char s3dEdidStr[PAGE_SIZE] = {'\0'};
766
767 const char *s3dModeString = getS3DStringFromMode(s3dMode);
768
769 if(s3dModeString == NULL)
770 return false;
771
772 int s3dEdidNode = openDeviceNode("edid_3d_modes", O_RDONLY);
773 if(s3dEdidNode >= 0) {
774 ssize_t len = read(s3dEdidNode, s3dEdidStr, sizeof(s3dEdidStr)-1);
775 if (len > 0) {
776 ALOGI("%s: s3dEdidStr: %s mCurrentMode:%d", __FUNCTION__,
777 s3dEdidStr, mCurrentMode);
778 //Three level inception!
779 //The string looks like 16=SSH,4=FP:TAB:SSH,5=FP:SSH,32=FP:TAB:SSH
780 char *saveptr_l1, *saveptr_l2, *saveptr_l3;
781 char *l1, *l2, *l3;
782 int mode = 0;
783 l1 = strtok_r(s3dEdidStr,",", &saveptr_l1);
784 while (l1 != NULL) {
785 l2 = strtok_r(l1, "=", &saveptr_l2);
786 if (l2 != NULL)
787 mode = atoi(l2);
788 while (l2 != NULL) {
789 if (mode != mCurrentMode) {
790 break;
791 }
792 l3 = strtok_r(l2, ":", &saveptr_l3);
793 while (l3 != NULL) {
794 if (strncmp(l3, s3dModeString,
795 strlen(s3dModeString)) == 0) {
796 close(s3dEdidNode);
797 return true;
798 }
799 l3 = strtok_r(NULL, ":", &saveptr_l3);
800 }
801 l2 = strtok_r(NULL, "=", &saveptr_l2);
802 }
803 l1 = strtok_r(NULL, ",", &saveptr_l1);
804 }
805
806 }
807 } else {
808 ALOGI("%s: /sys/class/graphics/fb%d/edid_3d_modes could not be opened : %s",
809 __FUNCTION__, mFbNum, strerror(errno));
810 }
811 close(s3dEdidNode);
812 return false;
813}
814
815bool HDMIDisplay::writeS3DMode(int s3dMode) {
816 bool ret = true;
817 if(mFbNum != -1) {
818 int hdmiS3DModeFile = openDeviceNode("s3d_mode", O_RDWR);
819 if(hdmiS3DModeFile >=0 ) {
820 char curModeStr[PROPERTY_VALUE_MAX];
821 int currentS3DMode = -1;
822 size_t len = read(hdmiS3DModeFile, curModeStr, sizeof(curModeStr) - 1);
823 if(len > 0) {
824 currentS3DMode = atoi(curModeStr);
825 } else {
826 ret = false;
827 ALOGE("%s: Failed to read s3d_mode", __FUNCTION__);
828 }
829
830 if (currentS3DMode >=0 && currentS3DMode != s3dMode) {
831 ssize_t err = -1;
832 ALOGD_IF(DEBUG, "%s: mode = %d",
833 __FUNCTION__, s3dMode);
834 char mode[PROPERTY_VALUE_MAX];
835 snprintf(mode,sizeof(mode),"%d",s3dMode);
836 err = write(hdmiS3DModeFile, mode, sizeof(mode));
837 if (err <= 0) {
838 ALOGE("%s: file write failed 's3d_mode'", __FUNCTION__);
839 ret = false;
840 }
841 }
842 close(hdmiS3DModeFile);
843 }
844 }
845 return ret;
846}
847
848bool HDMIDisplay::configure3D(int s3dMode) {
849 if(isS3DModeSupported(s3dMode)) {
850 if(!writeS3DMode(s3dMode))
851 return false;
852 } else {
853 ALOGE("%s: 3D mode: %d is not supported", __FUNCTION__, s3dMode);
854 return false;
855 }
856 return true;
857}
858
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700859// returns false if the xres or yres of the new config do
860// not match the current config
861bool HDMIDisplay::isValidConfigChange(int newConfig) {
862 int newMode = mEDIDModes[newConfig];
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800863 uint32_t width = 0, height = 0, refresh = 0, fps = 0;
864 getAttrForConfig(newConfig, width, height, refresh, fps);
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700865 return ((mXres == width) && (mYres == height)) || mEnableResolutionChange;
866}
867
868int HDMIDisplay::getModeIndex(int mode) {
869 int modeIndex = -1;
870 for(int i = 0; i < mModeCount; i++) {
871 if(mode == mEDIDModes[i]) {
872 modeIndex = i;
873 break;
874 }
875 }
876 return modeIndex;
877}
878
879int HDMIDisplay::getAttrForConfig(int config, uint32_t& xres,
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800880 uint32_t& yres, uint32_t& refresh, uint32_t& fps) const {
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700881 if(config < 0 || config > mModeCount) {
882 ALOGE("%s Invalid configuration %d", __FUNCTION__, config);
883 return -EINVAL;
884 }
Tatenda Chipeperekwa16301192014-11-14 17:39:27 -0800885
886 xres = mDisplayConfigs[config].active_h;
887 yres = mDisplayConfigs[config].active_v;
888 fps = (mDisplayConfigs[config].refresh_rate / 1000);
889
Tatenda Chipeperekwa319eec12014-09-18 15:08:09 -0700890 refresh = (uint32_t) 1000000000l / fps;
891 ALOGD_IF(DEBUG, "%s xres(%d) yres(%d) fps(%d) refresh(%d)", __FUNCTION__,
892 xres, yres, fps, refresh);
893 return 0;
894}
895
896int HDMIDisplay::getDisplayConfigs(uint32_t* configs,
897 size_t* numConfigs) const {
898 if (*numConfigs <= 0) {
899 ALOGE("%s Invalid number of configs (%d)", __FUNCTION__, *numConfigs);
900 return -EINVAL;
901 }
902 *numConfigs = mModeCount;
903 for (int configIndex = 0; configIndex < mModeCount; configIndex++) {
904 configs[configIndex] = (uint32_t)configIndex;
905 }
906 return 0;
907}
908
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700909};