HWComposer.cpp revision 7296051995fbaea919480a6cc2dabb729dabc95b
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
517296051995fbaea919480a6cc2dabb729dabc95bJesse Hall#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
529eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
539eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
549eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
559eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
569eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
579eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
589eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
599eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    uint32_t hwcVersion = hwc->common.version;
609eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
619eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall}
629eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall
639eb1eb5bb55740982ceae9966fc536824edc302aJesse Hallstatic bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
649eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        uint32_t version) {
659eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall    return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
66bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
67bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
68a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
69a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
833eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
843eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
85b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        EventHandler& handler)
86c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
87b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mFbDev(0), mHwc(0), mNumDisplays(1),
883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
89e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
90e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
91a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
92e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
93e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
94e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
95b685c542836b93c99cd85053e07696406ea37adbJesse Hall
96e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
97e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
98e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
99e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
100028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
1015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
102b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // Note: some devices may insist that the FB HAL be opened before HWC.
103b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadFbHalModule();
104b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    loadHwcModule();
105bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
106da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
107da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // close FB HAL if we don't needed it.
108da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: this is temporary until we're not forced to open FB HAL
109da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // before HWC.
110da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        framebuffer_close(mFbDev);
111da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mFbDev = NULL;
112da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
113da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
114babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
115babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
116babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden            && !mFbDev) {
11743601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        ALOGE("ERROR: failed to open framebuffer, aborting");
11843601a2dc320a271ff8c3765ff61414a07221635Andy McFadden        abort();
11943601a2dc320a271ff8c3765ff61414a07221635Andy McFadden    }
12043601a2dc320a271ff8c3765ff61414a07221635Andy McFadden
121620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    // these display IDs are always reserved
122620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
123620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        mAllocatedDisplayIDs.markBit(i);
124620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
125620685c2e684082c82657732d1e35cefd0c79006Andy McFadden
126b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mHwc) {
127b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
128b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 24) & 0xff,
129b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              (hwcApiVersion(mHwc) >> 16) & 0xff);
130b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (mHwc->registerProcs) {
131b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->hwc = this;
132b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.invalidate = &hook_invalidate;
133b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mCBContext->procs.vsync = &hook_vsync;
134b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
135b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = &hook_hotplug;
136b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            else
137b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden                mCBContext->procs.hotplug = NULL;
138b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
139b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mHwc->registerProcs(mHwc, &mCBContext->procs);
140b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        }
141b685c542836b93c99cd85053e07696406ea37adbJesse Hall
142b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // don't need a vsync thread if we have a hardware composer
143b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        needVSyncThread = false;
144b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // always turn vsync off when we start
14581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
1468f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
147b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // the number of displays we actually have depends on the
148b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        // hw composer version
149b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
150b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.2 adds support for virtual displays
151b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = MAX_DISPLAYS;
152b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
153b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            // 1.1 adds support for multiple displays
154b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = HWC_NUM_DISPLAY_TYPES;
155b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        } else {
156b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            mNumDisplays = 1;
157c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1583a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1593a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
160b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
1611bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
1621bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                "should only have fbdev if no hwc or hwc is 1.0");
1631bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
164f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
16538e623bc5c2d7224ebd5b82efaea401bc8e5367aMathias Agopian        disp.connected = true;
166db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.width = mFbDev->width;
167db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.height = mFbDev->height;
168b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.format = mFbDev->format;
169b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.xdpi = mFbDev->xdpi;
170b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        disp.ydpi = mFbDev->ydpi;
171f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
172b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            disp.refresh = nsecs_t(1e9 / mFbDev->fps);
173f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
174f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
175f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
176f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.refresh = nsecs_t(1e9 / 60.0);
1771bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            ALOGW("getting VSYNC period from thin air: %lld",
1781bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                    mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
179888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        }
1801bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    } else if (mHwc) {
1811604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // here we're guaranteed to have at least HWC 1.1
1821604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        for (size_t i =0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
1831604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian            queryDisplayProperties(i);
1841604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        }
185888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
186888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1873a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1883a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1893a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
190a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
191a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
192a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
193a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
194babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    if (mHwc) {
19581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
196babba1868773eba5edf8a8e335b8e109a32292e0Andy McFadden    }
1973eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
1983eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
1993eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
200a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
202a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
203b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (mFbDev) {
204b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        framebuffer_close(mFbDev);
205b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
2063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
207a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
208a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
209b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the hardware composer module.  Sets mHwc.
210b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadHwcModule()
211b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
212b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
213b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
214b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
215b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
216b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
217b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
218b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
219b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = hwc_open_1(module, &mHwc);
220b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
221b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device failed to initialize (%s)",
222b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, strerror(-err));
223b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
224b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
225b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
226b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
227b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
228b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden            hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
229b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s device version %#x unsupported, will not be used",
230b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden              HWC_HARDWARE_COMPOSER, mHwc->common.version);
231b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        hwc_close_1(mHwc);
232b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mHwc = NULL;
233b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
234b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
235b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
236b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
237b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden// Load and prepare the FB HAL, which uses the gralloc module.  Sets mFbDev.
238b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::loadFbHalModule()
239b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden{
240b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    hw_module_t const* module;
241b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
242b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) != 0) {
243b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
244b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
245b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
246b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
247b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    int err = framebuffer_open(module, &mFbDev);
248b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    if (err) {
249b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        ALOGE("framebuffer_open failed (%s)", strerror(-err));
250b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        return;
251b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
252b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
253b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
254a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
255a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
256a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
257a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
258bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
259bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
260bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
261bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
262c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
263c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2641bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
265bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
266bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
267bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
2681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->vsync(disp, timestamp);
2691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
2701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
2711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
2721bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        int connected) {
2731bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
2741bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            const_cast<hwc_procs_t*>(procs));
2751bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    ctx->hwc->hotplug(disp, connected);
27631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
27731d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
278c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
279e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
280c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
281c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
2821bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::vsync(int disp, int64_t timestamp) {
2832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
2841bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    mEventHandler.onVSyncReceived(disp, timestamp);
285d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
286d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mLastHwVSync = timestamp;
287d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
288d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
2891bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallvoid HWComposer::hotplug(int disp, int connected) {
2901bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    if (disp == HWC_DISPLAY_PRIMARY || disp >= HWC_NUM_DISPLAY_TYPES) {
2911bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
2921bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall                disp, connected);
2931bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        return;
2941bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
295f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    queryDisplayProperties(disp);
296148994e5f33ce240ff24ceb5bc0500b7f2001959Mathias Agopian    mEventHandler.onHotplugReceived(disp, bool(connected));
2971bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
2981bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
2991bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hallstatic const uint32_t DISPLAY_ATTRIBUTES[] = {
3001bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_VSYNC_PERIOD,
301db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_WIDTH,
302db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    HWC_DISPLAY_HEIGHT,
3031bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_X,
3041bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_DPI_Y,
3051bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    HWC_DISPLAY_NO_ATTRIBUTE,
3061bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall};
3071bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
3081bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3091bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall// http://developer.android.com/reference/android/util/DisplayMetrics.html
3101bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define ANDROID_DENSITY_TV    213
3111bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall#define ANDROID_DENSITY_XHIGH 320
3121bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3131604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopianstatus_t HWComposer::queryDisplayProperties(int disp) {
3141604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
315da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
3161bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
317b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    // use zero as default value for unspecified attributes
3181bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
3191bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    memset(values, 0, sizeof(values));
3201bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3211bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    uint32_t config;
3221bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    size_t numConfigs = 1;
3231bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
3241604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    if (err != NO_ERROR) {
3251604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        // this can happen if an unpluggable display is not connected
326f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian        mDisplayData[disp].connected = false;
3271604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian        return err;
3281bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3291bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
3309e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
3319e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    if (err != NO_ERROR) {
3329e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        // we can't get this display's info. turn it off.
3339e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        mDisplayData[disp].connected = false;
3349e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian        return err;
3359e2463e71796964cfaa06bf09a880875ac3537bcMathias Agopian    }
3361604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian
3371bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    int32_t w = 0, h = 0;
3381bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
3391bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        switch (DISPLAY_ATTRIBUTES[i]) {
3401bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_VSYNC_PERIOD:
3411bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].refresh = nsecs_t(values[i]);
3421bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
343db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        case HWC_DISPLAY_WIDTH:
344db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            mDisplayData[disp].width = values[i];
3451bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
346db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        case HWC_DISPLAY_HEIGHT:
347db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            mDisplayData[disp].height = values[i];
3481bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3491bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_DPI_X:
3501bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = values[i] / 1000.0f;
3511bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3521bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        case HWC_DISPLAY_DPI_Y:
3531bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = values[i] / 1000.0f;
3541bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3551bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        default:
356da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
357da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    i, DISPLAY_ATTRIBUTES[i]);
3581bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            break;
3591bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3601bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3611bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
362f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    // FIXME: what should we set the format to?
363f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
364f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    mDisplayData[disp].connected = true;
3651bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
3661bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        // is there anything smarter we can do?
3671bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        if (h >= 1080) {
3681bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = ANDROID_DENSITY_XHIGH;
3691bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = ANDROID_DENSITY_XHIGH;
3701bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        } else {
3711bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].xdpi = ANDROID_DENSITY_TV;
3721bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall            mDisplayData[disp].ydpi = ANDROID_DENSITY_TV;
3731bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall        }
3741bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall    }
3751604f777d11c40daae8ec91d8ea75625996bfbacMathias Agopian    return NO_ERROR;
3761bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall}
3771bd20e0f9ecd27cc5ad2bdf08b01837ecc10c357Jesse Hall
378e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
379f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
380e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
381e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
382f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
383f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
384e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
385e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
386e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
387e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
388f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (id < HWC_NUM_DISPLAY_TYPES) {
389f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
390e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
391e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
392f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
393e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
394e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
395f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
396e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
397e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
398e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
399b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshPeriod(int disp) const {
400b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].refresh;
401888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian}
402888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
403b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddennsecs_t HWComposer::getRefreshTimestamp(int disp) const {
404d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
405d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
406d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
407d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
408d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
409b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return now - ((now - mLastHwVSync) %  mDisplayData[disp].refresh);
410b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
411b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
412db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getWidth(int disp) const {
413db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].width;
414b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
415b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
416db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getHeight(int disp) const {
417db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].height;
418b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
419b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
420b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
421b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].format;
4223eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4233eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
424b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
425b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].xdpi;
4268b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4278b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
428b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
429b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].ydpi;
4308b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4318b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
432f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopianbool HWComposer::isConnected(int disp) const {
433f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    return mDisplayData[disp].connected;
434f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
435f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
43681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
43781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
438620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
439620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
440620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
441620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
442620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
443620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
444620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
44581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
44681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
4473eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
44881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
44981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
45081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
45181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
45281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
45381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
45481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
45581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
45681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
45781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
45881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
45981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
46081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
46181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
46281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
46381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
464e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
46581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
46681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
46781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
46881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
4693eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
4703a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
4713a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
4723a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
4733a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
47431d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
47531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
4765f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
477f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
4781e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
479e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
4801e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
48145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
482f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
483da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
484da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
485da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
486da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
48713a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
488da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
489f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
490f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
491f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
492f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
49345721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
494da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
495da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
496da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
497da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            const hwc_rect_t r = { 0, 0, disp.width, disp.height };
498da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
499da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
500da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
501da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
502da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
503da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
504da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->sourceCrop = r;
505da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
506da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
507da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
508da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
509da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
510da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
511da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
512db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
513f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
514f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
515a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
516a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
517a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
518a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
519da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
520da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
521da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
522da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
523da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
524da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
525da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
526da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
527da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // triggers a SurfaceTextureClient::queueBuffer()  on some
528da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
529da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
530f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        CallStack stack;
531f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.update();
532f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.dump("");
533da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
534da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
535da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
536da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
537da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (acquireFence != NULL) {
538da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
539da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
540da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
541da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
542da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
543da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
544da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
545da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
546da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
547da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
548e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
549f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
550da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
551da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
552da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
553da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
554da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
555da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
55627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
557da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
558da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
55927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
56027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
56127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
56227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
563da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
564f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
565db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
566db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbuf = NULL;
567db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
568db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
569db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
570db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
571db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
572db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
573db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
574db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
575db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
576f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
577f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
578db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
579f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
580da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
581da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
5829c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
5835f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
5845f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
5855f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
586f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
587f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
588f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
589f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
590f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
591f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
592f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
593da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
594da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
595da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
596da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
597f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
598f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
599f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
600f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
601f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
602f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
603f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
604f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
605f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
606f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
6079c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
6089c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
6099c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
610a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
611a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
612a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
613e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
614f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
615e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
616e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
617e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
6185f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
619e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
620f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
621e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
622e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
6239c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
6249c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
625da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::getAndResetReleaseFenceFd(int32_t id) {
626da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
627da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
628da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
629da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
634d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
635da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
637da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
638da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return fd;
639da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
640da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
64130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
6428630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
6438630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
6449eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
64530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
646f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
647f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
64830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
64930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
65030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
651f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
65230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
653f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
654f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
655f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
656f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
657db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
658db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    close(disp.list->retireFenceFd);
659db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
660f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
661f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
662f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
6638630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
66458959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
665a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
666a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
667a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
66881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::release(int disp) {
669c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
6707ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
67181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(disp, HWC_EVENT_VSYNC, 0);
672c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 1);
6737ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
6747ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
6757ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
6767ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
67781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::acquire(int disp) {
678c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
67910fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
680c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 0);
68110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
68210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
68310fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
68410fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
68527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
6865a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
6875a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    if (disp >= HWC_NUM_DISPLAY_TYPES) {
6885a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        // nothing to do for these yet
6895a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        return;
6905a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    }
69127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
69227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    if (dd.list != NULL) {
69327ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        free(dd.list);
69427ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.list = NULL;
69527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.framebufferTarget = NULL;    // points into dd.list
6969e9689c11148521d2c16a121a0b87b062be0714cAndy McFadden        dd.fbTargetHandle = NULL;
69727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    }
69827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
69927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
700cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
701d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
702da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
703da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
704da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
705da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
706da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
707cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
708cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
709cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
710b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
711b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
712da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool HWComposer::supportsFramebufferTarget() const {
713da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
714da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
715da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
716da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::fbPost(int32_t id,
717da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
718d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
719da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return setFramebufferTarget(id, acquireFence, buffer);
720da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
721da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (acquireFence != NULL) {
722ba607d53c6a94ea8c4c12571980c4ad159af308bJesse Hall            acquireFence->waitForever(1000, "HWComposer::fbPost");
723da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
724da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->post(mFbDev, buffer->handle);
725cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
726cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian}
727cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
728cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::fbCompositionComplete() {
729d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
730da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
731da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
732da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev->compositionComplete) {
733da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->compositionComplete(mFbDev);
734da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
735da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return INVALID_OPERATION;
736b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
737b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
738b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
739b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::fbDump(String8& result) {
740cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
741b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        const size_t SIZE = 4096;
742b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        char buffer[SIZE];
743b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFbDev->dump(mFbDev, buffer, SIZE);
744b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        result.append(buffer);
745b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
746b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
747b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
7483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
7493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
7503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
7513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
7523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
7533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
7543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
7553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
7563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
7573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
7583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
7593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
7603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
7613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
7623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
7633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
7643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
7653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
7663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
7683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
7693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
7703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
7723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7735880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
7745880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
7755880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
7765880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
7775880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
7783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
7795880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion1(hwc_layer_1_t* layer)
7805880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
7813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
7833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
7843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
7863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
7873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
788ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
789ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
790ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
791ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
792ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
793dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
794dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
795dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
7963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
7983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->compositionType = HWC_FRAMEBUFFER;
7993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->hints = 0;
8003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->flags = HWC_SKIP_LAYER;
80155882dea1b1b305f15192b96ff7814f18babb212Mathias Agopian        getLayer()->handle = 0;
8023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = 0;
8033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = HWC_BLENDING_NONE;
8043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.numRects = 0;
8053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects = NULL;
8065880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->acquireFenceFd = -1;
8075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->releaseFenceFd = -1;
8083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
8103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
8113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
8143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
8173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
8183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
8203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
8213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
8233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
8243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
8263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
8273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
829c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // Region::getSharedBuffer creates a reference to the underlying
830c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // SharedBuffer of this Region, this reference is freed
831c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // in onDisplayed()
832c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
833c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
834c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
8353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
8373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
8383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
8393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
8413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
8433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
845c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    virtual void onDisplayed() {
846c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
847c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
848c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        if (sb) {
849c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            sb->release();
850c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            // not technically needed but safer
851c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.numRects = 0;
852c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.rects = NULL;
853c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        }
854e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall
855e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall        getLayer()->acquireFenceFd = -1;
856c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    }
8573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
8583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
8613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8625f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
863f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
8645f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
865f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
866f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
867f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
8683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
869f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
870f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
8713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
8723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
8753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8765f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
8771e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
878a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
879a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
8803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
8823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8835f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
884da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    size_t numLayers = 0;
885da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
886da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
887da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (mHwc && disp.list) {
888da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers = disp.list->numHwLayers;
889da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
890da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
891da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // which we ignore when iterating through the layer list.
892da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
893da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                if (numLayers) {
894da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    numLayers--;
895da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                }
896da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            }
897da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
898da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
899da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return getLayerIterator(id, numLayers);
9003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
9013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
902cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE) const {
903b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
904cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
905f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
906f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
907f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
908b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
909cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian            const Vector< sp<LayerBase> >& visibleLayersSortedByZ =
910cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian                    mFlinger->getLayerSortedByZForHwcDisplay(i);
911cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
912b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.connected) {
913b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
914b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
915b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
916b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            }
917b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
918b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.list && disp.connected) {
919b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
920b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  numHwLayers=%u, flags=%08x\n",
921b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        disp.list->numHwLayers, disp.list->flags);
922b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
923f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
924da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "    type    |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
925da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
926da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                //      " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
927f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
928f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
929f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
930da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    String8 name("unknown");
931cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
932da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (i < visibleLayersSortedByZ.size()) {
933da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        const sp<LayerBase>& layer(visibleLayersSortedByZ[i]);
934da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        if (layer->getLayer() != NULL) {
935da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            const sp<GraphicBuffer>& buffer(
936f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                layer->getLayer()->getActiveBuffer());
937da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            if (buffer != NULL) {
938da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                format = buffer->getPixelFormat();
939da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            }
940f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
941da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = layer->getName();
942da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
943da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
944da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    int type = l.compositionType;
945da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type == HWC_FRAMEBUFFER_TARGET) {
946da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = "HWC_FRAMEBUFFER_TARGET";
947da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        format = disp.format;
948f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
949da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
950da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    static char const* compositionTypeName[] = {
951da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "GLES",
952da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "HWC",
953da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "BACKGROUND",
954da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "FB TARGET",
955da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "UNKNOWN"};
956da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type >= NELEM(compositionTypeName))
957da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        type = NELEM(compositionTypeName) - 1;
958da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
959f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    result.appendFormat(
960da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
961da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    compositionTypeName[type],
962f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
963f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
964f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
965da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    name.string());
966fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
967fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
9688372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
9691d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
97030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
97130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
97230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
9731d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
9748372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
9758372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
9768372785879d329f592f6883620b5a32d80d74691Mathias Agopian
977a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
9782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9792965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
9802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
9812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
982b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
9832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
9842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
9852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
9872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
98881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mEnabled != enabled) {
98981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mEnabled = enabled;
99081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mCondition.signal();
99181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
9922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
9932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
9952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
9962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
9972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
9992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
10002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
10012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
10022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
10032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
10042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10062965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
10072965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
10082965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
10092965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
10102965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
10112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
10122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
10132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
10142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
10162965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
10182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
10192965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
10202965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
10222965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
10232965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
10242965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
10252965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10262965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
10272965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
10282965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10292965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10302965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
10312965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10332965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
1034a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
1035