HWComposer.cpp revision 99c7dbb24994df2f3e175f7b25dd2c9dd92a72f0
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
195880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// Uncomment this to remove support for HWC_DEVICE_API_VERSION_0_3 and older
2030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian#define HWC_REMOVE_DEPRECATED_VERSIONS 1
215880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
22a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <stdint.h>
23f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdio.h>
24f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdlib.h>
25f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <string.h>
26a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <sys/types.h>
27a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
28a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <utils/Errors.h>
29da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian#include <utils/misc.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
40a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
41e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian#include <cutils/properties.h>
42a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
43921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include "Layer.h"           // needed only for debugging
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
45c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
46da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian#include <utils/CallStack.h>
47a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
48a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
495880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
507296051995fbaea919480a6cc2dabb729dabc95bJesse Hall#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
519eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
529eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
539eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
549eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
559eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
569eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
579eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
589eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
599eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
609eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
619eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
629eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
639eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        uint32_t version) {
649eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
65bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
66bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
67a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
68a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
823eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
833eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
84b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        EventHandler& handler)
85c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
86b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mFbDev(0), mHwc(0), mNumDisplays(1),
873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
88e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
89e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
90a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
91e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
92e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
93e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
94b685c542836b93c99cd85053e07696406ea37adbJesse Hall
95e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
96e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
97e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
98e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
99028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
1005880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
101b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Note: some devices may insist that the FB HAL be opened before HWC.
102b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadFbHalModule();
103b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadHwcModule();
104bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
105da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
106da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // close FB HAL if we don't needed it.
107da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: this is temporary until we're not forced to open FB HAL
108da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // before HWC.
109da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        framebuffer_close(mFbDev);
110da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mFbDev = NULL;
111da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
112da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
113babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
114babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
115babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden            && !mFbDev) {
11643601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        ALOGE("ERROR: failed to open framebuffer, aborting");
11743601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        abort();
11843601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    }
11943601a2dc320a271ff8c3765ff61414a07221635Andy McFadden
120620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    // these display IDs are always reserved
121620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
122620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        mAllocatedDisplayIDs.markBit(i);
123620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
124620685c2e684082c82657732d1e35cefd0c79006Andy McFadden
125b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mHwc) {
126b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
127b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 24) & 0xff,
128b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 16) & 0xff);
129b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (mHwc->registerProcs) {
130b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->hwc = this;
131b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.invalidate = &hook_invalidate;
132b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.vsync = &hook_vsync;
133b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
134b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = &hook_hotplug;
135b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            else
136b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = NULL;
137b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
138b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mHwc->registerProcs(mHwc, &mCBContext->procs);
139b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        }
140b685c542836b93c99cd85053e07696406ea37adbJesse Hall
141b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // don't need a vsync thread if we have a hardware composer
142b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        needVSyncThread = false;
143b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // always turn vsync off when we start
14481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
1458f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
146b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // the number of displays we actually have depends on the
147b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // hw composer version
148b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
149b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.2 adds support for virtual displays
150b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = MAX_DISPLAYS;
151b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
152b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.1 adds support for multiple displays
153b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = HWC_NUM_DISPLAY_TYPES;
154b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else {
155b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = 1;
156c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1573a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1583a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
159b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
1601bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
1611bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                "should only have fbdev if no hwc or hwc is 1.0");
1621bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
163f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
16438e623bc5c2d7224ebd5b82efaea401bc8e5367aMathias Agopian        disp.connected = true;
165db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.width = mFbDev->width;
166db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.height = mFbDev->height;
167b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.format = mFbDev->format;
168b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.xdpi = mFbDev->xdpi;
169b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.ydpi = mFbDev->ydpi;
170f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
171b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            disp.refresh = nsecs_t(1e9 / mFbDev->fps);
172f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
173f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
174f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
175f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.refresh = nsecs_t(1e9 / 60.0);
1761bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            ALOGW("getting VSYNC period from thin air: %lld",
1771bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                    mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
178888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        }
1791bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    } else if (mHwc) {
1801604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // here we're guaranteed to have at least HWC 1.1
1811604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        for (size_t i =0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
1821604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            queryDisplayProperties(i);
1831604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
184888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
185888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1863a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1873a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1883a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
189a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
190a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
191a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
192a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
193babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if (mHwc) {
19481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
195babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    }
1963eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
1973eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
1983eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
199a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2005880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
201a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
202b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
203b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        framebuffer_close(mFbDev);
204b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
2053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
206a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
207a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
208b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the hardware composer module.  Sets mHwc.
209b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadHwcModule()
210b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
211b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
212b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
213b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
214b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
215b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
216b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
217b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
218b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = hwc_open_1(module, &mHwc);
219b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
220b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device failed to initialize (%s)",
221b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, strerror(-err));
222b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
223b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
224b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
225b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
226b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
227b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
228b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device version %#x unsupported, will not be used",
229b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, mHwc->common.version);
230b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        hwc_close_1(mHwc);
231b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mHwc = NULL;
232b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
233b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
234b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
235b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
236b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the FB HAL, which uses the gralloc module.  Sets mFbDev.
237b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadFbHalModule()
238b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
239b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
240b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
241b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) != 0) {
242b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
243b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
244b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
245b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
246b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = framebuffer_open(module, &mFbDev);
247b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
248b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("framebuffer_open failed (%s)", strerror(-err));
249b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
250b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
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) {
2822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
2831bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    mEventHandler.onVSyncReceived(disp, timestamp);
284d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
285d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mLastHwVSync = timestamp;
286d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
287d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
2881bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hotplug(int disp, int connected) {
2891bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    if (disp == HWC_DISPLAY_PRIMARY || disp >= HWC_NUM_DISPLAY_TYPES) {
2901bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
2911bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                disp, connected);
2921bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        return;
2931bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
294f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    queryDisplayProperties(disp);
295148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    mEventHandler.onHotplugReceived(disp, bool(connected));
2961bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
2971bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
2981bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallstatic const uint32_t DISPLAY_ATTRIBUTES[] = {
2991bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_VSYNC_PERIOD,
300db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_WIDTH,
301db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_HEIGHT,
3021bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_X,
3031bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_Y,
3041bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_NO_ATTRIBUTE,
3051bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall};
3061bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
3071bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3081bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall// http://developer.android.com/reference/android/util/DisplayMetrics.html
3091bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define ANDROID_DENSITY_TV    213
3101bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define ANDROID_DENSITY_XHIGH 320
3111bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3121604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopianstatus_t HWComposer::queryDisplayProperties(int disp) {
3131604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
314da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
3151bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
316b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // use zero as default value for unspecified attributes
3171bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
3181bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    memset(values, 0, sizeof(values));
3191bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3201bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    uint32_t config;
3211bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    size_t numConfigs = 1;
3221bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
3231604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (err != NO_ERROR) {
3241604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // this can happen if an unpluggable display is not connected
325f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        mDisplayData[disp].connected = false;
3261604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return err;
3271bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3281bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3299e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
3309e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    if (err != NO_ERROR) {
3319e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        // we can't get this display's info. turn it off.
3329e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        mDisplayData[disp].connected = false;
3339e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        return err;
3349e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    }
3351604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
3361bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t w = 0, h = 0;
3371bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
3381bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        switch (DISPLAY_ATTRIBUTES[i]) {
3391bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_VSYNC_PERIOD:
3401bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].refresh = nsecs_t(values[i]);
3411bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
342db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        case HWC_DISPLAY_WIDTH:
343db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            mDisplayData[disp].width = values[i];
3441bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
345db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        case HWC_DISPLAY_HEIGHT:
346db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            mDisplayData[disp].height = values[i];
3471bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3481bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_DPI_X:
3491bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = values[i] / 1000.0f;
3501bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3511bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_DPI_Y:
3521bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = values[i] / 1000.0f;
3531bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3541bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        default:
355da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
356da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    i, DISPLAY_ATTRIBUTES[i]);
3571bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3581bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3591bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3601bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
361f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    // FIXME: what should we set the format to?
362f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
363f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].connected = true;
3641bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
3651bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        // is there anything smarter we can do?
3661bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        if (h >= 1080) {
3671bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = ANDROID_DENSITY_XHIGH;
3681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = ANDROID_DENSITY_XHIGH;
3691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        } else {
3701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = ANDROID_DENSITY_TV;
3711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = ANDROID_DENSITY_TV;
3721bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3731bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3741604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    return NO_ERROR;
3751bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3761bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
377e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
378f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
379e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
380e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
381f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
382f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
3837adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = true;
384e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
385e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
386e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
387e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
388f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (id < HWC_NUM_DISPLAY_TYPES) {
389f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
390e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
391e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
392f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
393e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
394e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
395f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
3967adb0f8a9fdb961692ffd2f0c65cacb155143f64Jesse Hall    mDisplayData[id].connected = false;
397e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
398e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
399e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
400b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshPeriod(int disp) const {
401b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].refresh;
402888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian}
403888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
404b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshTimestamp(int disp) const {
405d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
406d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
407d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
408d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
409d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
410b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return now - ((now - mLastHwVSync) %  mDisplayData[disp].refresh);
411b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
412b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
4132ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennissp<Fence> HWComposer::getDisplayFence(int disp) const {
4142ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis    return mDisplayData[disp].lastDisplayFence;
4152ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis}
4162ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
4172ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
418db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getWidth(int disp) const {
419db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].width;
420b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
421b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
422db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getHeight(int disp) const {
423db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].height;
424b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
425b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
426b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
427b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].format;
4283eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4293eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
430b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
431b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].xdpi;
4328b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4338b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
434b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
435b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].ydpi;
4368b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4378b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
438f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopianbool HWComposer::isConnected(int disp) const {
439f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    return mDisplayData[disp].connected;
440f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
441f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
44281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
44381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
444620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
445620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
446620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
447620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
448620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
449620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
450620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
45181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
45281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
4533eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
45481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
45581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
45681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
45781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
45881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
45981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
46081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
46181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
46281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
46381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
46481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
46581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
46681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
46781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
46881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
46981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
470e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
47181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
47281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
47381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
47481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
4753eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
4763a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
4773a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
4783a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
4793a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
48031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
48131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
4825f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
483f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
4841e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
485e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
4861e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
48745721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
488f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
489da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
490da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
491da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
492da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
49313a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
494da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
495f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
496f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
497f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
498f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
49945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
500da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
501da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
502da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
5038f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden            const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
504da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
505da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
506da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
507da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
508da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
509da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
510da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->sourceCrop = r;
511da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
512da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
513da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
514da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
515da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
516da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
517da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
518db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
519f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
520f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
521a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
522a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
523a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
524a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
525da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
526da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
527da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
528da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
529da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
530da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
531da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
532da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
533e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian        // triggers a Surface::queueBuffer()  on some
534da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
535da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
536f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        CallStack stack;
537f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.update();
538f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.dump("");
539da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
540da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
541da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
542da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
5431df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    if (acquireFence->isValid()) {
544da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
545da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
546da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
547da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
548da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
549da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
550da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
551da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
552da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
553da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
554e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
555f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
556da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
557da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
558da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
559da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
560da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
561da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
56227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
563da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
564da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
56527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
56627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
56727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
56827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
569da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
570f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
571db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
572db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbuf = NULL;
573db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
574db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
575db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
576db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
577db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
578db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
579db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
580db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
581db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
582f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
583f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
584db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
585f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
586da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
587da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
5889c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
5895f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
5905f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
5915f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
592f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
593f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
594f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
595f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
596f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
597f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
598f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
599da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
600da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
601da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
602da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
603f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
604f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
605f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
606f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
607f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
608f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
609f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
610f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
611f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
612f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
6139c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
6149c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
6159c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
616a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
617a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
618a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
619e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
62099c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
621e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
622e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
623e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
6245f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
625e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
62699c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall    if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
62799c7dbb24994df2f3e175f7b25dd2c9dd92a72f0Jesse Hall        return true;
628e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
6299c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
6309c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::getAndResetReleaseFenceFd(int32_t id) {
632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
634da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
635da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
637da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
638da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
639da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
640d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
641da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
642da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
643da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
644da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return fd;
645da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
646da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
64730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
6488630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
6498630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
6509eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
65130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
652f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
653f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
65430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
65530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
65630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
657f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
65830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
659f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
660f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
661f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
6622ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis            disp.lastDisplayFence = disp.lastRetireFence;
6631df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis            disp.lastRetireFence = Fence::NO_FENCE;
664f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
665db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
6662ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis                    disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
667db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
668f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
669f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
670f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
6718630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
67258959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
673a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
674a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
675a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
67681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::release(int disp) {
677c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
6787ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
67981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(disp, HWC_EVENT_VSYNC, 0);
680c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 1);
6817ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
6827ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
6837ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
6847ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
68581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::acquire(int disp) {
686c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
68710fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
688c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 0);
68910fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
69010fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
69110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
69210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
69327ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
6945a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
6955a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    if (disp >= HWC_NUM_DISPLAY_TYPES) {
6965a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        // nothing to do for these yet
6975a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        return;
6985a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    }
69927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
70027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    if (dd.list != NULL) {
70127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        free(dd.list);
70227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.list = NULL;
70327ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.framebufferTarget = NULL;    // points into dd.list
7049e9689c11148521d2c16a121a0b87b062be0714cAndy McFadden        dd.fbTargetHandle = NULL;
70527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    }
70627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
70727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
708cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
709d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
710da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
711da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
712da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
713da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
714da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
715cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
716cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
717cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
718b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
719b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
720da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool HWComposer::supportsFramebufferTarget() const {
721da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
722da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
723da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
724da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::fbPost(int32_t id,
725da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
726d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
727da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return setFramebufferTarget(id, acquireFence, buffer);
728da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
7291df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis        acquireFence->waitForever(1000, "HWComposer::fbPost");
730da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->post(mFbDev, buffer->handle);
731cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
732cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian}
733cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
734cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::fbCompositionComplete() {
735d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
736da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
737da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
738da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev->compositionComplete) {
739da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->compositionComplete(mFbDev);
740da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
741da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return INVALID_OPERATION;
742b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
743b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
744b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
745b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::fbDump(String8& result) {
746cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
747b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        const size_t SIZE = 4096;
748b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        char buffer[SIZE];
749b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFbDev->dump(mFbDev, buffer, SIZE);
750b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        result.append(buffer);
751b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
752b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
753b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
7543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
7553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
7563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
7573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
7583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
7593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
7603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
7613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
7623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
7633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
7643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
7653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
7663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
7673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
7683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
7693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
7703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
7713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
7723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
7743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
7753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
7763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
7783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7795880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
7805880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
7815880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
7825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
7835880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
7849f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    struct hwc_composer_device_1* mHwc;
7853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
7869f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
7879f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
7883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
7903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
7913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
7933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
7943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
795ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
796ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
797ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
798ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
799ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
800dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
801dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
802dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
8039f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPerFrameDefaultState() {
8049f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        //getLayer()->compositionType = HWC_FRAMEBUFFER;
8059f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
8069f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPlaneAlpha(uint8_t alpha) {
8079f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
8089f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian            getLayer()->planeAlpha = alpha;
8099f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        } else {
8105fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            if (alpha < 0xFF) {
8115fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian                getLayer()->flags |= HWC_SKIP_LAYER;
8125fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            }
8139f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        }
8149f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
8153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
8169f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        hwc_layer_1_t* const l = getLayer();
8179f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->compositionType = HWC_FRAMEBUFFER;
8189f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->hints = 0;
8199f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->flags = HWC_SKIP_LAYER;
8209f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->handle = 0;
8219f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->transform = 0;
8229f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->blending = HWC_BLENDING_NONE;
8239f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.numRects = 0;
8249f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.rects = NULL;
8259f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->acquireFenceFd = -1;
8269f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->releaseFenceFd = -1;
8279f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->planeAlpha = 0xFF;
8283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
8303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
8313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
8343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
8373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
8383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
8403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
8413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
8433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
8443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8453e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
8463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
8473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
849c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // Region::getSharedBuffer creates a reference to the underlying
850c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // SharedBuffer of this Region, this reference is freed
851c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // in onDisplayed()
852c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
853c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
854c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
8553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
8573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
8583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
8593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
8613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
8633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
865c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    virtual void onDisplayed() {
866c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
867c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
868c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        if (sb) {
869c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            sb->release();
870c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            // not technically needed but safer
871c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.numRects = 0;
872c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.rects = NULL;
873c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        }
874e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall
875e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall        getLayer()->acquireFenceFd = -1;
876c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    }
8773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
8783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
8813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8825f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
883f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
8845f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
885f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
886f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
887f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
8883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
889f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
8909f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
8913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
8923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
8953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8965f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
8971e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
898a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
899a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
9003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
9013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
9023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
9035f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
904da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    size_t numLayers = 0;
905da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
906da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
907da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (mHwc && disp.list) {
908da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers = disp.list->numHwLayers;
909da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
910da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
911da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // which we ignore when iterating through the layer list.
912da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
913da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                if (numLayers) {
914da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    numLayers--;
915da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                }
916da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            }
917da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
918da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
919da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return getLayerIterator(id, numLayers);
9203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
9213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
922cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE) const {
923b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
924cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
925f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
926f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
927f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
928b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
92913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            const Vector< sp<Layer> >& visibleLayersSortedByZ =
930cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian                    mFlinger->getLayerSortedByZForHwcDisplay(i);
931cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
932b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.connected) {
933b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
934b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
935b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
936b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            }
937b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
938b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.list && disp.connected) {
939b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
940b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  numHwLayers=%u, flags=%08x\n",
941b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        disp.list->numHwLayers, disp.list->flags);
942b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
943f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
944da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "    type    |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
945da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
946da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                //      " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
947f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
948f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
949f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
950da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    String8 name("unknown");
951cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
952da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (i < visibleLayersSortedByZ.size()) {
95313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<Layer>& layer(visibleLayersSortedByZ[i]);
95413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        const sp<GraphicBuffer>& buffer(
95513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                                layer->getActiveBuffer());
95613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                        if (buffer != NULL) {
95713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian                            format = buffer->getPixelFormat();
958f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
959da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = layer->getName();
960da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
961da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
962da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    int type = l.compositionType;
963da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type == HWC_FRAMEBUFFER_TARGET) {
964da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = "HWC_FRAMEBUFFER_TARGET";
965da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        format = disp.format;
966f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
967da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
968da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    static char const* compositionTypeName[] = {
969da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "GLES",
970da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "HWC",
971da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "BACKGROUND",
972da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "FB TARGET",
973da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "UNKNOWN"};
974da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type >= NELEM(compositionTypeName))
975da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        type = NELEM(compositionTypeName) - 1;
976da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
977f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    result.appendFormat(
978da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
979da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    compositionTypeName[type],
980f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
981f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
982f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
983da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    name.string());
984fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
985fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
9868372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
9871d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
98830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
98930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
99030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
9911d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
9928372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
9938372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
9948372785879d329f592f6883620b5a32d80d74691Mathias Agopian
995a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
9962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9972965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
9982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
9992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
1000b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
10012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
10022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
10052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
100681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mEnabled != enabled) {
100781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mEnabled = enabled;
100881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mCondition.signal();
100981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
10102965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
10132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
10142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10162965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
10172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
10182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
10192965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
10202965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
10212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
10222965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10232965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10242965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
10252965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
10262965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
10272965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
10282965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
10292965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
10302965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
10312965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
10322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10332965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
10342965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10352965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
10362965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
10372965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
10382965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10392965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
10402965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
10412965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
10422965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
10432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10442965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
10452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
10462965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10472965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10482965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
10492965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10502965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
1051a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::DisplayData()
1052a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall:   width(0), height(0), format(0),
1053a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    xdpi(0.0f), ydpi(0.0f),
1054a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    refresh(0),
1055a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    connected(false),
1056a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    hasFbComp(false), hasOvComp(false),
1057a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    capacity(0), list(NULL),
1058a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    framebufferTarget(NULL), fbTargetHandle(0),
1059a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
1060a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    events(0)
1061a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall{}
1062a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
1063a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse HallHWComposer::DisplayData::~DisplayData() {
1064a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall    free(list);
1065a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall}
1066a9a1b006e48320f5c501473e51e6c4a5f7a17b88Jesse Hall
10672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
1068a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
1069