blob: 300a65fe3c6e98c2bd111da5b6bb9abeadd9ce3e [file] [log] [blame]
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R25187572013-02-28 18:47:36 -08003 * Copyright (C) 2012-2013, 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 Ahmed0c8b7b52012-07-20 09:06:13 -070022#include <ctype.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070023#include <fcntl.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070024#include <media/IAudioPolicyService.h>
25#include <media/AudioSystem.h>
26#include <utils/threads.h>
27#include <utils/Errors.h>
28#include <utils/Log.h>
29
30#include <linux/msm_mdp.h>
Arun Kumar K.R25187572013-02-28 18:47:36 -080031#include <video/msm_hdmi_modes.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070032#include <linux/fb.h>
33#include <sys/ioctl.h>
34#include <sys/poll.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070035#include <sys/resource.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070036#include <cutils/properties.h>
37#include "hwc_utils.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070038#include "external.h"
39#include "overlayUtils.h"
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080040#include "overlay.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070041
42using namespace android;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070043
44namespace qhwc {
45
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080046#define MAX_FRAME_BUFFER_NAME_SIZE (80)
47#define MAX_DISPLAY_DEVICES (3)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070048
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070049
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080050const char* msmFbDevicePath[] = { "/dev/graphics/fb1",
51 "/dev/graphics/fb2"};
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070052
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080053/*
54 * Updates extDeviceFbIndex Array with the correct frame buffer indices
55 * of avaiable external devices
56 *
57 */
58void ExternalDisplay::updateExtDispDevFbIndex()
59{
60 FILE *displayDeviceFP = NULL;
61 char fbType[MAX_FRAME_BUFFER_NAME_SIZE];
62 char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE];
63
64 for(int j = 1; j < MAX_DISPLAY_DEVICES; j++) {
65 sprintf (msmFbTypePath,"/sys/class/graphics/fb%d/msm_fb_type", j);
66 displayDeviceFP = fopen(msmFbTypePath, "r");
67 if(displayDeviceFP){
68 fread(fbType, sizeof(char), MAX_FRAME_BUFFER_NAME_SIZE,
69 displayDeviceFP);
70 if(strncmp(fbType, "dtv panel", strlen("dtv panel")) == 0){
71 ALOGD_IF(DEBUG,"hdmi framebuffer index is %d",j);
72 mHdmiFbNum = j;
73 } else if(strncmp(fbType, "writeback panel",
74 strlen("writeback panel")) == 0){
75 ALOGD_IF(DEBUG,"wfd framebuffer index is %d",j);
76 mWfdFbNum = j;
77 }
78 fclose(displayDeviceFP);
79 }
80 }
81 ALOGD_IF(DEBUG,"%s: mHdmiFbNum: %d mWfdFbNum: %d ",__FUNCTION__,
82 mHdmiFbNum, mWfdFbNum);
83}
84
85int ExternalDisplay::configureHDMIDisplay() {
86 openFrameBuffer(mHdmiFbNum);
87 if(mFd == -1)
88 return -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -080089 readCEUnderscanInfo();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080090 readResolution();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080091 // TODO: Move this to activate
Arun Kumar K.R37552c52012-12-10 12:47:18 -080092 /* Used for changing the resolution
93 * getUserMode will get the preferred
94 * mode set thru adb shell */
95 int mode = getUserMode();
96 if (mode == -1) {
97 //Get the best mode and set
98 mode = getBestMode();
99 }
100 setResolution(mode);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800101 setDpyHdmiAttr();
102 setExternalDisplay(true, mHdmiFbNum);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800103 // set system property
104 property_set("hw.hdmiON", "1");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800105 return 0;
106}
107
108int ExternalDisplay::configureWFDDisplay() {
109 int ret = 0;
110 if(mConnectedFbNum == mHdmiFbNum) {
111 ALOGE("%s: Cannot process WFD connection while HDMI is active",
112 __FUNCTION__);
113 return -1;
114 }
115 openFrameBuffer(mWfdFbNum);
116 if(mFd == -1)
117 return -1;
118 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
119 if(ret < 0) {
120 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
121 strerror(errno));
122 }
123 setDpyWfdAttr();
124 setExternalDisplay(true, mWfdFbNum);
125 return 0;
126}
127
128int ExternalDisplay::teardownHDMIDisplay() {
129 if(mConnectedFbNum == mHdmiFbNum) {
130 // hdmi offline event..!
131 closeFrameBuffer();
132 resetInfo();
133 setExternalDisplay(false);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800134 // unset system property
135 property_set("hw.hdmiON", "0");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800136 }
137 return 0;
138}
139
140int ExternalDisplay::teardownWFDDisplay() {
141 if(mConnectedFbNum == mWfdFbNum) {
142 // wfd offline event..!
143 closeFrameBuffer();
144 memset(&mVInfo, 0, sizeof(mVInfo));
145 setExternalDisplay(false);
146 }
147 return 0;
148}
149
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800150int ExternalDisplay::ignoreRequest(const char *str) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800151 const char *s1 = str + strlen("change@/devices/virtual/switch/");
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800152 if(!strncmp(s1,"wfd",strlen(s1))) {
153 if(mConnectedFbNum == mHdmiFbNum) {
154 ALOGE("Ignore wfd event when HDMI is active");
155 return true;
156 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800157 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800158 return false;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800159}
160
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700161
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700162ExternalDisplay::ExternalDisplay(hwc_context_t* ctx):mFd(-1),
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800163 mCurrentMode(-1), mConnected(0), mConnectedFbNum(0), mModeCount(0),
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800164 mUnderscanSupported(false), mHwcContext(ctx), mHdmiFbNum(-1),
165 mWfdFbNum(-1), mExtDpyNum(HWC_DISPLAY_EXTERNAL)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700166{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700167 memset(&mVInfo, 0, sizeof(mVInfo));
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800168 //Determine the fb index for external display devices.
169 updateExtDispDevFbIndex();
Arun Kumar K.Re746ac52013-03-20 15:56:15 -0700170 // disable HPD at start, it will be enabled later
171 // when the display powers on
172 // This helps for framework reboot or adb shell stop/start
173 writeHPDOption(0);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800174
Arun Kumar K.R25187572013-02-28 18:47:36 -0800175 // for HDMI - retreive all the modes supported by the driver
176 if(mHdmiFbNum != -1) {
177 supported_video_mode_lut =
178 new msm_hdmi_mode_timing_info[HDMI_VFRMT_MAX];
179 // Populate the mode table for supported modes
180 MSM_HDMI_MODES_INIT_TIMINGS(supported_video_mode_lut);
181 MSM_HDMI_MODES_SET_SUPP_TIMINGS(supported_video_mode_lut,
182 MSM_HDMI_MODES_ALL);
183 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700184}
185
Saurabh Shah56f610d2012-08-07 15:27:06 -0700186void ExternalDisplay::setEDIDMode(int resMode) {
187 ALOGD_IF(DEBUG,"resMode=%d ", resMode);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700188 {
189 Mutex::Autolock lock(mExtDispLock);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800190 setExternalDisplay(false);
191 openFrameBuffer(mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700192 setResolution(resMode);
193 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800194 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700195}
196
197void ExternalDisplay::setHPD(uint32_t startEnd) {
198 ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
199 writeHPDOption(startEnd);
200}
201
202void ExternalDisplay::setActionSafeDimension(int w, int h) {
203 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
204 Mutex::Autolock lock(mExtDispLock);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800205 char actionsafeWidth[PROPERTY_VALUE_MAX];
206 char actionsafeHeight[PROPERTY_VALUE_MAX];
207 sprintf(actionsafeWidth, "%d", w);
208 property_set("hw.actionsafe.width", actionsafeWidth);
209 sprintf(actionsafeHeight, "%d", h);
210 property_set("hw.actionsafe.height", actionsafeHeight);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800211 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700212}
213
214int ExternalDisplay::getModeCount() const {
215 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
216 Mutex::Autolock lock(mExtDispLock);
217 return mModeCount;
218}
219
220void ExternalDisplay::getEDIDModes(int *out) const {
221 Mutex::Autolock lock(mExtDispLock);
222 for(int i = 0;i < mModeCount;i++) {
223 out[i] = mEDIDModes[i];
224 }
225}
226
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800227void ExternalDisplay::readCEUnderscanInfo()
228{
229 int hdmiScanInfoFile = -1;
230 int len = -1;
231 char scanInfo[17];
232 char *ce_info_str = NULL;
233 const char token[] = ", \n";
234 int ce_info = -1;
235 char sysFsScanInfoFilePath[128];
236 sprintf(sysFsScanInfoFilePath, "/sys/devices/virtual/graphics/fb%d/"
237 "scan_info", mHdmiFbNum);
238
239 memset(scanInfo, 0, sizeof(scanInfo));
240 hdmiScanInfoFile = open(sysFsScanInfoFilePath, O_RDONLY, 0);
241 if (hdmiScanInfoFile < 0) {
242 ALOGD_IF(DEBUG, "%s: scan_info file '%s' not found",
243 __FUNCTION__, sysFsScanInfoFilePath);
244 return;
245 } else {
246 len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
247 ALOGD("%s: Scan Info string: %s length = %d",
248 __FUNCTION__, scanInfo, len);
249 if (len <= 0) {
250 close(hdmiScanInfoFile);
251 ALOGE("%s: Scan Info file empty '%s'",
252 __FUNCTION__, sysFsScanInfoFilePath);
253 return;
254 }
255 scanInfo[len] = '\0'; /* null terminate the string */
256 }
257 close(hdmiScanInfoFile);
258
259 /*
260 * The scan_info contains the three fields
261 * PT - preferred video format
262 * IT - video format
263 * CE video format - containing the underscan support information
264 */
265
266 /* PT */
267 ce_info_str = strtok(scanInfo, token);
268 if (ce_info_str) {
269 /* IT */
270 ce_info_str = strtok(NULL, token);
271 if (ce_info_str) {
272 /* CE */
273 ce_info_str = strtok(NULL, token);
274 if (ce_info_str)
275 ce_info = atoi(ce_info_str);
276 }
277 }
278
279 if (ce_info_str) {
280 // ce_info contains the underscan information
281 if (ce_info == EXT_SCAN_ALWAYS_UNDERSCANED ||
282 ce_info == EXT_SCAN_BOTH_SUPPORTED)
283 // if TV supported underscan, then driver will always underscan
284 // hence no need to apply action safe rectangle
285 mUnderscanSupported = true;
286 } else {
287 ALOGE("%s: scan_info string error", __FUNCTION__);
288 }
289
290 // Store underscan support info in a system property
291 const char* prop = (mUnderscanSupported) ? "1" : "0";
292 property_set("hw.underscan_supported", prop);
293 return;
294}
295
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700296ExternalDisplay::~ExternalDisplay()
297{
Arun Kumar K.R25187572013-02-28 18:47:36 -0800298 delete [] supported_video_mode_lut;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700299 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700300}
301
Arun Kumar K.R25187572013-02-28 18:47:36 -0800302/*
303 * sets the fb_var_screeninfo from the hdmi_mode_timing_info
304 */
305void setDisplayTiming(struct fb_var_screeninfo &info,
306 const msm_hdmi_mode_timing_info* mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700307{
308 info.reserved[0] = 0;
309 info.reserved[1] = 0;
310 info.reserved[2] = 0;
Ken Zhang7b03a952013-01-16 13:23:48 -0500311#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Arun Kumar K.R25187572013-02-28 18:47:36 -0800312 info.reserved[3] = (info.reserved[3] & 0xFFFF) |
313 (mode->video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500314#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700315 info.xoffset = 0;
316 info.yoffset = 0;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800317 info.xres = mode->active_h;
318 info.yres = mode->active_v;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700319
Arun Kumar K.R25187572013-02-28 18:47:36 -0800320 info.pixclock = (mode->pixel_freq)*1000;
321 info.vmode = mode->interlaced ?
322 FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700323
Arun Kumar K.R25187572013-02-28 18:47:36 -0800324 info.right_margin = mode->front_porch_h;
325 info.hsync_len = mode->pulse_width_h;
326 info.left_margin = mode->back_porch_h;
327 info.lower_margin = mode->front_porch_v;
328 info.vsync_len = mode->pulse_width_v;
329 info.upper_margin = mode->back_porch_v;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700330}
331
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700332int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700333{
334 char delim = ',';
335 int count = 0;
336 char *start, *end;
337 // EDIDs are string delimited by ','
338 // Ex: 16,4,5,3,32,34,1
339 // Parse this string to get mode(int)
340 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700341 end = &delim;
342 while(*end == delim) {
343 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700344 start = end+1;
345 count++;
346 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700347 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
348 for (int i = 0; i < count; i++)
349 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700350 return count;
351}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700352
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700353bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700354{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800355 char sysFsEDIDFilePath[255];
356 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
357 mHdmiFbNum);
358
359 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700360 int len = -1;
361
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700362 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700363 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800364 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700365 return false;
366 } else {
367 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700368 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
369 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700370 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700371 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800372 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700373 }
374 else {
375 while (len > 1 && isspace(mEDIDs[len-1]))
376 --len;
377 mEDIDs[len] = 0;
378 }
379 }
380 close(hdmiEDIDFile);
381 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800382 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700383 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700384 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
385 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700386 }
387
388 return (strlen(mEDIDs) > 0);
389}
390
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800391bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700392{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700393 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800394 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700395 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800396 ALOGE("%s: %s is not available", __FUNCTION__,
397 msmFbDevicePath[fbNum-1]);
398 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800399 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800400 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700401 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700402 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700403}
404
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700405bool ExternalDisplay::closeFrameBuffer()
406{
407 int ret = 0;
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500408 if(mFd >= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700409 ret = close(mFd);
410 mFd = -1;
411 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700412 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800413 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700414 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700415 return (ret == 0);
416}
417
418// clears the vinfo, edid, best modes
419void ExternalDisplay::resetInfo()
420{
421 memset(&mVInfo, 0, sizeof(mVInfo));
422 memset(mEDIDs, 0, sizeof(mEDIDs));
423 memset(mEDIDModes, 0, sizeof(mEDIDModes));
424 mModeCount = 0;
425 mCurrentMode = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800426 mUnderscanSupported = false;
427 // Reset the underscan supported system property
428 const char* prop = "0";
429 property_set("hw.underscan_supported", prop);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700430}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700431
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700432int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700433{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800434 // XXX: We dont support interlaced modes but having
Arun Kumar K.R25187572013-02-28 18:47:36 -0800435 // it here for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700436 switch (mode) {
437 default:
Arun Kumar K.R25187572013-02-28 18:47:36 -0800438 case HDMI_VFRMT_1440x480i60_4_3:
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700439 return 1; // 480i 4:3
Arun Kumar K.R25187572013-02-28 18:47:36 -0800440 case HDMI_VFRMT_1440x480i60_16_9:
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700441 return 2; // 480i 16:9
Arun Kumar K.R25187572013-02-28 18:47:36 -0800442 case HDMI_VFRMT_1440x576i50_4_3:
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700443 return 3; // i576i 4:3
Arun Kumar K.R25187572013-02-28 18:47:36 -0800444 case HDMI_VFRMT_1440x576i50_16_9:
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700445 return 4; // 576i 16:9
Arun Kumar K.R25187572013-02-28 18:47:36 -0800446 case HDMI_VFRMT_1920x1080i60_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800447 return 5; // 1080i 16:9
448 case HDMI_VFRMT_640x480p60_4_3:
449 return 6; // 640x480 4:3
450 case HDMI_VFRMT_720x480p60_4_3:
451 return 7; // 480p 4:3
452 case HDMI_VFRMT_720x480p60_16_9:
453 return 8; // 480p 16:9
454 case HDMI_VFRMT_720x576p50_4_3:
455 return 9; // 576p 4:3
456 case HDMI_VFRMT_720x576p50_16_9:
457 return 10; // 576p 16:9
458 case HDMI_VFRMT_1280x1024p60_5_4:
459 return 11; // 1024p; Vesa format
Arun Kumar K.R25187572013-02-28 18:47:36 -0800460 case HDMI_VFRMT_1280x720p50_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800461 return 12; // 720p@50Hz
Arun Kumar K.R25187572013-02-28 18:47:36 -0800462 case HDMI_VFRMT_1280x720p60_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800463 return 13; // 720p@60Hz
Arun Kumar K.R25187572013-02-28 18:47:36 -0800464 case HDMI_VFRMT_1920x1080p24_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800465 return 14; //1080p@24Hz
Arun Kumar K.R25187572013-02-28 18:47:36 -0800466 case HDMI_VFRMT_1920x1080p25_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800467 return 15; //108-p@25Hz
Arun Kumar K.R25187572013-02-28 18:47:36 -0800468 case HDMI_VFRMT_1920x1080p30_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800469 return 16; //1080p@30Hz
Arun Kumar K.R25187572013-02-28 18:47:36 -0800470 case HDMI_VFRMT_1920x1080p50_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800471 return 17; //1080p@50Hz
Arun Kumar K.R25187572013-02-28 18:47:36 -0800472 case HDMI_VFRMT_1920x1080p60_16_9:
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800473 return 18; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700474 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700475}
476
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800477/// Returns the user mode set(if any) using adb shell
478int ExternalDisplay::getUserMode() {
479 /* Based on the property set the resolution */
480 char property_value[PROPERTY_VALUE_MAX];
Arun Kumar K.Rc31bdcb2013-02-25 17:47:42 -0800481 property_get("hw.hdmi.resolution", property_value, "-1");
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800482 int mode = atoi(property_value);
483 // We dont support interlaced modes
484 if(isValidMode(mode) && !isInterlacedMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500485 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800486 return mode;
487 }
488 return -1;
489}
490
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700491// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700492int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700493 int bestOrder = 0;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800494 int bestMode = HDMI_VFRMT_640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700495 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700496 // for all the edid read, get the best mode
497 for(int i = 0; i < mModeCount; i++) {
498 int mode = mEDIDModes[i];
499 int order = getModeOrder(mode);
500 if (order > bestOrder) {
501 bestOrder = order;
502 bestMode = mode;
503 }
504 }
505 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700506}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700507
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700508inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700509{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800510 bool valid = false;
511 for (int i = 0; i < mModeCount; i++) {
512 if(ID == mEDIDModes[i]) {
513 valid = true;
514 break;
515 }
516 }
517 return valid;
518}
519
520// returns true if the mode(ID) is interlaced mode format
521bool ExternalDisplay::isInterlacedMode(int ID) {
522 bool interlaced = false;
523 switch(ID) {
Arun Kumar K.R25187572013-02-28 18:47:36 -0800524 case HDMI_VFRMT_1440x480i60_4_3:
525 case HDMI_VFRMT_1440x480i60_16_9:
526 case HDMI_VFRMT_1440x576i50_4_3:
527 case HDMI_VFRMT_1440x576i50_16_9:
528 case HDMI_VFRMT_1920x1080i60_16_9:
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800529 interlaced = true;
530 default:
531 interlaced = false;
532 }
533 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700534}
535
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700536void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700537{
538 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700539 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700540 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
541 if(ret < 0) {
542 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
543 strerror(errno));
544 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700545 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
546 "(%d,%d,%d) %dMHz>", __FUNCTION__,
547 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
548 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
549 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
550 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800551 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700552 if ((isValidMode(ID)) && mCurrentMode != ID) {
Arun Kumar K.R25187572013-02-28 18:47:36 -0800553 const struct msm_hdmi_mode_timing_info *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700554 &supported_video_mode_lut[0];
Arun Kumar K.R25187572013-02-28 18:47:36 -0800555 for (unsigned int i = 0; i < HDMI_VFRMT_MAX; ++i) {
556 const struct msm_hdmi_mode_timing_info *cur =
557 &supported_video_mode_lut[i];
558 if (cur->video_format == (uint32_t)ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700559 mode = cur;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800560 break;
561 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700562 }
Arun Kumar K.R25187572013-02-28 18:47:36 -0800563 setDisplayTiming(mVInfo, mode);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700564 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700565 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800566 mode->video_format, mVInfo.xres, mVInfo.yres,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700567 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
568 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
569 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500570#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
571 struct msmfb_metadata metadata;
572 memset(&metadata, 0 , sizeof(metadata));
573 metadata.op = metadata_op_vic;
574 metadata.data.video_info_code = mode->video_format;
575 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
576 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
577 __FUNCTION__, strerror(errno));
578 }
579#endif
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800580 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
581 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
582 if(ret < 0) {
583 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
584 __FUNCTION__, strerror(errno));
585 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700586 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700587 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700588}
589
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800590void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700591{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700592 hwc_context_t* ctx = mHwcContext;
593 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800594 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700595 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800596 mConnected = connected;
597 mConnectedFbNum = extFbNum;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800598 mHwcContext->dpyAttr[mExtDpyNum].connected = connected;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800599 // Update external fb number in Overlay context
600 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700601 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800602}
603
604int ExternalDisplay::getExtFbNum(int &fbNum) {
605 int ret = -1;
606 if(mConnected) {
607 fbNum = mConnectedFbNum;
608 ret = 0;
609 }
610 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700611}
612
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700613bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700614{
615 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800616 char sysFsHPDFilePath[255];
617 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
618 mHdmiFbNum);
619 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700620 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800621 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
622 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700623 ret = false;
624 } else {
625 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800626 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700627 if(userOption)
628 err = write(hdmiHPDFile, "1", 2);
629 else
630 err = write(hdmiHPDFile, "0" , 2);
631 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800632 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700633 ret = false;
634 }
635 close(hdmiHPDFile);
636 }
637 return ret;
638}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700639
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800640void ExternalDisplay::setDpyWfdAttr() {
641 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800642 mHwcContext->dpyAttr[mExtDpyNum].xres = mVInfo.xres;
643 mHwcContext->dpyAttr[mExtDpyNum].yres = mVInfo.yres;
644 mHwcContext->dpyAttr[mExtDpyNum].vsync_period =
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800645 1000000000l /60;
646 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
647 }
648}
649
650void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700651 int width = 0, height = 0, fps = 0;
652 getAttrForMode(width, height, fps);
653 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700654 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700655 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
656 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700657 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
658 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700659 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700660}
661
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800662void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700663 switch (mCurrentMode) {
Arun Kumar K.R25187572013-02-28 18:47:36 -0800664 case HDMI_VFRMT_640x480p60_4_3:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700665 width = 640;
666 height = 480;
667 fps = 60;
668 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800669 case HDMI_VFRMT_720x480p60_4_3:
670 case HDMI_VFRMT_720x480p60_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700671 width = 720;
672 height = 480;
673 fps = 60;
674 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800675 case HDMI_VFRMT_720x576p50_4_3:
676 case HDMI_VFRMT_720x576p50_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700677 width = 720;
678 height = 576;
679 fps = 50;
680 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800681 case HDMI_VFRMT_1280x720p50_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700682 width = 1280;
683 height = 720;
684 fps = 50;
685 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800686 case HDMI_VFRMT_1280x720p60_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700687 width = 1280;
688 height = 720;
689 fps = 60;
690 break;
Arun Kumar K.R6ce73ff2013-01-24 19:48:24 -0800691 case HDMI_VFRMT_1280x1024p60_5_4:
692 width = 1280;
693 height = 1024;
694 fps = 60;
695 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800696 case HDMI_VFRMT_1920x1080p24_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700697 width = 1920;
698 height = 1080;
699 fps = 24;
700 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800701 case HDMI_VFRMT_1920x1080p25_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700702 width = 1920;
703 height = 1080;
704 fps = 25;
705 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800706 case HDMI_VFRMT_1920x1080p30_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700707 width = 1920;
708 height = 1080;
709 fps = 30;
710 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800711 case HDMI_VFRMT_1920x1080p50_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700712 width = 1920;
713 height = 1080;
714 fps = 50;
715 break;
Arun Kumar K.R25187572013-02-28 18:47:36 -0800716 case HDMI_VFRMT_1920x1080p60_16_9:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700717 width = 1920;
718 height = 1080;
719 fps = 60;
720 break;
721 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700722}
723
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700724};