HWComposer.cpp revision e3c697fb929c856b59fa56a8e05a2a7eba187c3d
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
4122ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennissp<Fence> HWComposer::getDisplayFence(int disp) const {
4132ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis    return mDisplayData[disp].lastDisplayFence;
4142ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis}
4152ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
4162ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis
417db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getWidth(int disp) const {
418db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].width;
419b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
420b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
421db27621e22559a1b16414f890677ef04242fbc3bJesse Halluint32_t HWComposer::getHeight(int disp) const {
422db27621e22559a1b16414f890677ef04242fbc3bJesse Hall    return mDisplayData[disp].height;
423b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
424b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
425b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenuint32_t HWComposer::getFormat(int disp) const {
426b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].format;
4273eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
4283eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
429b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiX(int disp) const {
430b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].xdpi;
4318b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4328b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
433b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenfloat HWComposer::getDpiY(int disp) const {
434b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    return mDisplayData[disp].ydpi;
4358b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
4368b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
437f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopianbool HWComposer::isConnected(int disp) const {
438f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian    return mDisplayData[disp].connected;
439f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian}
440f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian
44181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianvoid HWComposer::eventControl(int disp, int event, int enabled) {
44281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
443620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
444620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
445620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        return;
446620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    }
447620685c2e684082c82657732d1e35cefd0c79006Andy McFadden    if (event != EVENT_VSYNC) {
448620685c2e684082c82657732d1e35cefd0c79006Andy McFadden        ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
449620685c2e684082c82657732d1e35cefd0c79006Andy McFadden              event, disp, enabled);
45081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        return;
45181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
4523eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
45381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mHwc && !mDebugForceFakeVSync) {
45481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // NOTE: we use our own internal lock here because we have to call
45581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // into the HWC with the lock held, and we want to make sure
45681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // that even if HWC blocks (which it shouldn't), it won't
45781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // affect other threads.
45881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        Mutex::Autolock _l(mEventControlLock);
45981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t eventBit = 1UL << event;
46081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t newValue = enabled ? eventBit : 0;
46181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        const int32_t oldValue = mDisplayData[disp].events & eventBit;
46281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        if (newValue != oldValue) {
46381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            ATRACE_CALL();
46481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            err = mHwc->eventControl(mHwc, disp, event, enabled);
46581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            if (!err) {
46681cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                int32_t& events(mDisplayData[disp].events);
46781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                events = (events & ~eventBit) | newValue;
46881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian            }
469e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
47081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // error here should not happen -- not sure what we should
47181cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        // do if it does.
47281cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        ALOGE_IF(err, "eventControl(%d, %d) failed %s",
47381cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian                event, enabled, strerror(-err));
4743eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
4753a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
4763a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
4773a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
4783a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
47931d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
48031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
4815f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
482f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
4831e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
484e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
4851e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
48645721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
487f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
488da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
489da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // we need space for the HWC_FRAMEBUFFER_TARGET
490da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers++;
491da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
49213a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
493da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t)
494f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
495f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
496f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
497f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
49845721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
499da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
500da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
501da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
5028f06a8c2c80491465e8742c1bf45315dab7017e3Andy McFadden            const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
503da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
504da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->hints = 0;
505da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->flags = 0;
506da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->handle = disp.fbTargetHandle;
507da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->transform = 0;
508da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
509da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->sourceCrop = r;
510da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->displayFrame = r;
511da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.numRects = 1;
512da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->visibleRegionScreen.rects =
513da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                &disp.framebufferTarget->displayFrame;
514da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->acquireFenceFd = -1;
515da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
516da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
517db27621e22559a1b16414f890677ef04242fbc3bJesse Hall        disp.list->retireFenceFd = -1;
518f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
519f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
520a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
521a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
522a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
523a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
524da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianstatus_t HWComposer::setFramebufferTarget(int32_t id,
525da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
526da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
527da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
528da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
529da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    DisplayData& disp(mDisplayData[id]);
530da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (!disp.framebufferTarget) {
531da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // this should never happen, but apparently eglCreateWindowSurface()
532e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian        // triggers a Surface::queueBuffer()  on some
533da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // devices (!?) -- log and ignore.
534da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        ALOGE("HWComposer: framebufferTarget is null");
535f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        CallStack stack;
536f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.update();
537f5a33928349bebc8eebc9f466618997e98c24e68Mathias Agopian//        stack.dump("");
538da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
539da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
540da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
541da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int acquireFenceFd = -1;
5421df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis    if (acquireFence->isValid()) {
543da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        acquireFenceFd = acquireFence->dup();
544da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
545da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
546da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
547da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.fbTargetHandle = buf->handle;
548da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->handle = disp.fbTargetHandle;
549da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
550da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return NO_ERROR;
551da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
552da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
553e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
554f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
555da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        DisplayData& disp(mDisplayData[i]);
556da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
557da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
558da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // DO NOT reset the handle field to NULL, because it's possible
559da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // that we have nothing to redraw (eg: eglSwapBuffers() not called)
560da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            // in which case, we should continue to use the same buffer.
56127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            LOG_FATAL_IF(disp.list == NULL);
562da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
563da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
56427ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        if (!disp.connected && disp.list != NULL) {
56527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden            ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
56627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden                  i, disp.list->numHwLayers);
56727ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        }
568da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        mLists[i] = disp.list;
569f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
570db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
571db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbuf = NULL;
572db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->outbufAcquireFenceFd = -1;
573db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
574db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                // garbage data to catch improper use
575db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
576db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
577db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            } else {
578db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->dpy = EGL_NO_DISPLAY;
579db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                mLists[i]->sur = EGL_NO_SURFACE;
580db27621e22559a1b16414f890677ef04242fbc3bJesse Hall            }
581f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
582f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
583db27621e22559a1b16414f890677ef04242fbc3bJesse Hall
584f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
585da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
586da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
5879c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
5885f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
5895f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
5905f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
591f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
592f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
593f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
594f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
595f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
596f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
597f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
598da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
599da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //ALOGD("prepare: %d, type=%d, handle=%p",
600da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    //        i, l.compositionType, l.handle);
601da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
602f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
603f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
604f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
605f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
606f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
607f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
608f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
609f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
610f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
611f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
6129c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
6139c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
6149c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
615a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
616a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
617a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
618e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
619f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
620e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
621e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
622e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
6235f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
624e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
625f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
626e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
627e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
6289c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
6299c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
630da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::getAndResetReleaseFenceFd(int32_t id) {
631da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
632da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return BAD_INDEX;
633da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
634da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    int fd = INVALID_OPERATION;
635da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
636da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
637da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (disp.framebufferTarget) {
638da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            fd = disp.framebufferTarget->releaseFenceFd;
639d30b36d1fd0c7ab80553e784a155266be9703e47Jamie Gennis            disp.framebufferTarget->acquireFenceFd = -1;
640da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            disp.framebufferTarget->releaseFenceFd = -1;
641da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
642da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
643da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return fd;
644da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
645da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
64630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
6478630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
6488630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
6499eb1eb5bb55740982ceae9966fc536824edc302aJesse Hall        if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
65030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
651f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
652f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
65330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
65430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
65530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
656f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
65730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
658f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
659f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
660f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
6612ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis            disp.lastDisplayFence = disp.lastRetireFence;
6621df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis            disp.lastRetireFence = Fence::NO_FENCE;
663f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
664db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                if (disp.list->retireFenceFd != -1) {
6652ec3e0748bff8d75baade2ddda9fbfa21a3b7d3fJamie Gennis                    disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
666db27621e22559a1b16414f890677ef04242fbc3bJesse Hall                    disp.list->retireFenceFd = -1;
667f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
668f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
669f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
6708630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
67158959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
672a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
673a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
674a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
67581cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::release(int disp) {
676c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
6777ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
67881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        eventControl(disp, HWC_EVENT_VSYNC, 0);
679c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 1);
6807ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
6817ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
6827ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
6837ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
68481cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopianstatus_t HWComposer::acquire(int disp) {
685c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden    LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
68610fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
687c01a79d77b829e7de86ef137694e8ad708470ca1Andy McFadden        return (status_t)mHwc->blank(mHwc, disp, 0);
68810fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
68910fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
69010fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
69110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
69227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFaddenvoid HWComposer::disconnectDisplay(int disp) {
6935a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
6945a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    if (disp >= HWC_NUM_DISPLAY_TYPES) {
6955a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        // nothing to do for these yet
6965a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden        return;
6975a8f9012ee8bb9dc1ad14432b96a821f08802ee3Andy McFadden    }
69827ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    DisplayData& dd(mDisplayData[disp]);
69927ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    if (dd.list != NULL) {
70027ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        free(dd.list);
70127ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.list = NULL;
70227ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden        dd.framebufferTarget = NULL;    // points into dd.list
7039e9689c11148521d2c16a121a0b87b062be0714cAndy McFadden        dd.fbTargetHandle = NULL;
70427ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden    }
70527ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden}
70627ec5739bc05330e08b02f25b62a8f597bad897aAndy McFadden
707cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::getVisualID() const {
708d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
709da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
710da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // is supported by the implementation. we can only be in this case
711da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        // if we have HWC 1.1
712da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return HAL_PIXEL_FORMAT_RGBA_8888;
713da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
714cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    } else {
715cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        return mFbDev->format;
716cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
717b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
718b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
719da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianbool HWComposer::supportsFramebufferTarget() const {
720da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
721da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian}
722da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
723da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopianint HWComposer::fbPost(int32_t id,
724da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
725d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
726da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return setFramebufferTarget(id, acquireFence, buffer);
727da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
7281df8c345854155cbbcb9f80de9d12d66ea70ac08Jamie Gennis        acquireFence->waitForever(1000, "HWComposer::fbPost");
729da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->post(mFbDev, buffer->handle);
730cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    }
731cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian}
732cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian
733cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopianint HWComposer::fbCompositionComplete() {
734d3d35f18345c3ef93217313a583ace473b5a47adJesse Hall    if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
735da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return NO_ERROR;
736da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
737da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (mFbDev->compositionComplete) {
738da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return mFbDev->compositionComplete(mFbDev);
739da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    } else {
740da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        return INVALID_OPERATION;
741b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
742b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
743b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
744b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFaddenvoid HWComposer::fbDump(String8& result) {
745cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian    if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
746b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        const size_t SIZE = 4096;
747b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        char buffer[SIZE];
748b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        mFbDev->dump(mFbDev, buffer, SIZE);
749b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden        result.append(buffer);
750b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden    }
751b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden}
752b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden
7533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
7543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
7553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
7563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
7573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
7583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
7593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
7603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
7613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
7623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
7633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
7643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
7653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
7663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
7673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
7683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
7693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
7703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
7713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
7733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
7743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
7753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
7773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7785880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
7795880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
7805880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
7815880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
7825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
7839f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    struct hwc_composer_device_1* mHwc;
7843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
7859f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
7869f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
7873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
7893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
7903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
7913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
7923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
7933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
794ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
795ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
796ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
797ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
798ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
799dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
800dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
801dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
8029f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPerFrameDefaultState() {
8039f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        //getLayer()->compositionType = HWC_FRAMEBUFFER;
8049f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
8059f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    virtual void setPlaneAlpha(uint8_t alpha) {
8069f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
8079f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian            getLayer()->planeAlpha = alpha;
8089f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        } else {
8095fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            if (alpha < 0xFF) {
8105fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian                getLayer()->flags |= HWC_SKIP_LAYER;
8115fe58b8ba6c5d78481730874236a1be48a90d61dMathias Agopian            }
8129f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        }
8139f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    }
8143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
8159f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        hwc_layer_1_t* const l = getLayer();
8169f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->compositionType = HWC_FRAMEBUFFER;
8179f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->hints = 0;
8189f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->flags = HWC_SKIP_LAYER;
8199f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->handle = 0;
8209f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->transform = 0;
8219f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->blending = HWC_BLENDING_NONE;
8229f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.numRects = 0;
8239f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->visibleRegionScreen.rects = NULL;
8249f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->acquireFenceFd = -1;
8259f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->releaseFenceFd = -1;
8269f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian        l->planeAlpha = 0xFF;
8273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
8293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
8303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
8333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
8363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
8373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
8393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
8403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
8423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
8433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
8453e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
8463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
848c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // Region::getSharedBuffer creates a reference to the underlying
849c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // SharedBuffer of this Region, this reference is freed
850c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        // in onDisplayed()
851c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
852c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
853c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
8543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
8553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
8563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
8573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
8583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
8593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
8603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
8613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
8623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
8633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
864c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    virtual void onDisplayed() {
865c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
866c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
867c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        if (sb) {
868c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            sb->release();
869c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            // not technically needed but safer
870c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.numRects = 0;
871c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian            visibleRegion.rects = NULL;
872c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian        }
873e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall
874e25d005d91344c500450f01d4a258ca9c3676627Jesse Hall        getLayer()->acquireFenceFd = -1;
875c39736088398a9b4367b30fc307ed7de96ac342eMathias Agopian    }
8763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
8773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
8803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8815f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
882f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
8835f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
884f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
885f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
886f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
8873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
888f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
8899f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian    return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
8903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
8913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
8923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
8933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
8943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
8955f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
8961e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
897a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
898a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
8993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
9003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
9013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
9025f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
903da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    size_t numLayers = 0;
904da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
905da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        const DisplayData& disp(mDisplayData[id]);
906da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        if (mHwc && disp.list) {
907da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            numLayers = disp.list->numHwLayers;
908da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
909da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
910da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                // which we ignore when iterating through the layer list.
911da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
912da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                if (numLayers) {
913da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    numLayers--;
914da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                }
915da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian            }
916da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian        }
917da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    }
918da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian    return getLayerIterator(id, numLayers);
9193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
9203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
921cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE) const {
922b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
923cde87a3b9d3f8dc15232d927b56ee9e5e520f58dMathias Agopian        result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
924f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
925f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
926f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
927b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
928cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian            const Vector< sp<LayerBase> >& visibleLayersSortedByZ =
929cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian                    mFlinger->getLayerSortedByZForHwcDisplay(i);
930cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
931b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.connected) {
932b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
933b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
934b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
935b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            }
936b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
937b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian            if (disp.list && disp.connected) {
938b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                result.appendFormat(
939b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        "  numHwLayers=%u, flags=%08x\n",
940b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian                        disp.list->numHwLayers, disp.list->flags);
941b4d18ed34e3513f3a14ea0876c7e330bee72a529Mathias Agopian
942f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
943da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "    type    |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
944da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
945da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                //      " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
946f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
947f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
948f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
949da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    String8 name("unknown");
950cb55857bbde34a06c19dde3db5064d1717a0173eMathias Agopian
951da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (i < visibleLayersSortedByZ.size()) {
952da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        const sp<LayerBase>& layer(visibleLayersSortedByZ[i]);
953da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        if (layer->getLayer() != NULL) {
954da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            const sp<GraphicBuffer>& buffer(
955f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                layer->getLayer()->getActiveBuffer());
956da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            if (buffer != NULL) {
957da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                format = buffer->getPixelFormat();
958da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            }
959f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
960da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = layer->getName();
961da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    }
962da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
963da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    int type = l.compositionType;
964da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type == HWC_FRAMEBUFFER_TARGET) {
965da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        name = "HWC_FRAMEBUFFER_TARGET";
966da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        format = disp.format;
967f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
968da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
969da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    static char const* compositionTypeName[] = {
970da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "GLES",
971da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "HWC",
972da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "BACKGROUND",
973da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "FB TARGET",
974da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            "UNKNOWN"};
975da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                    if (type >= NELEM(compositionTypeName))
976da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                        type = NELEM(compositionTypeName) - 1;
977da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian
978f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    result.appendFormat(
979da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                            " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
980da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    compositionTypeName[type],
981f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
982f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
983f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
984da27af9832a0170f1fc40ef3f21371c4d30d21b3Mathias Agopian                                    name.string());
985fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
986fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
9878372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
9881d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
98930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
99030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
99130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
9921d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
9938372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
9948372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
9958372785879d329f592f6883620b5a32d80d74691Mathias Agopian
996a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
9972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
9982965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
9992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
10002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
1001b0d1dd36f104c0b581674adc7f830cbf44b7db06Andy McFadden      mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
10022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
10032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
10062965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
100781cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    if (mEnabled != enabled) {
100881cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mEnabled = enabled;
100981cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian        mCondition.signal();
101081cd5d3b94d21253a0be925f4ae58cc7f4afeef7Mathias Agopian    }
10112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
10142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
10152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10162965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
10182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
10192965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
10202965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
10212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
10222965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
10232965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10242965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10252965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
10262965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
10272965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
10282965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
10292965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
10302965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
10312965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
10322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
10332965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10342965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
10352965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10362965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
10372965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
10382965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
10392965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10402965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
10412965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
10422965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
10432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
10442965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
10462965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
10472965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
10482965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10492965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
10502965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
10512965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
10522965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
1053a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
1054