1a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian/*
2a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
4a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * you may not use this file except in compliance with the License.
6a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * You may obtain a copy of the License at
7a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
8a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
10a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Unless required by applicable law or agreed to in writing, software
11a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * See the License for the specific language governing permissions and
14a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * limitations under the License.
15a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian */
16a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#define ATRACE_TAG ATRACE_TAG_GRAPHICS
182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
1992dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn#include <inttypes.h>
2092dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn#include <math.h>
21a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <stdint.h>
22f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdio.h>
23f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdlib.h>
24f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <string.h>
25a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <sys/types.h>
26a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
27a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <utils/Errors.h>
28da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian#include <utils/misc.h>
29399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall#include <utils/NativeHandle.h>
308372785879d329f592f6883620b5a32d80d74691Mathias Agopian#include <utils/String8.h>
313eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <utils/Thread.h>
322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#include <utils/Trace.h>
3322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include <utils/Vector.h>
34a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
35921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <ui/GraphicBuffer.h>
36921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
383eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <hardware/hwcomposer.h>
39a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
401c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall#include <android/configuration.h>
411c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
42a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
43e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian#include <cutils/properties.h>
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
4633ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian
4733ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian#include "../Layer.h"           // needed only for debugging
4833ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian#include "../SurfaceFlinger.h"
49a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
50a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
527296051995fbaea919480a6cc2dabb729dabc95bJesse Hall#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
539eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
549eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
559eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
569eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
579eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
589eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
599eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
609eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
619eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
629eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
639eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
649eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
659eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        uint32_t version) {
669eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
67bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
68bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
69a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
70a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
843eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
853eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
86b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        EventHandler& handler)
87c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
88b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mFbDev(0), mHwc(0), mNumDisplays(1),
893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
90e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
91bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian      mDebugForceFakeVSync(false)
92a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
939e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
94e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
95e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
96b685c542836b93c99cd85053e07696406ea37adbJesse Hall
97bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
98bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mLastHwVSync[i] = 0;
99bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mVSyncCounts[i] = 0;
100bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    }
101bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian
102e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
103e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
104e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
105e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
106028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
1075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
108b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Note: some devices may insist that the FB HAL be opened before HWC.
109ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    int fberr = loadFbHalModule();
110b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadHwcModule();
111bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
112da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
113da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // close FB HAL if we don't needed it.
114da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: this is temporary until we're not forced to open FB HAL
115da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // before HWC.
116da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        framebuffer_close(mFbDev);
117da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mFbDev = NULL;
118da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
119da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
120babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
121babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
122babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden            && !mFbDev) {
123ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall        ALOGE("ERROR: failed to open framebuffer (%s), aborting",
124ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall                strerror(-fberr));
12543601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        abort();
12643601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    }
12743601a2dc320a271ff8c3765ff61414a07221635Andy McFadden
128620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    // these display IDs are always reserved
1299e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
130620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        mAllocatedDisplayIDs.markBit(i);
131620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
132620685c2e684082c82657732d1e35cefd0c79006Andy McFadden
133b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mHwc) {
134b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
135b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 24) & 0xff,
136b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 16) & 0xff);
137b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (mHwc->registerProcs) {
138b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->hwc = this;
139b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.invalidate = &hook_invalidate;
140b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.vsync = &hook_vsync;
141b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
142b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = &hook_hotplug;
143b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            else
144b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = NULL;
145b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
146b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mHwc->registerProcs(mHwc, &mCBContext->procs);
147b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        }
148b685c542836b93c99cd85053e07696406ea37adbJesse Hall
149b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // don't need a vsync thread if we have a hardware composer
150b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        needVSyncThread = false;
151b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // always turn vsync off when we start
15281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
1538f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
154b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // the number of displays we actually have depends on the
155b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // hw composer version
15638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
15738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            // 1.3 adds support for virtual displays
1589e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall            mNumDisplays = MAX_HWC_DISPLAYS;
159b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
160b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.1 adds support for multiple displays
1619e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall            mNumDisplays = NUM_BUILTIN_DISPLAYS;
162b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else {
163b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = 1;
164c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1653a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1663a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
167b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
1681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
1691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                "should only have fbdev if no hwc or hwc is 1.0");
1701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
171f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
17238e623bc5c2d7224ebd5b82efaea401bc8e5367aMathias Agopian        disp.connected = true;
173b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.format = mFbDev->format;
1747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        DisplayConfig config = DisplayConfig();
1757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.width = mFbDev->width;
1767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.height = mFbDev->height;
1777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.xdpi = mFbDev->xdpi;
1787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.ydpi = mFbDev->ydpi;
1797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.refresh = nsecs_t(1e9 / mFbDev->fps);
1807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        disp.configs.push_back(config);
1817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        disp.currentConfig = 0;
1821bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    } else if (mHwc) {
1831604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // here we're guaranteed to have at least HWC 1.1
1849e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall        for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
1851604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            queryDisplayProperties(i);
1861604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
187888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
188888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1893a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1903a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1913a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
192a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
193a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
194a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
195a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
196babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if (mHwc) {
19781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
198babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    }
1993eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
2003eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
2013eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
202a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
204a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
205b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
206b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        framebuffer_close(mFbDev);
207b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
2083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
209a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
210a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
211b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the hardware composer module.  Sets mHwc.
212b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadHwcModule()
213b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
214b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
215b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
216b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
217b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
218b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
219b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
220b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
221b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = hwc_open_1(module, &mHwc);
222b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
223b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device failed to initialize (%s)",
224b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, strerror(-err));
225b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
226b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
227b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
228b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
229b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
230b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
231b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device version %#x unsupported, will not be used",
232b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, mHwc->common.version);
233b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        hwc_close_1(mHwc);
234b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mHwc = NULL;
235b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
236b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
237b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
238b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
239b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the FB HAL, which uses the gralloc module.  Sets mFbDev.
240ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hallint HWComposer::loadFbHalModule()
241b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
242b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
243b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
244ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
245ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    if (err != 0) {
246b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
247ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall        return err;
248b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
249b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
250ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    return framebuffer_open(module, &mFbDev);
251b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
252b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
253a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
254a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
255a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
256a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
257bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
258bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
259bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
260bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
261c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
262c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2631bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
264bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
265bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
266bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
2671bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->vsync(disp, timestamp);
2681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
2691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
2701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
2711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        int connected) {
2721bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
2731bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            const_cast<hwc_procs_t*>(procs));
2741bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->hotplug(disp, connected);
27531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
27631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
277c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
278e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
279c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
280c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2811bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::vsync(int disp, int64_t timestamp) {
282bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
2838e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall        {
2848e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            Mutex::Autolock _l(mLock);
2858e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
2868e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // There have been reports of HWCs that signal several vsync events
2878e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // with the same timestamp when turning the display off and on. This
2888e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // is a bug in the HWC implementation, but filter the extra events
2898e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // out here so they don't cause havoc downstream.
2908e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            if (timestamp == mLastHwVSync[disp]) {
291258ee43fbcbc752cb08c134a12c08d5d99c4a866Michael Lentine                ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
2928e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall                        timestamp);
2938e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall                return;
2948e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            }
2958e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
2968e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            mLastHwVSync[disp] = timestamp;
2978e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall        }
2988e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
299bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        char tag[16];
300faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis        snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
301bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
302bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian
303bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mEventHandler.onVSyncReceived(disp, timestamp);
304bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    }
305d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
306d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
3071bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hotplug(int disp, int connected) {
308afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall    if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
3091bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
3101bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                disp, connected);
3111bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        return;
3121bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
313f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    queryDisplayProperties(disp);
314148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    mEventHandler.onHotplugReceived(disp, bool(connected));
3151bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3161bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
317bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hallstatic float getDefaultDensity(uint32_t width, uint32_t height) {
318bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // Default density is based on TVs: 1080p displays get XHIGH density,
319bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // lower-resolution displays get TV density. Maybe eventually we'll need
320bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // to update it for 4K displays, though hopefully those just report
321bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // accurate DPI information to begin with. This is also used for virtual
322bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // displays and even primary displays with older hwcomposers, so be
323bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // careful about orientation.
324bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall
325bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    uint32_t h = width < height ? width : height;
326bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    if (h >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
327bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    else           return ACONFIGURATION_DENSITY_TV;
3281c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
3291c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
3301bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallstatic const uint32_t DISPLAY_ATTRIBUTES[] = {
3311bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_VSYNC_PERIOD,
332db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_WIDTH,
333db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_HEIGHT,
3341bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_X,
3351bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_Y,
3361bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_NO_ATTRIBUTE,
3371bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall};
3381bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
3391bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3401604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopianstatus_t HWComposer::queryDisplayProperties(int disp) {
3411604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
342da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
3431bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
344b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // use zero as default value for unspecified attributes
3451bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
3461bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    memset(values, 0, sizeof(values));
3471bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3487f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    const size_t MAX_NUM_CONFIGS = 128;
3497f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    uint32_t configs[MAX_NUM_CONFIGS] = {0};
3507f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t numConfigs = MAX_NUM_CONFIGS;
3517f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    status_t err = mHwc->getDisplayConfigs(mHwc, disp, configs, &numConfigs);
3521604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (err != NO_ERROR) {
3531604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // this can happen if an unpluggable display is not connected
354f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        mDisplayData[disp].connected = false;
3551604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return err;
3561bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3571bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3587f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[disp].currentConfig = 0;
3597f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    for (size_t c = 0; c < numConfigs; ++c) {
3607f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
3617f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                DISPLAY_ATTRIBUTES, values);
3627f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (err != NO_ERROR) {
3637f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            // we can't get this display's info. turn it off.
3647f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].connected = false;
3657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            return err;
3667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
3677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
3687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        DisplayConfig config = DisplayConfig();
3697f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
3707f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            switch (DISPLAY_ATTRIBUTES[i]) {
3717f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_VSYNC_PERIOD:
3727f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.refresh = nsecs_t(values[i]);
3737f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_WIDTH:
3757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.width = values[i];
3767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_HEIGHT:
3787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.height = values[i];
3797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_X:
3817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.xdpi = values[i] / 1000.0f;
3827f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3837f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_Y:
3847f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.ydpi = values[i] / 1000.0f;
3857f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3867f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                default:
387b54ee7704e46529e02386cf39eba93a34a527c61Michael Lentine                    ALOG_ASSERT(false, "unknown display attribute[%zu] %#x",
3887f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                            i, DISPLAY_ATTRIBUTES[i]);
3897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3907f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            }
3917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
3921604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
3937f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (config.xdpi == 0.0f || config.ydpi == 0.0f) {
394bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall            float dpi = getDefaultDensity(config.width, config.height);
3957f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.xdpi = dpi;
3967f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.ydpi = dpi;
3971bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3987f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
3997f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        mDisplayData[disp].configs.push_back(config);
4001bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
4011bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
402f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    // FIXME: what should we set the format to?
403f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
404f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].connected = true;
4051604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    return NO_ERROR;
4061bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
4071bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
4081c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hallstatus_t HWComposer::setVirtualDisplayProperties(int32_t id,
4091c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        uint32_t w, uint32_t h, uint32_t format) {
4101c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
4111c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall            !mAllocatedDisplayIDs.hasBit(id)) {
4121c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        return BAD_INDEX;
4131c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    }
4147f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[id].currentConfig;
4151c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    mDisplayData[id].format = format;
4167f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    DisplayConfig& config = mDisplayData[id].configs.editItemAt(configId);
4177f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.width = w;
4187f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.height = h;
419bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    config.xdpi = config.ydpi = getDefaultDensity(w, h);
4201c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    return NO_ERROR;
4211c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
4221c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
423e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
424f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
425e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
426e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
427f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
428f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
4297adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = true;
4307f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].configs.resize(1);
4317f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].currentConfig = 0;
432e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
433e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
434e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
435e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
4369e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    if (id < NUM_BUILTIN_DISPLAYS) {
437f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
438e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
439e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
440f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
441e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
442e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
443f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
4447adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = false;
445e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
446e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
447e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
448b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshTimestamp(int disp) const {
449d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
450d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
451d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
452d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
453d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
4547f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[disp].currentConfig;
4557f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return now - ((now - mLastHwVSync[disp]) %
4567f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].configs[configId].refresh);
457b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
458b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
4592ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennissp<Fence> HWComposer::getDisplayFence(int disp) const {
4602ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis    return mDisplayData[disp].lastDisplayFence;
4612ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis}
4622ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
463b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
46419e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
46519e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return HAL_PIXEL_FORMAT_RGBA_8888;
46619e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    } else {
46719e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return mDisplayData[disp].format;
46819e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    }
4693eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4703eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
4717f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozabool HWComposer::isConnected(int disp) const {
4727f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].connected;
4737f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getWidth(int disp) const {
4767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].width;
4787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getHeight(int disp) const {
4817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4827f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].height;
4837f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4847f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
485b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
4867f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4877f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].xdpi;
4888b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4898b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
490b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
4917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4927f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].ydpi;
4938b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4948b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
4957f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozansecs_t HWComposer::getRefreshPeriod(int disp) const {
4967f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4977f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].refresh;
4987f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4997f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
5007f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozaconst Vector<HWComposer::DisplayConfig>& HWComposer::getConfigs(int disp) const {
5017f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs;
5027f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
5037f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
5047f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozasize_t HWComposer::getCurrentConfig(int disp) const {
5057f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].currentConfig;
506f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
507f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
50881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
50981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
510620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
511620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
512620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
513620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
514620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
515620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
516620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
51781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
51881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
5193eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
52081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
52181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
52281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
52381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
52481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
52581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
52681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
52781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
52881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
52981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
53081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
53181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
53281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
53381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
53481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
5354d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall
5364d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                char tag[16];
5374d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
5384d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                ATRACE_INT(tag, enabled);
53981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
540e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
54181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
54281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
54381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
54481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
5453eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
5463a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
5473a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
5483a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
5493a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
55031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
55131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
5525f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
553f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
5541e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
555e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
5561e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
55745721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
558f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
559da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
560da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
561da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
562da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
56313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
564da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
565f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
566f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
567f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
568f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
56945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
570da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
571da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
572da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
5737f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const DisplayConfig& currentConfig =
5747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    disp.configs[disp.currentConfig];
5757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const hwc_rect_t r = { 0, 0,
5767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    (int) currentConfig.width, (int) currentConfig.height };
577da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
578da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
579da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
580da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
581da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
582da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
5838f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
5848f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.left = 0;
5858f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.top = 0;
5867f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.right =
5877f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.width;
5887f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.bottom =
5897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.height;
5908f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            } else {
5918f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCrop = r;
5928f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            }
593da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
594da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
595da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
596da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
597da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
598da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
59970a6e88dbd52605f35f290eba52a05bdc6fc8042Mathias Agopian            disp.framebufferTarget->planeAlpha = 0xFF;
600da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
601db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
602f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
603f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
604a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
605a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
606a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
607a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
608da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
609da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
610da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
611da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
612da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
613da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
614da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
615da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
616e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian        // triggers a Surface::queueBuffer()  on some
617da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
618da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
619da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
620da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
621da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
622da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
6231df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    if (acquireFence->isValid()) {
624da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
625da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
626da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
627da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
628da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
629da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
634e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
635f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
637da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
638da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
639da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
640da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
641da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
64227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
643da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
644da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
64527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
646258ee43fbcbc752cb08c134a12c08d5d99c4a866Michael Lentine            ALOGW("WARNING: disp %zu: connected, non-null list, layers=%zu",
64727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
64827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
649da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
650f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
65138efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
652f7a675837bdad03d398c9b6f0f593b9c51c679b5Jesse Hall                mLists[i]->outbuf = disp.outbufHandle;
653db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
654db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
655db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
656db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
657db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
658db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
659db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
660db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
661db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
662f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
663f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
664db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
665f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
666da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
667da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
6689c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
6695f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
6705f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
6715f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
672d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        //
673d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // If there are no window layers, we treat the display has having FB
674d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // composition, because SurfaceFlinger will use GLES to draw the
675d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // wormhole region.
676f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
677f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
678f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
679f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
680f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
681f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
682f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
683da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
684da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
685da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
686da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
687f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
688f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
689f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
690f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
691f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
692f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
693f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
694f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
695f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
69603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    if (l.compositionType == HWC_CURSOR_OVERLAY) {
69703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                        disp.hasOvComp = true;
69803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    }
699f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
700d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
701d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                    disp.hasFbComp = true;
702d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                }
703d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall            } else {
704d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                disp.hasFbComp = true;
7059c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
7069c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
7079c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
708a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
709a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
710a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
711e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
71299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
713e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
714e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
715e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
7165f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
717e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
71899c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
71999c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall        return true;
720e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
7219c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
7229c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
72313f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hallsp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
724da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
72513f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        return Fence::NO_FENCE;
726da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
727da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
728da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
729da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
730da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
731da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
732d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
733da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
734da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
735da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
73613f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
737da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
738da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
73930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
7408630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
7418630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
7429eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
74330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
744f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
745f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
74630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
74730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
74830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
749f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
750afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall        for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
75180e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            DisplayData& disp(mDisplayData[i]);
752851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall            if (disp.outbufHandle) {
753851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                mLists[i]->outbuf = disp.outbufHandle;
75480e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall                mLists[i]->outbufAcquireFenceFd =
755851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                        disp.outbufAcquireFence->dup();
75680e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            }
75780e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall        }
75880e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall
75930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
760f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
761f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
762f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
7632ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis            disp.lastDisplayFence = disp.lastRetireFence;
7641df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis            disp.lastRetireFence = Fence::NO_FENCE;
765f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
766db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
7672ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis                    disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
768db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
769f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
770f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
771f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
7728630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
77358959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
774a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
775a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
776a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
7772c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malanistatus_t HWComposer::setPowerMode(int disp, int mode) {
778afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
7797ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
7802c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (mode == HWC_POWER_MODE_OFF) {
7812c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            eventControl(disp, HWC_EVENT_VSYNC, 0);
7822c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
7832c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
7842c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->setPowerMode(mHwc, disp, mode);
7852c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        } else {
7862c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->blank(mHwc, disp,
7872c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani                    mode == HWC_POWER_MODE_OFF ? 1 : 0);
7882c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
78910fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
79010fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
79110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
79210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
7936c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentinestatus_t HWComposer::setActiveConfig(int disp, int mode) {
7946c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
795d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    DisplayData& dd(mDisplayData[disp]);
796d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    dd.currentConfig = mode;
7976c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
7986c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        return (status_t)mHwc->setActiveConfig(mHwc, disp, mode);
7996c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    } else {
8006c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        LOG_FATAL_IF(mode != 0);
8016c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    }
8026c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    return NO_ERROR;
8036c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine}
8046c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine
80527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
8065a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
80727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
808851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    free(dd.list);
809851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.list = NULL;
810851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.framebufferTarget = NULL;    // points into dd.list
811851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.fbTargetHandle = NULL;
812851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufHandle = NULL;
813851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastRetireFence = Fence::NO_FENCE;
814851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastDisplayFence = Fence::NO_FENCE;
815851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufAcquireFence = Fence::NO_FENCE;
816949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // clear all the previous configs and repopulate when a new
817949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // device is added
818949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    dd.configs.clear();
81927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
82027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
821cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
822d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
823da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
824da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
825da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
826da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
827da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
828cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
829cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
830cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
831b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
832b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
833