blob: ddcdd1869f43f7619f6abaa1a6613fe8eb374751 [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;
Ken Zhang7b03a952013-01-16 13:23:48 -0500243#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
Saurabh Shah56f610d2012-08-07 15:27:06 -0700244 info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
Ken Zhang7b03a952013-01-16 13:23:48 -0500245#endif
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700246 info.xoffset = 0;
247 info.yoffset = 0;
248 info.xres = active_h;
249 info.yres = active_v;
250
251 info.pixclock = pixel_freq*1000;
252 info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
253
254 info.right_margin = front_porch_h;
255 info.hsync_len = pulse_width_h;
256 info.left_margin = back_porch_h;
257 info.lower_margin = front_porch_v;
258 info.vsync_len = pulse_width_v;
259 info.upper_margin = back_porch_v;
260}
261
262/* Video formates supported by the HDMI Standard */
263/* Indicates the resolution, pix clock and the aspect ratio */
264#define m640x480p60_4_3 1
265#define m720x480p60_4_3 2
266#define m720x480p60_16_9 3
267#define m1280x720p60_16_9 4
268#define m1920x1080i60_16_9 5
269#define m1440x480i60_4_3 6
270#define m1440x480i60_16_9 7
271#define m1920x1080p60_16_9 16
272#define m720x576p50_4_3 17
273#define m720x576p50_16_9 18
274#define m1280x720p50_16_9 19
275#define m1440x576i50_4_3 21
276#define m1440x576i50_16_9 22
277#define m1920x1080p50_16_9 31
278#define m1920x1080p24_16_9 32
279#define m1920x1080p25_16_9 33
280#define m1920x1080p30_16_9 34
281
282static struct disp_mode_timing_type supported_video_mode_lut[] = {
283 {m640x480p60_4_3, 640, 480, 16, 96, 48, 10, 2, 33, 25200, false},
284 {m720x480p60_4_3, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
285 {m720x480p60_16_9, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
286 {m1280x720p60_16_9, 1280, 720, 110, 40, 220, 5, 5, 20, 74250, false},
287 {m1920x1080i60_16_9, 1920, 540, 88, 44, 148, 2, 5, 5, 74250, false},
288 {m1440x480i60_4_3, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
289 {m1440x480i60_16_9, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
290 {m1920x1080p60_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 148500, false},
291 {m720x576p50_4_3, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
292 {m720x576p50_16_9, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
293 {m1280x720p50_16_9, 1280, 720, 440, 40, 220, 5, 5, 20, 74250, false},
294 {m1440x576i50_4_3, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
295 {m1440x576i50_16_9, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
296 {m1920x1080p50_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 148500, false},
297 {m1920x1080p24_16_9, 1920, 1080, 638, 44, 148, 4, 5, 36, 74250, false},
298 {m1920x1080p25_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 74250, false},
299 {m1920x1080p30_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 74250, false},
300};
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700301
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700302int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700303{
304 char delim = ',';
305 int count = 0;
306 char *start, *end;
307 // EDIDs are string delimited by ','
308 // Ex: 16,4,5,3,32,34,1
309 // Parse this string to get mode(int)
310 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700311 end = &delim;
312 while(*end == delim) {
313 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700314 start = end+1;
315 count++;
316 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700317 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
318 for (int i = 0; i < count; i++)
319 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700320 return count;
321}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700322
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700323bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700324{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800325 char sysFsEDIDFilePath[255];
326 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
327 mHdmiFbNum);
328
329 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700330 int len = -1;
331
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700332 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700333 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800334 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700335 return false;
336 } else {
337 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700338 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
339 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700340 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700341 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800342 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700343 }
344 else {
345 while (len > 1 && isspace(mEDIDs[len-1]))
346 --len;
347 mEDIDs[len] = 0;
348 }
349 }
350 close(hdmiEDIDFile);
351 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800352 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700353 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700354 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
355 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700356 }
357
358 return (strlen(mEDIDs) > 0);
359}
360
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800361bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700362{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700363 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800364 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700365 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800366 ALOGE("%s: %s is not available", __FUNCTION__,
367 msmFbDevicePath[fbNum-1]);
368 if(mHwcContext) {
369 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
370 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700371 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700372 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700373}
374
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700375bool ExternalDisplay::closeFrameBuffer()
376{
377 int ret = 0;
378 if(mFd > 0) {
379 ret = close(mFd);
380 mFd = -1;
381 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700382 if(mHwcContext) {
383 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
384 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700385 return (ret == 0);
386}
387
388// clears the vinfo, edid, best modes
389void ExternalDisplay::resetInfo()
390{
391 memset(&mVInfo, 0, sizeof(mVInfo));
392 memset(mEDIDs, 0, sizeof(mEDIDs));
393 memset(mEDIDModes, 0, sizeof(mEDIDModes));
394 mModeCount = 0;
395 mCurrentMode = -1;
396}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700397
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700398int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700399{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800400 // XXX: We dont support interlaced modes but having
401 // it here for for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700402 switch (mode) {
403 default:
404 case m1440x480i60_4_3:
405 return 1; // 480i 4:3
406 case m1440x480i60_16_9:
407 return 2; // 480i 16:9
408 case m1440x576i50_4_3:
409 return 3; // i576i 4:3
410 case m1440x576i50_16_9:
411 return 4; // 576i 16:9
412 case m640x480p60_4_3:
413 return 5; // 640x480 4:3
414 case m720x480p60_4_3:
415 return 6; // 480p 4:3
416 case m720x480p60_16_9:
417 return 7; // 480p 16:9
418 case m720x576p50_4_3:
419 return 8; // 576p 4:3
420 case m720x576p50_16_9:
421 return 9; // 576p 16:9
422 case m1920x1080i60_16_9:
423 return 10; // 1080i 16:9
424 case m1280x720p50_16_9:
425 return 11; // 720p@50Hz
426 case m1280x720p60_16_9:
427 return 12; // 720p@60Hz
428 case m1920x1080p24_16_9:
429 return 13; //1080p@24Hz
430 case m1920x1080p25_16_9:
431 return 14; //108-p@25Hz
432 case m1920x1080p30_16_9:
433 return 15; //1080p@30Hz
434 case m1920x1080p50_16_9:
435 return 16; //1080p@50Hz
436 case m1920x1080p60_16_9:
437 return 17; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700438 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700439}
440
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800441/// Returns the user mode set(if any) using adb shell
442int ExternalDisplay::getUserMode() {
443 /* Based on the property set the resolution */
444 char property_value[PROPERTY_VALUE_MAX];
445 property_get("hdmi.resolution", property_value, "-1");
446 int mode = atoi(property_value);
447 // We dont support interlaced modes
448 if(isValidMode(mode) && !isInterlacedMode(mode)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500449 ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800450 return mode;
451 }
452 return -1;
453}
454
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700455// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700456int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700457 int bestOrder = 0;
458 int bestMode = m640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700459 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700460 // for all the edid read, get the best mode
461 for(int i = 0; i < mModeCount; i++) {
462 int mode = mEDIDModes[i];
463 int order = getModeOrder(mode);
464 if (order > bestOrder) {
465 bestOrder = order;
466 bestMode = mode;
467 }
468 }
469 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700470}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700471
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700472inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700473{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800474 bool valid = false;
475 for (int i = 0; i < mModeCount; i++) {
476 if(ID == mEDIDModes[i]) {
477 valid = true;
478 break;
479 }
480 }
481 return valid;
482}
483
484// returns true if the mode(ID) is interlaced mode format
485bool ExternalDisplay::isInterlacedMode(int ID) {
486 bool interlaced = false;
487 switch(ID) {
488 case m1440x480i60_4_3:
489 case m1440x480i60_16_9:
490 case m1440x576i50_4_3:
491 case m1440x576i50_16_9:
492 case m1920x1080i60_16_9:
493 interlaced = true;
494 default:
495 interlaced = false;
496 }
497 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700498}
499
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700500void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700501{
502 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700503 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700504 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
505 if(ret < 0) {
506 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
507 strerror(errno));
508 }
509
510 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
511 "(%d,%d,%d) %dMHz>", __FUNCTION__,
512 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
513 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
514 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
515 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800516 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700517 if ((isValidMode(ID)) && mCurrentMode != ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700518 const struct disp_mode_timing_type *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700519 &supported_video_mode_lut[0];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700520 unsigned count = sizeof(supported_video_mode_lut)/sizeof
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700521 (*supported_video_mode_lut);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700522 for (unsigned int i = 0; i < count; ++i) {
523 const struct disp_mode_timing_type *cur =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700524 &supported_video_mode_lut[i];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700525 if (cur->video_format == ID)
526 mode = cur;
527 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700528 mode->set_info(mVInfo);
529 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700530 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700531 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
532 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
533 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
534 mVInfo.pixclock/1000/1000);
535 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
536 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
537 if(ret < 0) {
538 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
539 __FUNCTION__, strerror(errno));
540 }
Ken Zhang7b03a952013-01-16 13:23:48 -0500541#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
542 struct msmfb_metadata metadata;
543 memset(&metadata, 0 , sizeof(metadata));
544 metadata.op = metadata_op_vic;
545 metadata.data.video_info_code = mode->video_format;
546 if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
547 ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
548 __FUNCTION__, strerror(errno));
549 }
550#endif
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700551 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700552 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700553}
554
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800555void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700556{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700557 hwc_context_t* ctx = mHwcContext;
558 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800559 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700560 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800561 mConnected = connected;
562 mConnectedFbNum = extFbNum;
563 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].connected = connected;
564 // Update external fb number in Overlay context
565 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700566 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800567}
568
569int ExternalDisplay::getExtFbNum(int &fbNum) {
570 int ret = -1;
571 if(mConnected) {
572 fbNum = mConnectedFbNum;
573 ret = 0;
574 }
575 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700576}
577
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700578bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700579{
580 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800581 char sysFsHPDFilePath[255];
582 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
583 mHdmiFbNum);
584 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700585 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800586 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
587 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700588 ret = false;
589 } else {
590 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800591 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700592 if(userOption)
593 err = write(hdmiHPDFile, "1", 2);
594 else
595 err = write(hdmiHPDFile, "0" , 2);
596 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800597 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700598 ret = false;
599 }
600 close(hdmiHPDFile);
601 }
602 return ret;
603}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700604
Saurabh Shah3825f252012-09-21 16:32:18 -0700605/*
606 * commits the changes to the external display
Saurabh Shah3825f252012-09-21 16:32:18 -0700607 */
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700608bool ExternalDisplay::post()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700609{
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500610 if(mFd == -1)
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700611 return false;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500612 struct mdp_display_commit ext_commit;
Naseer Ahmed9da84662012-12-06 19:29:29 -0500613 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
614 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500615 if (ioctl(mFd, MSMFB_DISPLAY_COMMIT, &ext_commit) == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800616 ALOGE("%s: MSMFB_DISPLAY_COMMIT for external failed, str: %s",
617 __FUNCTION__, strerror(errno));
618 return false;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700619 }
620 return true;
621}
622
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800623void ExternalDisplay::setDpyWfdAttr() {
624 if(mHwcContext) {
625 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = mVInfo.xres;
626 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = mVInfo.yres;
627 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
628 1000000000l /60;
629 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
630 }
631}
632
633void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700634 int width = 0, height = 0, fps = 0;
635 getAttrForMode(width, height, fps);
636 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700637 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700638 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
639 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700640 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
641 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700642 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700643}
644
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800645void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700646 switch (mCurrentMode) {
647 case m640x480p60_4_3:
648 width = 640;
649 height = 480;
650 fps = 60;
651 break;
652 case m720x480p60_4_3:
653 case m720x480p60_16_9:
654 width = 720;
655 height = 480;
656 fps = 60;
657 break;
658 case m720x576p50_4_3:
659 case m720x576p50_16_9:
660 width = 720;
661 height = 576;
662 fps = 50;
663 break;
664 case m1280x720p50_16_9:
665 width = 1280;
666 height = 720;
667 fps = 50;
668 break;
669 case m1280x720p60_16_9:
670 width = 1280;
671 height = 720;
672 fps = 60;
673 break;
674 case m1920x1080p24_16_9:
675 width = 1920;
676 height = 1080;
677 fps = 24;
678 break;
679 case m1920x1080p25_16_9:
680 width = 1920;
681 height = 1080;
682 fps = 25;
683 break;
684 case m1920x1080p30_16_9:
685 width = 1920;
686 height = 1080;
687 fps = 30;
688 break;
689 case m1920x1080p50_16_9:
690 width = 1920;
691 height = 1080;
692 fps = 50;
693 break;
694 case m1920x1080p60_16_9:
695 width = 1920;
696 height = 1080;
697 fps = 60;
698 break;
699 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700700}
701
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700702};