DisplayAnalyzer.cpp revision 1747c24dc5251a0a062ef5f8a6791c30a715e820
165efc253a628175c7afa95c431b746ea20052794Andy Qiu/*
265efc253a628175c7afa95c431b746ea20052794Andy Qiu * Copyright © 2012 Intel Corporation
365efc253a628175c7afa95c431b746ea20052794Andy Qiu * All rights reserved.
465efc253a628175c7afa95c431b746ea20052794Andy Qiu *
565efc253a628175c7afa95c431b746ea20052794Andy Qiu * Permission is hereby granted, free of charge, to any person obtaining a
665efc253a628175c7afa95c431b746ea20052794Andy Qiu * copy of this software and associated documentation files (the "Software"),
765efc253a628175c7afa95c431b746ea20052794Andy Qiu * to deal in the Software without restriction, including without limitation
865efc253a628175c7afa95c431b746ea20052794Andy Qiu * the rights to use, copy, modify, merge, publish, distribute, sublicense,
965efc253a628175c7afa95c431b746ea20052794Andy Qiu * and/or sell copies of the Software, and to permit persons to whom the
1065efc253a628175c7afa95c431b746ea20052794Andy Qiu * Software is furnished to do so, subject to the following conditions:
1165efc253a628175c7afa95c431b746ea20052794Andy Qiu *
1265efc253a628175c7afa95c431b746ea20052794Andy Qiu * The above copyright notice and this permission notice (including the next
1365efc253a628175c7afa95c431b746ea20052794Andy Qiu * paragraph) shall be included in all copies or substantial portions of the
1465efc253a628175c7afa95c431b746ea20052794Andy Qiu * Software.
1565efc253a628175c7afa95c431b746ea20052794Andy Qiu *
1665efc253a628175c7afa95c431b746ea20052794Andy Qiu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1765efc253a628175c7afa95c431b746ea20052794Andy Qiu * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1865efc253a628175c7afa95c431b746ea20052794Andy Qiu * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1965efc253a628175c7afa95c431b746ea20052794Andy Qiu * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2065efc253a628175c7afa95c431b746ea20052794Andy Qiu * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2165efc253a628175c7afa95c431b746ea20052794Andy Qiu * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2265efc253a628175c7afa95c431b746ea20052794Andy Qiu * IN THE SOFTWARE.
2365efc253a628175c7afa95c431b746ea20052794Andy Qiu *
2465efc253a628175c7afa95c431b746ea20052794Andy Qiu * Authors:
2565efc253a628175c7afa95c431b746ea20052794Andy Qiu *    Jackie Li <yaodong.li@intel.com>
2665efc253a628175c7afa95c431b746ea20052794Andy Qiu *
2765efc253a628175c7afa95c431b746ea20052794Andy Qiu */
2865efc253a628175c7afa95c431b746ea20052794Andy Qiu
2965efc253a628175c7afa95c431b746ea20052794Andy Qiu#include <HwcTrace.h>
3065efc253a628175c7afa95c431b746ea20052794Andy Qiu#include <IDisplayDevice.h>
3165efc253a628175c7afa95c431b746ea20052794Andy Qiu#include <DisplayQuery.h>
3265efc253a628175c7afa95c431b746ea20052794Andy Qiu#include <BufferManager.h>
3365efc253a628175c7afa95c431b746ea20052794Andy Qiu#include <DisplayPlaneManager.h>
3465efc253a628175c7afa95c431b746ea20052794Andy Qiu#include <Hwcomposer.h>
3565efc253a628175c7afa95c431b746ea20052794Andy Qiu#include <DisplayAnalyzer.h>
365490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu#include <cutils/properties.h>
37ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi#include <GraphicBuffer.h>
382413102f33fdefa6bdb80ed2c4f56fae83cd5bd8fu jin#include <ExternalDevice.h>
3965efc253a628175c7afa95c431b746ea20052794Andy Qiu
4065efc253a628175c7afa95c431b746ea20052794Andy Qiunamespace android {
4165efc253a628175c7afa95c431b746ea20052794Andy Qiunamespace intel {
4265efc253a628175c7afa95c431b746ea20052794Andy Qiu
4365efc253a628175c7afa95c431b746ea20052794Andy QiuDisplayAnalyzer::DisplayAnalyzer()
4465efc253a628175c7afa95c431b746ea20052794Andy Qiu    : mInitialized(false),
45177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu      mVideoExtModeEnabled(true),
46177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu      mVideoExtModeEligible(false),
47177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu      mVideoExtModeActive(false),
48d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu      mBlankDevice(false),
49eb726af21649d79ed720bdf329e0849270995c45Andy Qiu      mOverlayAllowed(true),
50177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu      mActiveInputState(true),
51eb726af21649d79ed720bdf329e0849270995c45Andy Qiu      mCachedNumDisplays(0),
52eb726af21649d79ed720bdf329e0849270995c45Andy Qiu      mCachedDisplays(0),
53eb726af21649d79ed720bdf329e0849270995c45Andy Qiu      mPendingEvents(),
544ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu      mEventMutex(),
554ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu      mEventHandledCondition()
5665efc253a628175c7afa95c431b746ea20052794Andy Qiu{
5765efc253a628175c7afa95c431b746ea20052794Andy Qiu}
5865efc253a628175c7afa95c431b746ea20052794Andy Qiu
5965efc253a628175c7afa95c431b746ea20052794Andy QiuDisplayAnalyzer::~DisplayAnalyzer()
6065efc253a628175c7afa95c431b746ea20052794Andy Qiu{
6165efc253a628175c7afa95c431b746ea20052794Andy Qiu}
6265efc253a628175c7afa95c431b746ea20052794Andy Qiu
6365efc253a628175c7afa95c431b746ea20052794Andy Qiubool DisplayAnalyzer::initialize()
6465efc253a628175c7afa95c431b746ea20052794Andy Qiu{
655490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    // by default video extended mode is enabled
665490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    char prop[PROPERTY_VALUE_MAX];
675490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    if (property_get("hwc.video.extmode.enable", prop, "1") > 0) {
68177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        mVideoExtModeEnabled = atoi(prop) ? true : false;
695490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    }
70177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    mVideoExtModeEligible = false;
71177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    mVideoExtModeActive = false;
72d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu    mBlankDevice = false;
73eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mOverlayAllowed = true;
74177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    mActiveInputState = true;
75eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mCachedNumDisplays = 0;
76eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mCachedDisplays = 0;
77eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mPendingEvents.clear();
78b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    mVideoStateMap.clear();
7965efc253a628175c7afa95c431b746ea20052794Andy Qiu    mInitialized = true;
80b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie
8165efc253a628175c7afa95c431b746ea20052794Andy Qiu    return true;
8265efc253a628175c7afa95c431b746ea20052794Andy Qiu}
8365efc253a628175c7afa95c431b746ea20052794Andy Qiu
84e2ad4c047651d4442f6a0e002290016d45e9201afu jinvoid DisplayAnalyzer::deinitialize()
8565efc253a628175c7afa95c431b746ea20052794Andy Qiu{
86eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mPendingEvents.clear();
87b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    mVideoStateMap.clear();
8865efc253a628175c7afa95c431b746ea20052794Andy Qiu    mInitialized = false;
8965efc253a628175c7afa95c431b746ea20052794Andy Qiu}
9065efc253a628175c7afa95c431b746ea20052794Andy Qiu
91770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xievoid DisplayAnalyzer::analyzeContents(
92770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie        size_t numDisplays, hwc_display_contents_1_t** displays)
93770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie{
94eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    // cache and use them only in this context during analysis
95eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mCachedNumDisplays = numDisplays;
96eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mCachedDisplays = displays;
97eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
98eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    handlePendingEvents();
99eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
100177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (mVideoExtModeEnabled) {
101177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        handleVideoExtMode();
102770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie    }
103b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
104b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (mBlankDevice) {
105b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        // this will make sure device is blanked after geometry changes.
106b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        // blank event is only processed once
107b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        blankSecondaryDevice();
108b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
109770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie}
110770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie
111177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::handleVideoExtMode()
112770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie{
113177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    bool eligible = mVideoExtModeEligible;
114177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    checkVideoExtMode();
115177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (eligible == mVideoExtModeEligible) {
116177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        if (mVideoExtModeActive) {
117177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu            // need to mark all layers
118177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu            setCompositionType(0, HWC_OVERLAY, false);
119770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie        }
120177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
121770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie    }
122770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie
123177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (mVideoExtModeEligible) {
124177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        if (mActiveInputState) {
125177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu            VTRACE("input is active");
126177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        } else {
127177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu            enterVideoExtMode();
128177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        }
129177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    } else {
130177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        exitVideoExtMode();
131770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie    }
132770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie}
133770346b3b6f55e10c54c8a47d4db8f8907035173Lin Xie
134177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::checkVideoExtMode()
13565efc253a628175c7afa95c431b746ea20052794Andy Qiu{
136b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (mVideoStateMap.size() != 1) {
137177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        mVideoExtModeEligible = false;
138eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        return;
139eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    }
140eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
14165efc253a628175c7afa95c431b746ea20052794Andy Qiu    bool geometryChanged = false;
14265efc253a628175c7afa95c431b746ea20052794Andy Qiu    int activeDisplays = 0;
143d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu
14465efc253a628175c7afa95c431b746ea20052794Andy Qiu    hwc_display_contents_1_t *content = NULL;
145eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    for (int i = 0; i < (int)mCachedNumDisplays; i++) {
146eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        content = mCachedDisplays[i];
14765efc253a628175c7afa95c431b746ea20052794Andy Qiu        if (content == NULL) {
14865efc253a628175c7afa95c431b746ea20052794Andy Qiu            continue;
14965efc253a628175c7afa95c431b746ea20052794Andy Qiu        }
15065efc253a628175c7afa95c431b746ea20052794Andy Qiu        activeDisplays++;
15165efc253a628175c7afa95c431b746ea20052794Andy Qiu        if (content->flags & HWC_GEOMETRY_CHANGED) {
15265efc253a628175c7afa95c431b746ea20052794Andy Qiu            geometryChanged = true;
15365efc253a628175c7afa95c431b746ea20052794Andy Qiu        }
15465efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
15565efc253a628175c7afa95c431b746ea20052794Andy Qiu
15665efc253a628175c7afa95c431b746ea20052794Andy Qiu    if (activeDisplays <= 1) {
157177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        mVideoExtModeEligible = false;
15865efc253a628175c7afa95c431b746ea20052794Andy Qiu        return;
15965efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
16065efc253a628175c7afa95c431b746ea20052794Andy Qiu
161177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    // video state update event may come later than geometry change event.
162177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    // in that case, video extended mode is not detected properly.
163177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu#if 0
16465efc253a628175c7afa95c431b746ea20052794Andy Qiu    if (geometryChanged == false) {
16565efc253a628175c7afa95c431b746ea20052794Andy Qiu        // use previous analysis result
16665efc253a628175c7afa95c431b746ea20052794Andy Qiu        return;
16765efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
168177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu#endif
16965efc253a628175c7afa95c431b746ea20052794Andy Qiu
170177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    // reset eligibility of video extended mode
171177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    mVideoExtModeEligible = false;
17265efc253a628175c7afa95c431b746ea20052794Andy Qiu
17365efc253a628175c7afa95c431b746ea20052794Andy Qiu    // check if there is video layer in the primary device
174eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    content = mCachedDisplays[0];
17565efc253a628175c7afa95c431b746ea20052794Andy Qiu    if (content == NULL) {
17665efc253a628175c7afa95c431b746ea20052794Andy Qiu        return;
17765efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
17865efc253a628175c7afa95c431b746ea20052794Andy Qiu
179d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu    uint32_t videoHandle = 0;
18065efc253a628175c7afa95c431b746ea20052794Andy Qiu    bool videoLayerExist = false;
181319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu    bool videoFullScreenOnPrimary = false;
18265efc253a628175c7afa95c431b746ea20052794Andy Qiu    // exclude the frame buffer target layer
18365efc253a628175c7afa95c431b746ea20052794Andy Qiu    for (int j = 0; j < (int)content->numHwLayers - 1; j++) {
18465efc253a628175c7afa95c431b746ea20052794Andy Qiu        videoLayerExist = isVideoLayer(content->hwLayers[j]);
185bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie        if (videoLayerExist) {
18665efc253a628175c7afa95c431b746ea20052794Andy Qiu            videoHandle = (uint32_t)content->hwLayers[j].handle;
187319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu            videoFullScreenOnPrimary = isVideoFullScreen(0, content->hwLayers[j]);
18865efc253a628175c7afa95c431b746ea20052794Andy Qiu            break;
18965efc253a628175c7afa95c431b746ea20052794Andy Qiu        }
19065efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
19165efc253a628175c7afa95c431b746ea20052794Andy Qiu
19265efc253a628175c7afa95c431b746ea20052794Andy Qiu    if (videoLayerExist == false) {
19365efc253a628175c7afa95c431b746ea20052794Andy Qiu        // no video layer is found in the primary layer
19465efc253a628175c7afa95c431b746ea20052794Andy Qiu        return;
19565efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
19665efc253a628175c7afa95c431b746ea20052794Andy Qiu
19765efc253a628175c7afa95c431b746ea20052794Andy Qiu    // check whether video layer exists in external device or virtual device
19865efc253a628175c7afa95c431b746ea20052794Andy Qiu    // TODO: video may exist in virtual device but no in external device or vice versa
19965efc253a628175c7afa95c431b746ea20052794Andy Qiu    // TODO: multiple video layers are not addressed here
200eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    for (int i = 1; i < (int)mCachedNumDisplays; i++) {
201eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        content = mCachedDisplays[i];
20265efc253a628175c7afa95c431b746ea20052794Andy Qiu        if (content == NULL) {
20365efc253a628175c7afa95c431b746ea20052794Andy Qiu            continue;
20465efc253a628175c7afa95c431b746ea20052794Andy Qiu        }
20565efc253a628175c7afa95c431b746ea20052794Andy Qiu
20665efc253a628175c7afa95c431b746ea20052794Andy Qiu        // exclude the frame buffer target layer
20765efc253a628175c7afa95c431b746ea20052794Andy Qiu        for (int j = 0; j < (int)content->numHwLayers - 1; j++) {
20865efc253a628175c7afa95c431b746ea20052794Andy Qiu            if ((uint32_t)content->hwLayers[j].handle == videoHandle) {
2099332f677fa9b02f55ff910ca08a36eb6f23030d6Andy Qiu                VTRACE("video layer exists in device %d", i);
210319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu                if (videoFullScreenOnPrimary) {
211319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu                    mVideoExtModeEligible = true;
212319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu                } else {
213319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu                    mVideoExtModeEligible = isVideoFullScreen(i, content->hwLayers[j]);
214319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu                }
21565efc253a628175c7afa95c431b746ea20052794Andy Qiu                return;
21665efc253a628175c7afa95c431b746ea20052794Andy Qiu            }
21765efc253a628175c7afa95c431b746ea20052794Andy Qiu        }
21865efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
21965efc253a628175c7afa95c431b746ea20052794Andy Qiu}
22065efc253a628175c7afa95c431b746ea20052794Andy Qiu
221177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiubool DisplayAnalyzer::isVideoExtModeActive()
22265efc253a628175c7afa95c431b746ea20052794Andy Qiu{
223177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    return mVideoExtModeActive;
22465efc253a628175c7afa95c431b746ea20052794Andy Qiu}
22565efc253a628175c7afa95c431b746ea20052794Andy Qiu
226177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiubool DisplayAnalyzer::isVideoExtModeEnabled()
2275490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu{
2285490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu#if 1
2295490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    // enable it for run-time debugging purpose.
2305490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    char prop[PROPERTY_VALUE_MAX];
2315490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    if (property_get("hwc.video.extmode.enable", prop, "1") > 0) {
232177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        mVideoExtModeEnabled = atoi(prop) ? true : false;
2335490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu    }
234177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    ITRACE("video extended mode enabled: %d", mVideoExtModeEnabled);
2355490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu#endif
2365490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu
237177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    return mVideoExtModeEnabled;
2385490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu}
2395490411cb9330d4e92d59878da3693c33a948ad6Andy Qiu
24065efc253a628175c7afa95c431b746ea20052794Andy Qiubool DisplayAnalyzer::isVideoLayer(hwc_layer_1_t &layer)
24165efc253a628175c7afa95c431b746ea20052794Andy Qiu{
24265efc253a628175c7afa95c431b746ea20052794Andy Qiu    bool ret = false;
24365efc253a628175c7afa95c431b746ea20052794Andy Qiu    BufferManager *bm = Hwcomposer::getInstance().getBufferManager();
244eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    if (!layer.handle) {
245eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        return false;
246eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    }
247eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    DataBuffer *buffer = bm->lockDataBuffer((uint32_t)layer.handle);
24865efc253a628175c7afa95c431b746ea20052794Andy Qiu     if (!buffer) {
24965efc253a628175c7afa95c431b746ea20052794Andy Qiu         ETRACE("failed to get buffer");
25065efc253a628175c7afa95c431b746ea20052794Andy Qiu     } else {
25165efc253a628175c7afa95c431b746ea20052794Andy Qiu        ret = DisplayQuery::isVideoFormat(buffer->getFormat());
252eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        bm->unlockDataBuffer(buffer);
25365efc253a628175c7afa95c431b746ea20052794Andy Qiu    }
25465efc253a628175c7afa95c431b746ea20052794Andy Qiu    return ret;
25565efc253a628175c7afa95c431b746ea20052794Andy Qiu}
25665efc253a628175c7afa95c431b746ea20052794Andy Qiu
2570131f123eba74813aca3eaaf99cd418b6b558416Andy Qiubool DisplayAnalyzer::isVideoFullScreen(int device, hwc_layer_1_t &layer)
258bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie{
2590131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu    IDisplayDevice *displayDevice = Hwcomposer::getInstance().getDisplayDevice(device);
2600131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu    if (!displayDevice) {
261bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie        return false;
262bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie    }
2630131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu    int width = 0, height = 0;
2640131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu    if (!displayDevice->getDisplaySize(&width, &height)) {
2650131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu        return false;
2660131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu    }
2670131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu
2689332f677fa9b02f55ff910ca08a36eb6f23030d6Andy Qiu    VTRACE("video left %d, right %d, top %d, bottom %d, device width %d, height %d",
2690131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu        layer.displayFrame.left, layer.displayFrame.right,
2700131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu        layer.displayFrame.top, layer.displayFrame.bottom,
2710131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu        width, height);
272bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie
273319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu    // full-screen defintion:
274319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu    // width of target display frame == width of target device, with 1 pixel of tolerance, or
275319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu    // Height of target display frame == height of target device, with 1 pixel of tolerance, or
276319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu    // width * height of display frame > 90% of width * height of display device, or
277319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu    // any of above condition is met on either primary display or secondary display
278319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu
279bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie    int dstW = layer.displayFrame.right - layer.displayFrame.left;
280bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie    int dstH = layer.displayFrame.bottom - layer.displayFrame.top;
281319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu    if (dstW < width - 1 &&
282319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu        dstH < height - 1 &&
283319e5478ad0ed4a50df6190a7d529e020051eb50Andy Qiu        dstW * dstH * 10 < width * height * 9) {
2849332f677fa9b02f55ff910ca08a36eb6f23030d6Andy Qiu        VTRACE("video is not full-screen");
2850131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu        return false;
286bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie    }
2870131f123eba74813aca3eaaf99cd418b6b558416Andy Qiu    return true;
288bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie}
289bd5ed146ffeb236f3c5cc20b2ebaf6a850b54007Lin Xie
290eb726af21649d79ed720bdf329e0849270995c45Andy Qiubool DisplayAnalyzer::isOverlayAllowed()
291eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
292eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    return mOverlayAllowed;
293eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
294eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
29535fc4846ba9657d6d2638e68d810618fedec7ecdAshish Singhiint DisplayAnalyzer::getVideoInstances()
29635fc4846ba9657d6d2638e68d810618fedec7ecdAshish Singhi{
297b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    return (int)mVideoStateMap.size();
29835fc4846ba9657d6d2638e68d810618fedec7ecdAshish Singhi}
29935fc4846ba9657d6d2638e68d810618fedec7ecdAshish Singhi
300eb726af21649d79ed720bdf329e0849270995c45Andy Qiuvoid DisplayAnalyzer::postHotplugEvent(bool connected)
301eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
302eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    if (!connected) {
303eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        // enable vsync on the primary device immediately
304177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        Hwcomposer::getInstance().getVsyncManager()->enableDynamicVsync(true);
305eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    }
306177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
307177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    // handle hotplug event (vsync switch) asynchronously
308177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Event e;
309177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    e.type = HOTPLUG_EVENT;
310177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    e.bValue = connected;
311177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    postEvent(e);
312177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Hwcomposer::getInstance().invalidate();
313eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
314eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
315b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpevoid DisplayAnalyzer::postVideoEvent(int instanceID, int state)
316eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
317eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    Event e;
318eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    e.type = VIDEO_EVENT;
319b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie    e.videoEvent.instanceID = instanceID;
320b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    e.videoEvent.state = state;
321eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    postEvent(e);
322b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
323b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if ((state == VIDEO_PLAYBACK_STARTING) ||
324b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        (state == VIDEO_PLAYBACK_STOPPING && hasProtectedLayer())) {
3254ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu        Hwcomposer::getInstance().invalidate();
3264ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu        Mutex::Autolock lock(mEventMutex);
3274ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu        // ideally overlay should be disabled in the surface flinger thread, if it is not processed
328b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        // in close to one vsync cycle (50ms)  it will be safely disabled in this thread context
3294ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu        // there is no threading issue
330b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        status_t err = mEventHandledCondition.waitRelative(mEventMutex, milliseconds(50));
3314ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu        if (err == -ETIMEDOUT) {
3324ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu            WTRACE("timeout waiting for event handling");
3334ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu            Hwcomposer::getInstance().getPlaneManager()->disableOverlayPlanes();
3344ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu        }
3354ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu    }
336eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
337eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
338eb726af21649d79ed720bdf329e0849270995c45Andy Qiuvoid DisplayAnalyzer::postBlankEvent(bool blank)
339eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
340eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    Event e;
341eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    e.type = BLANK_EVENT;
342b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    e.bValue = blank;
343eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    postEvent(e);
344d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu    Hwcomposer::getInstance().invalidate();
345d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu}
346d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu
347177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::postInputEvent(bool active)
348177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu{
349177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Event e;
350177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    e.type = INPUT_EVENT;
351177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    e.bValue = active;
352177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    postEvent(e);
353177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Hwcomposer::getInstance().invalidate();
354177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu}
355177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
356466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Huvoid DisplayAnalyzer::postIdleEntryEvent(void)
357466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu{
358466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    Event e;
359466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    e.type = IDLE_ENTRY_EVENT;
360466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    e.nValue = 0;
361466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    postEvent(e);
362466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu}
363466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
364eb726af21649d79ed720bdf329e0849270995c45Andy Qiuvoid DisplayAnalyzer::postEvent(Event& e)
365d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu{
366eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    Mutex::Autolock lock(mEventMutex);
367eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mPendingEvents.add(e);
368eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
369eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
370b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpebool DisplayAnalyzer::getEvent(Event& e)
371eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
372eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    Mutex::Autolock lock(mEventMutex);
373eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    if (mPendingEvents.size() == 0) {
374b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        return false;
375b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
376b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    e = mPendingEvents[0];
377b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    mPendingEvents.removeAt(0);
378b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    return true;
379b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe}
380b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
381b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpevoid DisplayAnalyzer::handlePendingEvents()
382b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe{
383b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    // handle one event per analysis to avoid blocking surface flinger
384b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    // some event may take lengthy time to process
385b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    Event e;
386b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (!getEvent(e)) {
387eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        return;
388eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    }
389eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
390b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    switch (e.type) {
391b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    case HOTPLUG_EVENT:
392b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        handleHotplugEvent(e.bValue);
393b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        break;
394b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    case BLANK_EVENT:
395b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        handleBlankEvent(e.bValue);
396b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        break;
397b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    case VIDEO_EVENT:
398b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        handleVideoEvent(e.videoEvent.instanceID, e.videoEvent.state);
399b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        break;
400b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    case TIMING_EVENT:
401b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        handleTimingEvent();
402b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        break;
403177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    case INPUT_EVENT:
404177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        handleInputEvent(e.bValue);
405177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        break;
406177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    case DPMS_EVENT:
407177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        handleDpmsEvent(e.nValue);
408177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        break;
409466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    case IDLE_ENTRY_EVENT:
410466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        handleIdleEntryEvent(e.nValue);
411466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        break;
412466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    case IDLE_EXIT_EVENT:
413466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        handleIdleExitEvent();
414466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        break;
415eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    }
416eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
417eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
418eb726af21649d79ed720bdf329e0849270995c45Andy Qiuvoid DisplayAnalyzer::handleHotplugEvent(bool connected)
419eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
420466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    if (connected) {
421466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        Hwcomposer::getInstance().getPowerManager()->disableIdleControl();
422466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    } else if (mVideoStateMap.size() == 0) {
423466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        Hwcomposer::getInstance().getPowerManager()->enableIdleControl();
424466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    }
425eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
426eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
427eb726af21649d79ed720bdf329e0849270995c45Andy Qiuvoid DisplayAnalyzer::handleBlankEvent(bool blank)
428eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
429eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    mBlankDevice = blank;
430eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    // force geometry changed in the secondary device to reset layer composition type
431eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    for (int i = 0; i < (int)mCachedNumDisplays; i++) {
432eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        if (i == IDisplayDevice::DEVICE_PRIMARY) {
433eb726af21649d79ed720bdf329e0849270995c45Andy Qiu            continue;
434eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        }
435eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        if (mCachedDisplays[i]) {
436eb726af21649d79ed720bdf329e0849270995c45Andy Qiu            mCachedDisplays[i]->flags |= HWC_GEOMETRY_CHANGED;
437eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        }
438d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu    }
439eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    blankSecondaryDevice();
440eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
441d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu
442b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpevoid DisplayAnalyzer::handleTimingEvent()
443b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie{
444b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie    // check whether external device is connected, reset refresh rate to match video frame rate
445b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie    // if video is in playing state or reset refresh rate to default preferred one if video is not
446b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie    // at playing state
447b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie    Hwcomposer *hwc = &Hwcomposer::getInstance();
448b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    ExternalDevice *dev = NULL;
449b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    dev = (ExternalDevice *)hwc->getDisplayDevice(IDisplayDevice::DEVICE_EXTERNAL);
450b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (!dev) {
451b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie        return;
452b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
453b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
454b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (!dev->isConnected()) {
455b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        return;
456b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
457b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie
458f320f1a53d9adba5885304f3589962a58a1b4e67Lin Xie    if (hwc->getMultiDisplayObserver()->isExternalDeviceTimingFixed()) {
459b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        VTRACE("Timing of external device is fixed.");
460f320f1a53d9adba5885304f3589962a58a1b4e67Lin Xie        return;
461f320f1a53d9adba5885304f3589962a58a1b4e67Lin Xie    }
462f320f1a53d9adba5885304f3589962a58a1b4e67Lin Xie
463853b12aa319e4557a300923590538282c88314beAndy Qiu    int hz = 0;
464b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (mVideoStateMap.size() == 1) {
46584e17af0350016243463b4fe7a82e76524e01651Lin Xie        VideoSourceInfo info;
466b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        int instanceID = mVideoStateMap.keyAt(0);
467b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie        status_t err = hwc->getMultiDisplayObserver()->getVideoSourceInfo(
468b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe                instanceID, &info);
46984e17af0350016243463b4fe7a82e76524e01651Lin Xie        if (err == NO_ERROR) {
470853b12aa319e4557a300923590538282c88314beAndy Qiu            hz = info.frameRate;
471b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie        }
472b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie    }
4732413102f33fdefa6bdb80ed2c4f56fae83cd5bd8fu jin
474b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    dev->setRefreshRate(hz);
475b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie}
476b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie
477b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpevoid DisplayAnalyzer::handleVideoEvent(int instanceID, int state)
478eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
479b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    mVideoStateMap.removeItem(instanceID);
480b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (state != VIDEO_PLAYBACK_STOPPED) {
481b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        mVideoStateMap.add(instanceID, state);
482b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
483b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
484b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    Hwcomposer *hwc = &Hwcomposer::getInstance();
485b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
486b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    // sanity check
487b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (hwc->getMultiDisplayObserver()->getVideoSessionNumber() !=
488b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        (int)mVideoStateMap.size()) {
489b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        WTRACE("session number does not match!!");
490b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        mVideoStateMap.clear();
491b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        if (state != VIDEO_PLAYBACK_STOPPED) {
492b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe            mVideoStateMap.add(instanceID, state);
493b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        }
494b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
495b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
496f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu    // check if composition type needs to be reset
497f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu    bool reset = false;
498b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if ((state == VIDEO_PLAYBACK_STARTING) ||
499b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        (state == VIDEO_PLAYBACK_STOPPING && hasProtectedLayer())) {
500b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        // if video is in starting or stopping stage, overlay use is temporarily not allowed to
501b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        // avoid scrambed RGB overlay if video is protected.
502b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        mOverlayAllowed = false;
503f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu        reset = true;
504b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie
505b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        // disable overlay plane and acknolwdge the waiting thread
506b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        hwc->getPlaneManager()->disableOverlayPlanes();
507b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        mEventHandledCondition.signal();
508b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    } else {
509f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu        reset = !mOverlayAllowed;
510b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        mOverlayAllowed = true;
511b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie    }
512b58f29390918c0df3bd6d0731bf98daf57511b9fLin Xie
513f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu    if (reset) {
514f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu        hwc_display_contents_1_t *content = NULL;
515f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu        for (int i = 0; i < (int)mCachedNumDisplays; i++) {
516177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu            setCompositionType(i, HWC_FRAMEBUFFER, true);
517f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu        }
518f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu    }
519f98aecf56f66cd2501eb621a45658e421b3e3a8cAndy Qiu
520c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang    if (mVideoStateMap.size() == 0) {
521c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang        // reset active input state after video playback stops.
522c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang        // MDS should update input state in 5 seconds after video playback starts
523c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang        mActiveInputState = true;
524c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang    }
525466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
526466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    if (mVideoStateMap.size() > 0) {
527466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        hwc->getPowerManager()->disableIdleControl();
528466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    } else if (!hwc->getDrm()->isConnected(IDisplayDevice::DEVICE_EXTERNAL)) {
529466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu         hwc->getPowerManager()->enableIdleControl();
530466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    }
531466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
532b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    // delay changing timing as it is a lengthy operation
533b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (state == VIDEO_PLAYBACK_STARTED ||
534b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        state == VIDEO_PLAYBACK_STOPPED) {
535b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        Event e;
536b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        e.type = TIMING_EVENT;
537b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        postEvent(e);
5384ffa9f40b823e4c99cd7a21598373969517d93a8Andy Qiu    }
539eb726af21649d79ed720bdf329e0849270995c45Andy Qiu}
540eb726af21649d79ed720bdf329e0849270995c45Andy Qiu
541eb726af21649d79ed720bdf329e0849270995c45Andy Qiuvoid DisplayAnalyzer::blankSecondaryDevice()
542eb726af21649d79ed720bdf329e0849270995c45Andy Qiu{
543d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu    hwc_display_contents_1_t *content = NULL;
544d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu    hwc_layer_1 *layer = NULL;
545eb726af21649d79ed720bdf329e0849270995c45Andy Qiu    for (int i = 0; i < (int)mCachedNumDisplays; i++) {
546d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu        if (i == IDisplayDevice::DEVICE_PRIMARY) {
547d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            continue;
548d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu        }
549eb726af21649d79ed720bdf329e0849270995c45Andy Qiu        content = mCachedDisplays[i];
550d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu        if (content == NULL) {
551d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            continue;
552d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu        }
553d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu
554d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu        for (int j = 0; j < (int)content->numHwLayers - 1; j++) {
555d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            layer = &content->hwLayers[j];
556d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            if (!layer) {
557d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu                continue;
558d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            }
559d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            if (mBlankDevice) {
560d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu                layer->hints |= HWC_HINT_CLEAR_FB;
561d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu                layer->flags &= ~HWC_SKIP_LAYER;
562d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu                layer->compositionType = HWC_OVERLAY;
563d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            } else {
564d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu                layer->hints &= ~HWC_HINT_CLEAR_FB;
565d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu                layer->compositionType = HWC_FRAMEBUFFER;
566d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu            }
567d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu        }
568d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu    }
569d4e9a8ac464ef6fde755c1a8c0902dc02a1270ecAndy Qiu}
57065efc253a628175c7afa95c431b746ea20052794Andy Qiu
571177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::handleInputEvent(bool active)
572177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu{
573c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang    if (active == mActiveInputState) {
574c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang        WTRACE("same input state: %d", active);
575c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang    }
576177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    mActiveInputState = active;
577177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (!mVideoExtModeEligible) {
578177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        ITRACE("not eligible for video extended mode");
579177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
580177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
581177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
582177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (active) {
583177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        exitVideoExtMode();
584177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    } else {
585177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        enterVideoExtMode();
586177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
587177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu}
588177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
589177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::handleDpmsEvent(int delayCount)
590177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu{
591177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (mActiveInputState || !mVideoExtModeEligible) {
592177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        ITRACE("aborting display power off in video extended mode");
593177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
594177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
595177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
596177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (delayCount < DELAY_BEFORE_DPMS_OFF) {
597177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        Event e;
598177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        e.type = DPMS_EVENT;
599177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        e.nValue = delayCount + 1;
600177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        postEvent(e);
601177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
602177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
603177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
604177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (Hwcomposer::getInstance().getVsyncManager()->getVsyncSource() ==
605177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        IDisplayDevice::DEVICE_PRIMARY) {
606177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        ETRACE("primary display is source of vsync, it can't be powered off");
607177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
608177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
609177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
610177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    ITRACE("powering off primary display...");
611177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Hwcomposer::getInstance().getDrm()->setDpmsMode(
612177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        IDisplayDevice::DEVICE_PRIMARY,
613177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        IDisplayDevice::DEVICE_DISPLAY_OFF);
614177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu}
615177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
616466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
617466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Huvoid DisplayAnalyzer::handleIdleEntryEvent(int count)
618466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu{
619466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    DTRACE("handling idle entry event, count %d", count);
620466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    if (hasProtectedLayer()) {
621466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        ITRACE("Ignoring idle entry as protected layer exists.");
622466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        setCompositionType(0, HWC_FRAMEBUFFER, true);
623466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        return;
624466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    }
625466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
626466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    // stop idle entry if external device is connected
627466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    if (mCachedDisplays && mCachedDisplays[IDisplayDevice::DEVICE_EXTERNAL]) {
628466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        ITRACE("Ignoring idle entry as external device is connected.");
629466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        setCompositionType(0, HWC_FRAMEBUFFER, true);
630466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        return;
631466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    }
632466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
633466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    // stop idle entry if video playback is active
634466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    // TODO: remove this check for Annidale
635466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    if (mVideoStateMap.size() > 0) {
636466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        ITRACE("Ignoring idle entry as video session is active.");
637466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        setCompositionType(0, HWC_FRAMEBUFFER, true);
638466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        return;
639466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    }
640466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
641466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    setCompositionType(0, HWC_FORCE_FRAMEBUFFER, true);
642466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
643466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    IPowerManager *pm = Hwcomposer::getInstance().getPowerManager();
644466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    if (count == 0) {
645466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        // ready to enter idel mode
646466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        pm->setIdleReady();
647466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    }
648466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
649466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    if (count >= DELAY_BEFORE_IDLE_ENTRY) {
650466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        pm->enterIdleState();
651466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        // next prepare/set will exit idle state.
652466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        Event e;
653466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        e.type = IDLE_EXIT_EVENT;
654466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        postEvent(e);
655466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    } else {
656466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        // invalidate surface flinger again
657466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        Event e;
658466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        e.type = IDLE_ENTRY_EVENT;
659466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        e.nValue = count + 1;
660466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        postEvent(e);
661466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu        Hwcomposer::getInstance().invalidate();
662466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    }
663466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu}
664466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
665466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Huvoid DisplayAnalyzer::handleIdleExitEvent()
666466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu{
667466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    DTRACE("handling idle exit event");
668466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
669466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    IPowerManager *pm = Hwcomposer::getInstance().getPowerManager();
670466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    pm->exitIdleState();
671466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu    setCompositionType(0, HWC_FRAMEBUFFER, true);
672466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu}
673466748a6f67ce4d1ad8baa944e68a88cf3c86d26Austin Hu
674177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::enterVideoExtMode()
675177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu{
676177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (mVideoExtModeActive) {
677177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        WTRACE("already in video extended mode.");
678177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
679177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
680177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
681177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    ITRACE("entering video extended mode...");
682177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    mVideoExtModeActive = true;
683177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Hwcomposer::getInstance().getVsyncManager()->resetVsyncSource();
684177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
685177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    setCompositionType(0, HWC_OVERLAY, true);
686177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
687177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    // Do not power off primary display immediately as flip is asynchronous
688177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Event e;
689177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    e.type = DPMS_EVENT;
690177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    e.nValue = 0;
691177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    postEvent(e);
692177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu}
693177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
694177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::exitVideoExtMode()
695177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu{
696177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (!mVideoExtModeActive) {
697177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        WTRACE("Not in video extended mode");
698177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
699177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
700177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
701c8ed4d7c744a7f59864fa5a7db8ae3d0c05a119fZhu,Tianyang    ITRACE("exiting video extended mode...");
702177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
703177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    mVideoExtModeActive = false;
704177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
705177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Hwcomposer::getInstance().getDrm()->setDpmsMode(
706177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        IDisplayDevice::DEVICE_PRIMARY,
707177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        IDisplayDevice::DEVICE_DISPLAY_ON);
708177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
709177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    Hwcomposer::getInstance().getVsyncManager()->resetVsyncSource();
710177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
711177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    setCompositionType(0, HWC_FRAMEBUFFER, true);
712177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu}
713177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
714d248396b4f48861e1e27c5e316c776121ab44938mamatha balguribool DisplayAnalyzer::isPresentationLayer(hwc_layer_1_t &layer)
715d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri{
716177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (layer.handle == NULL) {
717177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return false;
718177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
719d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    if (mCachedDisplays == NULL) {
720d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri        return false;
721d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    }
722d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    // check if the given layer exists in the primary device
723d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    hwc_display_contents_1_t *content = mCachedDisplays[0];
724d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    if (content == NULL) {
725d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri        return false;
726d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    }
727d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    for (size_t i = 0; i < content->numHwLayers - 1; i++) {
728d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri        if ((uint32_t)content->hwLayers[i].handle == (uint32_t)layer.handle) {
729d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri            VTRACE("Layer exists for Primary device");
730d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri            return false;
731d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri        }
732d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    }
733d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri    return true;
734d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri}
735d248396b4f48861e1e27c5e316c776121ab44938mamatha balguri
736b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpebool DisplayAnalyzer::hasProtectedLayer()
737b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe{
738b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    DataBuffer * buffer = NULL;
739b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    hwc_display_contents_1_t *content = NULL;
740b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    BufferManager *bm = Hwcomposer::getInstance().getBufferManager();
741b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
742b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (bm == NULL){
743b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        return false;
744b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
745b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
746b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    if (mCachedDisplays == NULL) {
747b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        return false;
748b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
749b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    // check if the given layer exists in the primary device
750b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    for (int index = 0; index < (int)mCachedNumDisplays; index++) {
751b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        content = mCachedDisplays[index];
752b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        if (content == NULL) {
753b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe            continue;
754b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        }
755b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
756b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        for (size_t i = 0; i < content->numHwLayers - 1; i++) {
757b16cf47b6865c4ba53e336558671d11b0325e78bLin Xie            if (isProtectedLayer(content->hwLayers[i]))
758b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe                return true;
759b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe        }
760b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    }
761b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
762b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe    return false;
763b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe}
764b7e04bcc10f0b8aaa698b1ccae40e87664ebef18mahongpe
765ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhibool DisplayAnalyzer::isProtectedLayer(hwc_layer_1_t &layer)
766ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi{
767ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi    if (!layer.handle) {
768177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return false;
769ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi    }
770177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    bool ret = false;
771177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    BufferManager *bm = Hwcomposer::getInstance().getBufferManager();
772ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi    DataBuffer *buffer = bm->lockDataBuffer((uint32_t)layer.handle);
773ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi    if (!buffer) {
774ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi        ETRACE("failed to get buffer");
775ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi    } else {
776ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi        ret = GraphicBuffer::isProtectedBuffer((GraphicBuffer*)buffer);
777ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi        bm->unlockDataBuffer(buffer);
778ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi    }
779ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi    return ret;
780ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi}
781ef111c2d5fb42bfcefca8e774837e12cc1940d80Ashish Singhi
782177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::setCompositionType(hwc_display_contents_1_t *display, int type)
783177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu{
784177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    for (size_t i = 0; i < display->numHwLayers - 1; i++) {
785177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        hwc_layer_1_t *layer = &display->hwLayers[i];
786177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        if (layer) layer->compositionType = type;
787177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
788177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu}
789177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
790177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiuvoid DisplayAnalyzer::setCompositionType(int device, int type, bool reset)
791177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu{
792177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    hwc_display_contents_1_t *content = mCachedDisplays[device];
793177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (content == NULL) {
794177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        ETRACE("Invalid device %d", device);
795177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        return;
796177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
797177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
798177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    // don't need to set geometry changed if layers are just needed to be marked
799177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    if (reset) {
800177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu        content->flags |= HWC_GEOMETRY_CHANGED;
801177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    }
802177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
803177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu    setCompositionType(content, type);
804177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu}
805177b44e0661a92d06f8f37c51e59af86423f7a95Andy Qiu
8061747c24dc5251a0a062ef5f8a6791c30a715e820Zhu,Tianyangint DisplayAnalyzer::getFirstVideoInstanceSessionID() {
8071747c24dc5251a0a062ef5f8a6791c30a715e820Zhu,Tianyang    if (mVideoStateMap.size() >= 1) {
8081747c24dc5251a0a062ef5f8a6791c30a715e820Zhu,Tianyang        return mVideoStateMap.keyAt(0);
8091747c24dc5251a0a062ef5f8a6791c30a715e820Zhu,Tianyang    }
8101747c24dc5251a0a062ef5f8a6791c30a715e820Zhu,Tianyang    return -1;
8111747c24dc5251a0a062ef5f8a6791c30a715e820Zhu,Tianyang}
8121747c24dc5251a0a062ef5f8a6791c30a715e820Zhu,Tianyang
81365efc253a628175c7afa95c431b746ea20052794Andy Qiu} // namespace intel
81465efc253a628175c7afa95c431b746ea20052794Andy Qiu} // namespace android
81565efc253a628175c7afa95c431b746ea20052794Andy Qiu
816