blob: ecacd7bc275118ea63786acf47b9544da30cb5b6 [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;
88 readResolution();
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080089 // TODO: Move this to activate
Arun Kumar K.R37552c52012-12-10 12:47:18 -080090 /* Used for changing the resolution
91 * getUserMode will get the preferred
92 * mode set thru adb shell */
93 int mode = getUserMode();
94 if (mode == -1) {
95 //Get the best mode and set
96 mode = getBestMode();
97 }
98 setResolution(mode);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -080099 setDpyHdmiAttr();
100 setExternalDisplay(true, mHdmiFbNum);
101 return 0;
102}
103
104int ExternalDisplay::configureWFDDisplay() {
105 int ret = 0;
106 if(mConnectedFbNum == mHdmiFbNum) {
107 ALOGE("%s: Cannot process WFD connection while HDMI is active",
108 __FUNCTION__);
109 return -1;
110 }
111 openFrameBuffer(mWfdFbNum);
112 if(mFd == -1)
113 return -1;
114 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
115 if(ret < 0) {
116 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
117 strerror(errno));
118 }
119 setDpyWfdAttr();
120 setExternalDisplay(true, mWfdFbNum);
121 return 0;
122}
123
124int ExternalDisplay::teardownHDMIDisplay() {
125 if(mConnectedFbNum == mHdmiFbNum) {
126 // hdmi offline event..!
127 closeFrameBuffer();
128 resetInfo();
129 setExternalDisplay(false);
130 }
131 return 0;
132}
133
134int ExternalDisplay::teardownWFDDisplay() {
135 if(mConnectedFbNum == mWfdFbNum) {
136 // wfd offline event..!
137 closeFrameBuffer();
138 memset(&mVInfo, 0, sizeof(mVInfo));
139 setExternalDisplay(false);
140 }
141 return 0;
142}
143
144void ExternalDisplay::processUEventOnline(const char *str) {
145 const char *s1 = str + strlen("change@/devices/virtual/switch/");
146 if(!strncmp(s1,"hdmi",strlen(s1))) {
147 // hdmi online event..!
148 configureHDMIDisplay();
Arun Kumar K.Rbfc79c22013-01-15 13:34:05 -0800149 // set system property
150 property_set("hw.hdmiON", "1");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800151 }else if(!strncmp(s1,"wfd",strlen(s1))) {
152 // wfd online event..!
153 configureWFDDisplay();
154 }
155}
156
157void ExternalDisplay::processUEventOffline(const char *str) {
158 const char *s1 = str + strlen("change@/devices/virtual/switch/");
159 if(!strncmp(s1,"hdmi",strlen(s1))) {
160 teardownHDMIDisplay();
Arun Kumar K.Rbfc79c22013-01-15 13:34:05 -0800161 // unset system property
162 property_set("hw.hdmiON", "0");
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800163 }else if(!strncmp(s1,"wfd",strlen(s1))) {
164 teardownWFDDisplay();
165 }
166}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700167
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700168ExternalDisplay::ExternalDisplay(hwc_context_t* ctx):mFd(-1),
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800169 mCurrentMode(-1), mConnected(0), mConnectedFbNum(0), mModeCount(0),
170 mHwcContext(ctx), mHdmiFbNum(-1), mWfdFbNum(-1)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700171{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700172 memset(&mVInfo, 0, sizeof(mVInfo));
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800173 //Determine the fb index for external display devices.
174 updateExtDispDevFbIndex();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700175}
176
Saurabh Shah56f610d2012-08-07 15:27:06 -0700177void ExternalDisplay::setEDIDMode(int resMode) {
178 ALOGD_IF(DEBUG,"resMode=%d ", resMode);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700179 {
180 Mutex::Autolock lock(mExtDispLock);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800181 setExternalDisplay(false);
182 openFrameBuffer(mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700183 setResolution(resMode);
184 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800185 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700186}
187
188void ExternalDisplay::setHPD(uint32_t startEnd) {
189 ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
190 writeHPDOption(startEnd);
191}
192
193void ExternalDisplay::setActionSafeDimension(int w, int h) {
194 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
195 Mutex::Autolock lock(mExtDispLock);
196 overlay::utils::ActionSafe::getInstance()->setDimension(w, h);
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
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700213ExternalDisplay::~ExternalDisplay()
214{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700215 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700216}
217
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700218struct disp_mode_timing_type {
219 int video_format;
220
221 int active_h;
222 int active_v;
223
224 int front_porch_h;
225 int pulse_width_h;
226 int back_porch_h;
227
228 int front_porch_v;
229 int pulse_width_v;
230 int back_porch_v;
231
232 int pixel_freq;
233 bool interlaced;
234
235 void set_info(struct fb_var_screeninfo &info) const;
236};
237
238void disp_mode_timing_type::set_info(struct fb_var_screeninfo &info) const
239{
240 info.reserved[0] = 0;
241 info.reserved[1] = 0;
242 info.reserved[2] = 0;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700243 info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700244
245 info.xoffset = 0;
246 info.yoffset = 0;
247 info.xres = active_h;
248 info.yres = active_v;
249
250 info.pixclock = pixel_freq*1000;
251 info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
252
253 info.right_margin = front_porch_h;
254 info.hsync_len = pulse_width_h;
255 info.left_margin = back_porch_h;
256 info.lower_margin = front_porch_v;
257 info.vsync_len = pulse_width_v;
258 info.upper_margin = back_porch_v;
259}
260
261/* Video formates supported by the HDMI Standard */
262/* Indicates the resolution, pix clock and the aspect ratio */
263#define m640x480p60_4_3 1
264#define m720x480p60_4_3 2
265#define m720x480p60_16_9 3
266#define m1280x720p60_16_9 4
267#define m1920x1080i60_16_9 5
268#define m1440x480i60_4_3 6
269#define m1440x480i60_16_9 7
270#define m1920x1080p60_16_9 16
271#define m720x576p50_4_3 17
272#define m720x576p50_16_9 18
273#define m1280x720p50_16_9 19
274#define m1440x576i50_4_3 21
275#define m1440x576i50_16_9 22
276#define m1920x1080p50_16_9 31
277#define m1920x1080p24_16_9 32
278#define m1920x1080p25_16_9 33
279#define m1920x1080p30_16_9 34
280
281static struct disp_mode_timing_type supported_video_mode_lut[] = {
282 {m640x480p60_4_3, 640, 480, 16, 96, 48, 10, 2, 33, 25200, false},
283 {m720x480p60_4_3, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
284 {m720x480p60_16_9, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
285 {m1280x720p60_16_9, 1280, 720, 110, 40, 220, 5, 5, 20, 74250, false},
286 {m1920x1080i60_16_9, 1920, 540, 88, 44, 148, 2, 5, 5, 74250, false},
287 {m1440x480i60_4_3, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
288 {m1440x480i60_16_9, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
289 {m1920x1080p60_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 148500, false},
290 {m720x576p50_4_3, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
291 {m720x576p50_16_9, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
292 {m1280x720p50_16_9, 1280, 720, 440, 40, 220, 5, 5, 20, 74250, false},
293 {m1440x576i50_4_3, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
294 {m1440x576i50_16_9, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
295 {m1920x1080p50_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 148500, false},
296 {m1920x1080p24_16_9, 1920, 1080, 638, 44, 148, 4, 5, 36, 74250, false},
297 {m1920x1080p25_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 74250, false},
298 {m1920x1080p30_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 74250, false},
299};
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700300
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700301int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700302{
303 char delim = ',';
304 int count = 0;
305 char *start, *end;
306 // EDIDs are string delimited by ','
307 // Ex: 16,4,5,3,32,34,1
308 // Parse this string to get mode(int)
309 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700310 end = &delim;
311 while(*end == delim) {
312 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700313 start = end+1;
314 count++;
315 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700316 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
317 for (int i = 0; i < count; i++)
318 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700319 return count;
320}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700321
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700322bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700323{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800324 char sysFsEDIDFilePath[255];
325 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
326 mHdmiFbNum);
327
328 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700329 int len = -1;
330
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700331 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700332 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800333 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700334 return false;
335 } else {
336 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700337 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
338 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700339 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700340 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800341 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700342 }
343 else {
344 while (len > 1 && isspace(mEDIDs[len-1]))
345 --len;
346 mEDIDs[len] = 0;
347 }
348 }
349 close(hdmiEDIDFile);
350 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800351 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700352 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700353 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
354 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700355 }
356
357 return (strlen(mEDIDs) > 0);
358}
359
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800360bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700361{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700362 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800363 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700364 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800365 ALOGE("%s: %s is not available", __FUNCTION__,
366 msmFbDevicePath[fbNum-1]);
367 if(mHwcContext) {
368 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
369 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700370 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700371 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700372}
373
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700374bool ExternalDisplay::closeFrameBuffer()
375{
376 int ret = 0;
377 if(mFd > 0) {
378 ret = close(mFd);
379 mFd = -1;
380 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700381 if(mHwcContext) {
382 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
383 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700384 return (ret == 0);
385}
386
387// clears the vinfo, edid, best modes
388void ExternalDisplay::resetInfo()
389{
390 memset(&mVInfo, 0, sizeof(mVInfo));
391 memset(mEDIDs, 0, sizeof(mEDIDs));
392 memset(mEDIDModes, 0, sizeof(mEDIDModes));
393 mModeCount = 0;
394 mCurrentMode = -1;
395}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700396
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700397int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700398{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800399 // XXX: We dont support interlaced modes but having
400 // it here for for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700401 switch (mode) {
402 default:
403 case m1440x480i60_4_3:
404 return 1; // 480i 4:3
405 case m1440x480i60_16_9:
406 return 2; // 480i 16:9
407 case m1440x576i50_4_3:
408 return 3; // i576i 4:3
409 case m1440x576i50_16_9:
410 return 4; // 576i 16:9
411 case m640x480p60_4_3:
412 return 5; // 640x480 4:3
413 case m720x480p60_4_3:
414 return 6; // 480p 4:3
415 case m720x480p60_16_9:
416 return 7; // 480p 16:9
417 case m720x576p50_4_3:
418 return 8; // 576p 4:3
419 case m720x576p50_16_9:
420 return 9; // 576p 16:9
421 case m1920x1080i60_16_9:
422 return 10; // 1080i 16:9
423 case m1280x720p50_16_9:
424 return 11; // 720p@50Hz
425 case m1280x720p60_16_9:
426 return 12; // 720p@60Hz
427 case m1920x1080p24_16_9:
428 return 13; //1080p@24Hz
429 case m1920x1080p25_16_9:
430 return 14; //108-p@25Hz
431 case m1920x1080p30_16_9:
432 return 15; //1080p@30Hz
433 case m1920x1080p50_16_9:
434 return 16; //1080p@50Hz
435 case m1920x1080p60_16_9:
436 return 17; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700437 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700438}
439
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800440/// Returns the user mode set(if any) using adb shell
441int ExternalDisplay::getUserMode() {
442 /* Based on the property set the resolution */
443 char property_value[PROPERTY_VALUE_MAX];
444 property_get("hdmi.resolution", property_value, "-1");
445 int mode = atoi(property_value);
446 // We dont support interlaced modes
447 if(isValidMode(mode) && !isInterlacedMode(mode)) {
448 ALOGD_IF("%s: setting the HDMI mode = %d", __FUNCTION__, mode);
449 return mode;
450 }
451 return -1;
452}
453
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700454// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700455int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700456 int bestOrder = 0;
457 int bestMode = m640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700458 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700459 // for all the edid read, get the best mode
460 for(int i = 0; i < mModeCount; i++) {
461 int mode = mEDIDModes[i];
462 int order = getModeOrder(mode);
463 if (order > bestOrder) {
464 bestOrder = order;
465 bestMode = mode;
466 }
467 }
468 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700469}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700470
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700471inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700472{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800473 bool valid = false;
474 for (int i = 0; i < mModeCount; i++) {
475 if(ID == mEDIDModes[i]) {
476 valid = true;
477 break;
478 }
479 }
480 return valid;
481}
482
483// returns true if the mode(ID) is interlaced mode format
484bool ExternalDisplay::isInterlacedMode(int ID) {
485 bool interlaced = false;
486 switch(ID) {
487 case m1440x480i60_4_3:
488 case m1440x480i60_16_9:
489 case m1440x576i50_4_3:
490 case m1440x576i50_16_9:
491 case m1920x1080i60_16_9:
492 interlaced = true;
493 default:
494 interlaced = false;
495 }
496 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700497}
498
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700499void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700500{
501 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700502 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700503 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
504 if(ret < 0) {
505 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
506 strerror(errno));
507 }
508
509 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
510 "(%d,%d,%d) %dMHz>", __FUNCTION__,
511 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
512 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
513 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
514 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800515 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700516 if ((isValidMode(ID)) && mCurrentMode != ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700517 const struct disp_mode_timing_type *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700518 &supported_video_mode_lut[0];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700519 unsigned count = sizeof(supported_video_mode_lut)/sizeof
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700520 (*supported_video_mode_lut);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700521 for (unsigned int i = 0; i < count; ++i) {
522 const struct disp_mode_timing_type *cur =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700523 &supported_video_mode_lut[i];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700524 if (cur->video_format == ID)
525 mode = cur;
526 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700527 mode->set_info(mVInfo);
528 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700529 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700530 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
531 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
532 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
533 mVInfo.pixclock/1000/1000);
534 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
535 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
536 if(ret < 0) {
537 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
538 __FUNCTION__, strerror(errno));
539 }
540 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700541 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700542}
543
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800544void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700545{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700546 hwc_context_t* ctx = mHwcContext;
547 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800548 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700549 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800550 mConnected = connected;
551 mConnectedFbNum = extFbNum;
552 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].connected = connected;
553 // Update external fb number in Overlay context
554 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700555 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800556}
557
558int ExternalDisplay::getExtFbNum(int &fbNum) {
559 int ret = -1;
560 if(mConnected) {
561 fbNum = mConnectedFbNum;
562 ret = 0;
563 }
564 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700565}
566
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700567bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700568{
569 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800570 char sysFsHPDFilePath[255];
571 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
572 mHdmiFbNum);
573 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700574 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800575 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
576 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700577 ret = false;
578 } else {
579 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800580 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700581 if(userOption)
582 err = write(hdmiHPDFile, "1", 2);
583 else
584 err = write(hdmiHPDFile, "0" , 2);
585 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800586 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700587 ret = false;
588 }
589 close(hdmiHPDFile);
590 }
591 return ret;
592}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700593
Saurabh Shah3825f252012-09-21 16:32:18 -0700594/*
595 * commits the changes to the external display
Saurabh Shah3825f252012-09-21 16:32:18 -0700596 */
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700597bool ExternalDisplay::post()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700598{
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500599 if(mFd == -1)
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700600 return false;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500601 struct mdp_display_commit ext_commit;
Naseer Ahmed9da84662012-12-06 19:29:29 -0500602 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
603 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500604 if (ioctl(mFd, MSMFB_DISPLAY_COMMIT, &ext_commit) == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800605 ALOGE("%s: MSMFB_DISPLAY_COMMIT for external failed, str: %s",
606 __FUNCTION__, strerror(errno));
607 return false;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700608 }
609 return true;
610}
611
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800612void ExternalDisplay::setDpyWfdAttr() {
613 if(mHwcContext) {
614 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = mVInfo.xres;
615 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = mVInfo.yres;
616 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
617 1000000000l /60;
618 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
619 }
620}
621
622void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700623 int width = 0, height = 0, fps = 0;
624 getAttrForMode(width, height, fps);
625 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700626 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700627 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
628 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700629 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
630 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700631 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700632}
633
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800634void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700635 switch (mCurrentMode) {
636 case m640x480p60_4_3:
637 width = 640;
638 height = 480;
639 fps = 60;
640 break;
641 case m720x480p60_4_3:
642 case m720x480p60_16_9:
643 width = 720;
644 height = 480;
645 fps = 60;
646 break;
647 case m720x576p50_4_3:
648 case m720x576p50_16_9:
649 width = 720;
650 height = 576;
651 fps = 50;
652 break;
653 case m1280x720p50_16_9:
654 width = 1280;
655 height = 720;
656 fps = 50;
657 break;
658 case m1280x720p60_16_9:
659 width = 1280;
660 height = 720;
661 fps = 60;
662 break;
663 case m1920x1080p24_16_9:
664 width = 1920;
665 height = 1080;
666 fps = 24;
667 break;
668 case m1920x1080p25_16_9:
669 width = 1920;
670 height = 1080;
671 fps = 25;
672 break;
673 case m1920x1080p30_16_9:
674 width = 1920;
675 height = 1080;
676 fps = 30;
677 break;
678 case m1920x1080p50_16_9:
679 width = 1920;
680 height = 1080;
681 fps = 50;
682 break;
683 case m1920x1080p60_16_9:
684 width = 1920;
685 height = 1080;
686 fps = 60;
687 break;
688 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700689}
690
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700691};