blob: b336b8475db283bf536003986b526d5d0ed680c2 [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();
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800169
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700170}
171
Saurabh Shah56f610d2012-08-07 15:27:06 -0700172void ExternalDisplay::setEDIDMode(int resMode) {
173 ALOGD_IF(DEBUG,"resMode=%d ", resMode);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700174 {
175 Mutex::Autolock lock(mExtDispLock);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800176 setExternalDisplay(false);
177 openFrameBuffer(mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700178 setResolution(resMode);
179 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800180 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700181}
182
183void ExternalDisplay::setHPD(uint32_t startEnd) {
184 ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
185 writeHPDOption(startEnd);
186}
187
188void ExternalDisplay::setActionSafeDimension(int w, int h) {
189 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
190 Mutex::Autolock lock(mExtDispLock);
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800191 char actionsafeWidth[PROPERTY_VALUE_MAX];
192 char actionsafeHeight[PROPERTY_VALUE_MAX];
193 sprintf(actionsafeWidth, "%d", w);
194 property_set("hw.actionsafe.width", actionsafeWidth);
195 sprintf(actionsafeHeight, "%d", h);
196 property_set("hw.actionsafe.height", actionsafeHeight);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800197 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700198}
199
200int ExternalDisplay::getModeCount() const {
201 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
202 Mutex::Autolock lock(mExtDispLock);
203 return mModeCount;
204}
205
206void ExternalDisplay::getEDIDModes(int *out) const {
207 Mutex::Autolock lock(mExtDispLock);
208 for(int i = 0;i < mModeCount;i++) {
209 out[i] = mEDIDModes[i];
210 }
211}
212
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800213void ExternalDisplay::readCEUnderscanInfo()
214{
215 int hdmiScanInfoFile = -1;
216 int len = -1;
217 char scanInfo[17];
218 char *ce_info_str = NULL;
219 const char token[] = ", \n";
220 int ce_info = -1;
221 char sysFsScanInfoFilePath[128];
222 sprintf(sysFsScanInfoFilePath, "/sys/devices/virtual/graphics/fb%d/"
223 "scan_info", mHdmiFbNum);
224
225 memset(scanInfo, 0, sizeof(scanInfo));
226 hdmiScanInfoFile = open(sysFsScanInfoFilePath, O_RDONLY, 0);
227 if (hdmiScanInfoFile < 0) {
228 ALOGD_IF(DEBUG, "%s: scan_info file '%s' not found",
229 __FUNCTION__, sysFsScanInfoFilePath);
230 return;
231 } else {
232 len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
233 ALOGD("%s: Scan Info string: %s length = %d",
234 __FUNCTION__, scanInfo, len);
235 if (len <= 0) {
236 close(hdmiScanInfoFile);
237 ALOGE("%s: Scan Info file empty '%s'",
238 __FUNCTION__, sysFsScanInfoFilePath);
239 return;
240 }
241 scanInfo[len] = '\0'; /* null terminate the string */
242 }
243 close(hdmiScanInfoFile);
244
245 /*
246 * The scan_info contains the three fields
247 * PT - preferred video format
248 * IT - video format
249 * CE video format - containing the underscan support information
250 */
251
252 /* PT */
253 ce_info_str = strtok(scanInfo, token);
254 if (ce_info_str) {
255 /* IT */
256 ce_info_str = strtok(NULL, token);
257 if (ce_info_str) {
258 /* CE */
259 ce_info_str = strtok(NULL, token);
260 if (ce_info_str)
261 ce_info = atoi(ce_info_str);
262 }
263 }
264
265 if (ce_info_str) {
266 // ce_info contains the underscan information
267 if (ce_info == EXT_SCAN_ALWAYS_UNDERSCANED ||
268 ce_info == EXT_SCAN_BOTH_SUPPORTED)
269 // if TV supported underscan, then driver will always underscan
270 // hence no need to apply action safe rectangle
271 mUnderscanSupported = true;
272 } else {
273 ALOGE("%s: scan_info string error", __FUNCTION__);
274 }
275
276 // Store underscan support info in a system property
277 const char* prop = (mUnderscanSupported) ? "1" : "0";
278 property_set("hw.underscan_supported", prop);
279 return;
280}
281
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700282ExternalDisplay::~ExternalDisplay()
283{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700284 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700285}
286
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700287struct disp_mode_timing_type {
288 int video_format;
289
290 int active_h;
291 int active_v;
292
293 int front_porch_h;
294 int pulse_width_h;
295 int back_porch_h;
296
297 int front_porch_v;
298 int pulse_width_v;
299 int back_porch_v;
300
301 int pixel_freq;
302 bool interlaced;
303
304 void set_info(struct fb_var_screeninfo &info) const;
305};
306
307void disp_mode_timing_type::set_info(struct fb_var_screeninfo &info) const
308{
309 info.reserved[0] = 0;
310 info.reserved[1] = 0;
311 info.reserved[2] = 0;
Ken Zhang7b03a952013-01-16 13:23:48 -0500312#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Saurabh Shah56f610d2012-08-07 15:27:06 -0700313 info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500314#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700315 info.xoffset = 0;
316 info.yoffset = 0;
317 info.xres = active_h;
318 info.yres = active_v;
319
320 info.pixclock = pixel_freq*1000;
321 info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
322
323 info.right_margin = front_porch_h;
324 info.hsync_len = pulse_width_h;
325 info.left_margin = back_porch_h;
326 info.lower_margin = front_porch_v;
327 info.vsync_len = pulse_width_v;
328 info.upper_margin = back_porch_v;
329}
330
331/* Video formates supported by the HDMI Standard */
332/* Indicates the resolution, pix clock and the aspect ratio */
333#define m640x480p60_4_3 1
334#define m720x480p60_4_3 2
335#define m720x480p60_16_9 3
336#define m1280x720p60_16_9 4
337#define m1920x1080i60_16_9 5
338#define m1440x480i60_4_3 6
339#define m1440x480i60_16_9 7
340#define m1920x1080p60_16_9 16
341#define m720x576p50_4_3 17
342#define m720x576p50_16_9 18
343#define m1280x720p50_16_9 19
344#define m1440x576i50_4_3 21
345#define m1440x576i50_16_9 22
346#define m1920x1080p50_16_9 31
347#define m1920x1080p24_16_9 32
348#define m1920x1080p25_16_9 33
349#define m1920x1080p30_16_9 34
350
351static struct disp_mode_timing_type supported_video_mode_lut[] = {
352 {m640x480p60_4_3, 640, 480, 16, 96, 48, 10, 2, 33, 25200, false},
353 {m720x480p60_4_3, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
354 {m720x480p60_16_9, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
355 {m1280x720p60_16_9, 1280, 720, 110, 40, 220, 5, 5, 20, 74250, false},
356 {m1920x1080i60_16_9, 1920, 540, 88, 44, 148, 2, 5, 5, 74250, false},
357 {m1440x480i60_4_3, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
358 {m1440x480i60_16_9, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
359 {m1920x1080p60_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 148500, false},
360 {m720x576p50_4_3, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
361 {m720x576p50_16_9, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
362 {m1280x720p50_16_9, 1280, 720, 440, 40, 220, 5, 5, 20, 74250, false},
363 {m1440x576i50_4_3, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
364 {m1440x576i50_16_9, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
365 {m1920x1080p50_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 148500, false},
366 {m1920x1080p24_16_9, 1920, 1080, 638, 44, 148, 4, 5, 36, 74250, false},
367 {m1920x1080p25_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 74250, false},
368 {m1920x1080p30_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 74250, false},
369};
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700370
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700371int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700372{
373 char delim = ',';
374 int count = 0;
375 char *start, *end;
376 // EDIDs are string delimited by ','
377 // Ex: 16,4,5,3,32,34,1
378 // Parse this string to get mode(int)
379 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700380 end = &delim;
381 while(*end == delim) {
382 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700383 start = end+1;
384 count++;
385 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700386 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
387 for (int i = 0; i < count; i++)
388 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700389 return count;
390}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700391
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700392bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700393{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800394 char sysFsEDIDFilePath[255];
395 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
396 mHdmiFbNum);
397
398 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700399 int len = -1;
400
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700401 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700402 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800403 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700404 return false;
405 } else {
406 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700407 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
408 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700409 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700410 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800411 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700412 }
413 else {
414 while (len > 1 && isspace(mEDIDs[len-1]))
415 --len;
416 mEDIDs[len] = 0;
417 }
418 }
419 close(hdmiEDIDFile);
420 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800421 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700422 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700423 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
424 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700425 }
426
427 return (strlen(mEDIDs) > 0);
428}
429
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800430bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700431{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700432 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800433 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700434 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800435 ALOGE("%s: %s is not available", __FUNCTION__,
436 msmFbDevicePath[fbNum-1]);
437 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800438 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800439 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700440 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700441 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700442}
443
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700444bool ExternalDisplay::closeFrameBuffer()
445{
446 int ret = 0;
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500447 if(mFd >= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700448 ret = close(mFd);
449 mFd = -1;
450 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700451 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800452 mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700453 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700454 return (ret == 0);
455}
456
457// clears the vinfo, edid, best modes
458void ExternalDisplay::resetInfo()
459{
460 memset(&mVInfo, 0, sizeof(mVInfo));
461 memset(mEDIDs, 0, sizeof(mEDIDs));
462 memset(mEDIDModes, 0, sizeof(mEDIDModes));
463 mModeCount = 0;
464 mCurrentMode = -1;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800465 mUnderscanSupported = false;
466 // Reset the underscan supported system property
467 const char* prop = "0";
468 property_set("hw.underscan_supported", prop);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700469}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700470
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700471int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700472{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800473 // XXX: We dont support interlaced modes but having
474 // it here for for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700475 switch (mode) {
476 default:
477 case m1440x480i60_4_3:
478 return 1; // 480i 4:3
479 case m1440x480i60_16_9:
480 return 2; // 480i 16:9
481 case m1440x576i50_4_3:
482 return 3; // i576i 4:3
483 case m1440x576i50_16_9:
484 return 4; // 576i 16:9
485 case m640x480p60_4_3:
486 return 5; // 640x480 4:3
487 case m720x480p60_4_3:
488 return 6; // 480p 4:3
489 case m720x480p60_16_9:
490 return 7; // 480p 16:9
491 case m720x576p50_4_3:
492 return 8; // 576p 4:3
493 case m720x576p50_16_9:
494 return 9; // 576p 16:9
495 case m1920x1080i60_16_9:
496 return 10; // 1080i 16:9
497 case m1280x720p50_16_9:
498 return 11; // 720p@50Hz
499 case m1280x720p60_16_9:
500 return 12; // 720p@60Hz
501 case m1920x1080p24_16_9:
502 return 13; //1080p@24Hz
503 case m1920x1080p25_16_9:
504 return 14; //108-p@25Hz
505 case m1920x1080p30_16_9:
506 return 15; //1080p@30Hz
507 case m1920x1080p50_16_9:
508 return 16; //1080p@50Hz
509 case m1920x1080p60_16_9:
510 return 17; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700511 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700512}
513
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800514/// Returns the user mode set(if any) using adb shell
515int ExternalDisplay::getUserMode() {
516 /* Based on the property set the resolution */
517 char property_value[PROPERTY_VALUE_MAX];
Arun Kumar K.Rc31bdcb2013-02-25 17:47:42 -0800518 property_get("hw.hdmi.resolution", property_value, "-1");
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800519 int mode = atoi(property_value);
520 // We dont support interlaced modes
521 if(isValidMode(mode) && !isInterlacedMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500522 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800523 return mode;
524 }
525 return -1;
526}
527
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700528// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700529int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700530 int bestOrder = 0;
531 int bestMode = m640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700532 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700533 // for all the edid read, get the best mode
534 for(int i = 0; i < mModeCount; i++) {
535 int mode = mEDIDModes[i];
536 int order = getModeOrder(mode);
537 if (order > bestOrder) {
538 bestOrder = order;
539 bestMode = mode;
540 }
541 }
542 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700543}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700544
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700545inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700546{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800547 bool valid = false;
548 for (int i = 0; i < mModeCount; i++) {
549 if(ID == mEDIDModes[i]) {
550 valid = true;
551 break;
552 }
553 }
554 return valid;
555}
556
557// returns true if the mode(ID) is interlaced mode format
558bool ExternalDisplay::isInterlacedMode(int ID) {
559 bool interlaced = false;
560 switch(ID) {
561 case m1440x480i60_4_3:
562 case m1440x480i60_16_9:
563 case m1440x576i50_4_3:
564 case m1440x576i50_16_9:
565 case m1920x1080i60_16_9:
566 interlaced = true;
567 default:
568 interlaced = false;
569 }
570 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700571}
572
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700573void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700574{
575 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700576 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700577 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
578 if(ret < 0) {
579 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
580 strerror(errno));
581 }
582
583 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
584 "(%d,%d,%d) %dMHz>", __FUNCTION__,
585 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
586 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
587 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
588 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800589 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700590 if ((isValidMode(ID)) && mCurrentMode != ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700591 const struct disp_mode_timing_type *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700592 &supported_video_mode_lut[0];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700593 unsigned count = sizeof(supported_video_mode_lut)/sizeof
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700594 (*supported_video_mode_lut);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700595 for (unsigned int i = 0; i < count; ++i) {
596 const struct disp_mode_timing_type *cur =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700597 &supported_video_mode_lut[i];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700598 if (cur->video_format == ID)
599 mode = cur;
600 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700601 mode->set_info(mVInfo);
602 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700603 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800604 mode->video_format, mVInfo.xres, mVInfo.yres,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700605 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
606 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
607 mVInfo.pixclock/1000/1000);
Ken Zhang7b03a952013-01-16 13:23:48 -0500608#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
609 struct msmfb_metadata metadata;
610 memset(&metadata, 0 , sizeof(metadata));
611 metadata.op = metadata_op_vic;
612 metadata.data.video_info_code = mode->video_format;
613 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
614 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
615 __FUNCTION__, strerror(errno));
616 }
617#endif
Arun Kumar K.Re1cea3e2013-02-06 16:57:43 -0800618 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
619 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
620 if(ret < 0) {
621 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
622 __FUNCTION__, strerror(errno));
623 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700624 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700625 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700626}
627
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800628void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700629{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700630 hwc_context_t* ctx = mHwcContext;
631 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800632 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700633 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800634 mConnected = connected;
635 mConnectedFbNum = extFbNum;
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800636 mHwcContext->dpyAttr[mExtDpyNum].connected = connected;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800637 // Update external fb number in Overlay context
638 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700639 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800640}
641
642int ExternalDisplay::getExtFbNum(int &fbNum) {
643 int ret = -1;
644 if(mConnected) {
645 fbNum = mConnectedFbNum;
646 ret = 0;
647 }
648 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700649}
650
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700651bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700652{
653 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800654 char sysFsHPDFilePath[255];
655 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
656 mHdmiFbNum);
657 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700658 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800659 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
660 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700661 ret = false;
662 } else {
663 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800664 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700665 if(userOption)
666 err = write(hdmiHPDFile, "1", 2);
667 else
668 err = write(hdmiHPDFile, "0" , 2);
669 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800670 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700671 ret = false;
672 }
673 close(hdmiHPDFile);
674 }
675 return ret;
676}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700677
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800678void ExternalDisplay::setDpyWfdAttr() {
679 if(mHwcContext) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800680 mHwcContext->dpyAttr[mExtDpyNum].xres = mVInfo.xres;
681 mHwcContext->dpyAttr[mExtDpyNum].yres = mVInfo.yres;
682 mHwcContext->dpyAttr[mExtDpyNum].vsync_period =
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800683 1000000000l /60;
684 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
685 }
686}
687
688void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700689 int width = 0, height = 0, fps = 0;
690 getAttrForMode(width, height, fps);
691 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700692 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700693 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
694 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700695 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
696 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700697 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700698}
699
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800700void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700701 switch (mCurrentMode) {
702 case m640x480p60_4_3:
703 width = 640;
704 height = 480;
705 fps = 60;
706 break;
707 case m720x480p60_4_3:
708 case m720x480p60_16_9:
709 width = 720;
710 height = 480;
711 fps = 60;
712 break;
713 case m720x576p50_4_3:
714 case m720x576p50_16_9:
715 width = 720;
716 height = 576;
717 fps = 50;
718 break;
719 case m1280x720p50_16_9:
720 width = 1280;
721 height = 720;
722 fps = 50;
723 break;
724 case m1280x720p60_16_9:
725 width = 1280;
726 height = 720;
727 fps = 60;
728 break;
729 case m1920x1080p24_16_9:
730 width = 1920;
731 height = 1080;
732 fps = 24;
733 break;
734 case m1920x1080p25_16_9:
735 width = 1920;
736 height = 1080;
737 fps = 25;
738 break;
739 case m1920x1080p30_16_9:
740 width = 1920;
741 height = 1080;
742 fps = 30;
743 break;
744 case m1920x1080p50_16_9:
745 width = 1920;
746 height = 1080;
747 fps = 50;
748 break;
749 case m1920x1080p60_16_9:
750 width = 1920;
751 height = 1080;
752 fps = 60;
753 break;
754 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700755}
756
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700757};