HWComposer.cpp revision 4d407a0f05ad9ba3c232ff5b6742495660c0e4ab
1a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian/*
2a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
4a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * you may not use this file except in compliance with the License.
6a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * You may obtain a copy of the License at
7a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
8a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
10a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Unless required by applicable law or agreed to in writing, software
11a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * See the License for the specific language governing permissions and
14a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * limitations under the License.
15a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian */
16a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#define ATRACE_TAG ATRACE_TAG_GRAPHICS
182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
1992dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn#include <inttypes.h>
2092dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn#include <math.h>
21a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <stdint.h>
22f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdio.h>
23f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdlib.h>
24f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <string.h>
25a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <sys/types.h>
26a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
27a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <utils/Errors.h>
28da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian#include <utils/misc.h>
29399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall#include <utils/NativeHandle.h>
308372785879d329f592f6883620b5a32d80d74691Mathias Agopian#include <utils/String8.h>
313eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <utils/Thread.h>
322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#include <utils/Trace.h>
3322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include <utils/Vector.h>
34a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
35921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <ui/GraphicBuffer.h>
36921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
383eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <hardware/hwcomposer.h>
39a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
401c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall#include <android/configuration.h>
411c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
42a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
43e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian#include <cutils/properties.h>
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
4633ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian
4733ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian#include "../Layer.h"           // needed only for debugging
4833ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian#include "../SurfaceFlinger.h"
49a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
50a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
527296051995fbaea919480a6cc2dabb729dabc95bJesse Hall#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
539eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
549eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
559eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
569eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
579eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
589eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
599eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
609eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
619eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
629eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
639eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
649eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
659eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        uint32_t version) {
669eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
67bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
68bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
69a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
70a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
843eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
853eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
86b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        EventHandler& handler)
87c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
88b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mFbDev(0), mHwc(0), mNumDisplays(1),
893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
90e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
91bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian      mDebugForceFakeVSync(false)
92a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
939e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
94e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
95e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
96b685c542836b93c99cd85053e07696406ea37adbJesse Hall
97bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
98bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mLastHwVSync[i] = 0;
99bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mVSyncCounts[i] = 0;
100bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    }
101bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian
102e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
103e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
104e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
105e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
106028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
1075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
108b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Note: some devices may insist that the FB HAL be opened before HWC.
109ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    int fberr = loadFbHalModule();
110b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadHwcModule();
111bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
112da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
113da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // close FB HAL if we don't needed it.
114da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: this is temporary until we're not forced to open FB HAL
115da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // before HWC.
116da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        framebuffer_close(mFbDev);
117da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mFbDev = NULL;
118da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
119da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
120babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
121babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
122babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden            && !mFbDev) {
123ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall        ALOGE("ERROR: failed to open framebuffer (%s), aborting",
124ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall                strerror(-fberr));
12543601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        abort();
12643601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    }
12743601a2dc320a271ff8c3765ff61414a07221635Andy McFadden
128620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    // these display IDs are always reserved
1299e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
130620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        mAllocatedDisplayIDs.markBit(i);
131620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
132620685c2e684082c82657732d1e35cefd0c79006Andy McFadden
133b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mHwc) {
134b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
135b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 24) & 0xff,
136b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 16) & 0xff);
137b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (mHwc->registerProcs) {
138b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->hwc = this;
139b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.invalidate = &hook_invalidate;
140b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.vsync = &hook_vsync;
141b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
142b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = &hook_hotplug;
143b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            else
144b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = NULL;
145b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
146b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mHwc->registerProcs(mHwc, &mCBContext->procs);
147b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        }
148b685c542836b93c99cd85053e07696406ea37adbJesse Hall
149b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // don't need a vsync thread if we have a hardware composer
150b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        needVSyncThread = false;
151b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // always turn vsync off when we start
15281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
1538f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
154b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // the number of displays we actually have depends on the
155b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // hw composer version
15638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
15738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            // 1.3 adds support for virtual displays
1589e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall            mNumDisplays = MAX_HWC_DISPLAYS;
159b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
160b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.1 adds support for multiple displays
1619e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall            mNumDisplays = NUM_BUILTIN_DISPLAYS;
162b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else {
163b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = 1;
164c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1653a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1663a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
167b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
1681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
1691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                "should only have fbdev if no hwc or hwc is 1.0");
1701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
171f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
17238e623bc5c2d7224ebd5b82efaea401bc8e5367aMathias Agopian        disp.connected = true;
173b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.format = mFbDev->format;
1747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        DisplayConfig config = DisplayConfig();
1757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.width = mFbDev->width;
1767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.height = mFbDev->height;
1777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.xdpi = mFbDev->xdpi;
1787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.ydpi = mFbDev->ydpi;
1797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.refresh = nsecs_t(1e9 / mFbDev->fps);
1807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        disp.configs.push_back(config);
1817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        disp.currentConfig = 0;
1821bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    } else if (mHwc) {
1831604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // here we're guaranteed to have at least HWC 1.1
1849e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall        for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
1851604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            queryDisplayProperties(i);
1861604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
187888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
188888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1893a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1903a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1913a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
192a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
193a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
194a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
195a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
196babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if (mHwc) {
19781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
198babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    }
1993eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
2003eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
2013eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
202a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
204a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
205b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
206b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        framebuffer_close(mFbDev);
207b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
2083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
209a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
210a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
211b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the hardware composer module.  Sets mHwc.
212b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadHwcModule()
213b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
214b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
215b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
216b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
217b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
218b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
219b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
220b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
221b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = hwc_open_1(module, &mHwc);
222b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
223b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device failed to initialize (%s)",
224b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, strerror(-err));
225b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
226b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
227b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
228b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
229b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
230b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
231b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device version %#x unsupported, will not be used",
232b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, mHwc->common.version);
233b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        hwc_close_1(mHwc);
234b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mHwc = NULL;
235b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
236b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
237b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
238b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
239b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the FB HAL, which uses the gralloc module.  Sets mFbDev.
240ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hallint HWComposer::loadFbHalModule()
241b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
242b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
243b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
244ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
245ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    if (err != 0) {
246b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
247ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall        return err;
248b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
249b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
250ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    return framebuffer_open(module, &mFbDev);
251b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
252b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
253a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
254a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
255a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
256a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
257bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
258bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
259bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
260bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
261c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
262c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2631bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
264bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
265bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
266bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
2671bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->vsync(disp, timestamp);
2681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
2691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
2701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
2711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        int connected) {
2721bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
2731bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            const_cast<hwc_procs_t*>(procs));
2741bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->hotplug(disp, connected);
27531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
27631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
277c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
278e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
279c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
280c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2811bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::vsync(int disp, int64_t timestamp) {
282bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
2838e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall        {
2848e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            Mutex::Autolock _l(mLock);
2858e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
2868e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // There have been reports of HWCs that signal several vsync events
2878e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // with the same timestamp when turning the display off and on. This
2888e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // is a bug in the HWC implementation, but filter the extra events
2898e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // out here so they don't cause havoc downstream.
2908e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            if (timestamp == mLastHwVSync[disp]) {
291258ee43fbcbc752cb08c134a12c08d5d99c4a866Michael Lentine                ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
2928e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall                        timestamp);
2938e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall                return;
2948e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            }
2958e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
2968e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            mLastHwVSync[disp] = timestamp;
2978e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall        }
2988e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
299bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        char tag[16];
300faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis        snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
301bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
302bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian
303bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mEventHandler.onVSyncReceived(disp, timestamp);
304bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    }
305d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
306d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
3071bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hotplug(int disp, int connected) {
308afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall    if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
3091bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
3101bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                disp, connected);
3111bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        return;
3121bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
313f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    queryDisplayProperties(disp);
314148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    mEventHandler.onHotplugReceived(disp, bool(connected));
3151bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3161bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3171c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hallstatic float getDefaultDensity(uint32_t height) {
3181c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
3191c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    else                return ACONFIGURATION_DENSITY_TV;
3201c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
3211c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
3221bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallstatic const uint32_t DISPLAY_ATTRIBUTES[] = {
3231bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_VSYNC_PERIOD,
324db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_WIDTH,
325db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_HEIGHT,
3261bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_X,
3271bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_Y,
3281bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_NO_ATTRIBUTE,
3291bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall};
3301bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
3311bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3321604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopianstatus_t HWComposer::queryDisplayProperties(int disp) {
3331604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
334da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
3351bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
336b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // use zero as default value for unspecified attributes
3371bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
3381bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    memset(values, 0, sizeof(values));
3391bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3407f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    const size_t MAX_NUM_CONFIGS = 128;
3417f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    uint32_t configs[MAX_NUM_CONFIGS] = {0};
3427f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t numConfigs = MAX_NUM_CONFIGS;
3437f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    status_t err = mHwc->getDisplayConfigs(mHwc, disp, configs, &numConfigs);
3441604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (err != NO_ERROR) {
3451604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // this can happen if an unpluggable display is not connected
346f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        mDisplayData[disp].connected = false;
3471604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return err;
3481bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3491bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3507f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[disp].currentConfig = 0;
3517f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    for (size_t c = 0; c < numConfigs; ++c) {
3527f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
3537f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                DISPLAY_ATTRIBUTES, values);
3547f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (err != NO_ERROR) {
3557f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            // we can't get this display's info. turn it off.
3567f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].connected = false;
3577f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            return err;
3587f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
3597f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
3607f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        DisplayConfig config = DisplayConfig();
3617f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
3627f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            switch (DISPLAY_ATTRIBUTES[i]) {
3637f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_VSYNC_PERIOD:
3647f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.refresh = nsecs_t(values[i]);
3657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_WIDTH:
3677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.width = values[i];
3687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3697f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_HEIGHT:
3707f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.height = values[i];
3717f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3727f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_X:
3737f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.xdpi = values[i] / 1000.0f;
3747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_Y:
3767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.ydpi = values[i] / 1000.0f;
3777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                default:
379b54ee7704e46529e02386cf39eba93a34a527c61Michael Lentine                    ALOG_ASSERT(false, "unknown display attribute[%zu] %#x",
3807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                            i, DISPLAY_ATTRIBUTES[i]);
3817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3827f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            }
3837f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
3841604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
3857f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (config.xdpi == 0.0f || config.ydpi == 0.0f) {
3867f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            float dpi = getDefaultDensity(config.height);
3877f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.xdpi = dpi;
3887f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.ydpi = dpi;
3891bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3907f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
3917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        mDisplayData[disp].configs.push_back(config);
3921bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3931bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
394f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    // FIXME: what should we set the format to?
395f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
396f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].connected = true;
3971604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    return NO_ERROR;
3981bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3991bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
4001c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hallstatus_t HWComposer::setVirtualDisplayProperties(int32_t id,
4011c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        uint32_t w, uint32_t h, uint32_t format) {
4021c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
4031c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall            !mAllocatedDisplayIDs.hasBit(id)) {
4041c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        return BAD_INDEX;
4051c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    }
4067f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[id].currentConfig;
4071c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    mDisplayData[id].format = format;
4087f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    DisplayConfig& config = mDisplayData[id].configs.editItemAt(configId);
4097f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.width = w;
4107f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.height = h;
4117f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.xdpi = config.ydpi = getDefaultDensity(h);
4121c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    return NO_ERROR;
4131c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
4141c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
415e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
416f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
417e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
418e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
419f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
420f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
4217adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = true;
4227f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].configs.resize(1);
4237f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].currentConfig = 0;
424e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
425e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
426e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
427e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
4289e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    if (id < NUM_BUILTIN_DISPLAYS) {
429f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
430e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
431e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
432f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
433e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
434e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
435f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
4367adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = false;
437e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
438e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
439e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
440b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshTimestamp(int disp) const {
441d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
442d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
443d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
444d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
445d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
4467f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[disp].currentConfig;
4477f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return now - ((now - mLastHwVSync[disp]) %
4487f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].configs[configId].refresh);
449b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
450b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
4512ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennissp<Fence> HWComposer::getDisplayFence(int disp) const {
4522ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis    return mDisplayData[disp].lastDisplayFence;
4532ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis}
4542ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
455b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
45619e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
45719e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return HAL_PIXEL_FORMAT_RGBA_8888;
45819e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    } else {
45919e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return mDisplayData[disp].format;
46019e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    }
4613eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4623eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
4637f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozabool HWComposer::isConnected(int disp) const {
4647f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].connected;
4657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getWidth(int disp) const {
4687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4697f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].width;
4707f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4717f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4727f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getHeight(int disp) const {
4737f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].height;
4757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
477b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
4787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].xdpi;
4808b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4818b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
482b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
4837f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4847f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].ydpi;
4858b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4868b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
4877f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozansecs_t HWComposer::getRefreshPeriod(int disp) const {
4887f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].refresh;
4907f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4927f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozaconst Vector<HWComposer::DisplayConfig>& HWComposer::getConfigs(int disp) const {
4937f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs;
4947f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4957f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4967f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozasize_t HWComposer::getCurrentConfig(int disp) const {
4977f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].currentConfig;
498f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
499f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
50081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
50181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
502620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
503620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
504620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
505620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
506620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
507620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
508620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
50981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
51081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
5113eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
51281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
51381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
51481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
51581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
51681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
51781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
51881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
51981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
52081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
52181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
52281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
52381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
52481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
52581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
52681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
5274d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall
5284d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                char tag[16];
5294d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
5304d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                ATRACE_INT(tag, enabled);
53181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
532e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
53381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
53481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
53581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
53681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
5373eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
5383a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
5393a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
5403a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
5413a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
54231d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
54331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
5445f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
545f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
5461e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
547e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
5481e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
54945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
550f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
551da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
552da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
553da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
554da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
55513a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
556da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
557f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
558f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
559f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
560f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
56145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
562da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
563da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
564da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
5657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const DisplayConfig& currentConfig =
5667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    disp.configs[disp.currentConfig];
5677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const hwc_rect_t r = { 0, 0,
5687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    (int) currentConfig.width, (int) currentConfig.height };
569da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
570da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
571da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
572da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
573da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
574da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
5758f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
5768f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.left = 0;
5778f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.top = 0;
5787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.right =
5797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.width;
5807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.bottom =
5817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.height;
5828f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            } else {
5838f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCrop = r;
5848f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            }
585da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
586da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
587da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
588da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
589da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
590da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
59170a6e88dbd52605f35f290eba52a05bdc6fc8042Mathias Agopian            disp.framebufferTarget->planeAlpha = 0xFF;
592da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
593db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
594f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
595f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
596a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
597a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
598a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
599a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
600da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
601da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
602da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
603da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
604da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
605da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
606da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
607da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
608e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian        // triggers a Surface::queueBuffer()  on some
609da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
610da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
611da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
612da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
613da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
614da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
6151df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    if (acquireFence->isValid()) {
616da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
617da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
618da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
619da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
620da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
621da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
622da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
623da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
624da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
625da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
626e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
627f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
628da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
629da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
63427ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
635da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
63727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
638258ee43fbcbc752cb08c134a12c08d5d99c4a866Michael Lentine            ALOGW("WARNING: disp %zu: connected, non-null list, layers=%zu",
63927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
64027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
641da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
642f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
64338efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
644f7a675837bdad03d398c9b6f0f593b9c51c679b5Jesse Hall                mLists[i]->outbuf = disp.outbufHandle;
645db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
646db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
647db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
648db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
649db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
650db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
651db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
652db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
653db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
654f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
655f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
656db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
657f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
658da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
659da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
6609c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
6615f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
6625f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
6635f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
664d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        //
665d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // If there are no window layers, we treat the display has having FB
666d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // composition, because SurfaceFlinger will use GLES to draw the
667d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // wormhole region.
668f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
669f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
670f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
671f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
672f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
673f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
674f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
675da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
676da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
677da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
678da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
679f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
680f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
681f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
682f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
683f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
684f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
685f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
686f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
687f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
68803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    if (l.compositionType == HWC_CURSOR_OVERLAY) {
68903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                        disp.hasOvComp = true;
69003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    }
691f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
692d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
693d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                    disp.hasFbComp = true;
694d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                }
695d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall            } else {
696d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                disp.hasFbComp = true;
6979c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
6989c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
6999c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
700a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
701a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
702a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
703e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
70499c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
705e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
706e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
707e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
7085f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
709e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
71099c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
71199c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall        return true;
712e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
7139c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
7149c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
71513f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hallsp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
716da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
71713f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        return Fence::NO_FENCE;
718da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
719da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
720da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
721da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
722da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
723da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
724d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
725da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
726da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
727da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
72813f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
729da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
730da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
73130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
7328630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
7338630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
7349eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
73530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
736f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
737f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
73830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
73930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
74030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
741f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
742afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall        for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
74380e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            DisplayData& disp(mDisplayData[i]);
744851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall            if (disp.outbufHandle) {
745851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                mLists[i]->outbuf = disp.outbufHandle;
74680e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall                mLists[i]->outbufAcquireFenceFd =
747851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                        disp.outbufAcquireFence->dup();
74880e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            }
74980e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall        }
75080e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall
75130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
752f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
753f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
754f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
7552ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis            disp.lastDisplayFence = disp.lastRetireFence;
7561df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis            disp.lastRetireFence = Fence::NO_FENCE;
757f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
758db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
7592ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis                    disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
760db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
761f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
762f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
763f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
7648630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
76558959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
766a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
767a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
768a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
7692c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malanistatus_t HWComposer::setPowerMode(int disp, int mode) {
770afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
7717ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
7722c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (mode == HWC_POWER_MODE_OFF) {
7732c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            eventControl(disp, HWC_EVENT_VSYNC, 0);
7742c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
7752c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
7762c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->setPowerMode(mHwc, disp, mode);
7772c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        } else {
7782c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->blank(mHwc, disp,
7792c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani                    mode == HWC_POWER_MODE_OFF ? 1 : 0);
7802c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
78110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
78210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
78310fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
78410fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
7856c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentinestatus_t HWComposer::setActiveConfig(int disp, int mode) {
7866c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
787d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    DisplayData& dd(mDisplayData[disp]);
788d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    dd.currentConfig = mode;
7896c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
7906c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        return (status_t)mHwc->setActiveConfig(mHwc, disp, mode);
7916c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    } else {
7926c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        LOG_FATAL_IF(mode != 0);
7936c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    }
7946c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    return NO_ERROR;
7956c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine}
7966c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine
79727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
7985a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
79927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
800851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    free(dd.list);
801851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.list = NULL;
802851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.framebufferTarget = NULL;    // points into dd.list
803851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.fbTargetHandle = NULL;
804851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufHandle = NULL;
805851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastRetireFence = Fence::NO_FENCE;
806851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastDisplayFence = Fence::NO_FENCE;
807851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufAcquireFence = Fence::NO_FENCE;
808949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // clear all the previous configs and repopulate when a new
809949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // device is added
810949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    dd.configs.clear();
81127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
81227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
813cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
814d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
815da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
816da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
817da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
818da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
819da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
820cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
821cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
822cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
823b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
824b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
825da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool HWComposer::supportsFramebufferTarget() const {
826da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
827da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
828da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
829da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::fbPost(int32_t id,
830da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
831d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
832da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return setFramebufferTarget(id, acquireFence, buffer);
833da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
834ea74d3b78d607cde17790a7bb83e6f68ffd34cfdMathias Agopian        acquireFence->waitForever("HWComposer::fbPost");
835da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->post(mFbDev, buffer->handle);
836cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
837cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian}
838cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
839cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::fbCompositionComplete() {
840d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
841da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
842da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
843da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev->compositionComplete) {
844da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->compositionComplete(mFbDev);
845da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
846da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return INVALID_OPERATION;
847b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
848b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
849b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
850b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::fbDump(String8& result) {
851cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
852b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        const size_t SIZE = 4096;
853b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        char buffer[SIZE];
854b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFbDev->dump(mFbDev, buffer, SIZE);
855b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        result.append(buffer);
856b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
857b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
858b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
859851cfe834295224cd64bdd499872b95b19c4de8cJesse Hallstatus_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
860851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        const sp<GraphicBuffer>& buf) {
861851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
862851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return BAD_INDEX;
863851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (id < VIRTUAL_DISPLAY_ID_BASE)
864851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return INVALID_OPERATION;
865851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
866851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    DisplayData& disp(mDisplayData[id]);
867851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    disp.outbufHandle = buf->handle;
868851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    disp.outbufAcquireFence = acquireFence;
869851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    return NO_ERROR;
870851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall}
871851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
87241d67d7ab4da1c393497a620a116a854b3c618e7Andy McFaddensp<Fence> HWComposer::getLastRetireFence(int32_t id) const {
873851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
874851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return Fence::NO_FENCE;
875851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    return mDisplayData[id].lastRetireFence;
876851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall}
877851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
87803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrewsstatus_t HWComposer::setCursorPositionAsync(int32_t id, const Rect& pos)
87903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews{
88003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    if (mHwc->setCursorPositionAsync) {
88103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        return (status_t)mHwc->setCursorPositionAsync(mHwc, id, pos.left, pos.top);
88203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
88303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    else {
88403414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        return NO_ERROR;
88503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
88603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews}
88703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews
8883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
8903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
8913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
8923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
8943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
8953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
8963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
8973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
8983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
8993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
9003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
9013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
9023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
9033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
9043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
9053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
9063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9073e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
9083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
9093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
9103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
9123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
9135880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
9145880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
9155880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
9165880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
9175880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
9189f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    struct hwc_composer_device_1* mHwc;
9193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
9209f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
9219f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
9223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
9233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
9243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
9253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
9273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
9283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
92913f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    virtual sp<Fence> getAndResetReleaseFence() {
930ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
931ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
93213f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
933ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
934dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
935dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
936dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
9379f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPerFrameDefaultState() {
9389f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        //getLayer()->compositionType = HWC_FRAMEBUFFER;
9399f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
9409f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPlaneAlpha(uint8_t alpha) {
9419f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
9429f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian            getLayer()->planeAlpha = alpha;
9439f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        } else {
9445fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            if (alpha < 0xFF) {
9455fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian                getLayer()->flags |= HWC_SKIP_LAYER;
9465fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            }
9479f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        }
9489f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
9493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
9509f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        hwc_layer_1_t* const l = getLayer();
9519f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->compositionType = HWC_FRAMEBUFFER;
9529f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->hints = 0;
9539f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->flags = HWC_SKIP_LAYER;
9549f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->handle = 0;
9559f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->transform = 0;
9569f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->blending = HWC_BLENDING_NONE;
9579f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.numRects = 0;
9589f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.rects = NULL;
9599f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->acquireFenceFd = -1;
9609f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->releaseFenceFd = -1;
9619f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->planeAlpha = 0xFF;
9623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
9643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
9653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
9663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
9673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
9683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
9693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
97003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    virtual void setIsCursorLayerHint(bool isCursor) {
97103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
97203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            if (isCursor) {
97303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                getLayer()->flags |= HWC_IS_CURSOR_LAYER;
97403414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            }
97503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            else {
97603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                getLayer()->flags &= ~HWC_IS_CURSOR_LAYER;
97703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            }
97803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        }
97903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
9803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
9813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
9823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
9843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
9853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
9876b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
9883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9896b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    virtual void setCrop(const FloatRect& crop) {
9906b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
9916b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
9926b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        } else {
9936b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            /*
9946b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * Since h/w composer didn't support a flot crop rect before version 1.3,
9956b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * using integer coordinates instead produces a different output from the GL code in
9966b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
9976b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * window size ratio is large and a window crop is defined
9986b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
9996b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             */
10006b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            hwc_rect_t& r = getLayer()->sourceCrop;
10016b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.left  = int(ceilf(crop.left));
10026b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.top   = int(ceilf(crop.top));
10036b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.right = int(floorf(crop.right));
10046b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.bottom= int(floorf(crop.bottom));
10056b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        }
10063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
10073e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
1008c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // Region::getSharedBuffer creates a reference to the underlying
1009c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // SharedBuffer of this Region, this reference is freed
1010c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // in onDisplayed()
1011c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
1012c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
1013c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
10143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
1015399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    virtual void setSidebandStream(const sp<NativeHandle>& stream) {
1016399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        ALOG_ASSERT(stream->handle() != NULL);
1017399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        getLayer()->compositionType = HWC_SIDEBAND;
1018399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        getLayer()->sidebandStream = stream->handle();
1019399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    }
10203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
10213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
10223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
10233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
10243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
10253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
1026399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall            if (getLayer()->compositionType == HWC_SIDEBAND) {
1027399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                // If this was a sideband layer but the stream was removed, reset
1028399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare.
1029399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                getLayer()->compositionType = HWC_FRAMEBUFFER;
1030399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall            }
10313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
10323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
10333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
1034c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    virtual void onDisplayed() {
1035c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
1036c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
1037c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        if (sb) {
1038c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            sb->release();
1039c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            // not technically needed but safer
1040c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.numRects = 0;
1041c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.rects = NULL;
1042c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        }
1043e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall
1044e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall        getLayer()->acquireFenceFd = -1;
1045c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    }
10463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
10473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
10483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
10493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
10503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
10515f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
1052f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
10535f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
1054f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
1055f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
1056f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
10573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
1058f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
10599f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
10603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
10613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
10623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
10633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
10643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
10655f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
10661e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
1067a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
1068a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
10693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
10703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
10713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
10725f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
1073da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    size_t numLayers = 0;
1074da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
1075da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
1076da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (mHwc && disp.list) {
1077da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers = disp.list->numHwLayers;
1078da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
1079da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
1080da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // which we ignore when iterating through the layer list.
1081da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
1082da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                if (numLayers) {
1083da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    numLayers--;
1084da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                }
1085da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            }
1086da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
1087da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
1088da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return getLayerIterator(id, numLayers);
10893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
10903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
10914df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden// Converts a PixelFormat to a human-readable string.  Max 11 chars.
10924df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden// (Could use a table of prefab String8 objects.)
10934df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFaddenstatic String8 getFormatStr(PixelFormat format) {
10944df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    switch (format) {
10954df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGBA_8888:    return String8("RGBA_8888");
10964df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGBX_8888:    return String8("RGBx_8888");
10974df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGB_888:      return String8("RGB_888");
10984df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGB_565:      return String8("RGB_565");
10994df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_BGRA_8888:    return String8("BGRA_8888");
11004df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_sRGB_A_8888:  return String8("sRGB_A_8888");
11014df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_sRGB_X_8888:  return String8("sRGB_x_8888");
1102f0058ca0e592485ac9e5a4aedb0dd8fa187625efAndy McFadden    case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
1103f0058ca0e592485ac9e5a4aedb0dd8fa187625efAndy McFadden                                    return String8("ImplDef");
11044df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    default:
11054df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        String8 result;
11064df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        result.appendFormat("? %08x", format);
11074df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        return result;
11084df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    }
11094df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden}
11104df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden
111174d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopianvoid HWComposer::dump(String8& result) const {
1112b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
11134df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        result.appendFormat("Hardware Composer state (version %08x):\n", hwcApiVersion(mHwc));
1114f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
1115f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
1116f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
111702d86567d95b99e1142941ed7ec23a4465822813Jesse Hall            if (!disp.connected)
111802d86567d95b99e1142941ed7ec23a4465822813Jesse Hall                continue;
1119b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
112013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const Vector< sp<Layer> >& visibleLayersSortedByZ =
1121cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian                    mFlinger->getLayerSortedByZForHwcDisplay(i);
1122cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
11237f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
11247f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            result.appendFormat("  Display[%zd] configurations (* current):\n", i);
11257f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            for (size_t c = 0; c < disp.configs.size(); ++c) {
11267f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                const DisplayConfig& config(disp.configs[c]);
11277f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                result.appendFormat("    %s%zd: %ux%u, xdpi=%f, ydpi=%f, refresh=%" PRId64 "\n",
11287f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        c == disp.currentConfig ? "* " : "", c, config.width, config.height,
11297f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        config.xdpi, config.ydpi, config.refresh);
11307f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            }
1131b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
113202d86567d95b99e1142941ed7ec23a4465822813Jesse Hall            if (disp.list) {
1133b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
113492dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn                        "  numHwLayers=%zu, flags=%08x\n",
1135b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        disp.list->numHwLayers, disp.list->flags);
1136b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
1137f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
11384df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                        "    type   |  handle  | hint | flag | tr | blnd |   format    |     source crop (l,t,r,b)      |          frame         | name \n"
11394df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                        "-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------\n");
11404df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                //      " _________ | ________ | ____ | ____ | __ | ____ | ___________ |_____._,_____._,_____._,_____._ |_____,_____,_____,_____ | ___...
1141f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1142f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
1143f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
1144da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    String8 name("unknown");
1145cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
1146da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (i < visibleLayersSortedByZ.size()) {
114713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<Layer>& layer(visibleLayersSortedByZ[i]);
114813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<GraphicBuffer>& buffer(
114913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                                layer->getActiveBuffer());
115013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        if (buffer != NULL) {
115113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                            format = buffer->getPixelFormat();
1152f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
1153da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = layer->getName();
1154da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
1155da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1156da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    int type = l.compositionType;
1157da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type == HWC_FRAMEBUFFER_TARGET) {
1158da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = "HWC_FRAMEBUFFER_TARGET";
1159da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        format = disp.format;
1160f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
1161da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1162da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    static char const* compositionTypeName[] = {
1163da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "GLES",
1164da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "HWC",
11654df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                            "BKGND",
1166da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "FB TARGET",
116703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                            "SIDEBAND",
116803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                            "HWC_CURSOR",
1169da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "UNKNOWN"};
1170da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type >= NELEM(compositionTypeName))
1171da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        type = NELEM(compositionTypeName) - 1;
1172da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
11734df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                    String8 formatStr = getFormatStr(format);
1174353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1175353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                        result.appendFormat(
11764df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d | %s\n",
1177353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        compositionTypeName[type],
11784df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                        intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
1179353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1180353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1181353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        name.string());
1182353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    } else {
1183353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                        result.appendFormat(
11844df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7d,%7d,%7d,%7d |%5d,%5d,%5d,%5d | %s\n",
1185353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        compositionTypeName[type],
11864df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                        intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
1187353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1188353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1189353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        name.string());
1190353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    }
1191fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
1192fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
11938372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
11941d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
119530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
119630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
119774d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian        const size_t SIZE = 4096;
119874d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian        char buffer[SIZE];
119930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
12001d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
12018372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
12028372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
12038372785879d329f592f6883620b5a32d80d74691Mathias Agopian
1204a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
12052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12062965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
12072965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
12082965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
1209b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
12102965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
12112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
12142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
121581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mEnabled != enabled) {
121681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mEnabled = enabled;
121781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mCondition.signal();
121881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
12192965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12202965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
12222965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
12232965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12242965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12252965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
12262965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
12272965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
12282965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
12292965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
12302965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
12312965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12332965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
12342965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
12352965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
12362965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
12372965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
12382965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
12392965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
12402965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
12412965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12422965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
12432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12442965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
12452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
12462965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
12472965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12482965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
12492965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
12502965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
12512965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
12522965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12532965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
12542965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
12552965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12562965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12572965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
12582965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12592965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
1260a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::DisplayData()
12617f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza:   configs(),
12627f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    currentConfig(0),
12637f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    format(HAL_PIXEL_FORMAT_RGBA_8888),
1264a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    connected(false),
1265a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    hasFbComp(false), hasOvComp(false),
1266a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    capacity(0), list(NULL),
1267a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    framebufferTarget(NULL), fbTargetHandle(0),
1268a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
1269851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
1270a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    events(0)
1271a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall{}
1272a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
1273a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::~DisplayData() {
1274a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    free(list);
1275a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall}
1276a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
12772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
1278a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
1279