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
4422da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include "LayerBase.h"
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
46c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
47da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian#include <utils/CallStack.h>
48a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
49a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
505880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
519eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall#define MIN_HWC_HEADER_VERSION 0
529eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
539eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
549eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
559eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    if (MIN_HWC_HEADER_VERSION == 0 &&
569eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall            (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
579eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        // legacy version encoding
589eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        hwcVersion <<= 16;
599eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    }
609eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
619eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
629eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
639eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
649eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
659eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    if (MIN_HWC_HEADER_VERSION == 0 &&
669eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall            (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
679eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        // legacy version encoding
689eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        hwcVersion <<= 16;
699eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    }
709eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
719eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
729eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
739eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
749eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        uint32_t version) {
759eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
76bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
77bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
78a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
79a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
933eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
943eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
95b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        EventHandler& handler)
96c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
97b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mFbDev(0), mHwc(0), mNumDisplays(1),
983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
99e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
100e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
101a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
102e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
103e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
104e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
105b685c542836b93c99cd85053e07696406ea37adbJesse Hall
106e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
107e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
108e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
109e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
110028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
1115880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
112b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Note: some devices may insist that the FB HAL be opened before HWC.
113b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadFbHalModule();
114b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadHwcModule();
115bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
116da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
117da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // close FB HAL if we don't needed it.
118da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: this is temporary until we're not forced to open FB HAL
119da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // before HWC.
120da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        framebuffer_close(mFbDev);
121da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mFbDev = NULL;
122da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
123da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
124babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
125babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
126babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden            && !mFbDev) {
12743601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        ALOGE("ERROR: failed to open framebuffer, aborting");
12843601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        abort();
12943601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    }
13043601a2dc320a271ff8c3765ff61414a07221635Andy McFadden
131620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    // these display IDs are always reserved
132620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
133620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        mAllocatedDisplayIDs.markBit(i);
134620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
135620685c2e684082c82657732d1e35cefd0c79006Andy McFadden
136b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mHwc) {
137b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
138b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 24) & 0xff,
139b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 16) & 0xff);
140b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (mHwc->registerProcs) {
141b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->hwc = this;
142b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.invalidate = &hook_invalidate;
143b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.vsync = &hook_vsync;
144b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
145b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = &hook_hotplug;
146b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            else
147b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = NULL;
148b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
149b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mHwc->registerProcs(mHwc, &mCBContext->procs);
150b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        }
151b685c542836b93c99cd85053e07696406ea37adbJesse Hall
152b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // don't need a vsync thread if we have a hardware composer
153b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        needVSyncThread = false;
154b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // always turn vsync off when we start
15581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
1568f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
157b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // the number of displays we actually have depends on the
158b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // hw composer version
159b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
160b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.2 adds support for virtual displays
161b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = MAX_DISPLAYS;
162b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
163b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.1 adds support for multiple displays
164b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = HWC_NUM_DISPLAY_TYPES;
165b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else {
166b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = 1;
167c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1683a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1693a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
170b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
1711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
1721bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                "should only have fbdev if no hwc or hwc is 1.0");
1731bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
174f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
17538e623bc5c2d7224ebd5b82efaea401bc8e5367aMathias Agopian        disp.connected = true;
176db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.width = mFbDev->width;
177db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.height = mFbDev->height;
178b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.format = mFbDev->format;
179b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.xdpi = mFbDev->xdpi;
180b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.ydpi = mFbDev->ydpi;
181f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
182b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            disp.refresh = nsecs_t(1e9 / mFbDev->fps);
183f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
184f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
185f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
186f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.refresh = nsecs_t(1e9 / 60.0);
1871bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            ALOGW("getting VSYNC period from thin air: %lld",
1881bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                    mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
189888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        }
1901bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    } else if (mHwc) {
1911604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // here we're guaranteed to have at least HWC 1.1
1921604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        for (size_t i =0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
1931604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            queryDisplayProperties(i);
1941604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
195888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
196888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1973a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1983a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1993a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
200a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
201a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
202a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
203a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
204babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if (mHwc) {
20581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
206babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    }
2073eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
2083eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
2093eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
210a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2115880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
212a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
213b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
214b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        framebuffer_close(mFbDev);
215b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
2163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
217a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
218a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
219b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the hardware composer module.  Sets mHwc.
220b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadHwcModule()
221b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
222b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
223b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
224b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
225b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
226b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
227b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
228b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
229b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = hwc_open_1(module, &mHwc);
230b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
231b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device failed to initialize (%s)",
232b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, strerror(-err));
233b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
234b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
235b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
236b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
237b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
238b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
239b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device version %#x unsupported, will not be used",
240b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, mHwc->common.version);
241b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        hwc_close_1(mHwc);
242b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mHwc = NULL;
243b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
244b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
245b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
246b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
247b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the FB HAL, which uses the gralloc module.  Sets mFbDev.
248b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadFbHalModule()
249b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
250b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
251b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
252b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) != 0) {
253b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
254b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
255b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
256b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
257b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = framebuffer_open(module, &mFbDev);
258b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
259b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("framebuffer_open failed (%s)", strerror(-err));
260b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
261b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
262b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
263b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
264a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
265a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
266a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
267a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
268bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
269bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
270bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
271bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
272c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
273c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2741bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
275bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
276bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
277bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
2781bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->vsync(disp, timestamp);
2791bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
2801bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
2811bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
2821bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        int connected) {
2831bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
2841bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            const_cast<hwc_procs_t*>(procs));
2851bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->hotplug(disp, connected);
28631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
28731d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
288c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
289e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
290c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
291c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2921bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::vsync(int disp, int64_t timestamp) {
2932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
2941bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    mEventHandler.onVSyncReceived(disp, timestamp);
295d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
296d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mLastHwVSync = timestamp;
297d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
298d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
2991bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hotplug(int disp, int connected) {
3001bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    if (disp == HWC_DISPLAY_PRIMARY || disp >= HWC_NUM_DISPLAY_TYPES) {
3011bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
3021bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                disp, connected);
3031bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        return;
3041bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
305f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    queryDisplayProperties(disp);
306148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    mEventHandler.onHotplugReceived(disp, bool(connected));
3071bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3081bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3091bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallstatic const uint32_t DISPLAY_ATTRIBUTES[] = {
3101bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_VSYNC_PERIOD,
311db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_WIDTH,
312db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_HEIGHT,
3131bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_X,
3141bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_Y,
3151bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_NO_ATTRIBUTE,
3161bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall};
3171bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
3181bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3191bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall// http://developer.android.com/reference/android/util/DisplayMetrics.html
3201bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define ANDROID_DENSITY_TV    213
3211bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define ANDROID_DENSITY_XHIGH 320
3221bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3231604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopianstatus_t HWComposer::queryDisplayProperties(int disp) {
3241604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
325da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
3261bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
327b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // use zero as default value for unspecified attributes
3281bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
3291bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    memset(values, 0, sizeof(values));
3301bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3311bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    uint32_t config;
3321bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    size_t numConfigs = 1;
3331bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
3341604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (err != NO_ERROR) {
3351604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // this can happen if an unpluggable display is not connected
336f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        mDisplayData[disp].connected = false;
3371604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return err;
3381bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3391bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3409e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
3419e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    if (err != NO_ERROR) {
3429e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        // we can't get this display's info. turn it off.
3439e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        mDisplayData[disp].connected = false;
3449e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        return err;
3459e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    }
3461604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
3471bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t w = 0, h = 0;
3481bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
3491bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        switch (DISPLAY_ATTRIBUTES[i]) {
3501bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_VSYNC_PERIOD:
3511bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].refresh = nsecs_t(values[i]);
3521bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
353db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        case HWC_DISPLAY_WIDTH:
354db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            mDisplayData[disp].width = values[i];
3551bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
356db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        case HWC_DISPLAY_HEIGHT:
357db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            mDisplayData[disp].height = values[i];
3581bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3591bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_DPI_X:
3601bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = values[i] / 1000.0f;
3611bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3621bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_DPI_Y:
3631bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = values[i] / 1000.0f;
3641bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3651bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        default:
366da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
367da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    i, DISPLAY_ATTRIBUTES[i]);
3681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
372f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    // FIXME: what should we set the format to?
373f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
374f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].connected = true;
3751bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
3761bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        // is there anything smarter we can do?
3771bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        if (h >= 1080) {
3781bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = ANDROID_DENSITY_XHIGH;
3791bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = ANDROID_DENSITY_XHIGH;
3801bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        } else {
3811bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = ANDROID_DENSITY_TV;
3821bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = ANDROID_DENSITY_TV;
3831bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3841bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3851604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    return NO_ERROR;
3861bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3871bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
388e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
389f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
390e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
391e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
392f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
393f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
394e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
395e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
396e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
397e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
398f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (id < HWC_NUM_DISPLAY_TYPES) {
399f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
400e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
401e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
402f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
403e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
404e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
405f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
406e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
407e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
408e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
409b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshPeriod(int disp) const {
410b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].refresh;
411888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian}
412888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
413b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshTimestamp(int disp) const {
414d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
415d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
416d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
417d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
418d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
419b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return now - ((now - mLastHwVSync) %  mDisplayData[disp].refresh);
420b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
421b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
422db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getWidth(int disp) const {
423db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].width;
424b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
425b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
426db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getHeight(int disp) const {
427db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].height;
428b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
429b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
430b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
431b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].format;
4323eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4333eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
434b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
435b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].xdpi;
4368b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4378b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
438b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
439b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].ydpi;
4408b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4418b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
442f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopianbool HWComposer::isConnected(int disp) const {
443f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    return mDisplayData[disp].connected;
444f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
445f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
44681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
44781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
448620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
449620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
450620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
451620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
452620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
453620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
454620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
45581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
45681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
4573eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
45881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
45981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
46081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
46181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
46281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
46381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
46481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
46581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
46681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
46781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
46881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
46981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
47081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
47181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
47281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
47381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
474e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
47581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
47681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
47781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
47881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
4793eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
4803a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
4813a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
4823a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
4833a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
48431d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
48531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
4865f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
487f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
4881e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
489e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
4901e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
49145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
492f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
493da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
494da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
495da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
496da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
49713a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
498da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
499f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
500f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
501f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
502f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
50345721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
504da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
505da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
506da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
507da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            const hwc_rect_t r = { 0, 0, disp.width, disp.height };
508da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
509da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
510da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
511da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
512da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
513da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
514da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->sourceCrop = r;
515da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
516da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
517da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
518da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
519da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
520da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
521da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
522db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
523f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
524f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
525a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
526a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
527a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
528a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
529da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
530da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
531da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
532da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
533da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
534da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
535da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
536da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
537da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // triggers a SurfaceTextureClient::queueBuffer()  on some
538da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
539da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
540f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        CallStack stack;
541f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.update();
542f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.dump("");
543da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
544da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
545da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
546da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
547da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (acquireFence != NULL) {
548da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
549da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
550da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
551da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
552da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
553da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
554da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
555da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
556da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
557da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
558e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
559f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
560da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
561da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
562da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
563da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
564da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
565da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
56627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
567da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
568da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
56927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
57027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
57127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
57227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
573da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
574f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
575db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
576db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbuf = NULL;
577db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
578db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
579db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
580db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
581db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
582db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
583db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
584db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
585db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
586f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
587f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
588db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
589f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
590da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
591da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
5929c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
5935f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
5945f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
5955f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
596f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
597f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
598f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
599f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
600f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
601f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
602f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
603da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
604da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
605da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
606da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
607f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
608f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
609f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
610f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
611f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
612f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
613f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
614f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
615f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
616f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
6179c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
6189c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
6199c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
620a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
621a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
622a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
623e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
624f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
625e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
626e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
627e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
6285f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
629e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
630f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
631e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
632e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
6339c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
6349c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
635da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::getAndResetReleaseFenceFd(int32_t id) {
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
637da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
638da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
639da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
640da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
641da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
642da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
643da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
644d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
645da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
646da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
647da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
648da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return fd;
649da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
650da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
65130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
6528630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
6538630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
6549eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
65530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
656f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
657f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
65830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
65930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
66030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
661f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
66230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
663f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
664f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
665f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
666f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
667db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
668db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    close(disp.list->retireFenceFd);
669db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
670f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
671f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
672f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
6738630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
67458959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
675a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
676a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
677a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
67881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::release(int disp) {
679c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
6807ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
68181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(disp, HWC_EVENT_VSYNC, 0);
682c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 1);
6837ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
6847ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
6857ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
6867ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
68781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::acquire(int disp) {
688c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
68910fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
690c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 0);
69110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
69210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
69310fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
69410fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
69527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
6965a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
6975a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    if (disp >= HWC_NUM_DISPLAY_TYPES) {
6985a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        // nothing to do for these yet
6995a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        return;
7005a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    }
70127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
70227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    if (dd.list != NULL) {
70327ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        free(dd.list);
70427ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.list = NULL;
70527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.framebufferTarget = NULL;    // points into dd.list
7069e9689c11148521d2c16a121a0b87b062be0714cAndy McFadden        dd.fbTargetHandle = NULL;
70727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    }
70827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
70927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
710cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
711d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
712da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
713da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
714da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
715da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
716da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
717cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
718cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
719cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
720b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
721b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
722da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool HWComposer::supportsFramebufferTarget() const {
723da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
724da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
725da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
726da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::fbPost(int32_t id,
727da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
728d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
729da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return setFramebufferTarget(id, acquireFence, buffer);
730da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
731da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (acquireFence != NULL) {
732ba607d53c6a94ea8c4c12571980c4ad159af308bJesse Hall            acquireFence->waitForever(1000, "HWComposer::fbPost");
733da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
734da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->post(mFbDev, buffer->handle);
735cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
736cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian}
737cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
738cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::fbCompositionComplete() {
739d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
740da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
741da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
742da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev->compositionComplete) {
743da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->compositionComplete(mFbDev);
744da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
745da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return INVALID_OPERATION;
746b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
747b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
748b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
749b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::fbDump(String8& result) {
750cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
751b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        const size_t SIZE = 4096;
752b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        char buffer[SIZE];
753b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFbDev->dump(mFbDev, buffer, SIZE);
754b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        result.append(buffer);
755b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
756b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
757b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
7583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
7593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
7603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
7613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
7623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
7633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
7643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
7653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
7663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
7673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
7683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
7693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
7703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
7713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
7723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
7733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
7743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
7753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
7763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
7783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
7793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
7803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
7823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7835880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
7845880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
7855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
7865880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
7875880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
7883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
7895880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion1(hwc_layer_1_t* layer)
7905880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
7913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
7933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
7943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
7963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
7973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
798ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
799ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
800ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
801ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
802ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
803dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
804dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
805dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
806ee932d0ad1a16cc93b4bd9eaf9cb3cc756fb2dfcMathias Agopian    virtual void setPerFrameDefaultState() {
8073e095b251503d71bea04d6b707e8188cd30034e2Mathias Agopian        //getLayer()->compositionType = HWC_FRAMEBUFFER;
808ee932d0ad1a16cc93b4bd9eaf9cb3cc756fb2dfcMathias Agopian    }
8093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
8103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->compositionType = HWC_FRAMEBUFFER;
8113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->hints = 0;
8123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->flags = HWC_SKIP_LAYER;
81355882dea1b1b305f15192b96ff7814f18babb212Mathias Agopian        getLayer()->handle = 0;
8143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = 0;
8153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = HWC_BLENDING_NONE;
8163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.numRects = 0;
8173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects = NULL;
8185880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->acquireFenceFd = -1;
8195880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->releaseFenceFd = -1;
8203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
8223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
8233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
8263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
8293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
8303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
8323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
8333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
8353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
8363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
8383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
8393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
841c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // Region::getSharedBuffer creates a reference to the underlying
842c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // SharedBuffer of this Region, this reference is freed
843c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // in onDisplayed()
844c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
845c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
846c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
8473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
8493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
8503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
8513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
8533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
8553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
857c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    virtual void onDisplayed() {
858c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
859c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
860c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        if (sb) {
861c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            sb->release();
862c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            // not technically needed but safer
863c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.numRects = 0;
864c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.rects = NULL;
865c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        }
866e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall
867e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall        getLayer()->acquireFenceFd = -1;
868c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    }
8693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
8703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
8733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8745f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
875f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
8765f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
877f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
878f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
879f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
8803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
881f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
882f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
8833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
8843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
8873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8885f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
8891e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
890a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
891a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
8923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
8943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8955f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
896da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    size_t numLayers = 0;
897da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
898da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
899da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (mHwc && disp.list) {
900da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers = disp.list->numHwLayers;
901da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
902da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
903da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // which we ignore when iterating through the layer list.
904da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
905da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                if (numLayers) {
906da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    numLayers--;
907da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                }
908da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            }
909da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
910da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
911da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return getLayerIterator(id, numLayers);
9123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
9133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
914cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE) const {
915b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
916cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
917f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
918f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
919f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
920b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
921cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian            const Vector< sp<LayerBase> >& visibleLayersSortedByZ =
922cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian                    mFlinger->getLayerSortedByZForHwcDisplay(i);
923cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
924b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.connected) {
925b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
926b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
927b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
928b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            }
929b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
930b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.list && disp.connected) {
931b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
932b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  numHwLayers=%u, flags=%08x\n",
933b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        disp.list->numHwLayers, disp.list->flags);
934b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
935f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
936da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "    type    |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
937da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
938da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                //      " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
939f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
940f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
941f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
942da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    String8 name("unknown");
943cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
944da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (i < visibleLayersSortedByZ.size()) {
945da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        const sp<LayerBase>& layer(visibleLayersSortedByZ[i]);
946da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        if (layer->getLayer() != NULL) {
947da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            const sp<GraphicBuffer>& buffer(
948f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                layer->getLayer()->getActiveBuffer());
949da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            if (buffer != NULL) {
950da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                format = buffer->getPixelFormat();
951da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            }
952f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
953da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = layer->getName();
954da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
955da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
956da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    int type = l.compositionType;
957da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type == HWC_FRAMEBUFFER_TARGET) {
958da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = "HWC_FRAMEBUFFER_TARGET";
959da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        format = disp.format;
960f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
961da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
962da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    static char const* compositionTypeName[] = {
963da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "GLES",
964da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "HWC",
965da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "BACKGROUND",
966da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "FB TARGET",
967da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "UNKNOWN"};
968da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type >= NELEM(compositionTypeName))
969da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        type = NELEM(compositionTypeName) - 1;
970da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
971f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    result.appendFormat(
972da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
973da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    compositionTypeName[type],
974f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
975f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
976f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
977da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    name.string());
978fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
979fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
9808372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
9811d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
98230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
98330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
98430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
9851d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
9868372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
9878372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
9888372785879d329f592f6883620b5a32d80d74691Mathias Agopian
989a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
9902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9912965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
9922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
9932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
994b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
9952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
9962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
9972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
9992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
100081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mEnabled != enabled) {
100181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mEnabled = enabled;
100281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mCondition.signal();
100381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
10042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10062965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
10072965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
10082965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10092965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10102965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
10112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
10122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
10132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
10142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
10152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
10162965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
10192965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
10202965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
10212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
10222965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
10232965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
10242965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
10252965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
10262965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10272965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
10282965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10292965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
10302965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
10312965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
10322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10332965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
10342965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
10352965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
10362965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
10372965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10382965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
10392965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
10402965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10412965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10422965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
10432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10442965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
1046a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
1047