blob: 883a39f5ffbf9cf4d69fb0345907bc0d62ce5c21 [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),
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800171 mUnderscanSupported(false), mHwcContext(ctx), mHdmiFbNum(-1), mWfdFbNum(-1)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700172{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700173 memset(&mVInfo, 0, sizeof(mVInfo));
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800174 //Determine the fb index for external display devices.
175 updateExtDispDevFbIndex();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700176}
177
Saurabh Shah56f610d2012-08-07 15:27:06 -0700178void ExternalDisplay::setEDIDMode(int resMode) {
179 ALOGD_IF(DEBUG,"resMode=%d ", resMode);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700180 {
181 Mutex::Autolock lock(mExtDispLock);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800182 setExternalDisplay(false);
183 openFrameBuffer(mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700184 setResolution(resMode);
185 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800186 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700187}
188
189void ExternalDisplay::setHPD(uint32_t startEnd) {
190 ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
191 writeHPDOption(startEnd);
192}
193
194void ExternalDisplay::setActionSafeDimension(int w, int h) {
195 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
196 Mutex::Autolock lock(mExtDispLock);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800197 char actionsafeWidth[PROPERTY_VALUE_MAX];
198 char actionsafeHeight[PROPERTY_VALUE_MAX];
199 sprintf(actionsafeWidth, "%d", w);
200 property_set("hw.actionsafe.width", actionsafeWidth);
201 sprintf(actionsafeHeight, "%d", h);
202 property_set("hw.actionsafe.height", actionsafeHeight);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800203 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700204}
205
206int ExternalDisplay::getModeCount() const {
207 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
208 Mutex::Autolock lock(mExtDispLock);
209 return mModeCount;
210}
211
212void ExternalDisplay::getEDIDModes(int *out) const {
213 Mutex::Autolock lock(mExtDispLock);
214 for(int i = 0;i < mModeCount;i++) {
215 out[i] = mEDIDModes[i];
216 }
217}
218
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800219void ExternalDisplay::readCEUnderscanInfo()
220{
221 int hdmiScanInfoFile = -1;
222 int len = -1;
223 char scanInfo[17];
224 char *ce_info_str = NULL;
225 const char token[] = ", \n";
226 int ce_info = -1;
227 char sysFsScanInfoFilePath[128];
228 sprintf(sysFsScanInfoFilePath, "/sys/devices/virtual/graphics/fb%d/"
229 "scan_info", mHdmiFbNum);
230
231 memset(scanInfo, 0, sizeof(scanInfo));
232 hdmiScanInfoFile = open(sysFsScanInfoFilePath, O_RDONLY, 0);
233 if (hdmiScanInfoFile < 0) {
234 ALOGD_IF(DEBUG, "%s: scan_info file '%s' not found",
235 __FUNCTION__, sysFsScanInfoFilePath);
236 return;
237 } else {
238 len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
239 ALOGD("%s: Scan Info string: %s length = %d",
240 __FUNCTION__, scanInfo, len);
241 if (len <= 0) {
242 close(hdmiScanInfoFile);
243 ALOGE("%s: Scan Info file empty '%s'",
244 __FUNCTION__, sysFsScanInfoFilePath);
245 return;
246 }
247 scanInfo[len] = '\0'; /* null terminate the string */
248 }
249 close(hdmiScanInfoFile);
250
251 /*
252 * The scan_info contains the three fields
253 * PT - preferred video format
254 * IT - video format
255 * CE video format - containing the underscan support information
256 */
257
258 /* PT */
259 ce_info_str = strtok(scanInfo, token);
260 if (ce_info_str) {
261 /* IT */
262 ce_info_str = strtok(NULL, token);
263 if (ce_info_str) {
264 /* CE */
265 ce_info_str = strtok(NULL, token);
266 if (ce_info_str)
267 ce_info = atoi(ce_info_str);
268 }
269 }
270
271 if (ce_info_str) {
272 // ce_info contains the underscan information
273 if (ce_info == EXT_SCAN_ALWAYS_UNDERSCANED ||
274 ce_info == EXT_SCAN_BOTH_SUPPORTED)
275 // if TV supported underscan, then driver will always underscan
276 // hence no need to apply action safe rectangle
277 mUnderscanSupported = true;
278 } else {
279 ALOGE("%s: scan_info string error", __FUNCTION__);
280 }
281
282 // Store underscan support info in a system property
283 const char* prop = (mUnderscanSupported) ? "1" : "0";
284 property_set("hw.underscan_supported", prop);
285 return;
286}
287
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700288ExternalDisplay::~ExternalDisplay()
289{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700290 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700291}
292
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700293struct disp_mode_timing_type {
294 int video_format;
295
296 int active_h;
297 int active_v;
298
299 int front_porch_h;
300 int pulse_width_h;
301 int back_porch_h;
302
303 int front_porch_v;
304 int pulse_width_v;
305 int back_porch_v;
306
307 int pixel_freq;
308 bool interlaced;
309
310 void set_info(struct fb_var_screeninfo &info) const;
311};
312
313void disp_mode_timing_type::set_info(struct fb_var_screeninfo &info) const
314{
315 info.reserved[0] = 0;
316 info.reserved[1] = 0;
317 info.reserved[2] = 0;
Ken Zhang7b03a952013-01-16 13:23:48 -0500318#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Saurabh Shah56f610d2012-08-07 15:27:06 -0700319 info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500320#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700321 info.xoffset = 0;
322 info.yoffset = 0;
323 info.xres = active_h;
324 info.yres = active_v;
325
326 info.pixclock = pixel_freq*1000;
327 info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
328
329 info.right_margin = front_porch_h;
330 info.hsync_len = pulse_width_h;
331 info.left_margin = back_porch_h;
332 info.lower_margin = front_porch_v;
333 info.vsync_len = pulse_width_v;
334 info.upper_margin = back_porch_v;
335}
336
337/* Video formates supported by the HDMI Standard */
338/* Indicates the resolution, pix clock and the aspect ratio */
339#define m640x480p60_4_3 1
340#define m720x480p60_4_3 2
341#define m720x480p60_16_9 3
342#define m1280x720p60_16_9 4
343#define m1920x1080i60_16_9 5
344#define m1440x480i60_4_3 6
345#define m1440x480i60_16_9 7
346#define m1920x1080p60_16_9 16
347#define m720x576p50_4_3 17
348#define m720x576p50_16_9 18
349#define m1280x720p50_16_9 19
350#define m1440x576i50_4_3 21
351#define m1440x576i50_16_9 22
352#define m1920x1080p50_16_9 31
353#define m1920x1080p24_16_9 32
354#define m1920x1080p25_16_9 33
355#define m1920x1080p30_16_9 34
356
357static struct disp_mode_timing_type supported_video_mode_lut[] = {
358 {m640x480p60_4_3, 640, 480, 16, 96, 48, 10, 2, 33, 25200, false},
359 {m720x480p60_4_3, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
360 {m720x480p60_16_9, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
361 {m1280x720p60_16_9, 1280, 720, 110, 40, 220, 5, 5, 20, 74250, false},
362 {m1920x1080i60_16_9, 1920, 540, 88, 44, 148, 2, 5, 5, 74250, false},
363 {m1440x480i60_4_3, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
364 {m1440x480i60_16_9, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
365 {m1920x1080p60_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 148500, false},
366 {m720x576p50_4_3, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
367 {m720x576p50_16_9, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
368 {m1280x720p50_16_9, 1280, 720, 440, 40, 220, 5, 5, 20, 74250, false},
369 {m1440x576i50_4_3, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
370 {m1440x576i50_16_9, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
371 {m1920x1080p50_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 148500, false},
372 {m1920x1080p24_16_9, 1920, 1080, 638, 44, 148, 4, 5, 36, 74250, false},
373 {m1920x1080p25_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 74250, false},
374 {m1920x1080p30_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 74250, false},
375};
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700376
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700377int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700378{
379 char delim = ',';
380 int count = 0;
381 char *start, *end;
382 // EDIDs are string delimited by ','
383 // Ex: 16,4,5,3,32,34,1
384 // Parse this string to get mode(int)
385 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700386 end = &delim;
387 while(*end == delim) {
388 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700389 start = end+1;
390 count++;
391 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700392 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
393 for (int i = 0; i < count; i++)
394 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700395 return count;
396}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700397
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700398bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700399{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800400 char sysFsEDIDFilePath[255];
401 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
402 mHdmiFbNum);
403
404 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700405 int len = -1;
406
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700407 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700408 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800409 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700410 return false;
411 } else {
412 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700413 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
414 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700415 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700416 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800417 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700418 }
419 else {
420 while (len > 1 && isspace(mEDIDs[len-1]))
421 --len;
422 mEDIDs[len] = 0;
423 }
424 }
425 close(hdmiEDIDFile);
426 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800427 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700428 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700429 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
430 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700431 }
432
433 return (strlen(mEDIDs) > 0);
434}
435
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800436bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700437{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700438 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800439 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700440 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800441 ALOGE("%s: %s is not available", __FUNCTION__,
442 msmFbDevicePath[fbNum-1]);
443 if(mHwcContext) {
444 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
445 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700446 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700447 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700448}
449
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700450bool ExternalDisplay::closeFrameBuffer()
451{
452 int ret = 0;
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500453 if(mFd >= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700454 ret = close(mFd);
455 mFd = -1;
456 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700457 if(mHwcContext) {
458 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
459 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700460 return (ret == 0);
461}
462
463// clears the vinfo, edid, best modes
464void ExternalDisplay::resetInfo()
465{
466 memset(&mVInfo, 0, sizeof(mVInfo));
467 memset(mEDIDs, 0, sizeof(mEDIDs));
468 memset(mEDIDModes, 0, sizeof(mEDIDModes));
469 mModeCount = 0;
470 mCurrentMode = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800471 mUnderscanSupported = false;
472 // Reset the underscan supported system property
473 const char* prop = "0";
474 property_set("hw.underscan_supported", prop);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700475}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700476
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700477int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700478{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800479 // XXX: We dont support interlaced modes but having
480 // it here for for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700481 switch (mode) {
482 default:
483 case m1440x480i60_4_3:
484 return 1; // 480i 4:3
485 case m1440x480i60_16_9:
486 return 2; // 480i 16:9
487 case m1440x576i50_4_3:
488 return 3; // i576i 4:3
489 case m1440x576i50_16_9:
490 return 4; // 576i 16:9
491 case m640x480p60_4_3:
492 return 5; // 640x480 4:3
493 case m720x480p60_4_3:
494 return 6; // 480p 4:3
495 case m720x480p60_16_9:
496 return 7; // 480p 16:9
497 case m720x576p50_4_3:
498 return 8; // 576p 4:3
499 case m720x576p50_16_9:
500 return 9; // 576p 16:9
501 case m1920x1080i60_16_9:
502 return 10; // 1080i 16:9
503 case m1280x720p50_16_9:
504 return 11; // 720p@50Hz
505 case m1280x720p60_16_9:
506 return 12; // 720p@60Hz
507 case m1920x1080p24_16_9:
508 return 13; //1080p@24Hz
509 case m1920x1080p25_16_9:
510 return 14; //108-p@25Hz
511 case m1920x1080p30_16_9:
512 return 15; //1080p@30Hz
513 case m1920x1080p50_16_9:
514 return 16; //1080p@50Hz
515 case m1920x1080p60_16_9:
516 return 17; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700517 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700518}
519
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800520/// Returns the user mode set(if any) using adb shell
521int ExternalDisplay::getUserMode() {
522 /* Based on the property set the resolution */
523 char property_value[PROPERTY_VALUE_MAX];
524 property_get("hdmi.resolution", property_value, "-1");
525 int mode = atoi(property_value);
526 // We dont support interlaced modes
527 if(isValidMode(mode) && !isInterlacedMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500528 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800529 return mode;
530 }
531 return -1;
532}
533
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700534// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700535int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700536 int bestOrder = 0;
537 int bestMode = m640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700538 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700539 // for all the edid read, get the best mode
540 for(int i = 0; i < mModeCount; i++) {
541 int mode = mEDIDModes[i];
542 int order = getModeOrder(mode);
543 if (order > bestOrder) {
544 bestOrder = order;
545 bestMode = mode;
546 }
547 }
548 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700549}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700550
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700551inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700552{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800553 bool valid = false;
554 for (int i = 0; i < mModeCount; i++) {
555 if(ID == mEDIDModes[i]) {
556 valid = true;
557 break;
558 }
559 }
560 return valid;
561}
562
563// returns true if the mode(ID) is interlaced mode format
564bool ExternalDisplay::isInterlacedMode(int ID) {
565 bool interlaced = false;
566 switch(ID) {
567 case m1440x480i60_4_3:
568 case m1440x480i60_16_9:
569 case m1440x576i50_4_3:
570 case m1440x576i50_16_9:
571 case m1920x1080i60_16_9:
572 interlaced = true;
573 default:
574 interlaced = false;
575 }
576 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700577}
578
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700579void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700580{
581 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700582 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700583 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
584 if(ret < 0) {
585 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
586 strerror(errno));
587 }
588
589 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
590 "(%d,%d,%d) %dMHz>", __FUNCTION__,
591 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
592 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
593 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
594 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800595 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700596 if ((isValidMode(ID)) && mCurrentMode != ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700597 const struct disp_mode_timing_type *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700598 &supported_video_mode_lut[0];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700599 unsigned count = sizeof(supported_video_mode_lut)/sizeof
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700600 (*supported_video_mode_lut);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700601 for (unsigned int i = 0; i < count; ++i) {
602 const struct disp_mode_timing_type *cur =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700603 &supported_video_mode_lut[i];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700604 if (cur->video_format == ID)
605 mode = cur;
606 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700607 mode->set_info(mVInfo);
608 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700609 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800610 mode->video_format, mVInfo.xres, mVInfo.yres,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700611 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
612 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
613 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500614#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
615 struct msmfb_metadata metadata;
616 memset(&metadata, 0 , sizeof(metadata));
617 metadata.op = metadata_op_vic;
618 metadata.data.video_info_code = mode->video_format;
619 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
620 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
621 __FUNCTION__, strerror(errno));
622 }
623#endif
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800624 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
625 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
626 if(ret < 0) {
627 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
628 __FUNCTION__, strerror(errno));
629 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700630 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700631 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700632}
633
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800634void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700635{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700636 hwc_context_t* ctx = mHwcContext;
637 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800638 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700639 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800640 mConnected = connected;
641 mConnectedFbNum = extFbNum;
642 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].connected = connected;
643 // Update external fb number in Overlay context
644 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700645 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800646}
647
648int ExternalDisplay::getExtFbNum(int &fbNum) {
649 int ret = -1;
650 if(mConnected) {
651 fbNum = mConnectedFbNum;
652 ret = 0;
653 }
654 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700655}
656
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700657bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700658{
659 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800660 char sysFsHPDFilePath[255];
661 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
662 mHdmiFbNum);
663 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700664 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800665 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
666 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700667 ret = false;
668 } else {
669 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800670 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700671 if(userOption)
672 err = write(hdmiHPDFile, "1", 2);
673 else
674 err = write(hdmiHPDFile, "0" , 2);
675 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800676 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700677 ret = false;
678 }
679 close(hdmiHPDFile);
680 }
681 return ret;
682}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700683
Saurabh Shah3825f252012-09-21 16:32:18 -0700684/*
685 * commits the changes to the external display
Saurabh Shah3825f252012-09-21 16:32:18 -0700686 */
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700687bool ExternalDisplay::post()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700688{
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500689 if(mFd == -1)
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700690 return false;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500691 struct mdp_display_commit ext_commit;
Naseer Ahmed9da84662012-12-06 19:29:29 -0500692 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
693 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500694 if (ioctl(mFd, MSMFB_DISPLAY_COMMIT, &ext_commit) == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800695 ALOGE("%s: MSMFB_DISPLAY_COMMIT for external failed, str: %s",
696 __FUNCTION__, strerror(errno));
697 return false;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700698 }
699 return true;
700}
701
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800702void ExternalDisplay::setDpyWfdAttr() {
703 if(mHwcContext) {
704 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = mVInfo.xres;
705 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = mVInfo.yres;
706 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
707 1000000000l /60;
708 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
709 }
710}
711
712void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700713 int width = 0, height = 0, fps = 0;
714 getAttrForMode(width, height, fps);
715 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700716 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700717 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
718 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700719 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
720 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700721 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700722}
723
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800724void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700725 switch (mCurrentMode) {
726 case m640x480p60_4_3:
727 width = 640;
728 height = 480;
729 fps = 60;
730 break;
731 case m720x480p60_4_3:
732 case m720x480p60_16_9:
733 width = 720;
734 height = 480;
735 fps = 60;
736 break;
737 case m720x576p50_4_3:
738 case m720x576p50_16_9:
739 width = 720;
740 height = 576;
741 fps = 50;
742 break;
743 case m1280x720p50_16_9:
744 width = 1280;
745 height = 720;
746 fps = 50;
747 break;
748 case m1280x720p60_16_9:
749 width = 1280;
750 height = 720;
751 fps = 60;
752 break;
753 case m1920x1080p24_16_9:
754 width = 1920;
755 height = 1080;
756 fps = 24;
757 break;
758 case m1920x1080p25_16_9:
759 width = 1920;
760 height = 1080;
761 fps = 25;
762 break;
763 case m1920x1080p30_16_9:
764 width = 1920;
765 height = 1080;
766 fps = 30;
767 break;
768 case m1920x1080p50_16_9:
769 width = 1920;
770 height = 1080;
771 fps = 50;
772 break;
773 case m1920x1080p60_16_9:
774 width = 1920;
775 height = 1080;
776 fps = 60;
777 break;
778 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700779}
780
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700781};