blob: 1a41775d8f1cb1e1f50ce8875977b587f0a78cd8 [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();
149 }else if(!strncmp(s1,"wfd",strlen(s1))) {
150 // wfd online event..!
151 configureWFDDisplay();
152 }
153}
154
155void ExternalDisplay::processUEventOffline(const char *str) {
156 const char *s1 = str + strlen("change@/devices/virtual/switch/");
157 if(!strncmp(s1,"hdmi",strlen(s1))) {
158 teardownHDMIDisplay();
159 }else if(!strncmp(s1,"wfd",strlen(s1))) {
160 teardownWFDDisplay();
161 }
162}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700163
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700164ExternalDisplay::ExternalDisplay(hwc_context_t* ctx):mFd(-1),
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800165 mCurrentMode(-1), mConnected(0), mConnectedFbNum(0), mModeCount(0),
166 mHwcContext(ctx), mHdmiFbNum(-1), mWfdFbNum(-1)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700167{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700168 memset(&mVInfo, 0, sizeof(mVInfo));
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800169 //Determine the fb index for external display devices.
170 updateExtDispDevFbIndex();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700171}
172
Saurabh Shah56f610d2012-08-07 15:27:06 -0700173void ExternalDisplay::setEDIDMode(int resMode) {
174 ALOGD_IF(DEBUG,"resMode=%d ", resMode);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700175 {
176 Mutex::Autolock lock(mExtDispLock);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800177 setExternalDisplay(false);
178 openFrameBuffer(mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700179 setResolution(resMode);
180 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800181 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700182}
183
184void ExternalDisplay::setHPD(uint32_t startEnd) {
185 ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
186 writeHPDOption(startEnd);
187}
188
189void ExternalDisplay::setActionSafeDimension(int w, int h) {
190 ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
191 Mutex::Autolock lock(mExtDispLock);
192 overlay::utils::ActionSafe::getInstance()->setDimension(w, h);
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800193 setExternalDisplay(true, mHdmiFbNum);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700194}
195
196int ExternalDisplay::getModeCount() const {
197 ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
198 Mutex::Autolock lock(mExtDispLock);
199 return mModeCount;
200}
201
202void ExternalDisplay::getEDIDModes(int *out) const {
203 Mutex::Autolock lock(mExtDispLock);
204 for(int i = 0;i < mModeCount;i++) {
205 out[i] = mEDIDModes[i];
206 }
207}
208
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700209ExternalDisplay::~ExternalDisplay()
210{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700211 closeFrameBuffer();
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700212}
213
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700214struct disp_mode_timing_type {
215 int video_format;
216
217 int active_h;
218 int active_v;
219
220 int front_porch_h;
221 int pulse_width_h;
222 int back_porch_h;
223
224 int front_porch_v;
225 int pulse_width_v;
226 int back_porch_v;
227
228 int pixel_freq;
229 bool interlaced;
230
231 void set_info(struct fb_var_screeninfo &info) const;
232};
233
234void disp_mode_timing_type::set_info(struct fb_var_screeninfo &info) const
235{
236 info.reserved[0] = 0;
237 info.reserved[1] = 0;
238 info.reserved[2] = 0;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700239 info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700240
241 info.xoffset = 0;
242 info.yoffset = 0;
243 info.xres = active_h;
244 info.yres = active_v;
245
246 info.pixclock = pixel_freq*1000;
247 info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
248
249 info.right_margin = front_porch_h;
250 info.hsync_len = pulse_width_h;
251 info.left_margin = back_porch_h;
252 info.lower_margin = front_porch_v;
253 info.vsync_len = pulse_width_v;
254 info.upper_margin = back_porch_v;
255}
256
257/* Video formates supported by the HDMI Standard */
258/* Indicates the resolution, pix clock and the aspect ratio */
259#define m640x480p60_4_3 1
260#define m720x480p60_4_3 2
261#define m720x480p60_16_9 3
262#define m1280x720p60_16_9 4
263#define m1920x1080i60_16_9 5
264#define m1440x480i60_4_3 6
265#define m1440x480i60_16_9 7
266#define m1920x1080p60_16_9 16
267#define m720x576p50_4_3 17
268#define m720x576p50_16_9 18
269#define m1280x720p50_16_9 19
270#define m1440x576i50_4_3 21
271#define m1440x576i50_16_9 22
272#define m1920x1080p50_16_9 31
273#define m1920x1080p24_16_9 32
274#define m1920x1080p25_16_9 33
275#define m1920x1080p30_16_9 34
276
277static struct disp_mode_timing_type supported_video_mode_lut[] = {
278 {m640x480p60_4_3, 640, 480, 16, 96, 48, 10, 2, 33, 25200, false},
279 {m720x480p60_4_3, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
280 {m720x480p60_16_9, 720, 480, 16, 62, 60, 9, 6, 30, 27030, false},
281 {m1280x720p60_16_9, 1280, 720, 110, 40, 220, 5, 5, 20, 74250, false},
282 {m1920x1080i60_16_9, 1920, 540, 88, 44, 148, 2, 5, 5, 74250, false},
283 {m1440x480i60_4_3, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
284 {m1440x480i60_16_9, 1440, 240, 38, 124, 114, 4, 3, 15, 27000, true},
285 {m1920x1080p60_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 148500, false},
286 {m720x576p50_4_3, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
287 {m720x576p50_16_9, 720, 576, 12, 64, 68, 5, 5, 39, 27000, false},
288 {m1280x720p50_16_9, 1280, 720, 440, 40, 220, 5, 5, 20, 74250, false},
289 {m1440x576i50_4_3, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
290 {m1440x576i50_16_9, 1440, 288, 24, 126, 138, 2, 3, 19, 27000, true},
291 {m1920x1080p50_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 148500, false},
292 {m1920x1080p24_16_9, 1920, 1080, 638, 44, 148, 4, 5, 36, 74250, false},
293 {m1920x1080p25_16_9, 1920, 1080, 528, 44, 148, 4, 5, 36, 74250, false},
294 {m1920x1080p30_16_9, 1920, 1080, 88, 44, 148, 4, 5, 36, 74250, false},
295};
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700296
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700297int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700298{
299 char delim = ',';
300 int count = 0;
301 char *start, *end;
302 // EDIDs are string delimited by ','
303 // Ex: 16,4,5,3,32,34,1
304 // Parse this string to get mode(int)
305 start = (char*) edidStr;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700306 end = &delim;
307 while(*end == delim) {
308 edidModes[count] = (int) strtol(start, &end, 10);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700309 start = end+1;
310 count++;
311 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700312 ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
313 for (int i = 0; i < count; i++)
314 ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700315 return count;
316}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700317
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700318bool ExternalDisplay::readResolution()
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700319{
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800320 char sysFsEDIDFilePath[255];
321 sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
322 mHdmiFbNum);
323
324 int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700325 int len = -1;
326
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700327 if (hdmiEDIDFile < 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700328 ALOGE("%s: edid_modes file '%s' not found",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800329 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700330 return false;
331 } else {
332 len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700333 ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
334 __FUNCTION__, mEDIDs, len);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700335 if ( len <= 0) {
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700336 ALOGE("%s: edid_modes file empty '%s'",
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800337 __FUNCTION__, sysFsEDIDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700338 }
339 else {
340 while (len > 1 && isspace(mEDIDs[len-1]))
341 --len;
342 mEDIDs[len] = 0;
343 }
344 }
345 close(hdmiEDIDFile);
346 if(len > 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800347 // Get EDID modes from the EDID strings
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700348 mModeCount = parseResolution(mEDIDs, mEDIDModes);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700349 ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
350 mModeCount);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700351 }
352
353 return (strlen(mEDIDs) > 0);
354}
355
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800356bool ExternalDisplay::openFrameBuffer(int fbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700357{
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700358 if (mFd == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800359 mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700360 if (mFd < 0)
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800361 ALOGE("%s: %s is not available", __FUNCTION__,
362 msmFbDevicePath[fbNum-1]);
363 if(mHwcContext) {
364 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
365 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700366 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700367 return (mFd > 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700368}
369
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700370bool ExternalDisplay::closeFrameBuffer()
371{
372 int ret = 0;
373 if(mFd > 0) {
374 ret = close(mFd);
375 mFd = -1;
376 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700377 if(mHwcContext) {
378 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].fd = mFd;
379 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700380 return (ret == 0);
381}
382
383// clears the vinfo, edid, best modes
384void ExternalDisplay::resetInfo()
385{
386 memset(&mVInfo, 0, sizeof(mVInfo));
387 memset(mEDIDs, 0, sizeof(mEDIDs));
388 memset(mEDIDModes, 0, sizeof(mEDIDModes));
389 mModeCount = 0;
390 mCurrentMode = -1;
391}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700392
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700393int ExternalDisplay::getModeOrder(int mode)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700394{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800395 // XXX: We dont support interlaced modes but having
396 // it here for for future
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700397 switch (mode) {
398 default:
399 case m1440x480i60_4_3:
400 return 1; // 480i 4:3
401 case m1440x480i60_16_9:
402 return 2; // 480i 16:9
403 case m1440x576i50_4_3:
404 return 3; // i576i 4:3
405 case m1440x576i50_16_9:
406 return 4; // 576i 16:9
407 case m640x480p60_4_3:
408 return 5; // 640x480 4:3
409 case m720x480p60_4_3:
410 return 6; // 480p 4:3
411 case m720x480p60_16_9:
412 return 7; // 480p 16:9
413 case m720x576p50_4_3:
414 return 8; // 576p 4:3
415 case m720x576p50_16_9:
416 return 9; // 576p 16:9
417 case m1920x1080i60_16_9:
418 return 10; // 1080i 16:9
419 case m1280x720p50_16_9:
420 return 11; // 720p@50Hz
421 case m1280x720p60_16_9:
422 return 12; // 720p@60Hz
423 case m1920x1080p24_16_9:
424 return 13; //1080p@24Hz
425 case m1920x1080p25_16_9:
426 return 14; //108-p@25Hz
427 case m1920x1080p30_16_9:
428 return 15; //1080p@30Hz
429 case m1920x1080p50_16_9:
430 return 16; //1080p@50Hz
431 case m1920x1080p60_16_9:
432 return 17; //1080p@60Hz
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700433 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700434}
435
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800436/// Returns the user mode set(if any) using adb shell
437int ExternalDisplay::getUserMode() {
438 /* Based on the property set the resolution */
439 char property_value[PROPERTY_VALUE_MAX];
440 property_get("hdmi.resolution", property_value, "-1");
441 int mode = atoi(property_value);
442 // We dont support interlaced modes
443 if(isValidMode(mode) && !isInterlacedMode(mode)) {
444 ALOGD_IF("%s: setting the HDMI mode = %d", __FUNCTION__, mode);
445 return mode;
446 }
447 return -1;
448}
449
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700450// Get the best mode for the current HD TV
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700451int ExternalDisplay::getBestMode() {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700452 int bestOrder = 0;
453 int bestMode = m640x480p60_4_3;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700454 Mutex::Autolock lock(mExtDispLock);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700455 // for all the edid read, get the best mode
456 for(int i = 0; i < mModeCount; i++) {
457 int mode = mEDIDModes[i];
458 int order = getModeOrder(mode);
459 if (order > bestOrder) {
460 bestOrder = order;
461 bestMode = mode;
462 }
463 }
464 return bestMode;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700465}
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700466
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700467inline bool ExternalDisplay::isValidMode(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700468{
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800469 bool valid = false;
470 for (int i = 0; i < mModeCount; i++) {
471 if(ID == mEDIDModes[i]) {
472 valid = true;
473 break;
474 }
475 }
476 return valid;
477}
478
479// returns true if the mode(ID) is interlaced mode format
480bool ExternalDisplay::isInterlacedMode(int ID) {
481 bool interlaced = false;
482 switch(ID) {
483 case m1440x480i60_4_3:
484 case m1440x480i60_16_9:
485 case m1440x576i50_4_3:
486 case m1440x576i50_16_9:
487 case m1920x1080i60_16_9:
488 interlaced = true;
489 default:
490 interlaced = false;
491 }
492 return interlaced;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700493}
494
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700495void ExternalDisplay::setResolution(int ID)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700496{
497 struct fb_var_screeninfo info;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700498 int ret = 0;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700499 ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
500 if(ret < 0) {
501 ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
502 strerror(errno));
503 }
504
505 ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
506 "(%d,%d,%d) %dMHz>", __FUNCTION__,
507 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
508 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
509 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
510 mVInfo.pixclock/1000/1000);
Arun Kumar K.R37552c52012-12-10 12:47:18 -0800511 //If its a new ID - update var_screeninfo
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700512 if ((isValidMode(ID)) && mCurrentMode != ID) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700513 const struct disp_mode_timing_type *mode =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700514 &supported_video_mode_lut[0];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700515 unsigned count = sizeof(supported_video_mode_lut)/sizeof
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700516 (*supported_video_mode_lut);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700517 for (unsigned int i = 0; i < count; ++i) {
518 const struct disp_mode_timing_type *cur =
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700519 &supported_video_mode_lut[i];
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700520 if (cur->video_format == ID)
521 mode = cur;
522 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700523 mode->set_info(mVInfo);
524 ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700525 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700526 mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
527 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
528 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
529 mVInfo.pixclock/1000/1000);
530 mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
531 ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
532 if(ret < 0) {
533 ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
534 __FUNCTION__, strerror(errno));
535 }
536 mCurrentMode = ID;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700537 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700538}
539
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800540void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700541{
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700542 hwc_context_t* ctx = mHwcContext;
543 if(ctx) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800544 ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700545 // Store the external display
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800546 mConnected = connected;
547 mConnectedFbNum = extFbNum;
548 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].connected = connected;
549 // Update external fb number in Overlay context
550 overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700551 }
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800552}
553
554int ExternalDisplay::getExtFbNum(int &fbNum) {
555 int ret = -1;
556 if(mConnected) {
557 fbNum = mConnectedFbNum;
558 ret = 0;
559 }
560 return ret;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700561}
562
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700563bool ExternalDisplay::writeHPDOption(int userOption) const
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700564{
565 bool ret = true;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800566 char sysFsHPDFilePath[255];
567 sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
568 mHdmiFbNum);
569 int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700570 if (hdmiHPDFile < 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800571 ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
572 sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700573 ret = false;
574 } else {
575 int err = -1;
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800576 ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700577 if(userOption)
578 err = write(hdmiHPDFile, "1", 2);
579 else
580 err = write(hdmiHPDFile, "0" , 2);
581 if (err <= 0) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800582 ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700583 ret = false;
584 }
585 close(hdmiHPDFile);
586 }
587 return ret;
588}
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700589
Saurabh Shah3825f252012-09-21 16:32:18 -0700590/*
591 * commits the changes to the external display
Saurabh Shah3825f252012-09-21 16:32:18 -0700592 */
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700593bool ExternalDisplay::post()
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700594{
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500595 if(mFd == -1)
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700596 return false;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500597 struct mdp_display_commit ext_commit;
Naseer Ahmed9da84662012-12-06 19:29:29 -0500598 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
599 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500600 if (ioctl(mFd, MSMFB_DISPLAY_COMMIT, &ext_commit) == -1) {
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800601 ALOGE("%s: MSMFB_DISPLAY_COMMIT for external failed, str: %s",
602 __FUNCTION__, strerror(errno));
603 return false;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700604 }
605 return true;
606}
607
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800608void ExternalDisplay::setDpyWfdAttr() {
609 if(mHwcContext) {
610 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = mVInfo.xres;
611 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = mVInfo.yres;
612 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
613 1000000000l /60;
614 ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
615 }
616}
617
618void ExternalDisplay::setDpyHdmiAttr() {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700619 int width = 0, height = 0, fps = 0;
620 getAttrForMode(width, height, fps);
621 if(mHwcContext) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700622 ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700623 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
624 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700625 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
626 1000000000l / fps;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700627 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700628}
629
Amara Venkata Mastan Manoj Kumar5182a782012-12-03 12:08:48 -0800630void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700631 switch (mCurrentMode) {
632 case m640x480p60_4_3:
633 width = 640;
634 height = 480;
635 fps = 60;
636 break;
637 case m720x480p60_4_3:
638 case m720x480p60_16_9:
639 width = 720;
640 height = 480;
641 fps = 60;
642 break;
643 case m720x576p50_4_3:
644 case m720x576p50_16_9:
645 width = 720;
646 height = 576;
647 fps = 50;
648 break;
649 case m1280x720p50_16_9:
650 width = 1280;
651 height = 720;
652 fps = 50;
653 break;
654 case m1280x720p60_16_9:
655 width = 1280;
656 height = 720;
657 fps = 60;
658 break;
659 case m1920x1080p24_16_9:
660 width = 1920;
661 height = 1080;
662 fps = 24;
663 break;
664 case m1920x1080p25_16_9:
665 width = 1920;
666 height = 1080;
667 fps = 25;
668 break;
669 case m1920x1080p30_16_9:
670 width = 1920;
671 height = 1080;
672 fps = 30;
673 break;
674 case m1920x1080p50_16_9:
675 width = 1920;
676 height = 1080;
677 fps = 50;
678 break;
679 case m1920x1080p60_16_9:
680 width = 1920;
681 height = 1080;
682 fps = 60;
683 break;
684 }
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700685}
686
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700687};