HWComposer.cpp revision d814cf2a3e3a2fdb73efa80539fe8af0a93da1db
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) {
30810613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    if (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);
31410613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    // Do not teardown or recreate the primary display
31510613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    if (disp != HWC_DISPLAY_PRIMARY) {
31610613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine        mEventHandler.onHotplugReceived(disp, bool(connected));
31710613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    }
3181bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3191bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
320bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hallstatic float getDefaultDensity(uint32_t width, uint32_t height) {
321bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // Default density is based on TVs: 1080p displays get XHIGH density,
322bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // lower-resolution displays get TV density. Maybe eventually we'll need
323bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // to update it for 4K displays, though hopefully those just report
324bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // accurate DPI information to begin with. This is also used for virtual
325bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // displays and even primary displays with older hwcomposers, so be
326bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // careful about orientation.
327bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall
328bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    uint32_t h = width < height ? width : height;
329bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    if (h >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
330bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    else           return ACONFIGURATION_DENSITY_TV;
3311c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
3321c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
3331bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallstatic const uint32_t DISPLAY_ATTRIBUTES[] = {
3341bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_VSYNC_PERIOD,
335db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_WIDTH,
336db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_HEIGHT,
3371bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_X,
3381bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_Y,
339f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza    HWC_DISPLAY_COLOR_TRANSFORM,
3401bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_NO_ATTRIBUTE,
3411bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall};
3421bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
3431bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
344f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stozastatic const uint32_t PRE_HWC15_DISPLAY_ATTRIBUTES[] = {
345f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza    HWC_DISPLAY_VSYNC_PERIOD,
346f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza    HWC_DISPLAY_WIDTH,
347f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza    HWC_DISPLAY_HEIGHT,
348f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza    HWC_DISPLAY_DPI_X,
349f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza    HWC_DISPLAY_DPI_Y,
350f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza    HWC_DISPLAY_NO_ATTRIBUTE,
351f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza};
352f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza
3531604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopianstatus_t HWComposer::queryDisplayProperties(int disp) {
3541604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
355da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
3561bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
357b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // use zero as default value for unspecified attributes
3581bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
3591bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    memset(values, 0, sizeof(values));
3601bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3617f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    const size_t MAX_NUM_CONFIGS = 128;
3627f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    uint32_t configs[MAX_NUM_CONFIGS] = {0};
3637f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t numConfigs = MAX_NUM_CONFIGS;
3647f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    status_t err = mHwc->getDisplayConfigs(mHwc, disp, configs, &numConfigs);
3651604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (err != NO_ERROR) {
3661604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // this can happen if an unpluggable display is not connected
367f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        mDisplayData[disp].connected = false;
3681604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return err;
3691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3717f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[disp].currentConfig = 0;
3727f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    for (size_t c = 0; c < numConfigs; ++c) {
3737f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
3747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                DISPLAY_ATTRIBUTES, values);
375f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza        // If this is a pre-1.5 HWC, it may not know about color transform, so
376f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza        // try again with a smaller set of attributes
377f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza        if (err != NO_ERROR) {
378f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza            err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
379f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                    PRE_HWC15_DISPLAY_ATTRIBUTES, values);
380f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza        }
3817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (err != NO_ERROR) {
3827f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            // we can't get this display's info. turn it off.
3837f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].connected = false;
3847f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            return err;
3857f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
3867f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
3877f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        DisplayConfig config = DisplayConfig();
3887f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
3897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            switch (DISPLAY_ATTRIBUTES[i]) {
3907f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_VSYNC_PERIOD:
3917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.refresh = nsecs_t(values[i]);
3927f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3937f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_WIDTH:
3947f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.width = values[i];
3957f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3967f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_HEIGHT:
3977f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.height = values[i];
3987f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3997f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_X:
4007f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.xdpi = values[i] / 1000.0f;
4017f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
4027f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_Y:
4037f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.ydpi = values[i] / 1000.0f;
4047f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
405f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                case HWC_DISPLAY_COLOR_TRANSFORM:
406f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                    config.colorTransform = values[i];
407f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                    break;
4087f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                default:
409b54ee7704e46529e02386cf39eba93a34a527c61Michael Lentine                    ALOG_ASSERT(false, "unknown display attribute[%zu] %#x",
4107f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                            i, DISPLAY_ATTRIBUTES[i]);
4117f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
4127f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            }
4137f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
4141604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
4157f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (config.xdpi == 0.0f || config.ydpi == 0.0f) {
416bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall            float dpi = getDefaultDensity(config.width, config.height);
4177f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.xdpi = dpi;
4187f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.ydpi = dpi;
4191bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
4207f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4217f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        mDisplayData[disp].configs.push_back(config);
4221bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
4231bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
424f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    // FIXME: what should we set the format to?
425f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
426f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].connected = true;
4271604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    return NO_ERROR;
4281bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
4291bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
4301c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hallstatus_t HWComposer::setVirtualDisplayProperties(int32_t id,
4311c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        uint32_t w, uint32_t h, uint32_t format) {
4321c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
4331c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall            !mAllocatedDisplayIDs.hasBit(id)) {
4341c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        return BAD_INDEX;
4351c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    }
4367f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[id].currentConfig;
4371c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    mDisplayData[id].format = format;
4387f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    DisplayConfig& config = mDisplayData[id].configs.editItemAt(configId);
4397f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.width = w;
4407f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.height = h;
441bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    config.xdpi = config.ydpi = getDefaultDensity(w, h);
4421c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    return NO_ERROR;
4431c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
4441c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
445e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
446f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
447e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
448e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
449f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
450f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
4517adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = true;
4527f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].configs.resize(1);
4537f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].currentConfig = 0;
454e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
455e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
456e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
457e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
4589e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    if (id < NUM_BUILTIN_DISPLAYS) {
459f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
460e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
461e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
462f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
463e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
464e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
465f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
4667adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = false;
467e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
468e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
469e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
470b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshTimestamp(int disp) const {
471d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
472d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
473d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
474d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
475d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
4767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[disp].currentConfig;
4777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return now - ((now - mLastHwVSync[disp]) %
4787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].configs[configId].refresh);
479b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
480b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
4812ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennissp<Fence> HWComposer::getDisplayFence(int disp) const {
4822ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis    return mDisplayData[disp].lastDisplayFence;
4832ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis}
4842ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
485b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
4864e3e30c2d5a2a6f7dc4ee8696b1ac2a52dffd5dcAjay Dudani    if (static_cast<uint32_t>(disp) >= MAX_HWC_DISPLAYS || !mAllocatedDisplayIDs.hasBit(disp)) {
48719e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return HAL_PIXEL_FORMAT_RGBA_8888;
48819e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    } else {
48919e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return mDisplayData[disp].format;
49019e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    }
4913eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4923eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
4937f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozabool HWComposer::isConnected(int disp) const {
4947f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].connected;
4957f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4967f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4977f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getWidth(int disp) const {
4987f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4997f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].width;
5007f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
5017f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
5027f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getHeight(int disp) const {
5037f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
5047f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].height;
5057f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
5067f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
507b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
5087f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
5097f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].xdpi;
5108b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
5118b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
512b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
5137f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
5147f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].ydpi;
5158b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
5168b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
5177f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozansecs_t HWComposer::getRefreshPeriod(int disp) const {
5187f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
5197f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].refresh;
5207f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
5217f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
5227f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozaconst Vector<HWComposer::DisplayConfig>& HWComposer::getConfigs(int disp) const {
5237f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs;
5247f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
5257f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
5267f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozasize_t HWComposer::getCurrentConfig(int disp) const {
5277f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].currentConfig;
528f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
529f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
53081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
53181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
532620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
533620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
534620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
535620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
536620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
537620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
538620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
53981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
54081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
5413eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
54281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
54381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
54481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
54581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
54681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
54781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
54881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
54981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
55081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
55181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
55281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
55381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
55481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
55581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
55681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
5574d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall
5584d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                char tag[16];
5594d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
5604d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                ATRACE_INT(tag, enabled);
56181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
562e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
56381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
56481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
56581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
56681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
5673eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
5683a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
5693a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
5703a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
5713a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
57231d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
57331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
5745f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
575f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
5761e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
577e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
5781e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
57945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
580f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
581da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
582da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
583da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
584da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
58513a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
586da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
587f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
588f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
589f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
590f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
59145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
592da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
593da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
594da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
5957f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const DisplayConfig& currentConfig =
5967f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    disp.configs[disp.currentConfig];
5977f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const hwc_rect_t r = { 0, 0,
5987f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    (int) currentConfig.width, (int) currentConfig.height };
599da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
600da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
601da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
602da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
603da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
604da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
6058f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
6068f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.left = 0;
6078f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.top = 0;
6087f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.right =
6097f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.width;
6107f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.bottom =
6117f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.height;
6128f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            } else {
6138f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCrop = r;
6148f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            }
615da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
616da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
617da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
618da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
619da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
620da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
62170a6e88dbd52605f35f290eba52a05bdc6fc8042Mathias Agopian            disp.framebufferTarget->planeAlpha = 0xFF;
622da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
623db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
624f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
625f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
626a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
627a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
628a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
629a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
634da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
635da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
637da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
638e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian        // triggers a Surface::queueBuffer()  on some
639da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
640da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
641da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
642da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
643da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
644da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
6451df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    if (acquireFence->isValid()) {
646da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
647da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
648da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
649da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
650da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
651da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
652da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
653da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
654da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
655da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
656e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
657e04e4edcd1a852058775caa34cf73e2c20dc1066Manoj Kumar AVM    Mutex::Autolock _l(mDisplayLock);
658f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
659da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
660da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
661da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
662da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
663da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
664da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
66527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
666da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
667da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
66827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
669258ee43fbcbc752cb08c134a12c08d5d99c4a866Michael Lentine            ALOGW("WARNING: disp %zu: connected, non-null list, layers=%zu",
67027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
67127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
672da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
673f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
67438efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
675f7a675837bdad03d398c9b6f0f593b9c51c679b5Jesse Hall                mLists[i]->outbuf = disp.outbufHandle;
676db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
677db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
678db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
679db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
680db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
681db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
682db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
683db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
684db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
685f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
686f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
687db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
688f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
689da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
690da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
6919c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
6925f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
6935f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
6945f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
695d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        //
696d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // If there are no window layers, we treat the display has having FB
697d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // composition, because SurfaceFlinger will use GLES to draw the
698d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // wormhole region.
699f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
700f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
701f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
702f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
703f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
704f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
705f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
706da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
707da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
708da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
709da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
710f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
711f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
712f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
713f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
714f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
715f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
716f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
717f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
718f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
71903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    if (l.compositionType == HWC_CURSOR_OVERLAY) {
72003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                        disp.hasOvComp = true;
72103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    }
722f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
723d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
724d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                    disp.hasFbComp = true;
725d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                }
726d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall            } else {
727d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                disp.hasFbComp = true;
7289c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
7299c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
7309c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
731a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
732a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
733a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
734e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
73599c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
736e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
737e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
738e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
7395f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
740e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
74199c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
74299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall        return true;
743e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
7449c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
7459c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
74613f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hallsp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
747da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
74813f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        return Fence::NO_FENCE;
749da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
750da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
751da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
752da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
753da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
754da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
755d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
756da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
757da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
758da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
75913f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
760da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
761da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
76230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
7638630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
7648630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
7659eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
76630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
767f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
768f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
76930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
77030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
77130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
772f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
773afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall        for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
77480e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            DisplayData& disp(mDisplayData[i]);
775851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall            if (disp.outbufHandle) {
776851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                mLists[i]->outbuf = disp.outbufHandle;
77780e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall                mLists[i]->outbufAcquireFenceFd =
778851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                        disp.outbufAcquireFence->dup();
77980e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            }
78080e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall        }
78180e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall
78230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
783f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
784f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
785f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
7862ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis            disp.lastDisplayFence = disp.lastRetireFence;
7871df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis            disp.lastRetireFence = Fence::NO_FENCE;
788f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
789db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
7902ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis                    disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
791db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
792f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
793f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
794f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
7958630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
79658959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
797a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
798a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
799a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
8002c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malanistatus_t HWComposer::setPowerMode(int disp, int mode) {
801afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
8027ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
8032c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (mode == HWC_POWER_MODE_OFF) {
8042c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            eventControl(disp, HWC_EVENT_VSYNC, 0);
8052c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
8062c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
8072c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->setPowerMode(mHwc, disp, mode);
8082c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        } else {
8092c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->blank(mHwc, disp,
8102c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani                    mode == HWC_POWER_MODE_OFF ? 1 : 0);
8112c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
81210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
81310fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
81410fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
81510fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
8166c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentinestatus_t HWComposer::setActiveConfig(int disp, int mode) {
8176c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
818d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    DisplayData& dd(mDisplayData[disp]);
819d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    dd.currentConfig = mode;
8206c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
8216c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        return (status_t)mHwc->setActiveConfig(mHwc, disp, mode);
8226c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    } else {
8236c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        LOG_FATAL_IF(mode != 0);
8246c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    }
8256c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    return NO_ERROR;
8266c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine}
8276c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine
82827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
8295a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
83027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
831851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    free(dd.list);
832851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.list = NULL;
833851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.framebufferTarget = NULL;    // points into dd.list
834851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.fbTargetHandle = NULL;
835851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufHandle = NULL;
836851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastRetireFence = Fence::NO_FENCE;
837851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastDisplayFence = Fence::NO_FENCE;
838851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufAcquireFence = Fence::NO_FENCE;
839949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // clear all the previous configs and repopulate when a new
840949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // device is added
841949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    dd.configs.clear();
84227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
84327ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
844cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
845d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
846da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
847da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
848da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
849da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
850da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
851cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
852cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
853cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
854b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
855b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
856da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool HWComposer::supportsFramebufferTarget() const {
857da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
858da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
859da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
860da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::fbPost(int32_t id,
861da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
862d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
863da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return setFramebufferTarget(id, acquireFence, buffer);
864da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
865ea74d3b78d607cde17790a7bb83e6f68ffd34cfdMathias Agopian        acquireFence->waitForever("HWComposer::fbPost");
866da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->post(mFbDev, buffer->handle);
867cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
868cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian}
869cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
870cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::fbCompositionComplete() {
871d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
872da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
873da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
874da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev->compositionComplete) {
875da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->compositionComplete(mFbDev);
876da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
877da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return INVALID_OPERATION;
878b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
879b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
880b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
881b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::fbDump(String8& result) {
882cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
883b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        const size_t SIZE = 4096;
884b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        char buffer[SIZE];
885b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFbDev->dump(mFbDev, buffer, SIZE);
886b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        result.append(buffer);
887b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
888b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
889b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
890851cfe834295224cd64bdd499872b95b19c4de8cJesse Hallstatus_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
891851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        const sp<GraphicBuffer>& buf) {
892851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
893851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return BAD_INDEX;
894851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (id < VIRTUAL_DISPLAY_ID_BASE)
895851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return INVALID_OPERATION;
896851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
897851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    DisplayData& disp(mDisplayData[id]);
898851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    disp.outbufHandle = buf->handle;
899851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    disp.outbufAcquireFence = acquireFence;
900851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    return NO_ERROR;
901851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall}
902851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
90341d67d7ab4da1c393497a620a116a854b3c618e7Andy McFaddensp<Fence> HWComposer::getLastRetireFence(int32_t id) const {
904851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
905851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return Fence::NO_FENCE;
906851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    return mDisplayData[id].lastRetireFence;
907851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall}
908851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
90903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrewsstatus_t HWComposer::setCursorPositionAsync(int32_t id, const Rect& pos)
91003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews{
91103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    if (mHwc->setCursorPositionAsync) {
91203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        return (status_t)mHwc->setCursorPositionAsync(mHwc, id, pos.left, pos.top);
91303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
91403414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    else {
91503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        return NO_ERROR;
91603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
91703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews}
91803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews
9193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
9203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
9213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
9223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
9233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
9243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
9253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
9263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
9273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
9283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
9293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
9303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
9313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
9323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
9333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
9343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
9353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
9363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
9373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
9393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
9403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
9413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
9433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
9445880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
9455880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
9465880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
9475880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
9485880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
9499f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    struct hwc_composer_device_1* mHwc;
9503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
9519f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
9529f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
9533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
9543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
9553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
9563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
9583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
9593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
96013f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    virtual sp<Fence> getAndResetReleaseFence() {
961ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
962ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
96313f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
964ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
965dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
966dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
967dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
9689f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPerFrameDefaultState() {
9699f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        //getLayer()->compositionType = HWC_FRAMEBUFFER;
9709f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
9719f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPlaneAlpha(uint8_t alpha) {
9729f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
9739f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian            getLayer()->planeAlpha = alpha;
9749f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        } else {
9755fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            if (alpha < 0xFF) {
9765fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian                getLayer()->flags |= HWC_SKIP_LAYER;
9775fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            }
9789f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        }
9799f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
9803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
9819f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        hwc_layer_1_t* const l = getLayer();
9829f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->compositionType = HWC_FRAMEBUFFER;
9839f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->hints = 0;
9849f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->flags = HWC_SKIP_LAYER;
9859f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->handle = 0;
9869f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->transform = 0;
9879f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->blending = HWC_BLENDING_NONE;
9889f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.numRects = 0;
9899f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.rects = NULL;
9909f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->acquireFenceFd = -1;
9919f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->releaseFenceFd = -1;
9929f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->planeAlpha = 0xFF;
9933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
9953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
9963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
9973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
9983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
9993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
10003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
100103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    virtual void setIsCursorLayerHint(bool isCursor) {
100203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
100303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            if (isCursor) {
100403414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                getLayer()->flags |= HWC_IS_CURSOR_LAYER;
100503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            }
100603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            else {
100703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                getLayer()->flags &= ~HWC_IS_CURSOR_LAYER;
100803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            }
100903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        }
101003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
10113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
10123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
10133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
10143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
10153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
10163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
10173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
10186b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
10193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
10206b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    virtual void setCrop(const FloatRect& crop) {
10216b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
10226b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
10236b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        } else {
10246b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            /*
10256b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * Since h/w composer didn't support a flot crop rect before version 1.3,
10266b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * using integer coordinates instead produces a different output from the GL code in
10276b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
10286b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * window size ratio is large and a window crop is defined
10296b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
10306b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             */
10316b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            hwc_rect_t& r = getLayer()->sourceCrop;
10326b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.left  = int(ceilf(crop.left));
10336b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.top   = int(ceilf(crop.top));
10346b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.right = int(floorf(crop.right));
10356b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.bottom= int(floorf(crop.bottom));
10366b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        }
10373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
10383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
1039c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
1040d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos        mVisibleRegion = reg;
1041d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos        visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(
1042d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos                mVisibleRegion.getArray(&visibleRegion.numRects));
10433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
1044ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza    virtual void setSurfaceDamage(const Region& reg) {
1045ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_5)) {
1046ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            return;
1047ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        }
1048ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        hwc_region_t& surfaceDamage = getLayer()->surfaceDamage;
1049ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        // We encode default full-screen damage as INVALID_RECT upstream, but as
1050ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        // 0 rects for HWComposer
1051ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        if (reg.isRect() && reg.getBounds() == Rect::INVALID_RECT) {
1052ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            surfaceDamage.numRects = 0;
1053ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            surfaceDamage.rects = NULL;
1054ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            return;
1055ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        }
1056d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos        mSurfaceDamage = reg;
1057d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos        surfaceDamage.rects = reinterpret_cast<hwc_rect_t const *>(
1058d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos                mSurfaceDamage.getArray(&surfaceDamage.numRects));
1059ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza    }
1060399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    virtual void setSidebandStream(const sp<NativeHandle>& stream) {
1061399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        ALOG_ASSERT(stream->handle() != NULL);
1062399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        getLayer()->compositionType = HWC_SIDEBAND;
1063399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        getLayer()->sidebandStream = stream->handle();
1064399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    }
10653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
10663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
10673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
10683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
10693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
10703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
1071399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall            if (getLayer()->compositionType == HWC_SIDEBAND) {
1072399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                // If this was a sideband layer but the stream was removed, reset
1073399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare.
1074399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                getLayer()->compositionType = HWC_FRAMEBUFFER;
1075399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall            }
10763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
10773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
10783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
1079c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    virtual void onDisplayed() {
1080e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall        getLayer()->acquireFenceFd = -1;
1081c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    }
1082d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos
1083d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballosprotected:
1084d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos    // We need to hold "copies" of these for memory management purposes. The
1085d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos    // actual hwc_layer_1_t holds pointers to the memory within. Vector<>
1086d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos    // internally doesn't copy the memory unless one of the copies is modified.
1087d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos    Region mVisibleRegion;
1088d814cf2a3e3a2fdb73efa80539fe8af0a93da1dbPablo Ceballos    Region mSurfaceDamage;
10893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
10903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
10913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
10923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
10933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
10945f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
1095f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
10965f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
1097f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
1098f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
1099f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
11003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
1101f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
11029f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
11033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
11043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
11053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
11063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
11073e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
11085f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
11091e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
1110a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
1111a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
11123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
11133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
11143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
11155f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
1116da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    size_t numLayers = 0;
1117da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
1118da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
1119da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (mHwc && disp.list) {
1120da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers = disp.list->numHwLayers;
1121da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
1122da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
1123da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // which we ignore when iterating through the layer list.
1124da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
1125da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                if (numLayers) {
1126da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    numLayers--;
1127da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                }
1128da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            }
1129da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
1130da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
1131da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return getLayerIterator(id, numLayers);
11323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
11333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
11344df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden// Converts a PixelFormat to a human-readable string.  Max 11 chars.
11354df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden// (Could use a table of prefab String8 objects.)
11364df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFaddenstatic String8 getFormatStr(PixelFormat format) {
11374df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    switch (format) {
11384df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGBA_8888:    return String8("RGBA_8888");
11394df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGBX_8888:    return String8("RGBx_8888");
11404df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGB_888:      return String8("RGB_888");
11414df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGB_565:      return String8("RGB_565");
11424df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_BGRA_8888:    return String8("BGRA_8888");
1143f0058ca0e592485ac9e5a4aedb0dd8fa187625efAndy McFadden    case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
1144f0058ca0e592485ac9e5a4aedb0dd8fa187625efAndy McFadden                                    return String8("ImplDef");
11454df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    default:
11464df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        String8 result;
11474df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        result.appendFormat("? %08x", format);
11484df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        return result;
11494df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    }
11504df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden}
11514df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden
115274d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopianvoid HWComposer::dump(String8& result) const {
1153e04e4edcd1a852058775caa34cf73e2c20dc1066Manoj Kumar AVM    Mutex::Autolock _l(mDisplayLock);
1154b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
11554df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        result.appendFormat("Hardware Composer state (version %08x):\n", hwcApiVersion(mHwc));
1156f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
1157f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
1158f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
115902d86567d95b99e1142941ed7ec23a4465822813Jesse Hall            if (!disp.connected)
116002d86567d95b99e1142941ed7ec23a4465822813Jesse Hall                continue;
1161b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
116213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const Vector< sp<Layer> >& visibleLayersSortedByZ =
1163cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian                    mFlinger->getLayerSortedByZForHwcDisplay(i);
1164cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
11657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
11667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            result.appendFormat("  Display[%zd] configurations (* current):\n", i);
11677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            for (size_t c = 0; c < disp.configs.size(); ++c) {
11687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                const DisplayConfig& config(disp.configs[c]);
1169f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                result.appendFormat("    %s%zd: %ux%u, xdpi=%f, ydpi=%f"
1170f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                        ", refresh=%" PRId64 ", colorTransform=%d\n",
1171f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                        c == disp.currentConfig ? "* " : "", c,
1172f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                        config.width, config.height, config.xdpi, config.ydpi,
1173f2699fc3a8c12b2bf95120c068801e050168bd96Dan Stoza                        config.refresh, config.colorTransform);
11747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            }
1175b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
117602d86567d95b99e1142941ed7ec23a4465822813Jesse Hall            if (disp.list) {
1177b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
117892dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn                        "  numHwLayers=%zu, flags=%08x\n",
1179b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        disp.list->numHwLayers, disp.list->flags);
1180b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
1181f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
11824df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                        "    type   |  handle  | hint | flag | tr | blnd |   format    |     source crop (l,t,r,b)      |          frame         | name \n"
11834df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                        "-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------\n");
11844df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                //      " _________ | ________ | ____ | ____ | __ | ____ | ___________ |_____._,_____._,_____._,_____._ |_____,_____,_____,_____ | ___...
1185f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1186f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
1187f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
1188da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    String8 name("unknown");
1189cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
1190da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (i < visibleLayersSortedByZ.size()) {
119113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<Layer>& layer(visibleLayersSortedByZ[i]);
119213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<GraphicBuffer>& buffer(
119313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                                layer->getActiveBuffer());
119413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        if (buffer != NULL) {
119513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                            format = buffer->getPixelFormat();
1196f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
1197da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = layer->getName();
1198da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
1199da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1200da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    int type = l.compositionType;
1201da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type == HWC_FRAMEBUFFER_TARGET) {
1202da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = "HWC_FRAMEBUFFER_TARGET";
1203da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        format = disp.format;
1204f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
1205da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1206da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    static char const* compositionTypeName[] = {
1207da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "GLES",
1208da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "HWC",
12094df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                            "BKGND",
1210da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "FB TARGET",
121103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                            "SIDEBAND",
121203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                            "HWC_CURSOR",
1213da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "UNKNOWN"};
1214da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type >= NELEM(compositionTypeName))
1215da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        type = NELEM(compositionTypeName) - 1;
1216da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
12174df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                    String8 formatStr = getFormatStr(format);
1218353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1219353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                        result.appendFormat(
12204df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d | %s\n",
1221353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        compositionTypeName[type],
12224df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                        intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
1223353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1224353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1225353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        name.string());
1226353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    } else {
1227353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                        result.appendFormat(
12284df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7d,%7d,%7d,%7d |%5d,%5d,%5d,%5d | %s\n",
1229353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        compositionTypeName[type],
12304df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                        intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
1231353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1232353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1233353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        name.string());
1234353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    }
1235fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
1236fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
12378372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
12381d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
123930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
124030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
124174d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian        const size_t SIZE = 4096;
124274d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian        char buffer[SIZE];
124330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
12441d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
12458372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
12468372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
12478372785879d329f592f6883620b5a32d80d74691Mathias Agopian
1248a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
12492965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12502965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
12512965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
12522965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
1253b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
12542965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
12552965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12562965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12572965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
12582965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
125981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mEnabled != enabled) {
126081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mEnabled = enabled;
126181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mCondition.signal();
126281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
12632965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12642965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12652965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
12662965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
12672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12682965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12692965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
12702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
12712965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
12722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
12732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
12742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
12752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
12782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
12792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
12802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
12812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
12822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
12832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
12842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
12852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
12872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
12892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
12902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
12912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
12932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
12942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
12952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
12962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
12982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
12992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
13002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
13012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
13022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
13032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
1304a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::DisplayData()
13057f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza:   configs(),
13067f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    currentConfig(0),
13077f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    format(HAL_PIXEL_FORMAT_RGBA_8888),
1308a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    connected(false),
1309a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    hasFbComp(false), hasOvComp(false),
1310a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    capacity(0), list(NULL),
1311a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    framebufferTarget(NULL), fbTargetHandle(0),
1312a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
1313851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
1314a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    events(0)
1315a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall{}
1316a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
1317a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::~DisplayData() {
1318a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    free(list);
1319a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall}
1320a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
13212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
1322a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
1323