1a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian/*
2a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
4a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * you may not use this file except in compliance with the License.
6a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * You may obtain a copy of the License at
7a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
8a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian *
10a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * Unless required by applicable law or agreed to in writing, software
11a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * See the License for the specific language governing permissions and
14a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian * limitations under the License.
15a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian */
16a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#define ATRACE_TAG ATRACE_TAG_GRAPHICS
182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
1992dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn#include <inttypes.h>
2092dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn#include <math.h>
21a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <stdint.h>
22f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdio.h>
23f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdlib.h>
24f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <string.h>
25a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <sys/types.h>
26a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
27a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <utils/Errors.h>
28da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian#include <utils/misc.h>
29399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall#include <utils/NativeHandle.h>
308372785879d329f592f6883620b5a32d80d74691Mathias Agopian#include <utils/String8.h>
313eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <utils/Thread.h>
322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#include <utils/Trace.h>
3322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include <utils/Vector.h>
34a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
35921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <ui/GraphicBuffer.h>
36921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
383eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <hardware/hwcomposer.h>
39a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
401c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall#include <android/configuration.h>
411c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
42a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
43e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian#include <cutils/properties.h>
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
4633ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian
4733ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian#include "../Layer.h"           // needed only for debugging
4833ceeb32582739dd74e404593d9ddf8adf5100bbMathias Agopian#include "../SurfaceFlinger.h"
49a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
50a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
527296051995fbaea919480a6cc2dabb729dabc95bJesse Hall#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
539eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
549eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
559eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
569eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
579eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
589eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
599eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
609eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
619eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
629eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
639eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
649eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
659eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        uint32_t version) {
669eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
67bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
68bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
69a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
70a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
843eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
853eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
86b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        EventHandler& handler)
87c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
88b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mFbDev(0), mHwc(0), mNumDisplays(1),
893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
90e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
91bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian      mDebugForceFakeVSync(false)
92a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
939e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
94e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
95e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
96b685c542836b93c99cd85053e07696406ea37adbJesse Hall
97bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
98bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mLastHwVSync[i] = 0;
99bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mVSyncCounts[i] = 0;
100bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    }
101bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian
102e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
103e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
104e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
105e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
106028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
1075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
108b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Note: some devices may insist that the FB HAL be opened before HWC.
109ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    int fberr = loadFbHalModule();
110b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadHwcModule();
111bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
112da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
113da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // close FB HAL if we don't needed it.
114da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: this is temporary until we're not forced to open FB HAL
115da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // before HWC.
116da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        framebuffer_close(mFbDev);
117da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mFbDev = NULL;
118da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
119da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
120babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
121babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
122babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden            && !mFbDev) {
123ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall        ALOGE("ERROR: failed to open framebuffer (%s), aborting",
124ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall                strerror(-fberr));
12543601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        abort();
12643601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    }
12743601a2dc320a271ff8c3765ff61414a07221635Andy McFadden
128620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    // these display IDs are always reserved
1299e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
130620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        mAllocatedDisplayIDs.markBit(i);
131620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
132620685c2e684082c82657732d1e35cefd0c79006Andy McFadden
133b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mHwc) {
134b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
135b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 24) & 0xff,
136b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 16) & 0xff);
137b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (mHwc->registerProcs) {
138b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->hwc = this;
139b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.invalidate = &hook_invalidate;
140b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.vsync = &hook_vsync;
141b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
142b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = &hook_hotplug;
143b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            else
144b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = NULL;
145b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
146b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mHwc->registerProcs(mHwc, &mCBContext->procs);
147b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        }
148b685c542836b93c99cd85053e07696406ea37adbJesse Hall
149b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // don't need a vsync thread if we have a hardware composer
150b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        needVSyncThread = false;
151b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // always turn vsync off when we start
15281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
1538f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
154b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // the number of displays we actually have depends on the
155b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // hw composer version
15638efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
15738efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            // 1.3 adds support for virtual displays
1589e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall            mNumDisplays = MAX_HWC_DISPLAYS;
159b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
160b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.1 adds support for multiple displays
1619e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall            mNumDisplays = NUM_BUILTIN_DISPLAYS;
162b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else {
163b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = 1;
164c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1653a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1663a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
167b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
1681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
1691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                "should only have fbdev if no hwc or hwc is 1.0");
1701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
171f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
17238e623bc5c2d7224ebd5b82efaea401bc8e5367aMathias Agopian        disp.connected = true;
173b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.format = mFbDev->format;
1747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        DisplayConfig config = DisplayConfig();
1757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.width = mFbDev->width;
1767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.height = mFbDev->height;
1777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.xdpi = mFbDev->xdpi;
1787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.ydpi = mFbDev->ydpi;
1797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        config.refresh = nsecs_t(1e9 / mFbDev->fps);
1807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        disp.configs.push_back(config);
1817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        disp.currentConfig = 0;
1821bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    } else if (mHwc) {
1831604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // here we're guaranteed to have at least HWC 1.1
1849e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall        for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
1851604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            queryDisplayProperties(i);
1861604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
187888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
188888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1893a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1903a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1913a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
192a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
193a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
194a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
195a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
196babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if (mHwc) {
19781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
198babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    }
1993eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
2003eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
2013eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
202a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
204a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
205b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
206b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        framebuffer_close(mFbDev);
207b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
2083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
209a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
210a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
211b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the hardware composer module.  Sets mHwc.
212b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadHwcModule()
213b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
214b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
215b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
216b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
217b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
218b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
219b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
220b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
221b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = hwc_open_1(module, &mHwc);
222b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
223b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device failed to initialize (%s)",
224b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, strerror(-err));
225b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
226b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
227b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
228b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
229b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
230b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
231b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device version %#x unsupported, will not be used",
232b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, mHwc->common.version);
233b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        hwc_close_1(mHwc);
234b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mHwc = NULL;
235b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
236b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
237b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
238b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
239b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the FB HAL, which uses the gralloc module.  Sets mFbDev.
240ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hallint HWComposer::loadFbHalModule()
241b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
242b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
243b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
244ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
245ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    if (err != 0) {
246b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
247ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall        return err;
248b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
249b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
250ef64b75a936ffd194673dbce6766995f86b42ba9Jesse Hall    return framebuffer_open(module, &mFbDev);
251b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
252b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
253a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
254a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
255a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
256a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
257bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
258bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
259bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
260bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
261c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
262c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2631bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
264bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
265bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
266bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
2671bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->vsync(disp, timestamp);
2681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
2691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
2701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
2711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        int connected) {
2721bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
2731bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            const_cast<hwc_procs_t*>(procs));
2741bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->hotplug(disp, connected);
27531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
27631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
277c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
278e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
279c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
280c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2811bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::vsync(int disp, int64_t timestamp) {
282bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
2838e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall        {
2848e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            Mutex::Autolock _l(mLock);
2858e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
2868e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // There have been reports of HWCs that signal several vsync events
2878e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // with the same timestamp when turning the display off and on. This
2888e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // is a bug in the HWC implementation, but filter the extra events
2898e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            // out here so they don't cause havoc downstream.
2908e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            if (timestamp == mLastHwVSync[disp]) {
291258ee43fbcbc752cb08c134a12c08d5d99c4a866Michael Lentine                ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
2928e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall                        timestamp);
2938e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall                return;
2948e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            }
2958e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
2968e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall            mLastHwVSync[disp] = timestamp;
2978e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall        }
2988e26b28be62e7c0e093900540db5e5cdcf52a0ffJesse Hall
299bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        char tag[16];
300faf77cce9d9ec0238d6999b3bd0d40c71ff403c5Jamie Gennis        snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
301bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
302bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian
303bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian        mEventHandler.onVSyncReceived(disp, timestamp);
304bef42c50ebda2d63400f92611e1dd857c03bb38cMathias Agopian    }
305d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
306d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
3071bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hotplug(int disp, int connected) {
30810613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    if (disp >= VIRTUAL_DISPLAY_ID_BASE) {
3091bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
3101bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                disp, connected);
3111bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        return;
3121bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
313f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    queryDisplayProperties(disp);
31410613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    // Do not teardown or recreate the primary display
31510613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    if (disp != HWC_DISPLAY_PRIMARY) {
31610613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine        mEventHandler.onHotplugReceived(disp, bool(connected));
31710613dc3b565e95d3d5f459ec70fd2f6a77f807aMichael Lentine    }
3181bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3191bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
320bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hallstatic float getDefaultDensity(uint32_t width, uint32_t height) {
321bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // Default density is based on TVs: 1080p displays get XHIGH density,
322bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // lower-resolution displays get TV density. Maybe eventually we'll need
323bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // to update it for 4K displays, though hopefully those just report
324bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // accurate DPI information to begin with. This is also used for virtual
325bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // displays and even primary displays with older hwcomposers, so be
326bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    // careful about orientation.
327bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall
328bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    uint32_t h = width < height ? width : height;
329bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    if (h >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
330bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    else           return ACONFIGURATION_DENSITY_TV;
3311c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
3321c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
3331bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallstatic const uint32_t DISPLAY_ATTRIBUTES[] = {
3341bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_VSYNC_PERIOD,
335db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_WIDTH,
336db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_HEIGHT,
3371bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_X,
3381bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_Y,
3391bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_NO_ATTRIBUTE,
3401bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall};
3411bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
3421bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3431604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopianstatus_t HWComposer::queryDisplayProperties(int disp) {
3441604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
345da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
3461bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
347b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // use zero as default value for unspecified attributes
3481bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
3491bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    memset(values, 0, sizeof(values));
3501bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3517f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    const size_t MAX_NUM_CONFIGS = 128;
3527f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    uint32_t configs[MAX_NUM_CONFIGS] = {0};
3537f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t numConfigs = MAX_NUM_CONFIGS;
3547f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    status_t err = mHwc->getDisplayConfigs(mHwc, disp, configs, &numConfigs);
3551604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (err != NO_ERROR) {
3561604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // this can happen if an unpluggable display is not connected
357f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        mDisplayData[disp].connected = false;
3581604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return err;
3591bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3601bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3617f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[disp].currentConfig = 0;
3627f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    for (size_t c = 0; c < numConfigs; ++c) {
3637f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
3647f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                DISPLAY_ATTRIBUTES, values);
3657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (err != NO_ERROR) {
3667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            // we can't get this display's info. turn it off.
3677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].connected = false;
3687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            return err;
3697f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
3707f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
3717f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        DisplayConfig config = DisplayConfig();
3727f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
3737f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            switch (DISPLAY_ATTRIBUTES[i]) {
3747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_VSYNC_PERIOD:
3757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.refresh = nsecs_t(values[i]);
3767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_WIDTH:
3787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.width = values[i];
3797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_HEIGHT:
3817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.height = values[i];
3827f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3837f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_X:
3847f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.xdpi = values[i] / 1000.0f;
3857f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3867f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                case HWC_DISPLAY_DPI_Y:
3877f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    config.ydpi = values[i] / 1000.0f;
3887f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                default:
390b54ee7704e46529e02386cf39eba93a34a527c61Michael Lentine                    ALOG_ASSERT(false, "unknown display attribute[%zu] %#x",
3917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                            i, DISPLAY_ATTRIBUTES[i]);
3927f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    break;
3937f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            }
3947f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        }
3951604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
3967f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        if (config.xdpi == 0.0f || config.ydpi == 0.0f) {
397bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall            float dpi = getDefaultDensity(config.width, config.height);
3987f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.xdpi = dpi;
3997f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            config.ydpi = dpi;
4001bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
4017f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4027f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza        mDisplayData[disp].configs.push_back(config);
4031bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
4041bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
405f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    // FIXME: what should we set the format to?
406f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
407f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].connected = true;
4081604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    return NO_ERROR;
4091bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
4101bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
4111c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hallstatus_t HWComposer::setVirtualDisplayProperties(int32_t id,
4121c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        uint32_t w, uint32_t h, uint32_t format) {
4131c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
4141c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall            !mAllocatedDisplayIDs.hasBit(id)) {
4151c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall        return BAD_INDEX;
4161c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    }
4177f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[id].currentConfig;
4181c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    mDisplayData[id].format = format;
4197f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    DisplayConfig& config = mDisplayData[id].configs.editItemAt(configId);
4207f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.width = w;
4217f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    config.height = h;
422bacc28ef1df329f4dc21bae44b09a6c5018af908Jesse Hall    config.xdpi = config.ydpi = getDefaultDensity(w, h);
4231c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall    return NO_ERROR;
4241c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall}
4251c569c4d45f89ec05abf8f8fe3a560e68bf39a8eJesse Hall
426e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
427f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
428e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
429e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
430f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
431f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
4327adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = true;
4337f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].configs.resize(1);
4347f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    mDisplayData[id].currentConfig = 0;
435e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
436e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
437e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
438e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
4399e663de4fe1dcc872373ee530c60a375624671c3Jesse Hall    if (id < NUM_BUILTIN_DISPLAYS) {
440f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
441e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
442e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
443f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
444e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
445e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
446f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
4477adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = false;
448e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
449e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
450e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
451b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshTimestamp(int disp) const {
452d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
453d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
454d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
455d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
456d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
4577f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t configId = mDisplayData[disp].currentConfig;
4587f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return now - ((now - mLastHwVSync[disp]) %
4597f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            mDisplayData[disp].configs[configId].refresh);
460b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
461b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
4622ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennissp<Fence> HWComposer::getDisplayFence(int disp) const {
4632ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis    return mDisplayData[disp].lastDisplayFence;
4642ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis}
4652ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
466b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
4674e3e30c2d5a2a6f7dc4ee8696b1ac2a52dffd5dcAjay Dudani    if (static_cast<uint32_t>(disp) >= MAX_HWC_DISPLAYS || !mAllocatedDisplayIDs.hasBit(disp)) {
46819e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return HAL_PIXEL_FORMAT_RGBA_8888;
46919e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    } else {
47019e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall        return mDisplayData[disp].format;
47119e872912af66c53a4350afcc333bbafaf6a2294Jesse Hall    }
4723eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4733eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
4747f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozabool HWComposer::isConnected(int disp) const {
4757f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].connected;
4767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getWidth(int disp) const {
4797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4807f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].width;
4817f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4827f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
4837f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozauint32_t HWComposer::getHeight(int disp) const {
4847f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4857f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].height;
4867f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
4877f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
488b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
4897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4907f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].xdpi;
4918b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4928b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
493b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
4947f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
4957f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].ydpi;
4968b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4978b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
4987f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozansecs_t HWComposer::getRefreshPeriod(int disp) const {
4997f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    size_t currentConfig = mDisplayData[disp].currentConfig;
5007f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs[currentConfig].refresh;
5017f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
5027f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
5037f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozaconst Vector<HWComposer::DisplayConfig>& HWComposer::getConfigs(int disp) const {
5047f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].configs;
5057f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza}
5067f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
5077f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stozasize_t HWComposer::getCurrentConfig(int disp) const {
5087f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    return mDisplayData[disp].currentConfig;
509f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
510f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
51181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
51281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
513620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
514620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
515620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
516620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
517620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
518620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
519620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
52081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
52181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
5223eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
52381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
52481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
52581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
52681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
52781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
52881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
52981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
53081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
53181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
53281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
53381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
53481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
53581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
53681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
53781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
5384d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall
5394d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                char tag[16];
5404d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
5414d407a0f05ad9ba3c232ff5b6742495660c0e4abJesse Hall                ATRACE_INT(tag, enabled);
54281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
543e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
54481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
54581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
54681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
54781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
5483eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
5493a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
5503a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
5513a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
5523a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
55331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
55431d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
5555f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
556f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
5571e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
558e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
5591e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
56045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
561f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
562da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
563da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
564da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
565da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
56613a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
567da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
568f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
569f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
570f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
571f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
57245721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
573da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
574da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
575da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
5767f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const DisplayConfig& currentConfig =
5777f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    disp.configs[disp.currentConfig];
5787f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            const hwc_rect_t r = { 0, 0,
5797f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                    (int) currentConfig.width, (int) currentConfig.height };
580da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
581da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
582da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
583da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
584da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
585da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
5868f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
5878f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.left = 0;
5888f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCropf.top = 0;
5897f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.right =
5907f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.width;
5917f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                disp.framebufferTarget->sourceCropf.bottom =
5927f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        currentConfig.height;
5938f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            } else {
5948f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian                disp.framebufferTarget->sourceCrop = r;
5958f63c2049b93819c41d390fc4222b9307e3a61c7Mathias Agopian            }
596da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
597da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
598da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
599da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
600da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
601da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
60270a6e88dbd52605f35f290eba52a05bdc6fc8042Mathias Agopian            disp.framebufferTarget->planeAlpha = 0xFF;
603da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
604db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
605f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
606f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
607a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
608a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
609a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
610a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
611da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
612da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
613da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
614da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
615da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
616da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
617da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
618da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
619e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian        // triggers a Surface::queueBuffer()  on some
620da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
621da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
622da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
623da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
624da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
625da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
6261df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    if (acquireFence->isValid()) {
627da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
628da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
629da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
634da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
635da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
637e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
638e04e4edcd1a852058775caa34cf73e2c20dc1066Manoj Kumar AVM    Mutex::Autolock _l(mDisplayLock);
639f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
640da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
641da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
642da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
643da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
644da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
645da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
64627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
647da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
648da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
64927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
650258ee43fbcbc752cb08c134a12c08d5d99c4a866Michael Lentine            ALOGW("WARNING: disp %zu: connected, non-null list, layers=%zu",
65127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
65227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
653da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
654f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
65538efe86d9459cf5c96a24a34cc5cbf31fdba7e19Jesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
656f7a675837bdad03d398c9b6f0f593b9c51c679b5Jesse Hall                mLists[i]->outbuf = disp.outbufHandle;
657db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
658db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
659db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
660db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
661db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
662db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
663db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
664db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
665db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
666f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
667f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
668db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
669f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
670da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
671da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
6729c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
6735f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
6745f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
6755f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
676d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        //
677d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // If there are no window layers, we treat the display has having FB
678d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // composition, because SurfaceFlinger will use GLES to draw the
679d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall        // wormhole region.
680f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
681f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
682f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
683f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
684f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
685f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
686f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
687da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
688da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
689da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
690da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
691f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
692f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
693f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
694f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
695f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
696f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
697f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
698f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
699f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
70003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    if (l.compositionType == HWC_CURSOR_OVERLAY) {
70103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                        disp.hasOvComp = true;
70203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                    }
703f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
704d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
705d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                    disp.hasFbComp = true;
706d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                }
707d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall            } else {
708d05a17fbb3772051d287f1f8830a7f00964f7ec2Jesse Hall                disp.hasFbComp = true;
7099c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
7109c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
7119c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
712a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
713a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
714a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
715e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
71699c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
717e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
718e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
719e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
7205f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
721e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
72299c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
72399c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall        return true;
724e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
7259c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
7269c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
72713f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hallsp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
728da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
72913f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        return Fence::NO_FENCE;
730da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
731da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
732da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
733da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
734da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
735da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
736d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
737da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
738da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
739da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
74013f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
741da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
742da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
74330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
7448630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
7458630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
7469eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
74730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
748f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
749f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
75030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
75130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
75230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
753f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
754afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall        for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
75580e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            DisplayData& disp(mDisplayData[i]);
756851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall            if (disp.outbufHandle) {
757851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                mLists[i]->outbuf = disp.outbufHandle;
75880e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall                mLists[i]->outbufAcquireFenceFd =
759851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall                        disp.outbufAcquireFence->dup();
76080e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall            }
76180e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall        }
76280e0a397a4712666661ecc629a64ec26e7f6aac3Jesse Hall
76330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
764f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
765f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
766f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
7672ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis            disp.lastDisplayFence = disp.lastRetireFence;
7681df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis            disp.lastRetireFence = Fence::NO_FENCE;
769f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
770db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
7712ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis                    disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
772db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
773f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
774f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
775f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
7768630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
77758959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
778a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
779a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
780a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
7812c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malanistatus_t HWComposer::setPowerMode(int disp, int mode) {
782afaf14b9fbfe8943d845e2f01e8a401ad7a4d854Jesse Hall    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
7837ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
7842c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (mode == HWC_POWER_MODE_OFF) {
7852c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            eventControl(disp, HWC_EVENT_VSYNC, 0);
7862c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
7872c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
7882c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->setPowerMode(mHwc, disp, mode);
7892c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        } else {
7902c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani            return (status_t)mHwc->blank(mHwc, disp,
7912c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani                    mode == HWC_POWER_MODE_OFF ? 1 : 0);
7922c9b11f0291210c9b9513a1a0cce6afebd361b3bPrashant Malani        }
79310fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
79410fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
79510fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
79610fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
7976c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentinestatus_t HWComposer::setActiveConfig(int disp, int mode) {
7986c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
799d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    DisplayData& dd(mDisplayData[disp]);
800d3e6914ceaafebaee09b0095aad6d001b75e787aMichael Lentine    dd.currentConfig = mode;
8016c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
8026c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        return (status_t)mHwc->setActiveConfig(mHwc, disp, mode);
8036c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    } else {
8046c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine        LOG_FATAL_IF(mode != 0);
8056c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    }
8066c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine    return NO_ERROR;
8076c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine}
8086c9e34a98a63033b80bd1c24c7aa1304f912f10aMichael Lentine
80927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
8105a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
81127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
812851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    free(dd.list);
813851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.list = NULL;
814851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.framebufferTarget = NULL;    // points into dd.list
815851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.fbTargetHandle = NULL;
816851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufHandle = NULL;
817851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastRetireFence = Fence::NO_FENCE;
818851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.lastDisplayFence = Fence::NO_FENCE;
819851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    dd.outbufAcquireFence = Fence::NO_FENCE;
820949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // clear all the previous configs and repopulate when a new
821949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    // device is added
822949ea0868de1df7e2f28a32d777cc41f901d6770Naseer Ahmed    dd.configs.clear();
82327ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
82427ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
825cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
826d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
827da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
828da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
829da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
830da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
831da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
832cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
833cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
834cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
835b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
836b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
837da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool HWComposer::supportsFramebufferTarget() const {
838da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
839da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
840da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
841da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::fbPost(int32_t id,
842da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
843d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
844da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return setFramebufferTarget(id, acquireFence, buffer);
845da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
846ea74d3b78d607cde17790a7bb83e6f68ffd34cfdMathias Agopian        acquireFence->waitForever("HWComposer::fbPost");
847da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->post(mFbDev, buffer->handle);
848cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
849cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian}
850cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
851cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::fbCompositionComplete() {
852d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
853da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
854da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
855da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev->compositionComplete) {
856da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->compositionComplete(mFbDev);
857da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
858da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return INVALID_OPERATION;
859b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
860b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
861b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
862b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::fbDump(String8& result) {
863cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
864b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        const size_t SIZE = 4096;
865b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        char buffer[SIZE];
866b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFbDev->dump(mFbDev, buffer, SIZE);
867b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        result.append(buffer);
868b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
869b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
870b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
871851cfe834295224cd64bdd499872b95b19c4de8cJesse Hallstatus_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
872851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        const sp<GraphicBuffer>& buf) {
873851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
874851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return BAD_INDEX;
875851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (id < VIRTUAL_DISPLAY_ID_BASE)
876851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return INVALID_OPERATION;
877851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
878851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    DisplayData& disp(mDisplayData[id]);
879851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    disp.outbufHandle = buf->handle;
880851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    disp.outbufAcquireFence = acquireFence;
881851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    return NO_ERROR;
882851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall}
883851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
88441d67d7ab4da1c393497a620a116a854b3c618e7Andy McFaddensp<Fence> HWComposer::getLastRetireFence(int32_t id) const {
885851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
886851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall        return Fence::NO_FENCE;
887851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    return mDisplayData[id].lastRetireFence;
888851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall}
889851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall
89003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrewsstatus_t HWComposer::setCursorPositionAsync(int32_t id, const Rect& pos)
89103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews{
89203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    if (mHwc->setCursorPositionAsync) {
89303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        return (status_t)mHwc->setCursorPositionAsync(mHwc, id, pos.left, pos.top);
89403414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
89503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    else {
89603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        return NO_ERROR;
89703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
89803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews}
89903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews
9003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
9013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
9023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
9033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
9043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
9053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
9063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
9073e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
9083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
9093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
9103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
9113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
9123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
9133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
9143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
9153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
9163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
9173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
9183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
9203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
9213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
9223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
9243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
9255880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
9265880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
9275880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
9285880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
9295880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
9309f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    struct hwc_composer_device_1* mHwc;
9313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
9329f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
9339f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
9343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
9353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
9363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
9373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
9393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
9403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
94113f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    virtual sp<Fence> getAndResetReleaseFence() {
942ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
943ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
94413f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall        return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
945ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
946dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
947dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
948dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
9499f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPerFrameDefaultState() {
9509f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        //getLayer()->compositionType = HWC_FRAMEBUFFER;
9519f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
9529f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPlaneAlpha(uint8_t alpha) {
9539f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
9549f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian            getLayer()->planeAlpha = alpha;
9559f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        } else {
9565fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            if (alpha < 0xFF) {
9575fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian                getLayer()->flags |= HWC_SKIP_LAYER;
9585fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            }
9599f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        }
9609f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
9613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
9629f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        hwc_layer_1_t* const l = getLayer();
9639f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->compositionType = HWC_FRAMEBUFFER;
9649f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->hints = 0;
9659f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->flags = HWC_SKIP_LAYER;
9669f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->handle = 0;
9679f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->transform = 0;
9689f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->blending = HWC_BLENDING_NONE;
9699f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.numRects = 0;
9709f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.rects = NULL;
9719f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->acquireFenceFd = -1;
9729f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->releaseFenceFd = -1;
9739f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->planeAlpha = 0xFF;
9743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
9763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
9773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
9783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
9793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
9803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
9813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
98203414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    virtual void setIsCursorLayerHint(bool isCursor) {
98303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
98403414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            if (isCursor) {
98503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                getLayer()->flags |= HWC_IS_CURSOR_LAYER;
98603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            }
98703414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            else {
98803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                getLayer()->flags &= ~HWC_IS_CURSOR_LAYER;
98903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews            }
99003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        }
99103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
9923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
9933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
9943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
9963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
9973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
9983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
9996b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
10003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
10016b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    virtual void setCrop(const FloatRect& crop) {
10026b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
10036b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
10046b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        } else {
10056b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            /*
10066b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * Since h/w composer didn't support a flot crop rect before version 1.3,
10076b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * using integer coordinates instead produces a different output from the GL code in
10086b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
10096b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * window size ratio is large and a window crop is defined
10106b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
10116b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian             */
10126b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            hwc_rect_t& r = getLayer()->sourceCrop;
10136b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.left  = int(ceilf(crop.left));
10146b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.top   = int(ceilf(crop.top));
10156b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.right = int(floorf(crop.right));
10166b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian            r.bottom= int(floorf(crop.bottom));
10176b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian        }
10183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
10193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
1020c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // Region::getSharedBuffer creates a reference to the underlying
1021c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // SharedBuffer of this Region, this reference is freed
1022c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // in onDisplayed()
1023c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
1024c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
1025c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
10263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
1027ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza    virtual void setSurfaceDamage(const Region& reg) {
1028ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_5)) {
1029ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            return;
1030ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        }
1031ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        hwc_region_t& surfaceDamage = getLayer()->surfaceDamage;
1032ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        // We encode default full-screen damage as INVALID_RECT upstream, but as
1033ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        // 0 rects for HWComposer
1034ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        if (reg.isRect() && reg.getBounds() == Rect::INVALID_RECT) {
1035ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            surfaceDamage.numRects = 0;
1036ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            surfaceDamage.rects = NULL;
1037ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            return;
1038ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        }
1039ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        SharedBuffer const* sb = reg.getSharedBuffer(&surfaceDamage.numRects);
1040ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        surfaceDamage.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
1041ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza    }
1042399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    virtual void setSidebandStream(const sp<NativeHandle>& stream) {
1043399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        ALOG_ASSERT(stream->handle() != NULL);
1044399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        getLayer()->compositionType = HWC_SIDEBAND;
1045399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        getLayer()->sidebandStream = stream->handle();
1046399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    }
10473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
10483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
10493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
10503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
10513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
10523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
1053399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall            if (getLayer()->compositionType == HWC_SIDEBAND) {
1054399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                // If this was a sideband layer but the stream was removed, reset
1055399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare.
1056399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall                getLayer()->compositionType = HWC_FRAMEBUFFER;
1057399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall            }
10583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
10593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
10603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
1061c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    virtual void onDisplayed() {
1062c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
1063c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
1064c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        if (sb) {
1065c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            sb->release();
1066c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            // not technically needed but safer
1067c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.numRects = 0;
1068c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.rects = NULL;
1069c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        }
1070e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall
1071e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall        getLayer()->acquireFenceFd = -1;
1072ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza
1073ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_5)) {
1074ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            return;
1075ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        }
1076ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza
1077ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        hwc_region_t& surfaceDamage = getLayer()->surfaceDamage;
1078ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        sb = SharedBuffer::bufferFromData(surfaceDamage.rects);
1079ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        if (sb) {
1080ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            sb->release();
1081ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            surfaceDamage.numRects = 0;
1082ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza            surfaceDamage.rects = NULL;
1083ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza        }
1084c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    }
10853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
10863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
10873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
10883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
10893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
10905f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
1091f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
10925f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
1093f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
1094f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
1095f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
10963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
1097f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
10989f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
10993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
11003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
11013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
11023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
11033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
11045f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
11051e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
1106a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
1107a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
11083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
11093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
11103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
11115f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
1112da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    size_t numLayers = 0;
1113da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
1114da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
1115da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (mHwc && disp.list) {
1116da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers = disp.list->numHwLayers;
1117da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
1118da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
1119da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // which we ignore when iterating through the layer list.
1120da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
1121da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                if (numLayers) {
1122da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    numLayers--;
1123da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                }
1124da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            }
1125da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
1126da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
1127da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return getLayerIterator(id, numLayers);
11283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
11293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
11304df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden// Converts a PixelFormat to a human-readable string.  Max 11 chars.
11314df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden// (Could use a table of prefab String8 objects.)
11324df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFaddenstatic String8 getFormatStr(PixelFormat format) {
11334df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    switch (format) {
11344df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGBA_8888:    return String8("RGBA_8888");
11354df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGBX_8888:    return String8("RGBx_8888");
11364df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGB_888:      return String8("RGB_888");
11374df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_RGB_565:      return String8("RGB_565");
11384df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    case PIXEL_FORMAT_BGRA_8888:    return String8("BGRA_8888");
1139f0058ca0e592485ac9e5a4aedb0dd8fa187625efAndy McFadden    case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
1140f0058ca0e592485ac9e5a4aedb0dd8fa187625efAndy McFadden                                    return String8("ImplDef");
11414df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    default:
11424df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        String8 result;
11434df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        result.appendFormat("? %08x", format);
11444df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        return result;
11454df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden    }
11464df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden}
11474df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden
114874d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopianvoid HWComposer::dump(String8& result) const {
1149e04e4edcd1a852058775caa34cf73e2c20dc1066Manoj Kumar AVM    Mutex::Autolock _l(mDisplayLock);
1150b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
11514df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden        result.appendFormat("Hardware Composer state (version %08x):\n", hwcApiVersion(mHwc));
1152f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
1153f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
1154f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
115502d86567d95b99e1142941ed7ec23a4465822813Jesse Hall            if (!disp.connected)
115602d86567d95b99e1142941ed7ec23a4465822813Jesse Hall                continue;
1157b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
115813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const Vector< sp<Layer> >& visibleLayersSortedByZ =
1159cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian                    mFlinger->getLayerSortedByZForHwcDisplay(i);
1160cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
11617f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza
11627f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            result.appendFormat("  Display[%zd] configurations (* current):\n", i);
11637f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            for (size_t c = 0; c < disp.configs.size(); ++c) {
11647f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                const DisplayConfig& config(disp.configs[c]);
11657f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                result.appendFormat("    %s%zd: %ux%u, xdpi=%f, ydpi=%f, refresh=%" PRId64 "\n",
11667f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        c == disp.currentConfig ? "* " : "", c, config.width, config.height,
11677f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza                        config.xdpi, config.ydpi, config.refresh);
11687f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza            }
1169b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
117002d86567d95b99e1142941ed7ec23a4465822813Jesse Hall            if (disp.list) {
1171b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
117292dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn                        "  numHwLayers=%zu, flags=%08x\n",
1173b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        disp.list->numHwLayers, disp.list->flags);
1174b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
1175f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
11764df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                        "    type   |  handle  | hint | flag | tr | blnd |   format    |     source crop (l,t,r,b)      |          frame         | name \n"
11774df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                        "-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------\n");
11784df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                //      " _________ | ________ | ____ | ____ | __ | ____ | ___________ |_____._,_____._,_____._,_____._ |_____,_____,_____,_____ | ___...
1179f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1180f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
1181f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
1182da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    String8 name("unknown");
1183cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
1184da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (i < visibleLayersSortedByZ.size()) {
118513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<Layer>& layer(visibleLayersSortedByZ[i]);
118613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<GraphicBuffer>& buffer(
118713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                                layer->getActiveBuffer());
118813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        if (buffer != NULL) {
118913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                            format = buffer->getPixelFormat();
1190f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
1191da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = layer->getName();
1192da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
1193da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1194da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    int type = l.compositionType;
1195da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type == HWC_FRAMEBUFFER_TARGET) {
1196da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = "HWC_FRAMEBUFFER_TARGET";
1197da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        format = disp.format;
1198f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
1199da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
1200da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    static char const* compositionTypeName[] = {
1201da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "GLES",
1202da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "HWC",
12034df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                            "BKGND",
1204da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "FB TARGET",
120503414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                            "SIDEBAND",
120603414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews                            "HWC_CURSOR",
1207da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "UNKNOWN"};
1208da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type >= NELEM(compositionTypeName))
1209da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        type = NELEM(compositionTypeName) - 1;
1210da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
12114df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                    String8 formatStr = getFormatStr(format);
1212353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1213353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                        result.appendFormat(
12144df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d | %s\n",
1215353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        compositionTypeName[type],
12164df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                        intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
1217353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1218353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1219353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        name.string());
1220353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    } else {
1221353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                        result.appendFormat(
12224df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7d,%7d,%7d,%7d |%5d,%5d,%5d,%5d | %s\n",
1223353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        compositionTypeName[type],
12244df87bd1f6f68126e5e9081fc1365ae500e375dbAndy McFadden                                        intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
1225353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1226353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1227353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                                        name.string());
1228353ddc6e77816352107537c215dc8302f6e8587aJesse Hall                    }
1229fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
1230fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
12318372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
12321d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
123330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
123430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
123574d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian        const size_t SIZE = 4096;
123674d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian        char buffer[SIZE];
123730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
12381d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
12398372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
12408372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
12418372785879d329f592f6883620b5a32d80d74691Mathias Agopian
1242a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
12432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12442965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
12452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
12462965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
1247b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
12482965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
12492965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12502965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12512965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
12522965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
125381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mEnabled != enabled) {
125481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mEnabled = enabled;
125581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mCondition.signal();
125681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
12572965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12582965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12592965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
12602965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
12612965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12622965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12632965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
12642965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
12652965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
12662965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
12672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
12682965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
12692965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12712965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
12722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
12732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
12742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
12752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
12762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
12772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
12782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
12792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
12812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
12832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
12842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
12852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
12872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
12882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
12892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
12902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
12922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
12932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
12942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
12952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
12962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
12972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
1298a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::DisplayData()
12997f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza:   configs(),
13007f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    currentConfig(0),
13017f7da32569f8e0b3d383a40b95f8ac1d55afd801Dan Stoza    format(HAL_PIXEL_FORMAT_RGBA_8888),
1302a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    connected(false),
1303a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    hasFbComp(false), hasOvComp(false),
1304a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    capacity(0), list(NULL),
1305a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    framebufferTarget(NULL), fbTargetHandle(0),
1306a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
1307851cfe834295224cd64bdd499872b95b19c4de8cJesse Hall    outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
1308a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    events(0)
1309a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall{}
1310a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
1311a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::~DisplayData() {
1312a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    free(list);
1313a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall}
1314a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
13152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
1316a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
1317