HWComposer.cpp revision 13a082e160c2d1d8006b93a555a57035213d568b
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>
298372785879d329f592f6883620b5a32d80d74691Mathias Agopian#include <utils/String8.h>
303eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <utils/Thread.h>
312965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#include <utils/Trace.h>
3222da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include <utils/Vector.h>
33a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
34921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include <ui/GraphicBuffer.h>
35921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian
36a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
373eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <hardware/hwcomposer.h>
38a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
39a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
40e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian#include <cutils/properties.h>
41a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
42921e6ac4b7610a178285898d191eb0e3afe906c0Mathias Agopian#include "Layer.h"           // needed only for debugging
4322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include "LayerBase.h"
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
45c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
46a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
47a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
485880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
495880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallstatic bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
5030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    return hwc->common.version >= version;
51bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
52bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
53a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
54a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
683eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
693eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
708b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        EventHandler& handler,
718b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        framebuffer_device_t const* fbDev)
72c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
73f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian      mModule(0), mHwc(0), mNumDisplays(1),
743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
75e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
76e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
77a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
78e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
79e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
80e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
81b685c542836b93c99cd85053e07696406ea37adbJesse Hall
82e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
83e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
84e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
85e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
86028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
87a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
8832397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
89a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
905880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        err = hwc_open_1(mModule, &mHwc);
91e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err, "%s device failed to initialize (%s)",
92a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
93c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
9430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            if (mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
955880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                ALOGE("%s device version %#x too old, will not be used",
965880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                        HWC_HARDWARE_COMPOSER, mHwc->common.version);
975880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                hwc_close_1(mHwc);
985880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                mHwc = NULL;
995880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
1005880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
1015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
1025880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (mHwc) {
103bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            if (mHwc->registerProcs) {
104bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mCBContext->hwc = this;
105bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mCBContext->procs.invalidate = &hook_invalidate;
106bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mCBContext->procs.vsync = &hook_vsync;
107bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
108bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mHwc->registerProcs(mHwc, &mCBContext->procs);
109bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            }
110bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
111b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // always turn vsync off when we start
112b685c542836b93c99cd85053e07696406ea37adbJesse Hall            needVSyncThread = false;
11330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
11430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
11530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            int period;
11630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
117f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(period);
118028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian            }
119b685c542836b93c99cd85053e07696406ea37adbJesse Hall
120f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // these IDs are always reserved
121f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
122f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                mAllocatedDisplayIDs.markBit(i);
123f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                // TODO: we query xdpi / ydpi / refresh
124f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
1258f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
126f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // the number of displays we actually have depends on the
127f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // hw composer version
128f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (mHwc->common.version == HWC_DEVICE_API_VERSION_1_1) {
129f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                // 1.1 adds support for multiple displays
130f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                mNumDisplays = HWC_NUM_DISPLAY_TYPES;
131f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            } else if (mHwc->common.version > HWC_DEVICE_API_VERSION_1_1) {
132f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                // 1.2 adds support for virtual displays
133f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                mNumDisplays = MAX_DISPLAYS;
134f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
135c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1363a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1373a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
1388b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    if (fbDev) {
139f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // if we're here it means we are on version 1.0
140f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
141f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.xdpi = fbDev->xdpi;
142f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.ydpi = fbDev->ydpi;
143f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
144f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.refresh = nsecs_t(1e9 / fbDev->fps);
145f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
146f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
147f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (disp.refresh == 0) {
148f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.refresh = nsecs_t(1e9 / 60.0);
149f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            ALOGW("getting VSYNC period thin air: %lld", mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
150888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        }
151888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
152888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1533a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1543a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1553a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
156a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
157a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
158a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
159a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
16030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    mHwc->eventControl(mHwc, 0, EVENT_VSYNC, 0);
1613eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
1623eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
1633eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
164a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
1655880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
166a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
1673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
168a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
169a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
170a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
171a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
172a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
173a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
174bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
175bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
176bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
177bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
178c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
179c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
180bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int dpy,
181bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
182bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
183bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
184bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->vsync(dpy, timestamp);
18531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
18631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
187c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
188e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
189c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
190c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
19131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::vsync(int dpy, int64_t timestamp) {
1922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
1933eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    mEventHandler.onVSyncReceived(dpy, timestamp);
194d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
195d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mLastHwVSync = timestamp;
196d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
197d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
198e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
199f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
200e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
201e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
202f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
203f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.markBit(id);
204e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
205e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
206e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
207e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
208f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (id < HWC_NUM_DISPLAY_TYPES) {
209f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        // cannot free the reserved IDs
210e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
211e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
212f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
213e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
214e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
215f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    mAllocatedDisplayIDs.clearBit(id);
216e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
217e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
218e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
219888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopiannsecs_t HWComposer::getRefreshPeriod() const {
220f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    return mDisplayData[HWC_DISPLAY_PRIMARY].refresh;
221888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian}
222888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
223d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopiannsecs_t HWComposer::getRefreshTimestamp() const {
224d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
225d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
226d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
227d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
228d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
229f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    return now - ((now - mLastHwVSync) %  mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
2303eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
2313eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
2328b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopianfloat HWComposer::getDpiX() const {
233e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi;
2348b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
2358b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
2368b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopianfloat HWComposer::getDpiY() const {
237e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi;
2388b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
2398b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
24003e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopianvoid HWComposer::eventControl(int event, int enabled) {
2413eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
24230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc) {
243e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        if (!mDebugForceFakeVSync) {
24430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            err = mHwc->eventControl(mHwc, 0, event, enabled);
24503e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // error here should not happen -- not sure what we should
24603e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // do if it does.
24703e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            ALOGE_IF(err, "eventControl(%d, %d) failed %s",
24803e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian                    event, enabled, strerror(-err));
249e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
2503eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
2513a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
2523a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
2533a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
2543a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
25531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
25631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
2575f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
258f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
2591e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
260e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
2611e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
26245721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
263f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        DisplayData& disp(mDisplayData[id]);
26413a082e160c2d1d8006b93a555a57035213d568bAndy McFadden        if (disp.capacity < numLayers || disp.list == NULL) {
265f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const size_t size = sizeof(hwc_display_contents_1_t)
266f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    + numLayers * sizeof(hwc_layer_1_t);
267f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            free(disp.list);
268f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.list = (hwc_display_contents_1_t*)malloc(size);
269f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.capacity = numLayers;
27045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
271f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flags = HWC_GEOMETRY_CHANGED;
272f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->numHwLayers = numLayers;
273f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        disp.list->flipFenceFd = -1;
274a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
275a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
276a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
277a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
278e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
279f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    for (size_t i=0 ; i<mNumDisplays ; i++) {
280f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        mLists[i] = mDisplayData[i].list;
281f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mLists[i]) {
282f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            mLists[i]->dpy = EGL_NO_DISPLAY;
283f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            mLists[i]->sur = EGL_NO_SURFACE;
284f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        }
285f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
286f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
2879c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
2885f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
2895f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
2905f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
291f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
292f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
293f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasFbComp = false;
294f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            disp.hasOvComp = false;
295f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
296f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
297f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    hwc_layer_1_t& l = disp.list->hwLayers[i];
298f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.flags & HWC_SKIP_LAYER) {
299f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        l.compositionType = HWC_FRAMEBUFFER;
300f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
301f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_FRAMEBUFFER) {
302f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasFbComp = true;
303f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
304f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (l.compositionType == HWC_OVERLAY) {
305f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        disp.hasOvComp = true;
306f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
307f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
3089c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
3099c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
3109c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
311a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
312a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
313a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
314e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
315f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
316e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
317e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
318e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
3195f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
320e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
321f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
322e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
323e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
3249c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
3259c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
32630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
3278630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
3288630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
329f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        if (mHwc->common.version == HWC_DEVICE_API_VERSION_1_0) {
33030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
331f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // by the (dpy, sur) fields and we are guaranteed to have only
332f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            // a single display.
33330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
33430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
33530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
336f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
33730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
338f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian
339f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
340f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            DisplayData& disp(mDisplayData[i]);
341f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
342f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                if (disp.list->flipFenceFd != -1) {
343f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    close(disp.list->flipFenceFd);
344f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    disp.list->flipFenceFd = -1;
345f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                }
346f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
347f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            }
3488630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
34958959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
350a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
351a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
352a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
353f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
3547ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
35530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
35630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        return (status_t)mHwc->blank(mHwc, 0, 1);
3577ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
3587ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
3597ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
3607ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
36110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Crossstatus_t HWComposer::acquire() const {
36210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
36330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        return (status_t)mHwc->blank(mHwc, 0, 0);
36410fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
36510fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
36610fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
36710fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
368f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopiansize_t HWComposer::getNumLayers(int32_t id) const {
369f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
370f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        return 0;
3717ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
372f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    return (mHwc && mDisplayData[id].list) ?
373f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            mDisplayData[id].list->numHwLayers : 0;
374a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
375a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
3773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
3783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
3793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
3803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
3813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
3823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
3833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
3843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
3853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
3863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
3873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
3883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
3893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
3903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
3913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
3923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
3933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
3943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
3953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
3963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
3973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
3983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
3993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
4003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
4025880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
4035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
4045880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
4055880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
4063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
4075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion1(hwc_layer_1_t* layer)
4085880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
4093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
4113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
4123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
4143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
4153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
416ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
417ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
418ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
419ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
420ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
421dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
422dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
423dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
4243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
4263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->compositionType = HWC_FRAMEBUFFER;
4273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->hints = 0;
4283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->flags = HWC_SKIP_LAYER;
4293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = 0;
4303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = HWC_BLENDING_NONE;
4313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.numRects = 0;
4323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects = NULL;
4335880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->acquireFenceFd = -1;
4345880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->releaseFenceFd = -1;
4353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
4373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
4383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
4393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
4403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
4413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
4423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
4443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
4453e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
4473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
4483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
4503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
4513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
4533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
4543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
4563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects =
4573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                reinterpret_cast<hwc_rect_t const *>(
4583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
4593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
4613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
4623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
4633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
4643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
4653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
4663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
4673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
4683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
4703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
4733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4745f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
475f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
4765f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
477f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
478f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    const DisplayData& disp(mDisplayData[id]);
479f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
4803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
481f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    }
482f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian    return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
4833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
4843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
4873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4885f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
4891e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
490a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
491a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
4923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
4943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4955f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
4961e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, getNumLayers(id));
4973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
4983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
49922da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
50022da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
501b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
5028372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
503f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
504f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian        for (size_t i=0 ; i<mNumDisplays ; i++) {
505f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            const DisplayData& disp(mDisplayData[i]);
506f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian            if (disp.list) {
507f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.appendFormat("  id=%d, numHwLayers=%u, flags=%08x\n",
508f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        i, disp.list->numHwLayers, disp.list->flags);
509f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                result.append(
510f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
511f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
512f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
513f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
514f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const hwc_layer_1_t&l = disp.list->hwLayers[i];
515f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
516f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    int32_t format = -1;
517f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    if (layer->getLayer() != NULL) {
518f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        const sp<GraphicBuffer>& buffer(
519f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                layer->getLayer()->getActiveBuffer());
520f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        if (buffer != NULL) {
521f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                            format = buffer->getPixelFormat();
522f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                        }
523f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    }
524f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                    result.appendFormat(
525f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                            " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
526f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                            l.compositionType ? "OVERLAY" : "FB",
527f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
528f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
529f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
530f435863467ab407f2a482604beed5fa6f0144c62Mathias Agopian                                    layer->getName().string());
531fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
532fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
5338372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
5341d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
53530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
53630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
53730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
5381d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
5398372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
5408372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
5418372785879d329f592f6883620b5a32d80d74691Mathias Agopian
542a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
5432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5442965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
5452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
5462965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
547e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mRefreshPeriod(hwc.getRefreshPeriod())
5482965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
5492965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5502965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5512965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
5522965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
5532965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mEnabled = enabled;
5542965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mCondition.signal();
5552965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5562965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5572965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
5582965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
5592965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5602965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5612965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
5622965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
5632965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
5642965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
5652965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
5662965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
5672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5682965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5692965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
5702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
5712965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
5722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
5732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
5742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
5752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
5762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
5772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
5792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
5812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
5822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
5832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
5852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
5862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
5872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
5882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
5902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
5912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
5942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
597a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
598