external.cpp revision 848d0d35701cacacee11129a08cff4bd7b8f7f26
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, The Linux Foundation. All rights reserved.
4 *
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
21#define DEBUG 0
22#include <ctype.h>
23#include <fcntl.h>
24#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>
34#include <sys/resource.h>
35#include <cutils/properties.h>
36#include "hwc_utils.h"
37#include "external.h"
38#include "overlayUtils.h"
39#include "overlay.h"
40
41using namespace android;
42
43namespace qhwc {
44
45#define MAX_FRAME_BUFFER_NAME_SIZE      (80)
46#define MAX_DISPLAY_DEVICES             (3)
47
48
49const char* msmFbDevicePath[] = {  "/dev/graphics/fb1",
50                                   "/dev/graphics/fb2"};
51
52/*
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    readCEUnderscanInfo();
89    readResolution();
90    // TODO: Move this to activate
91    /* 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);
100    setDpyHdmiAttr();
101    setExternalDisplay(true, mHdmiFbNum);
102    // set system property
103    property_set("hw.hdmiON", "1");
104    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);
133        // unset system property
134        property_set("hw.hdmiON", "0");
135    }
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
149int ExternalDisplay::ignoreRequest(const char *str) {
150    const char *s1 = str + strlen("change@/devices/virtual/switch/");
151    if(!strncmp(s1,"wfd",strlen(s1))) {
152        if(mConnectedFbNum == mHdmiFbNum) {
153            ALOGE("Ignore wfd event when HDMI is active");
154            return true;
155        }
156    }
157    return false;
158}
159
160
161ExternalDisplay::ExternalDisplay(hwc_context_t* ctx):mFd(-1),
162    mCurrentMode(-1), mConnected(0), mConnectedFbNum(0), mModeCount(0),
163    mUnderscanSupported(false), mHwcContext(ctx), mHdmiFbNum(-1),
164    mWfdFbNum(-1), mExtDpyNum(HWC_DISPLAY_EXTERNAL)
165{
166    memset(&mVInfo, 0, sizeof(mVInfo));
167    //Determine the fb index for external display devices.
168    updateExtDispDevFbIndex();
169    // disable HPD at start, it will be enabled later
170    // when the display powers on
171    // This helps for framework reboot or adb shell stop/start
172    writeHPDOption(0);
173
174}
175
176void ExternalDisplay::setEDIDMode(int resMode) {
177    ALOGD_IF(DEBUG,"resMode=%d ", resMode);
178    {
179        Mutex::Autolock lock(mExtDispLock);
180        setExternalDisplay(false);
181        openFrameBuffer(mHdmiFbNum);
182        setResolution(resMode);
183    }
184    setExternalDisplay(true, mHdmiFbNum);
185}
186
187void ExternalDisplay::setHPD(uint32_t startEnd) {
188    ALOGD_IF(DEBUG,"HPD enabled=%d", startEnd);
189    writeHPDOption(startEnd);
190}
191
192void ExternalDisplay::setActionSafeDimension(int w, int h) {
193    ALOGD_IF(DEBUG,"ActionSafe w=%d h=%d", w, h);
194    Mutex::Autolock lock(mExtDispLock);
195    char actionsafeWidth[PROPERTY_VALUE_MAX];
196    char actionsafeHeight[PROPERTY_VALUE_MAX];
197    sprintf(actionsafeWidth, "%d", w);
198    property_set("hw.actionsafe.width", actionsafeWidth);
199    sprintf(actionsafeHeight, "%d", h);
200    property_set("hw.actionsafe.height", actionsafeHeight);
201    setExternalDisplay(true, mHdmiFbNum);
202}
203
204int ExternalDisplay::getModeCount() const {
205    ALOGD_IF(DEBUG,"HPD mModeCount=%d", mModeCount);
206    Mutex::Autolock lock(mExtDispLock);
207    return mModeCount;
208}
209
210void ExternalDisplay::getEDIDModes(int *out) const {
211    Mutex::Autolock lock(mExtDispLock);
212    for(int i = 0;i < mModeCount;i++) {
213        out[i] = mEDIDModes[i];
214    }
215}
216
217void ExternalDisplay::readCEUnderscanInfo()
218{
219    int hdmiScanInfoFile = -1;
220    int len = -1;
221    char scanInfo[17];
222    char *ce_info_str = NULL;
223    const char token[] = ", \n";
224    int ce_info = -1;
225    char sysFsScanInfoFilePath[128];
226    sprintf(sysFsScanInfoFilePath, "/sys/devices/virtual/graphics/fb%d/"
227                                   "scan_info", mHdmiFbNum);
228
229    memset(scanInfo, 0, sizeof(scanInfo));
230    hdmiScanInfoFile = open(sysFsScanInfoFilePath, O_RDONLY, 0);
231    if (hdmiScanInfoFile < 0) {
232        ALOGD_IF(DEBUG, "%s: scan_info file '%s' not found",
233                                __FUNCTION__, sysFsScanInfoFilePath);
234        return;
235    } else {
236        len = read(hdmiScanInfoFile, scanInfo, sizeof(scanInfo)-1);
237        ALOGD("%s: Scan Info string: %s length = %d",
238                 __FUNCTION__, scanInfo, len);
239        if (len <= 0) {
240            close(hdmiScanInfoFile);
241            ALOGE("%s: Scan Info file empty '%s'",
242                                __FUNCTION__, sysFsScanInfoFilePath);
243            return;
244        }
245        scanInfo[len] = '\0';  /* null terminate the string */
246    }
247    close(hdmiScanInfoFile);
248
249    /*
250     * The scan_info contains the three fields
251     * PT - preferred video format
252     * IT - video format
253     * CE video format - containing the underscan support information
254     */
255
256    /* PT */
257    ce_info_str = strtok(scanInfo, token);
258    if (ce_info_str) {
259        /* IT */
260        ce_info_str = strtok(NULL, token);
261        if (ce_info_str) {
262            /* CE */
263            ce_info_str = strtok(NULL, token);
264            if (ce_info_str)
265                ce_info = atoi(ce_info_str);
266        }
267    }
268
269    if (ce_info_str) {
270        // ce_info contains the underscan information
271        if (ce_info == EXT_SCAN_ALWAYS_UNDERSCANED ||
272            ce_info == EXT_SCAN_BOTH_SUPPORTED)
273            // if TV supported underscan, then driver will always underscan
274            // hence no need to apply action safe rectangle
275            mUnderscanSupported = true;
276    } else {
277        ALOGE("%s: scan_info string error", __FUNCTION__);
278    }
279
280    // Store underscan support info in a system property
281    const char* prop = (mUnderscanSupported) ? "1" : "0";
282    property_set("hw.underscan_supported", prop);
283    return;
284}
285
286ExternalDisplay::~ExternalDisplay()
287{
288    closeFrameBuffer();
289}
290
291struct disp_mode_timing_type {
292    int  video_format;
293
294    int  active_h;
295    int  active_v;
296
297    int  front_porch_h;
298    int  pulse_width_h;
299    int  back_porch_h;
300
301    int  front_porch_v;
302    int  pulse_width_v;
303    int  back_porch_v;
304
305    int  pixel_freq;
306    bool interlaced;
307
308    void set_info(struct fb_var_screeninfo &info) const;
309};
310
311void disp_mode_timing_type::set_info(struct fb_var_screeninfo &info) const
312{
313    info.reserved[0] = 0;
314    info.reserved[1] = 0;
315    info.reserved[2] = 0;
316#ifndef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
317    info.reserved[3] = (info.reserved[3] & 0xFFFF) | (video_format << 16);
318#endif
319    info.xoffset = 0;
320    info.yoffset = 0;
321    info.xres = active_h;
322    info.yres = active_v;
323
324    info.pixclock = pixel_freq*1000;
325    info.vmode = interlaced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
326
327    info.right_margin = front_porch_h;
328    info.hsync_len = pulse_width_h;
329    info.left_margin = back_porch_h;
330    info.lower_margin = front_porch_v;
331    info.vsync_len = pulse_width_v;
332    info.upper_margin = back_porch_v;
333}
334
335/* Video formates supported by the HDMI Standard */
336/* Indicates the resolution, pix clock and the aspect ratio */
337#define m640x480p60_4_3         1
338#define m720x480p60_4_3         2
339#define m720x480p60_16_9        3
340#define m1280x720p60_16_9       4
341#define m1920x1080i60_16_9      5
342#define m1440x480i60_4_3        6
343#define m1440x480i60_16_9       7
344#define m1920x1080p60_16_9      16
345#define m720x576p50_4_3         17
346#define m720x576p50_16_9        18
347#define m1280x720p50_16_9       19
348#define m1440x576i50_4_3        21
349#define m1440x576i50_16_9       22
350#define m1920x1080p50_16_9      31
351#define m1920x1080p24_16_9      32
352#define m1920x1080p25_16_9      33
353#define m1920x1080p30_16_9      34
354
355static struct disp_mode_timing_type supported_video_mode_lut[] = {
356    {m640x480p60_4_3,     640,  480,  16,  96,  48, 10, 2, 33,  25200, false},
357    {m720x480p60_4_3,     720,  480,  16,  62,  60,  9, 6, 30,  27030, false},
358    {m720x480p60_16_9,    720,  480,  16,  62,  60,  9, 6, 30,  27030, false},
359    {m1280x720p60_16_9,  1280,  720, 110,  40, 220,  5, 5, 20,  74250, false},
360    {m1920x1080i60_16_9, 1920,  540,  88,  44, 148,  2, 5,  5,  74250, false},
361    {m1440x480i60_4_3,   1440,  240,  38, 124, 114,  4, 3, 15,  27000, true},
362    {m1440x480i60_16_9,  1440,  240,  38, 124, 114,  4, 3, 15,  27000, true},
363    {m1920x1080p60_16_9, 1920, 1080,  88,  44, 148,  4, 5, 36, 148500, false},
364    {m720x576p50_4_3,     720,  576,  12,  64,  68,  5, 5, 39,  27000, false},
365    {m720x576p50_16_9,    720,  576,  12,  64,  68,  5, 5, 39,  27000, false},
366    {m1280x720p50_16_9,  1280,  720, 440,  40, 220,  5, 5, 20,  74250, false},
367    {m1440x576i50_4_3,   1440,  288,  24, 126, 138,  2, 3, 19,  27000, true},
368    {m1440x576i50_16_9,  1440,  288,  24, 126, 138,  2, 3, 19,  27000, true},
369    {m1920x1080p50_16_9, 1920, 1080, 528,  44, 148,  4, 5, 36, 148500, false},
370    {m1920x1080p24_16_9, 1920, 1080, 638,  44, 148,  4, 5, 36,  74250, false},
371    {m1920x1080p25_16_9, 1920, 1080, 528,  44, 148,  4, 5, 36,  74250, false},
372    {m1920x1080p30_16_9, 1920, 1080,  88,  44, 148,  4, 5, 36,  74250, false},
373};
374
375int ExternalDisplay::parseResolution(char* edidStr, int* edidModes)
376{
377    char delim = ',';
378    int count = 0;
379    char *start, *end;
380    // EDIDs are string delimited by ','
381    // Ex: 16,4,5,3,32,34,1
382    // Parse this string to get mode(int)
383    start = (char*) edidStr;
384    end = &delim;
385    while(*end == delim) {
386        edidModes[count] = (int) strtol(start, &end, 10);
387        start = end+1;
388        count++;
389    }
390    ALOGD_IF(DEBUG, "In %s: count = %d", __FUNCTION__, count);
391    for (int i = 0; i < count; i++)
392        ALOGD_IF(DEBUG, "Mode[%d] = %d", i, edidModes[i]);
393    return count;
394}
395
396bool ExternalDisplay::readResolution()
397{
398    char sysFsEDIDFilePath[255];
399    sprintf(sysFsEDIDFilePath , "/sys/devices/virtual/graphics/fb%d/edid_modes",
400            mHdmiFbNum);
401
402    int hdmiEDIDFile = open(sysFsEDIDFilePath, O_RDONLY, 0);
403    int len = -1;
404
405    if (hdmiEDIDFile < 0) {
406        ALOGE("%s: edid_modes file '%s' not found",
407                 __FUNCTION__, sysFsEDIDFilePath);
408        return false;
409    } else {
410        len = read(hdmiEDIDFile, mEDIDs, sizeof(mEDIDs)-1);
411        ALOGD_IF(DEBUG, "%s: EDID string: %s length = %d",
412                 __FUNCTION__, mEDIDs, len);
413        if ( len <= 0) {
414            ALOGE("%s: edid_modes file empty '%s'",
415                     __FUNCTION__, sysFsEDIDFilePath);
416        }
417        else {
418            while (len > 1 && isspace(mEDIDs[len-1]))
419                --len;
420            mEDIDs[len] = 0;
421        }
422    }
423    close(hdmiEDIDFile);
424    if(len > 0) {
425        // Get EDID modes from the EDID strings
426        mModeCount = parseResolution(mEDIDs, mEDIDModes);
427        ALOGD_IF(DEBUG, "%s: mModeCount = %d", __FUNCTION__,
428                 mModeCount);
429    }
430
431    return (strlen(mEDIDs) > 0);
432}
433
434bool ExternalDisplay::openFrameBuffer(int fbNum)
435{
436    if (mFd == -1) {
437        mFd = open(msmFbDevicePath[fbNum-1], O_RDWR);
438        if (mFd < 0)
439            ALOGE("%s: %s is not available", __FUNCTION__,
440                                            msmFbDevicePath[fbNum-1]);
441        if(mHwcContext) {
442            mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
443        }
444    }
445    return (mFd > 0);
446}
447
448bool ExternalDisplay::closeFrameBuffer()
449{
450    int ret = 0;
451    if(mFd >= 0) {
452        ret = close(mFd);
453        mFd = -1;
454    }
455    if(mHwcContext) {
456        mHwcContext->dpyAttr[mExtDpyNum].fd = mFd;
457    }
458    return (ret == 0);
459}
460
461// clears the vinfo, edid, best modes
462void ExternalDisplay::resetInfo()
463{
464    memset(&mVInfo, 0, sizeof(mVInfo));
465    memset(mEDIDs, 0, sizeof(mEDIDs));
466    memset(mEDIDModes, 0, sizeof(mEDIDModes));
467    mModeCount = 0;
468    mCurrentMode = -1;
469    mUnderscanSupported = false;
470    // Reset the underscan supported system property
471    const char* prop = "0";
472    property_set("hw.underscan_supported", prop);
473}
474
475int ExternalDisplay::getModeOrder(int mode)
476{
477    // XXX: We dont support interlaced modes but having
478    // it here for for future
479    switch (mode) {
480        default:
481        case m1440x480i60_4_3:
482            return 1; // 480i 4:3
483        case m1440x480i60_16_9:
484            return 2; // 480i 16:9
485        case m1440x576i50_4_3:
486            return 3; // i576i 4:3
487        case m1440x576i50_16_9:
488            return 4; // 576i 16:9
489        case m640x480p60_4_3:
490            return 5; // 640x480 4:3
491        case m720x480p60_4_3:
492            return 6; // 480p 4:3
493        case m720x480p60_16_9:
494            return 7; // 480p 16:9
495        case m720x576p50_4_3:
496            return 8; // 576p 4:3
497        case m720x576p50_16_9:
498            return 9; // 576p 16:9
499        case m1920x1080i60_16_9:
500            return 10; // 1080i 16:9
501        case m1280x720p50_16_9:
502            return 11; // 720p@50Hz
503        case m1280x720p60_16_9:
504            return 12; // 720p@60Hz
505        case m1920x1080p24_16_9:
506            return 13; //1080p@24Hz
507        case m1920x1080p25_16_9:
508            return 14; //108-p@25Hz
509        case m1920x1080p30_16_9:
510            return 15; //1080p@30Hz
511        case m1920x1080p50_16_9:
512            return 16; //1080p@50Hz
513        case m1920x1080p60_16_9:
514            return 17; //1080p@60Hz
515    }
516}
517
518/// Returns the user mode set(if any) using adb shell
519int ExternalDisplay::getUserMode() {
520    /* Based on the property set the resolution */
521    char property_value[PROPERTY_VALUE_MAX];
522    property_get("hw.hdmi.resolution", property_value, "-1");
523    int mode = atoi(property_value);
524    // We dont support interlaced modes
525    if(isValidMode(mode) && !isInterlacedMode(mode)) {
526        ALOGD_IF(DEBUG, "%s: setting the HDMI mode = %d", __FUNCTION__, mode);
527        return mode;
528    }
529    return -1;
530}
531
532// Get the best mode for the current HD TV
533int ExternalDisplay::getBestMode() {
534    int bestOrder = 0;
535    int bestMode = m640x480p60_4_3;
536    Mutex::Autolock lock(mExtDispLock);
537    // for all the edid read, get the best mode
538    for(int i = 0; i < mModeCount; i++) {
539        int mode = mEDIDModes[i];
540        int order = getModeOrder(mode);
541        if (order > bestOrder) {
542            bestOrder = order;
543            bestMode = mode;
544        }
545    }
546    return bestMode;
547}
548
549inline bool ExternalDisplay::isValidMode(int ID)
550{
551    bool valid = false;
552    for (int i = 0; i < mModeCount; i++) {
553        if(ID == mEDIDModes[i]) {
554            valid = true;
555            break;
556        }
557    }
558    return valid;
559}
560
561// returns true if the mode(ID) is interlaced mode format
562bool ExternalDisplay::isInterlacedMode(int ID) {
563    bool interlaced = false;
564    switch(ID) {
565        case m1440x480i60_4_3:
566        case m1440x480i60_16_9:
567        case m1440x576i50_4_3:
568        case m1440x576i50_16_9:
569        case m1920x1080i60_16_9:
570            interlaced = true;
571        default:
572            interlaced = false;
573    }
574    return interlaced;
575}
576
577void ExternalDisplay::setResolution(int ID)
578{
579    struct fb_var_screeninfo info;
580    int ret = 0;
581    ret = ioctl(mFd, FBIOGET_VSCREENINFO, &mVInfo);
582    if(ret < 0) {
583        ALOGD("In %s: FBIOGET_VSCREENINFO failed Err Str = %s", __FUNCTION__,
584                                                            strerror(errno));
585    }
586
587    ALOGD_IF(DEBUG, "%s: GET Info<ID=%d %dx%d (%d,%d,%d),"
588            "(%d,%d,%d) %dMHz>", __FUNCTION__,
589            mVInfo.reserved[3], mVInfo.xres, mVInfo.yres,
590            mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
591            mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
592            mVInfo.pixclock/1000/1000);
593    //If its a new ID - update var_screeninfo
594    if ((isValidMode(ID)) && mCurrentMode != ID) {
595        const struct disp_mode_timing_type *mode =
596            &supported_video_mode_lut[0];
597        unsigned count =  sizeof(supported_video_mode_lut)/sizeof
598            (*supported_video_mode_lut);
599        for (unsigned int i = 0; i < count; ++i) {
600            const struct disp_mode_timing_type *cur =
601                &supported_video_mode_lut[i];
602            if (cur->video_format == ID)
603                mode = cur;
604        }
605        mode->set_info(mVInfo);
606        ALOGD_IF(DEBUG, "%s: SET Info<ID=%d => Info<ID=%d %dx %d"
607                 "(%d,%d,%d), (%d,%d,%d) %dMHz>", __FUNCTION__, ID,
608                 mode->video_format, mVInfo.xres, mVInfo.yres,
609                 mVInfo.right_margin, mVInfo.hsync_len, mVInfo.left_margin,
610                 mVInfo.lower_margin, mVInfo.vsync_len, mVInfo.upper_margin,
611                 mVInfo.pixclock/1000/1000);
612#ifdef FB_METADATA_VIDEO_INFO_CODE_SUPPORT
613        struct msmfb_metadata metadata;
614        memset(&metadata, 0 , sizeof(metadata));
615        metadata.op = metadata_op_vic;
616        metadata.data.video_info_code = mode->video_format;
617        if (ioctl(mFd, MSMFB_METADATA_SET, &metadata) == -1) {
618            ALOGD("In %s: MSMFB_METADATA_SET failed Err Str = %s",
619                                                 __FUNCTION__, strerror(errno));
620        }
621#endif
622        mVInfo.activate = FB_ACTIVATE_NOW | FB_ACTIVATE_ALL | FB_ACTIVATE_FORCE;
623        ret = ioctl(mFd, FBIOPUT_VSCREENINFO, &mVInfo);
624        if(ret < 0) {
625            ALOGD("In %s: FBIOPUT_VSCREENINFO failed Err Str = %s",
626                                                 __FUNCTION__, strerror(errno));
627        }
628        mCurrentMode = ID;
629    }
630}
631
632void ExternalDisplay::setExternalDisplay(bool connected, int extFbNum)
633{
634    hwc_context_t* ctx = mHwcContext;
635    if(ctx) {
636        ALOGD_IF(DEBUG, "%s: connected = %d", __FUNCTION__, connected);
637        // Store the external display
638        mConnected = connected;
639        mConnectedFbNum = extFbNum;
640        mHwcContext->dpyAttr[mExtDpyNum].connected = connected;
641        // Update external fb number in Overlay context
642        overlay::Overlay::getInstance()->setExtFbNum(extFbNum);
643    }
644}
645
646int ExternalDisplay::getExtFbNum(int &fbNum) {
647    int ret = -1;
648    if(mConnected) {
649        fbNum = mConnectedFbNum;
650        ret = 0;
651    }
652    return ret;
653}
654
655bool ExternalDisplay::writeHPDOption(int userOption) const
656{
657    bool ret = true;
658    char sysFsHPDFilePath[255];
659    sprintf(sysFsHPDFilePath ,"/sys/devices/virtual/graphics/fb%d/hpd",
660                                mHdmiFbNum);
661    int hdmiHPDFile = open(sysFsHPDFilePath,O_RDWR, 0);
662    if (hdmiHPDFile < 0) {
663        ALOGE("%s: state file '%s' not found : ret%d err str: %s", __FUNCTION__,
664                                sysFsHPDFilePath, hdmiHPDFile, strerror(errno));
665        ret = false;
666    } else {
667        int err = -1;
668        ALOGD_IF(DEBUG, "%s: option = %d", __FUNCTION__, userOption);
669        if(userOption)
670            err = write(hdmiHPDFile, "1", 2);
671        else
672            err = write(hdmiHPDFile, "0" , 2);
673        if (err <= 0) {
674            ALOGE("%s: file write failed '%s'", __FUNCTION__, sysFsHPDFilePath);
675            ret = false;
676        }
677        close(hdmiHPDFile);
678    }
679    return ret;
680}
681
682void ExternalDisplay::setDpyWfdAttr() {
683    if(mHwcContext) {
684        mHwcContext->dpyAttr[mExtDpyNum].xres = mVInfo.xres;
685        mHwcContext->dpyAttr[mExtDpyNum].yres = mVInfo.yres;
686        mHwcContext->dpyAttr[mExtDpyNum].vsync_period =
687                1000000000l /60;
688        ALOGD_IF(DEBUG,"%s: wfd...connected..!",__FUNCTION__);
689    }
690}
691
692void ExternalDisplay::setDpyHdmiAttr() {
693    int width = 0, height = 0, fps = 0;
694    getAttrForMode(width, height, fps);
695    if(mHwcContext) {
696        ALOGD("ExtDisplay setting xres = %d, yres = %d", width, height);
697        mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = width;
698        mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = height;
699        mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].vsync_period =
700            1000000000l / fps;
701    }
702}
703
704void ExternalDisplay::getAttrForMode(int& width, int& height, int& fps) {
705    switch (mCurrentMode) {
706        case m640x480p60_4_3:
707            width = 640;
708            height = 480;
709            fps = 60;
710            break;
711        case m720x480p60_4_3:
712        case m720x480p60_16_9:
713            width = 720;
714            height = 480;
715            fps = 60;
716            break;
717        case m720x576p50_4_3:
718        case m720x576p50_16_9:
719            width = 720;
720            height = 576;
721            fps = 50;
722            break;
723        case m1280x720p50_16_9:
724            width = 1280;
725            height = 720;
726            fps = 50;
727            break;
728        case m1280x720p60_16_9:
729            width = 1280;
730            height = 720;
731            fps = 60;
732            break;
733        case m1920x1080p24_16_9:
734            width = 1920;
735            height = 1080;
736            fps = 24;
737            break;
738        case m1920x1080p25_16_9:
739            width = 1920;
740            height = 1080;
741            fps = 25;
742            break;
743        case m1920x1080p30_16_9:
744            width = 1920;
745            height = 1080;
746            fps = 30;
747            break;
748        case m1920x1080p50_16_9:
749            width = 1920;
750            height = 1080;
751            fps = 50;
752            break;
753        case m1920x1080p60_16_9:
754            width = 1920;
755            height = 1080;
756            fps = 60;
757            break;
758    }
759}
760
761};
762