blob: af28bce7959f120e3a097a59c2400ff97a1eb7f0 [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);
102 return 0;
103}
104
105int ExternalDisplay::configureWFDDisplay() {
106 int ret = 0;
107 if(mConnectedFbNum == mHdmiFbNum) {
108 ALOGE("%s: Cannot process WFD connection while HDMI is active",
109 __FUNCTION__);
110 return -1;
111 }
112 openFrameBuffer(mWfdFbNum);
113 if(mFd == -1)
114 return -1;
115 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
116 if(ret < 0) {
117 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
118 strerror(errno));
119 }
120 setDpyWfdAttr();
121 setExternalDisplay(true, mWfdFbNum);
122 return 0;
123}
124
125int ExternalDisplay::teardownHDMIDisplay() {
126 if(mConnectedFbNum == mHdmiFbNum) {
127 // hdmi offline event..!
128 closeFrameBuffer();
129 resetInfo();
130 setExternalDisplay(false);
131 }
132 return 0;
133}
134
135int ExternalDisplay::teardownWFDDisplay() {
136 if(mConnectedFbNum == mWfdFbNum) {
137 // wfd offline event..!
138 closeFrameBuffer();
139 memset(&mVInfo, 0, sizeof(mVInfo));
140 setExternalDisplay(false);
141 }
142 return 0;
143}
144
145void ExternalDisplay::processUEventOnline(const char *str) {
146 const char *s1 = str + strlen("change@/devices/virtual/switch/");
147 if(!strncmp(s1,"hdmi",strlen(s1))) {
148 // hdmi online event..!
149 configureHDMIDisplay();
Arun Kumar K.Rbfc79c22013-01-15 13:34:05 -0800150 // set system property
151 property_set("hw.hdmiON", "1");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800152 }else if(!strncmp(s1,"wfd",strlen(s1))) {
153 // wfd online event..!
154 configureWFDDisplay();
155 }
156}
157
158void ExternalDisplay::processUEventOffline(const char *str) {
159 const char *s1 = str + strlen("change@/devices/virtual/switch/");
160 if(!strncmp(s1,"hdmi",strlen(s1))) {
161 teardownHDMIDisplay();
Arun Kumar K.Rbfc79c22013-01-15 13:34:05 -0800162 // unset system property
163 property_set("hw.hdmiON", "0");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800164 }else if(!strncmp(s1,"wfd",strlen(s1))) {
165 teardownWFDDisplay();
166 }
167}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700168
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700169ExternalDisplay::ExternalDisplay(hwc_context_t* ctx):mFd(-1),
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800170 mCurrentMode(-1), mConnected(0), mConnectedFbNum(0), mModeCount(0),
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800171 mUnderscanSupported(false), mHwcContext(ctx), mHdmiFbNum(-1),
172 mWfdFbNum(-1), mExtDpyNum(HWC_DISPLAY_EXTERNAL)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700173{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700174 memset(&mVInfo, 0, sizeof(mVInfo));
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800175 //Determine the fb index for external display devices.
176 updateExtDispDevFbIndex();
Arun Kumar K.Re746ac52013-03-20 15:56:15 -0700177 // disable HPD at start, it will be enabled later
178 // when the display powers on
179 // This helps for framework reboot or adb shell stop/start
180 writeHPDOption(0);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800181
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700182}
183
Saurabh Shah56f610d2012-08-07 15:27:06 -0700184void ExternalDisplay::setEDIDMode(int resMode) {
185 ALOGD_IF(DEBUG,"resMode=%d ", resMode);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700186 {
187 Mutex::Autolock lock(mExtDispLock);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800188 setExternalDisplay(false);
189 openFrameBuffer(mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700190 setResolution(resMode);
191 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800192 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700193}
194
195void ExternalDisplay::setHPD(uint32_t startEnd) {
196 ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
197 writeHPDOption(startEnd);
198}
199
200void ExternalDisplay::setActionSafeDimension(int w, int h) {
201 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
202 Mutex::Autolock lock(mExtDispLock);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800203 char actionsafeWidth[PROPERTY_VALUE_MAX];
204 char actionsafeHeight[PROPERTY_VALUE_MAX];
205 sprintf(actionsafeWidth, "%d", w);
206 property_set("hw.actionsafe.width", actionsafeWidth);
207 sprintf(actionsafeHeight, "%d", h);
208 property_set("hw.actionsafe.height", actionsafeHeight);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800209 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700210}
211
212int ExternalDisplay::getModeCount() const {
213 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
214 Mutex::Autolock lock(mExtDispLock);
215 return mModeCount;
216}
217
218void ExternalDisplay::getEDIDModes(int *out) const {
219 Mutex::Autolock lock(mExtDispLock);
220 for(int i = 0;i < mModeCount;i++) {
221 out[i] = mEDIDModes[i];
222 }
223}
224
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800225void ExternalDisplay::readCEUnderscanInfo()
226{
227 int hdmiScanInfoFile = -1;
228 int len = -1;
229 char scanInfo[17];
230 char *ce_info_str = NULL;
231 const char token[] = ", \n";
232 int ce_info = -1;
233 char sysFsScanInfoFilePath[128];
234 sprintf(sysFsScanInfoFilePath, "/sys/devices/virtual/graphics/fb%d/"
235 "scan_info", mHdmiFbNum);
236
237 memset(scanInfo, 0, sizeof(scanInfo));
238 hdmiScanInfoFile = open(sysFsScanInfoFilePath, O_RDONLY, 0);
239 if (hdmiScanInfoFile < 0) {
240 ALOGD_IF(DEBUG, "%s: scan_info file '%s' not found",
241 __FUNCTION__, sysFsScanInfoFilePath);
242 return;
243 } else {
244 len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
245 ALOGD("%s: Scan Info string: %s length = %d",
246 __FUNCTION__, scanInfo, len);
247 if (len <= 0) {
248 close(hdmiScanInfoFile);
249 ALOGE("%s: Scan Info file empty '%s'",
250 __FUNCTION__, sysFsScanInfoFilePath);
251 return;
252 }
253 scanInfo[len] = '\0'; /* null terminate the string */
254 }
255 close(hdmiScanInfoFile);
256
257 /*
258 * The scan_info contains the three fields
259 * PT - preferred video format
260 * IT - video format
261 * CE video format - containing the underscan support information
262 */
263
264 /* PT */
265 ce_info_str = strtok(scanInfo, token);
266 if (ce_info_str) {
267 /* IT */
268 ce_info_str = strtok(NULL, token);
269 if (ce_info_str) {
270 /* CE */
271 ce_info_str = strtok(NULL, token);
272 if (ce_info_str)
273 ce_info = atoi(ce_info_str);
274 }
275 }
276
277 if (ce_info_str) {
278 // ce_info contains the underscan information
279 if (ce_info == EXT_SCAN_ALWAYS_UNDERSCANED ||
280 ce_info == EXT_SCAN_BOTH_SUPPORTED)
281 // if TV supported underscan, then driver will always underscan
282 // hence no need to apply action safe rectangle
283 mUnderscanSupported = true;
284 } else {
285 ALOGE("%s: scan_info string error", __FUNCTION__);
286 }
287
288 // Store underscan support info in a system property
289 const char* prop = (mUnderscanSupported) ? "1" : "0";
290 property_set("hw.underscan_supported", prop);
291 return;
292}
293
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700294ExternalDisplay::~ExternalDisplay()
295{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700296 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700297}
298
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700299struct disp_mode_timing_type {
300 int video_format;
301
302 int active_h;
303 int active_v;
304
305 int front_porch_h;
306 int pulse_width_h;
307 int back_porch_h;
308
309 int front_porch_v;
310 int pulse_width_v;
311 int back_porch_v;
312
313 int pixel_freq;
314 bool interlaced;
315
316 void set_info(struct fb_var_screeninfo &info) const;
317};
318
319void disp_mode_timing_type::set_info(struct fb_var_screeninfo &info) const
320{
321 info.reserved[0] = 0;
322 info.reserved[1] = 0;
323 info.reserved[2] = 0;
Ken Zhang7b03a952013-01-16 13:23:48 -0500324#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Saurabh Shah56f610d2012-08-07 15:27:06 -0700325 info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500326#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700327 info.xoffset = 0;
328 info.yoffset = 0;
329 info.xres = active_h;
330 info.yres = active_v;
331
332 info.pixclock = pixel_freq*1000;
333 info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
334
335 info.right_margin = front_porch_h;
336 info.hsync_len = pulse_width_h;
337 info.left_margin = back_porch_h;
338 info.lower_margin = front_porch_v;
339 info.vsync_len = pulse_width_v;
340 info.upper_margin = back_porch_v;
341}
342
343/* Video formates supported by the HDMI Standard */
344/* Indicates the resolution, pix clock and the aspect ratio */
345#define m640x480p60_4_3 1
346#define m720x480p60_4_3 2
347#define m720x480p60_16_9 3
348#define m1280x720p60_16_9 4
349#define m1920x1080i60_16_9 5
350#define m1440x480i60_4_3 6
351#define m1440x480i60_16_9 7
352#define m1920x1080p60_16_9 16
353#define m720x576p50_4_3 17
354#define m720x576p50_16_9 18
355#define m1280x720p50_16_9 19
356#define m1440x576i50_4_3 21
357#define m1440x576i50_16_9 22
358#define m1920x1080p50_16_9 31
359#define m1920x1080p24_16_9 32
360#define m1920x1080p25_16_9 33
361#define m1920x1080p30_16_9 34
362
363static struct disp_mode_timing_type supported_video_mode_lut[] = {
364 {m640x480p60_4_3, 640, 480, 16, 96, 48, 10, 2, 33, 25200, false},
365 {m720x480p60_4_3, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
366 {m720x480p60_16_9, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
367 {m1280x720p60_16_9, 1280, 720, 110, 40, 220, 5, 5, 20, 74250, false},
368 {m1920x1080i60_16_9, 1920, 540, 88, 44, 148, 2, 5, 5, 74250, false},
369 {m1440x480i60_4_3, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
370 {m1440x480i60_16_9, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
371 {m1920x1080p60_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 148500, false},
372 {m720x576p50_4_3, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
373 {m720x576p50_16_9, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
374 {m1280x720p50_16_9, 1280, 720, 440, 40, 220, 5, 5, 20, 74250, false},
375 {m1440x576i50_4_3, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
376 {m1440x576i50_16_9, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
377 {m1920x1080p50_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 148500, false},
378 {m1920x1080p24_16_9, 1920, 1080, 638, 44, 148, 4, 5, 36, 74250, false},
379 {m1920x1080p25_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 74250, false},
380 {m1920x1080p30_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 74250, false},
381};
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700382
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700383int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700384{
385 char delim = ',';
386 int count = 0;
387 char *start, *end;
388 // EDIDs are string delimited by ','
389 // Ex: 16,4,5,3,32,34,1
390 // Parse this string to get mode(int)
391 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700392 end = &delim;
393 while(*end == delim) {
394 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700395 start = end+1;
396 count++;
397 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700398 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
399 for (int i = 0; i < count; i++)
400 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700401 return count;
402}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700403
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700404bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700405{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800406 char sysFsEDIDFilePath[255];
407 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
408 mHdmiFbNum);
409
410 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700411 int len = -1;
412
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700413 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700414 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800415 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700416 return false;
417 } else {
418 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700419 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
420 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700421 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700422 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800423 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700424 }
425 else {
426 while (len > 1 && isspace(mEDIDs[len-1]))
427 --len;
428 mEDIDs[len] = 0;
429 }
430 }
431 close(hdmiEDIDFile);
432 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800433 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700434 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700435 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
436 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700437 }
438
439 return (strlen(mEDIDs) > 0);
440}
441
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800442bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700443{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700444 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800445 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700446 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800447 ALOGE("%s: %s is not available", __FUNCTION__,
448 msmFbDevicePath[fbNum-1]);
449 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800450 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800451 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700452 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700453 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700454}
455
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700456bool ExternalDisplay::closeFrameBuffer()
457{
458 int ret = 0;
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500459 if(mFd >= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700460 ret = close(mFd);
461 mFd = -1;
462 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700463 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800464 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700465 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700466 return (ret == 0);
467}
468
469// clears the vinfo, edid, best modes
470void ExternalDisplay::resetInfo()
471{
472 memset(&mVInfo, 0, sizeof(mVInfo));
473 memset(mEDIDs, 0, sizeof(mEDIDs));
474 memset(mEDIDModes, 0, sizeof(mEDIDModes));
475 mModeCount = 0;
476 mCurrentMode = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800477 mUnderscanSupported = false;
478 // Reset the underscan supported system property
479 const char* prop = "0";
480 property_set("hw.underscan_supported", prop);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700481}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700482
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700483int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700484{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800485 // XXX: We dont support interlaced modes but having
486 // it here for for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700487 switch (mode) {
488 default:
489 case m1440x480i60_4_3:
490 return 1; // 480i 4:3
491 case m1440x480i60_16_9:
492 return 2; // 480i 16:9
493 case m1440x576i50_4_3:
494 return 3; // i576i 4:3
495 case m1440x576i50_16_9:
496 return 4; // 576i 16:9
497 case m640x480p60_4_3:
498 return 5; // 640x480 4:3
499 case m720x480p60_4_3:
500 return 6; // 480p 4:3
501 case m720x480p60_16_9:
502 return 7; // 480p 16:9
503 case m720x576p50_4_3:
504 return 8; // 576p 4:3
505 case m720x576p50_16_9:
506 return 9; // 576p 16:9
507 case m1920x1080i60_16_9:
508 return 10; // 1080i 16:9
509 case m1280x720p50_16_9:
510 return 11; // 720p@50Hz
511 case m1280x720p60_16_9:
512 return 12; // 720p@60Hz
513 case m1920x1080p24_16_9:
514 return 13; //1080p@24Hz
515 case m1920x1080p25_16_9:
516 return 14; //108-p@25Hz
517 case m1920x1080p30_16_9:
518 return 15; //1080p@30Hz
519 case m1920x1080p50_16_9:
520 return 16; //1080p@50Hz
521 case m1920x1080p60_16_9:
522 return 17; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700523 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700524}
525
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800526/// Returns the user mode set(if any) using adb shell
527int ExternalDisplay::getUserMode() {
528 /* Based on the property set the resolution */
529 char property_value[PROPERTY_VALUE_MAX];
Arun Kumar K.Rc31bdcb2013-02-25 17:47:42 -0800530 property_get("hw.hdmi.resolution", property_value, "-1");
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800531 int mode = atoi(property_value);
532 // We dont support interlaced modes
533 if(isValidMode(mode) && !isInterlacedMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500534 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800535 return mode;
536 }
537 return -1;
538}
539
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700540// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700541int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700542 int bestOrder = 0;
543 int bestMode = m640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700544 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700545 // for all the edid read, get the best mode
546 for(int i = 0; i < mModeCount; i++) {
547 int mode = mEDIDModes[i];
548 int order = getModeOrder(mode);
549 if (order > bestOrder) {
550 bestOrder = order;
551 bestMode = mode;
552 }
553 }
554 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700555}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700556
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700557inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700558{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800559 bool valid = false;
560 for (int i = 0; i < mModeCount; i++) {
561 if(ID == mEDIDModes[i]) {
562 valid = true;
563 break;
564 }
565 }
566 return valid;
567}
568
569// returns true if the mode(ID) is interlaced mode format
570bool ExternalDisplay::isInterlacedMode(int ID) {
571 bool interlaced = false;
572 switch(ID) {
573 case m1440x480i60_4_3:
574 case m1440x480i60_16_9:
575 case m1440x576i50_4_3:
576 case m1440x576i50_16_9:
577 case m1920x1080i60_16_9:
578 interlaced = true;
579 default:
580 interlaced = false;
581 }
582 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700583}
584
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700585void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700586{
587 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700588 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700589 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
590 if(ret < 0) {
591 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
592 strerror(errno));
593 }
594
595 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
596 "(%d,%d,%d) %dMHz>", __FUNCTION__,
597 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
598 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
599 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
600 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800601 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700602 if ((isValidMode(ID)) && mCurrentMode != ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700603 const struct disp_mode_timing_type *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700604 &supported_video_mode_lut[0];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700605 unsigned count = sizeof(supported_video_mode_lut)/sizeof
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700606 (*supported_video_mode_lut);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700607 for (unsigned int i = 0; i < count; ++i) {
608 const struct disp_mode_timing_type *cur =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700609 &supported_video_mode_lut[i];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700610 if (cur->video_format == ID)
611 mode = cur;
612 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700613 mode->set_info(mVInfo);
614 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700615 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800616 mode->video_format, mVInfo.xres, mVInfo.yres,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700617 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
618 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
619 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500620#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
621 struct msmfb_metadata metadata;
622 memset(&metadata, 0 , sizeof(metadata));
623 metadata.op = metadata_op_vic;
624 metadata.data.video_info_code = mode->video_format;
625 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
626 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
627 __FUNCTION__, strerror(errno));
628 }
629#endif
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800630 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
631 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
632 if(ret < 0) {
633 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
634 __FUNCTION__, strerror(errno));
635 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700636 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700637 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700638}
639
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800640void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700641{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700642 hwc_context_t* ctx = mHwcContext;
643 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800644 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700645 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800646 mConnected = connected;
647 mConnectedFbNum = extFbNum;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800648 mHwcContext->dpyAttr[mExtDpyNum].connected = connected;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800649 // Update external fb number in Overlay context
650 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700651 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800652}
653
654int ExternalDisplay::getExtFbNum(int &fbNum) {
655 int ret = -1;
656 if(mConnected) {
657 fbNum = mConnectedFbNum;
658 ret = 0;
659 }
660 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700661}
662
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700663bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700664{
665 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800666 char sysFsHPDFilePath[255];
667 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
668 mHdmiFbNum);
669 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700670 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800671 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
672 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700673 ret = false;
674 } else {
675 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800676 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700677 if(userOption)
678 err = write(hdmiHPDFile, "1", 2);
679 else
680 err = write(hdmiHPDFile, "0" , 2);
681 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800682 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700683 ret = false;
684 }
685 close(hdmiHPDFile);
686 }
687 return ret;
688}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700689
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800690void ExternalDisplay::setDpyWfdAttr() {
691 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800692 mHwcContext->dpyAttr[mExtDpyNum].xres = mVInfo.xres;
693 mHwcContext->dpyAttr[mExtDpyNum].yres = mVInfo.yres;
694 mHwcContext->dpyAttr[mExtDpyNum].vsync_period =
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800695 1000000000l /60;
696 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
697 }
698}
699
700void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700701 int width = 0, height = 0, fps = 0;
702 getAttrForMode(width, height, fps);
703 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700704 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700705 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
706 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700707 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
708 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700709 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700710}
711
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800712void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700713 switch (mCurrentMode) {
714 case m640x480p60_4_3:
715 width = 640;
716 height = 480;
717 fps = 60;
718 break;
719 case m720x480p60_4_3:
720 case m720x480p60_16_9:
721 width = 720;
722 height = 480;
723 fps = 60;
724 break;
725 case m720x576p50_4_3:
726 case m720x576p50_16_9:
727 width = 720;
728 height = 576;
729 fps = 50;
730 break;
731 case m1280x720p50_16_9:
732 width = 1280;
733 height = 720;
734 fps = 50;
735 break;
736 case m1280x720p60_16_9:
737 width = 1280;
738 height = 720;
739 fps = 60;
740 break;
741 case m1920x1080p24_16_9:
742 width = 1920;
743 height = 1080;
744 fps = 24;
745 break;
746 case m1920x1080p25_16_9:
747 width = 1920;
748 height = 1080;
749 fps = 25;
750 break;
751 case m1920x1080p30_16_9:
752 width = 1920;
753 height = 1080;
754 fps = 30;
755 break;
756 case m1920x1080p50_16_9:
757 width = 1920;
758 height = 1080;
759 fps = 50;
760 break;
761 case m1920x1080p60_16_9:
762 width = 1920;
763 height = 1080;
764 fps = 60;
765 break;
766 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700767}
768
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700769};