blob: cf84a39644d3a4478b59e5909f992c3b2dce1363 [file] [log] [blame]
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Saurabh Shah56f610d2012-08-07 15:27:06 -07003 * Copyright (C) 2012, 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>
31#include <linux/fb.h>
32#include <sys/ioctl.h>
33#include <sys/poll.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070034#include <sys/resource.h>
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070035#include <cutils/properties.h>
36#include "hwc_utils.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070037#include "external.h"
38#include "overlayUtils.h"
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080039#include "overlay.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070040
41using namespace android;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070042
43namespace qhwc {
44
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080045#define MAX_FRAME_BUFFER_NAME_SIZE (80)
46#define MAX_DISPLAY_DEVICES (3)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070047
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070048
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080049const char* msmFbDevicePath[] = { "/dev/graphics/fb1",
50 "/dev/graphics/fb2"};
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070051
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080052/*
53 * Updates extDeviceFbIndex Array with the correct frame buffer indices
54 * of avaiable external devices
55 *
56 */
57void ExternalDisplay::updateExtDispDevFbIndex()
58{
59 FILE *displayDeviceFP = NULL;
60 char fbType[MAX_FRAME_BUFFER_NAME_SIZE];
61 char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE];
62
63 for(int j = 1; j < MAX_DISPLAY_DEVICES; j++) {
64 sprintf (msmFbTypePath,"/sys/class/graphics/fb%d/msm_fb_type", j);
65 displayDeviceFP = fopen(msmFbTypePath, "r");
66 if(displayDeviceFP){
67 fread(fbType, sizeof(char), MAX_FRAME_BUFFER_NAME_SIZE,
68 displayDeviceFP);
69 if(strncmp(fbType, "dtv panel", strlen("dtv panel")) == 0){
70 ALOGD_IF(DEBUG,"hdmi framebuffer index is %d",j);
71 mHdmiFbNum = j;
72 } else if(strncmp(fbType, "writeback panel",
73 strlen("writeback panel")) == 0){
74 ALOGD_IF(DEBUG,"wfd framebuffer index is %d",j);
75 mWfdFbNum = j;
76 }
77 fclose(displayDeviceFP);
78 }
79 }
80 ALOGD_IF(DEBUG,"%s: mHdmiFbNum: %d mWfdFbNum: %d ",__FUNCTION__,
81 mHdmiFbNum, mWfdFbNum);
82}
83
84int ExternalDisplay::configureHDMIDisplay() {
85 openFrameBuffer(mHdmiFbNum);
86 if(mFd == -1)
87 return -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -080088 readCEUnderscanInfo();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080089 readResolution();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080090 // TODO: Move this to activate
Arun Kumar K.R37552c52012-12-10 12:47:18 -080091 /* Used for changing the resolution
92 * getUserMode will get the preferred
93 * mode set thru adb shell */
94 int mode = getUserMode();
95 if (mode == -1) {
96 //Get the best mode and set
97 mode = getBestMode();
98 }
99 setResolution(mode);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800100 setDpyHdmiAttr();
101 setExternalDisplay(true, mHdmiFbNum);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800102 // set system property
103 property_set("hw.hdmiON", "1");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800104 return 0;
105}
106
107int ExternalDisplay::configureWFDDisplay() {
108 int ret = 0;
109 if(mConnectedFbNum == mHdmiFbNum) {
110 ALOGE("%s: Cannot process WFD connection while HDMI is active",
111 __FUNCTION__);
112 return -1;
113 }
114 openFrameBuffer(mWfdFbNum);
115 if(mFd == -1)
116 return -1;
117 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
118 if(ret < 0) {
119 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
120 strerror(errno));
121 }
122 setDpyWfdAttr();
123 setExternalDisplay(true, mWfdFbNum);
124 return 0;
125}
126
127int ExternalDisplay::teardownHDMIDisplay() {
128 if(mConnectedFbNum == mHdmiFbNum) {
129 // hdmi offline event..!
130 closeFrameBuffer();
131 resetInfo();
132 setExternalDisplay(false);
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800133 // unset system property
134 property_set("hw.hdmiON", "0");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800135 }
136 return 0;
137}
138
139int ExternalDisplay::teardownWFDDisplay() {
140 if(mConnectedFbNum == mWfdFbNum) {
141 // wfd offline event..!
142 closeFrameBuffer();
143 memset(&mVInfo, 0, sizeof(mVInfo));
144 setExternalDisplay(false);
145 }
146 return 0;
147}
148
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800149int ExternalDisplay::ignoreRequest(const char *str) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800150 const char *s1 = str + strlen("change@/devices/virtual/switch/");
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800151 if(!strncmp(s1,"wfd",strlen(s1))) {
152 if(mConnectedFbNum == mHdmiFbNum) {
153 ALOGE("Ignore wfd event when HDMI is active");
154 return true;
155 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800156 }
Amara Venkata Mastan Manoj Kumarb156a2f2013-02-07 16:42:50 -0800157 return false;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800158}
159
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700160
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700161ExternalDisplay::ExternalDisplay(hwc_context_t* ctx):mFd(-1),
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800162 mCurrentMode(-1), mConnected(0), mConnectedFbNum(0), mModeCount(0),
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800163 mUnderscanSupported(false), mHwcContext(ctx), mHdmiFbNum(-1),
164 mWfdFbNum(-1), mExtDpyNum(HWC_DISPLAY_EXTERNAL)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700165{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700166 memset(&mVInfo, 0, sizeof(mVInfo));
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800167 //Determine the fb index for external display devices.
168 updateExtDispDevFbIndex();
Arun Kumar K.Re746ac52013-03-20 15:56:15 -0700169 // disable HPD at start, it will be enabled later
170 // when the display powers on
171 // This helps for framework reboot or adb shell stop/start
172 writeHPDOption(0);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800173
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700174}
175
Saurabh Shah56f610d2012-08-07 15:27:06 -0700176void ExternalDisplay::setEDIDMode(int resMode) {
177 ALOGD_IF(DEBUG,"resMode=%d ", resMode);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700178 {
179 Mutex::Autolock lock(mExtDispLock);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800180 setExternalDisplay(false);
181 openFrameBuffer(mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700182 setResolution(resMode);
183 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800184 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700185}
186
187void ExternalDisplay::setHPD(uint32_t startEnd) {
188 ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
189 writeHPDOption(startEnd);
190}
191
192void ExternalDisplay::setActionSafeDimension(int w, int h) {
193 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
194 Mutex::Autolock lock(mExtDispLock);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800195 char actionsafeWidth[PROPERTY_VALUE_MAX];
196 char actionsafeHeight[PROPERTY_VALUE_MAX];
197 sprintf(actionsafeWidth, "%d", w);
198 property_set("hw.actionsafe.width", actionsafeWidth);
199 sprintf(actionsafeHeight, "%d", h);
200 property_set("hw.actionsafe.height", actionsafeHeight);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800201 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700202}
203
204int ExternalDisplay::getModeCount() const {
205 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
206 Mutex::Autolock lock(mExtDispLock);
207 return mModeCount;
208}
209
210void ExternalDisplay::getEDIDModes(int *out) const {
211 Mutex::Autolock lock(mExtDispLock);
212 for(int i = 0;i < mModeCount;i++) {
213 out[i] = mEDIDModes[i];
214 }
215}
216
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800217void ExternalDisplay::readCEUnderscanInfo()
218{
219 int hdmiScanInfoFile = -1;
220 int len = -1;
221 char scanInfo[17];
222 char *ce_info_str = NULL;
223 const char token[] = ", \n";
224 int ce_info = -1;
225 char sysFsScanInfoFilePath[128];
226 sprintf(sysFsScanInfoFilePath, "/sys/devices/virtual/graphics/fb%d/"
227 "scan_info", mHdmiFbNum);
228
229 memset(scanInfo, 0, sizeof(scanInfo));
230 hdmiScanInfoFile = open(sysFsScanInfoFilePath, O_RDONLY, 0);
231 if (hdmiScanInfoFile < 0) {
232 ALOGD_IF(DEBUG, "%s: scan_info file '%s' not found",
233 __FUNCTION__, sysFsScanInfoFilePath);
234 return;
235 } else {
236 len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
237 ALOGD("%s: Scan Info string: %s length = %d",
238 __FUNCTION__, scanInfo, len);
239 if (len <= 0) {
240 close(hdmiScanInfoFile);
241 ALOGE("%s: Scan Info file empty '%s'",
242 __FUNCTION__, sysFsScanInfoFilePath);
243 return;
244 }
245 scanInfo[len] = '\0'; /* null terminate the string */
246 }
247 close(hdmiScanInfoFile);
248
249 /*
250 * The scan_info contains the three fields
251 * PT - preferred video format
252 * IT - video format
253 * CE video format - containing the underscan support information
254 */
255
256 /* PT */
257 ce_info_str = strtok(scanInfo, token);
258 if (ce_info_str) {
259 /* IT */
260 ce_info_str = strtok(NULL, token);
261 if (ce_info_str) {
262 /* CE */
263 ce_info_str = strtok(NULL, token);
264 if (ce_info_str)
265 ce_info = atoi(ce_info_str);
266 }
267 }
268
269 if (ce_info_str) {
270 // ce_info contains the underscan information
271 if (ce_info == EXT_SCAN_ALWAYS_UNDERSCANED ||
272 ce_info == EXT_SCAN_BOTH_SUPPORTED)
273 // if TV supported underscan, then driver will always underscan
274 // hence no need to apply action safe rectangle
275 mUnderscanSupported = true;
276 } else {
277 ALOGE("%s: scan_info string error", __FUNCTION__);
278 }
279
280 // Store underscan support info in a system property
281 const char* prop = (mUnderscanSupported) ? "1" : "0";
282 property_set("hw.underscan_supported", prop);
283 return;
284}
285
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700286ExternalDisplay::~ExternalDisplay()
287{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700288 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700289}
290
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700291struct disp_mode_timing_type {
292 int video_format;
293
294 int active_h;
295 int active_v;
296
297 int front_porch_h;
298 int pulse_width_h;
299 int back_porch_h;
300
301 int front_porch_v;
302 int pulse_width_v;
303 int back_porch_v;
304
305 int pixel_freq;
306 bool interlaced;
307
308 void set_info(struct fb_var_screeninfo &info) const;
309};
310
311void disp_mode_timing_type::set_info(struct fb_var_screeninfo &info) const
312{
313 info.reserved[0] = 0;
314 info.reserved[1] = 0;
315 info.reserved[2] = 0;
Ken Zhang7b03a952013-01-16 13:23:48 -0500316#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Saurabh Shah56f610d2012-08-07 15:27:06 -0700317 info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500318#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700319 info.xoffset = 0;
320 info.yoffset = 0;
321 info.xres = active_h;
322 info.yres = active_v;
323
324 info.pixclock = pixel_freq*1000;
325 info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
326
327 info.right_margin = front_porch_h;
328 info.hsync_len = pulse_width_h;
329 info.left_margin = back_porch_h;
330 info.lower_margin = front_porch_v;
331 info.vsync_len = pulse_width_v;
332 info.upper_margin = back_porch_v;
333}
334
335/* Video formates supported by the HDMI Standard */
336/* Indicates the resolution, pix clock and the aspect ratio */
337#define m640x480p60_4_3 1
338#define m720x480p60_4_3 2
339#define m720x480p60_16_9 3
340#define m1280x720p60_16_9 4
341#define m1920x1080i60_16_9 5
342#define m1440x480i60_4_3 6
343#define m1440x480i60_16_9 7
344#define m1920x1080p60_16_9 16
345#define m720x576p50_4_3 17
346#define m720x576p50_16_9 18
347#define m1280x720p50_16_9 19
348#define m1440x576i50_4_3 21
349#define m1440x576i50_16_9 22
350#define m1920x1080p50_16_9 31
351#define m1920x1080p24_16_9 32
352#define m1920x1080p25_16_9 33
353#define m1920x1080p30_16_9 34
354
355static struct disp_mode_timing_type supported_video_mode_lut[] = {
356 {m640x480p60_4_3, 640, 480, 16, 96, 48, 10, 2, 33, 25200, false},
357 {m720x480p60_4_3, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
358 {m720x480p60_16_9, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
359 {m1280x720p60_16_9, 1280, 720, 110, 40, 220, 5, 5, 20, 74250, false},
360 {m1920x1080i60_16_9, 1920, 540, 88, 44, 148, 2, 5, 5, 74250, false},
361 {m1440x480i60_4_3, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
362 {m1440x480i60_16_9, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
363 {m1920x1080p60_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 148500, false},
364 {m720x576p50_4_3, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
365 {m720x576p50_16_9, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
366 {m1280x720p50_16_9, 1280, 720, 440, 40, 220, 5, 5, 20, 74250, false},
367 {m1440x576i50_4_3, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
368 {m1440x576i50_16_9, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
369 {m1920x1080p50_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 148500, false},
370 {m1920x1080p24_16_9, 1920, 1080, 638, 44, 148, 4, 5, 36, 74250, false},
371 {m1920x1080p25_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 74250, false},
372 {m1920x1080p30_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 74250, false},
373};
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700374
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700375int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700376{
377 char delim = ',';
378 int count = 0;
379 char *start, *end;
380 // EDIDs are string delimited by ','
381 // Ex: 16,4,5,3,32,34,1
382 // Parse this string to get mode(int)
383 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700384 end = &delim;
385 while(*end == delim) {
386 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700387 start = end+1;
388 count++;
389 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700390 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
391 for (int i = 0; i < count; i++)
392 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700393 return count;
394}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700395
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700396bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700397{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800398 char sysFsEDIDFilePath[255];
399 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
400 mHdmiFbNum);
401
402 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700403 int len = -1;
404
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700405 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700406 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800407 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700408 return false;
409 } else {
410 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700411 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
412 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700413 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700414 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800415 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700416 }
417 else {
418 while (len > 1 && isspace(mEDIDs[len-1]))
419 --len;
420 mEDIDs[len] = 0;
421 }
422 }
423 close(hdmiEDIDFile);
424 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800425 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700426 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700427 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
428 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700429 }
430
431 return (strlen(mEDIDs) > 0);
432}
433
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800434bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700435{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700436 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800437 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700438 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800439 ALOGE("%s: %s is not available", __FUNCTION__,
440 msmFbDevicePath[fbNum-1]);
441 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800442 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800443 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700444 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700445 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700446}
447
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700448bool ExternalDisplay::closeFrameBuffer()
449{
450 int ret = 0;
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500451 if(mFd >= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700452 ret = close(mFd);
453 mFd = -1;
454 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700455 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800456 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700457 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700458 return (ret == 0);
459}
460
461// clears the vinfo, edid, best modes
462void ExternalDisplay::resetInfo()
463{
464 memset(&mVInfo, 0, sizeof(mVInfo));
465 memset(mEDIDs, 0, sizeof(mEDIDs));
466 memset(mEDIDModes, 0, sizeof(mEDIDModes));
467 mModeCount = 0;
468 mCurrentMode = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800469 mUnderscanSupported = false;
470 // Reset the underscan supported system property
471 const char* prop = "0";
472 property_set("hw.underscan_supported", prop);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700473}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700474
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700475int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700476{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800477 // XXX: We dont support interlaced modes but having
478 // it here for for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700479 switch (mode) {
480 default:
481 case m1440x480i60_4_3:
482 return 1; // 480i 4:3
483 case m1440x480i60_16_9:
484 return 2; // 480i 16:9
485 case m1440x576i50_4_3:
486 return 3; // i576i 4:3
487 case m1440x576i50_16_9:
488 return 4; // 576i 16:9
489 case m640x480p60_4_3:
490 return 5; // 640x480 4:3
491 case m720x480p60_4_3:
492 return 6; // 480p 4:3
493 case m720x480p60_16_9:
494 return 7; // 480p 16:9
495 case m720x576p50_4_3:
496 return 8; // 576p 4:3
497 case m720x576p50_16_9:
498 return 9; // 576p 16:9
499 case m1920x1080i60_16_9:
500 return 10; // 1080i 16:9
501 case m1280x720p50_16_9:
502 return 11; // 720p@50Hz
503 case m1280x720p60_16_9:
504 return 12; // 720p@60Hz
505 case m1920x1080p24_16_9:
506 return 13; //1080p@24Hz
507 case m1920x1080p25_16_9:
508 return 14; //108-p@25Hz
509 case m1920x1080p30_16_9:
510 return 15; //1080p@30Hz
511 case m1920x1080p50_16_9:
512 return 16; //1080p@50Hz
513 case m1920x1080p60_16_9:
514 return 17; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700515 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700516}
517
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800518/// Returns the user mode set(if any) using adb shell
519int ExternalDisplay::getUserMode() {
520 /* Based on the property set the resolution */
521 char property_value[PROPERTY_VALUE_MAX];
Arun Kumar K.Rc31bdcb2013-02-25 17:47:42 -0800522 property_get("hw.hdmi.resolution", property_value, "-1");
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800523 int mode = atoi(property_value);
524 // We dont support interlaced modes
525 if(isValidMode(mode) && !isInterlacedMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500526 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800527 return mode;
528 }
529 return -1;
530}
531
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700532// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700533int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700534 int bestOrder = 0;
535 int bestMode = m640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700536 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700537 // for all the edid read, get the best mode
538 for(int i = 0; i < mModeCount; i++) {
539 int mode = mEDIDModes[i];
540 int order = getModeOrder(mode);
541 if (order > bestOrder) {
542 bestOrder = order;
543 bestMode = mode;
544 }
545 }
546 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700547}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700548
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700549inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700550{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800551 bool valid = false;
552 for (int i = 0; i < mModeCount; i++) {
553 if(ID == mEDIDModes[i]) {
554 valid = true;
555 break;
556 }
557 }
558 return valid;
559}
560
561// returns true if the mode(ID) is interlaced mode format
562bool ExternalDisplay::isInterlacedMode(int ID) {
563 bool interlaced = false;
564 switch(ID) {
565 case m1440x480i60_4_3:
566 case m1440x480i60_16_9:
567 case m1440x576i50_4_3:
568 case m1440x576i50_16_9:
569 case m1920x1080i60_16_9:
570 interlaced = true;
571 default:
572 interlaced = false;
573 }
574 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700575}
576
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700577void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700578{
579 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700580 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700581 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
582 if(ret < 0) {
583 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
584 strerror(errno));
585 }
586
587 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
588 "(%d,%d,%d) %dMHz>", __FUNCTION__,
589 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
590 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
591 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
592 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800593 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700594 if ((isValidMode(ID)) && mCurrentMode != ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700595 const struct disp_mode_timing_type *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700596 &supported_video_mode_lut[0];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700597 unsigned count = sizeof(supported_video_mode_lut)/sizeof
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700598 (*supported_video_mode_lut);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700599 for (unsigned int i = 0; i < count; ++i) {
600 const struct disp_mode_timing_type *cur =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700601 &supported_video_mode_lut[i];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700602 if (cur->video_format == ID)
603 mode = cur;
604 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700605 mode->set_info(mVInfo);
606 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700607 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800608 mode->video_format, mVInfo.xres, mVInfo.yres,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700609 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
610 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
611 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500612#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
613 struct msmfb_metadata metadata;
614 memset(&metadata, 0 , sizeof(metadata));
615 metadata.op = metadata_op_vic;
616 metadata.data.video_info_code = mode->video_format;
617 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
618 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
619 __FUNCTION__, strerror(errno));
620 }
621#endif
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800622 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
623 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
624 if(ret < 0) {
625 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
626 __FUNCTION__, strerror(errno));
627 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700628 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700629 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700630}
631
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800632void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700633{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700634 hwc_context_t* ctx = mHwcContext;
635 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800636 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700637 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800638 mConnected = connected;
639 mConnectedFbNum = extFbNum;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800640 mHwcContext->dpyAttr[mExtDpyNum].connected = connected;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800641 // Update external fb number in Overlay context
642 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700643 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800644}
645
646int ExternalDisplay::getExtFbNum(int &fbNum) {
647 int ret = -1;
648 if(mConnected) {
649 fbNum = mConnectedFbNum;
650 ret = 0;
651 }
652 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700653}
654
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700655bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700656{
657 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800658 char sysFsHPDFilePath[255];
659 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
660 mHdmiFbNum);
661 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700662 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800663 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
664 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700665 ret = false;
666 } else {
667 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800668 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700669 if(userOption)
670 err = write(hdmiHPDFile, "1", 2);
671 else
672 err = write(hdmiHPDFile, "0" , 2);
673 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800674 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700675 ret = false;
676 }
677 close(hdmiHPDFile);
678 }
679 return ret;
680}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700681
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800682void ExternalDisplay::setDpyWfdAttr() {
683 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800684 mHwcContext->dpyAttr[mExtDpyNum].xres = mVInfo.xres;
685 mHwcContext->dpyAttr[mExtDpyNum].yres = mVInfo.yres;
686 mHwcContext->dpyAttr[mExtDpyNum].vsync_period =
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800687 1000000000l /60;
688 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
689 }
690}
691
692void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700693 int width = 0, height = 0, fps = 0;
694 getAttrForMode(width, height, fps);
695 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700696 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700697 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
698 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700699 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
700 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700701 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700702}
703
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800704void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700705 switch (mCurrentMode) {
706 case m640x480p60_4_3:
707 width = 640;
708 height = 480;
709 fps = 60;
710 break;
711 case m720x480p60_4_3:
712 case m720x480p60_16_9:
713 width = 720;
714 height = 480;
715 fps = 60;
716 break;
717 case m720x576p50_4_3:
718 case m720x576p50_16_9:
719 width = 720;
720 height = 576;
721 fps = 50;
722 break;
723 case m1280x720p50_16_9:
724 width = 1280;
725 height = 720;
726 fps = 50;
727 break;
728 case m1280x720p60_16_9:
729 width = 1280;
730 height = 720;
731 fps = 60;
732 break;
733 case m1920x1080p24_16_9:
734 width = 1920;
735 height = 1080;
736 fps = 24;
737 break;
738 case m1920x1080p25_16_9:
739 width = 1920;
740 height = 1080;
741 fps = 25;
742 break;
743 case m1920x1080p30_16_9:
744 width = 1920;
745 height = 1080;
746 fps = 30;
747 break;
748 case m1920x1080p50_16_9:
749 width = 1920;
750 height = 1080;
751 fps = 50;
752 break;
753 case m1920x1080p60_16_9:
754 width = 1920;
755 height = 1080;
756 fps = 60;
757 break;
758 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700759}
760
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700761};