HWComposer.cpp revision dc5b485f74edf2d2f31c62054eb6c180421a3ade
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
205880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// #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
34a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
353eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <hardware/hwcomposer.h>
36a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
38e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian#include <cutils/properties.h>
39a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
40a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <EGL/egl.h>
41a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
4222da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include "LayerBase.h"
43a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
44c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
46a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
475880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
485880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// ---------------------------------------------------------------------------
495880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// Support for HWC_DEVICE_API_VERSION_0_3 and older:
505880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// Since v0.3 is deprecated and support will be dropped soon, as much as
515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// possible the code is written to target v1.0. When using a v0.3 HWC, we
525880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// allocate v0.3 structures, but assign them to v1.0 pointers. Fields that
535880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// exist in both versions are located at the same offset, so in most cases we
545880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// can just use the v1.0 pointer without branches or casts.
555880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
565880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall#if HWC_REMOVE_DEPRECATED_VERSIONS
575880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// We need complete types with to satisfy semantic checks, even though the
585880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// code paths that use these won't get executed at runtime (and will likely be
595880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// dead-code-eliminated). When we remove the code to support v0.3 we can remove
605880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// these as well.
615880cc573823148237eac9ab7bc586b8e4eb7160Jesse Halltypedef hwc_layer_1_t hwc_layer_t;
625880cc573823148237eac9ab7bc586b8e4eb7160Jesse Halltypedef hwc_layer_list_1_t hwc_layer_list_t;
635880cc573823148237eac9ab7bc586b8e4eb7160Jesse Halltypedef hwc_composer_device_1_t hwc_composer_device_t;
645880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall#endif
655880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
665880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// This function assumes we've already rejected HWC's with lower-than-required
675880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// versions. Don't use it for the initial "does HWC meet requirements" check!
685880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallstatic bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
695880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (HWC_REMOVE_DEPRECATED_VERSIONS &&
705880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            version <= HWC_DEVICE_API_VERSION_1_0) {
715880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return true;
725880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    } else {
735880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return hwc->common.version >= version;
745880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
755880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall}
765880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
775880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallstatic size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc,
785880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        size_t numLayers) {
795880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
805880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return sizeof(hwc_layer_list_1_t) + numLayers*sizeof(hwc_layer_1_t);
815880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    } else {
825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t);
835880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
845880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall}
855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
86a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
87a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
1003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
1013eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
1023eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
1033eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        EventHandler& handler,
1043eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        nsecs_t refreshPeriod)
105c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
106c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian      mModule(0), mHwc(0), mList(0), mCapacity(0),
1079c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian      mNumOVLayers(0), mNumFBLayers(0),
1083eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian      mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE),
1093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
1103eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian      mEventHandler(handler),
111e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mRefreshPeriod(refreshPeriod),
112e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
113a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
114e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
115e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
116e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
117e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
1183a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    bool needVSyncThread = false;
119a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
12032397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
121a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
1225880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        err = hwc_open_1(mModule, &mHwc);
123e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err, "%s device failed to initialize (%s)",
124a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
125c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
1265880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (HWC_REMOVE_DEPRECATED_VERSIONS &&
1275880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                    mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
1285880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                ALOGE("%s device version %#x too old, will not be used",
1295880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                        HWC_HARDWARE_COMPOSER, mHwc->common.version);
1305880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                hwc_close_1(mHwc);
1315880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                mHwc = NULL;
1325880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
1335880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
1345880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
1355880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (mHwc) {
136c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            if (mHwc->registerProcs) {
1373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mCBContext->hwc = this;
1383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mCBContext->procs.invalidate = &hook_invalidate;
1393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mCBContext->procs.vsync = &hook_vsync;
1403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mHwc->registerProcs(mHwc, &mCBContext->procs);
1413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
142c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            }
1435880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
144e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                if (mDebugForceFakeVSync) {
145e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                    // make sure to turn h/w vsync off in "fake vsync" mode
146e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                    mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
147e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                }
148e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            } else {
1493a77871383bc1a03cc866686d81628493d14de7cMathias Agopian                needVSyncThread = true;
1503eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian            }
151c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1523a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    } else {
1533a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        needVSyncThread = true;
1543a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1553a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
1563a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1573a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1583a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
159a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
160a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
161a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
162a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
163e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    eventControl(EVENT_VSYNC, 0);
164a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    free(mList);
1653eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
1663eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
1673eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
168a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
1695880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
170a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
1713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
172a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
173a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
174a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
175a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
176a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
177a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
178c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::hook_invalidate(struct hwc_procs* procs) {
179c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
180c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
181c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
18231d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
18331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
18431d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
18531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
186c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
187e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
188c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
189c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
19031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::vsync(int dpy, int64_t timestamp) {
1912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
1923eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    mEventHandler.onVSyncReceived(dpy, timestamp);
1933eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
1943eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
19503e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopianvoid HWComposer::eventControl(int event, int enabled) {
1963eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
1971a3bf41b7165ba294af46bc32483eaad61e707eaErik Gilling    if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
198e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        if (!mDebugForceFakeVSync) {
199e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            err = mHwc->methods->eventControl(mHwc, event, enabled);
20003e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // error here should not happen -- not sure what we should
20103e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // do if it does.
20203e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            ALOGE_IF(err, "eventControl(%d, %d) failed %s",
20303e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian                    event, enabled, strerror(-err));
204e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
2053eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
2063a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
2073a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
2083a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
2093a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
21031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
21131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
212a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianvoid HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
213a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mDpy = (hwc_display_t)dpy;
214a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mSur = (hwc_surface_t)sur;
215a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
216a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
217a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::createWorkList(size_t numLayers) {
21845721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
21945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        if (!mList || mCapacity < numLayers) {
22045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            free(mList);
2215880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            size_t size = sizeofHwcLayerList(mHwc, numLayers);
2225880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            mList = (hwc_layer_list_1_t*)malloc(size);
22345721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
22445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
225a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->flags = HWC_GEOMETRY_CHANGED;
226a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->numHwLayers = numLayers;
227a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
228a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
229a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
230a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
231a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::prepare() const {
232a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->prepare(mHwc, mList);
2339c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
2349c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numOVLayers = 0;
2359c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numFBLayers = 0;
2369c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t count = mList->numHwLayers;
2379c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        for (size_t i=0 ; i<count ; i++) {
2385880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            hwc_layer_1_t* l = NULL;
2395880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
2405880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                l = &mList->hwLayers[i];
2415880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            } else {
2425880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                // mList really has hwc_layer_list_t memory layout
2435880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                hwc_layer_list_t* list = (hwc_layer_list_t*)mList;
2445880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                hwc_layer_t* layer = &list->hwLayers[i];
2455880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                l = (hwc_layer_1_t*)layer;
2469c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
2475880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (l->flags & HWC_SKIP_LAYER) {
2485880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                l->compositionType = HWC_FRAMEBUFFER;
2495880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
2505880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            switch (l->compositionType) {
2519c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_OVERLAY:
2529c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numOVLayers++;
2539c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
2549c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_FRAMEBUFFER:
2559c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numFBLayers++;
2569c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
2579c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
2589c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
2599c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumOVLayers = numOVLayers;
2609c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumFBLayers = numFBLayers;
2619c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
262a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
263a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
264a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
2659c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopiansize_t HWComposer::getLayerCount(int type) const {
2669c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    switch (type) {
2679c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_OVERLAY:
2689c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumOVLayers;
2699c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_FRAMEBUFFER:
2709c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumFBLayers;
2719c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
2729c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    return 0;
2739c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
2749c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
275a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::commit() const {
276a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->set(mHwc, mDpy, mSur, mList);
27758959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    if (mList) {
27858959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian        mList->flags &= ~HWC_GEOMETRY_CHANGED;
27958959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
280a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
281a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
282a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
283f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
2847ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
2855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) {
28622ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian            mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
28722ffb117b0c2a906bd04aef9738a52223cdd1dceMathias Agopian        }
2887ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->set(mHwc, NULL, NULL, NULL);
2897ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
2907ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
2917ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
2927ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
2937ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
2947ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
2957ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
2967ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        free(mList);
2977ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        mList = NULL;
2987ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->prepare(mHwc, NULL);
2997ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
3007ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
3017ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
302f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
303f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
30445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopiansize_t HWComposer::getNumLayers() const {
30545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->numHwLayers : 0;
306a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
307a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
3093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
3103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
3113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
3123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
3133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
3143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
3153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
3163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
3173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
3183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
3193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
3203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
3213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
3223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
3233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
3243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
3253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
3263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
3273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
3283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
3293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
3303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
3313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
3323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
3335880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// #if !HWC_REMOVE_DEPRECATED_VERSIONS
3343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
3353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
3363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This implements the HWCLayer side of HWCIterableLayer.
3373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
3385880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
3395880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallpublic:
3405880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion0(hwc_layer_t* layer)
3415880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
3425880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
3435880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual int32_t getCompositionType() const {
3445880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return getLayer()->compositionType;
3455880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3465880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual uint32_t getHints() const {
3475880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return getLayer()->hints;
3485880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
349ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
350ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        // not supported on VERSION_03
351ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return -1;
352ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
353dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
354dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        if (fenceFd != -1) {
355dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            ALOGE("HWC 0.x can't handle acquire fences");
356dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            close(fenceFd);
357dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        }
358dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
3595880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
3605880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setDefaultState() {
3615880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->compositionType = HWC_FRAMEBUFFER;
3625880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->hints = 0;
3635880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->flags = HWC_SKIP_LAYER;
3645880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->transform = 0;
3655880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->blending = HWC_BLENDING_NONE;
3665880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.numRects = 0;
3675880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.rects = NULL;
3685880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3695880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setSkip(bool skip) {
3705880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (skip) {
3715880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags |= HWC_SKIP_LAYER;
3725880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        } else {
3735880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags &= ~HWC_SKIP_LAYER;
3745880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
3755880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3765880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setBlending(uint32_t blending) {
3775880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->blending = blending;
3785880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3795880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setTransform(uint32_t transform) {
3805880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->transform = transform;
3815880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setFrame(const Rect& frame) {
3835880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
3845880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setCrop(const Rect& crop) {
3865880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
3875880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3885880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setVisibleRegionScreen(const Region& reg) {
3895880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.rects =
3905880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                reinterpret_cast<hwc_rect_t const *>(
3915880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
3925880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
3935880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
3945880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (buffer == 0 || buffer->handle == 0) {
3955880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->compositionType = HWC_FRAMEBUFFER;
3965880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags |= HWC_SKIP_LAYER;
3975880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->handle = 0;
3985880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        } else {
3995880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->handle = buffer->handle;
4005880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
4015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
4025880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall};
4035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
4045880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
4055880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
4065880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
4075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
4085880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
4095880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
4103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
4115880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion1(hwc_layer_1_t* layer)
4125880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
4133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
4153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
4163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
4183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
4193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
420ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
421ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
422ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
423ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
424ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
425dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
426dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
427dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
4283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
4303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->compositionType = HWC_FRAMEBUFFER;
4313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->hints = 0;
4323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->flags = HWC_SKIP_LAYER;
4333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = 0;
4343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = HWC_BLENDING_NONE;
4353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.numRects = 0;
4363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects = NULL;
4375880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->acquireFenceFd = -1;
4385880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->releaseFenceFd = -1;
4393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
4413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
4423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
4433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
4443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
4453e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
4463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
4483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
4493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
4513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
4523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
4543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
4553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
4573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
4583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
4603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects =
4613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                reinterpret_cast<hwc_rect_t const *>(
4623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
4633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
4653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
4663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
4673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
4683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
4693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
4703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
4713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
4723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
4743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
4773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(size_t index) {
4793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    if (!mList || index > mList->numHwLayers) {
4803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
4813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
4835880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return LayerListIterator(new HWCLayerVersion1(mList->hwLayers), index);
4845880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    } else {
4855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
4865880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
4875880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
4883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
4893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
4923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias AgopianHWComposer::LayerListIterator HWComposer::begin() {
4943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    return getLayerIterator(0);
495a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
496a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
4973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
4993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
5003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias AgopianHWComposer::LayerListIterator HWComposer::end() {
5013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    return getLayerIterator(getNumLayers());
5023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
5033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
5043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
5053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
50622da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
50722da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
5088372785879d329f592f6883620b5a32d80d74691Mathias Agopian    if (mHwc && mList) {
5098372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
510e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n",
511e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                mDebugForceFakeVSync);
512e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  numHwLayers=%u, flags=%08x\n",
5138372785879d329f592f6883620b5a32d80d74691Mathias Agopian                mList->numHwLayers, mList->flags);
514fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        result.append(
515aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
516aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
517aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
5188372785879d329f592f6883620b5a32d80d74691Mathias Agopian        for (size_t i=0 ; i<mList->numHwLayers ; i++) {
5195880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            hwc_layer_1_t l;
5205880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
5215880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                l = mList->hwLayers[i];
5225880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            } else {
5235880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList;
5245880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                *(hwc_layer_t*)&l = list0->hwLayers[i];
5255880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                l.acquireFenceFd = l.releaseFenceFd = -1;
5265880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
527fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
528fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            int32_t format = -1;
529fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            if (layer->getLayer() != NULL) {
530fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
531fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                if (buffer != NULL) {
532fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    format = buffer->getPixelFormat();
533fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
534fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
535e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            result.appendFormat(
536aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
5378372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
538aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
5398372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
54022da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
541fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    layer->getName().string());
5428372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
5431d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
5445880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) {
5451d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mHwc->dump(mHwc, buffer, SIZE);
5461d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
5478372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
5488372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
5498372785879d329f592f6883620b5a32d80d74691Mathias Agopian
550a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
5512965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5522965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
5532965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
5542965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
5552965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mRefreshPeriod(hwc.mRefreshPeriod)
5562965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
5572965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5582965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5592965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
5602965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
5612965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mEnabled = enabled;
5622965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mCondition.signal();
5632965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5642965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5652965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
5662965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
5672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5682965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5692965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
5702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
5712965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
5722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
5732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
5742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
5752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
5782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
5792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
5802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
5812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
5822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
5832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
5842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
5852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
5872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
5892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
5902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
5912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
5932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
5942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
5952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
5962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
5982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
5992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
6002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
6012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
6022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
6032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
6042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
605a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
606