HWComposer.cpp revision 30bcc61431d8e3bef779472dd52a7b156dcaba09
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),
738f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall      mModule(0), mHwc(0), mNumDisplays(1), mCapacity(0),
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
111e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            // these IDs are always reserved
112e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mTokens.markBit(HWC_DISPLAY_PRIMARY);
113e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mTokens.markBit(HWC_DISPLAY_EXTERNAL);
114e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
115b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // always turn vsync off when we start
116b685c542836b93c99cd85053e07696406ea37adbJesse Hall            needVSyncThread = false;
117888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
11830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
11930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
12030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            int period;
12130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
12230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian                mDisplayData[0].refresh = nsecs_t(period);
123028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian            }
124b685c542836b93c99cd85053e07696406ea37adbJesse Hall
1258f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
1268f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall                mNumDisplays = HWC_NUM_DISPLAY_TYPES;
1278f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
128b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // create initial empty display contents for display 0
1298f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall            createWorkList(HWC_DISPLAY_PRIMARY, 0);
130c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
1313a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1323a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
1338b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
1348b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    if (fbDev) {
135e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        if (mDisplayData[HWC_DISPLAY_PRIMARY].refresh == 0) {
136e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(1e9 / fbDev->fps);
137e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            ALOGW("getting VSYNC period from fb HAL: %lld", mDisplayData[0].refresh);
138888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        }
139e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mDisplayData[HWC_DISPLAY_PRIMARY].xdpi = fbDev->xdpi;
140e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mDisplayData[HWC_DISPLAY_PRIMARY].ydpi = fbDev->ydpi;
141888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
142888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
143e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (mDisplayData[HWC_DISPLAY_PRIMARY].refresh == 0) {
144e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(1e9 / 60.0);
145e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        ALOGW("getting VSYNC period thin air: %lld", mDisplayData[0].refresh);
146888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
147888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
1483a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
1493a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
1503a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
151a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
152a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
153a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
154a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
15530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    mHwc->eventControl(mHwc, 0, EVENT_VSYNC, 0);
156e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    for (size_t i = 0; i < MAX_DISPLAYS; i++) {
157b685c542836b93c99cd85053e07696406ea37adbJesse Hall        free(mLists[i]);
158e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
1593eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
1603eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
1613eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
162a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
1635880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
164a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
1653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
166a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
167a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
168a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
169a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
170a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
171a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
172bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
173bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
174bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
175bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
176c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
177c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
178bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int dpy,
179bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
180bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
181bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
182bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->vsync(dpy, timestamp);
18331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
18431d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
185c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
186e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
187c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
188c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
18931d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::vsync(int dpy, int64_t timestamp) {
1902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
1913eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    mEventHandler.onVSyncReceived(dpy, timestamp);
192d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
193d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mLastHwVSync = timestamp;
194d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
195d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
196e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
197e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (mTokens.isFull()) {
198e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
199e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
200e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
201e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // FIXME: for now we don't support h/w composition wifi displays
202e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return -1;
203e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
204e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    int32_t id = mTokens.firstUnmarkedBit();
205e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    mTokens.markBit(id);
206e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
207e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
208e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
209e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
210e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (id < MAX_DISPLAYS) {
211e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
212e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
213e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id)) {
214e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
215e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
216e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    mTokens.clearBit(id);
217e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
218e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
219e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
220888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopiannsecs_t HWComposer::getRefreshPeriod() const {
221e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[0].refresh;
222888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian}
223888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
224d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopiannsecs_t HWComposer::getRefreshTimestamp() const {
225d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
226d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
227d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
228d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
229d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
230e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return now - ((now - mLastHwVSync) %  mDisplayData[0].refresh);
2313eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
2323eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
2338b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopianfloat HWComposer::getDpiX() const {
234e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi;
2358b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
2368b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
2378b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopianfloat HWComposer::getDpiY() const {
238e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi;
2398b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
2408b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
24103e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopianvoid HWComposer::eventControl(int event, int enabled) {
2423eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
24330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc) {
244e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        if (!mDebugForceFakeVSync) {
24530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            err = mHwc->eventControl(mHwc, 0, event, enabled);
24603e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // error here should not happen -- not sure what we should
24703e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // do if it does.
24803e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            ALOGE_IF(err, "eventControl(%d, %d) failed %s",
24903e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian                    event, enabled, strerror(-err));
250e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
2513eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
2523a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
2533a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
2543a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
2553a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
25631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
25731d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
2585f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
259e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id)) {
2601e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
261e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
2621e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
263e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // FIXME: handle multiple displays
26445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
2651e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        // TODO: must handle multiple displays here
266b685c542836b93c99cd85053e07696406ea37adbJesse Hall        // mLists[0] is NULL only when this is called from the constructor
267b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (!mLists[0] || mCapacity < numLayers) {
268b685c542836b93c99cd85053e07696406ea37adbJesse Hall            free(mLists[0]);
26930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            size_t size = sizeof(hwc_display_contents_1_t) + numLayers * sizeof(hwc_layer_1_t);
270b685c542836b93c99cd85053e07696406ea37adbJesse Hall            mLists[0] = (hwc_display_contents_1_t*)malloc(size);
27145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
27245721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
27330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mLists[0]->flags = HWC_GEOMETRY_CHANGED;
27430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mLists[0]->numHwLayers = numLayers;
27530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mLists[0]->flipFenceFd = -1;
276a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
277a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
278a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
279a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
280e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
28130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    int err = mHwc->prepare(mHwc, mNumDisplays,
282b685c542836b93c99cd85053e07696406ea37adbJesse Hall            const_cast<hwc_display_contents_1_t**>(mLists));
283e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
2849c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
2855f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
2865f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
2875f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
2885f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
2895f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // It would be nice if we could get rid of this entirely, which I
2905f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // think is almost possible.
2915f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
2925f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // TODO: must handle multiple displays here
29330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
29430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        size_t count = getNumLayers(0);
29530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        struct hwc_display_contents_1* disp = mLists[0];
29630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mDisplayData[0].hasFbComp = false;
29730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mDisplayData[0].hasOvComp = false;
29830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        for (size_t i=0 ; i<count ; i++) {
29930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            hwc_layer_1_t* l = &disp->hwLayers[i];
30030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            if (l->flags & HWC_SKIP_LAYER) {
30130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian                l->compositionType = HWC_FRAMEBUFFER;
3029c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
30330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            if (l->compositionType == HWC_FRAMEBUFFER)
30430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian                mDisplayData[HWC_DISPLAY_PRIMARY].hasFbComp = true;
30530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            if (l->compositionType == HWC_OVERLAY)
30630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian                mDisplayData[HWC_DISPLAY_PRIMARY].hasOvComp = true;
3079c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
3089c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
309a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
310a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
311a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
312e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
313e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id))
314e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
315e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
316e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
3175f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
318e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
319e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id))
320e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
321e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
3229c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
3239c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
32430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopianstatus_t HWComposer::commit() {
3258630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
3268630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
32730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
32830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = EGL_NO_DISPLAY;
32930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = EGL_NO_SURFACE;
33030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        } else {
33130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // On version 1.0, the OpenGL ES target surface is communicated
33230bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            // by the (dpy, sur) fields
33330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->dpy = eglGetCurrentDisplay();
33430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
33530bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        }
33630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        err = mHwc->set(mHwc, mNumDisplays, mLists);
33730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        if (mLists[0]->flipFenceFd != -1) {
33830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            close(mLists[0]->flipFenceFd);
33930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            mLists[0]->flipFenceFd = -1;
3408630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
34130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mLists[0]->flags &= ~HWC_GEOMETRY_CHANGED;
34258959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
343a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
344a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
345a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
346f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
3477ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
34830bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
34930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        return (status_t)mHwc->blank(mHwc, 0, 1);
3507ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
3517ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
3527ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
3537ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
35410fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Crossstatus_t HWComposer::acquire() const {
35510fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
35630bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        return (status_t)mHwc->blank(mHwc, 0, 0);
35710fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
35810fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
35910fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
36010fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
3617ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
3627ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
36330bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mLists[0]->numHwLayers = 0;
36430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
3657ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
3667ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
3677ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
368f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
369f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
3701e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopiansize_t HWComposer::getNumLayers(int32_t id) const { // FIXME: handle multiple displays
37130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    return mHwc ? mLists[0]->numHwLayers : 0;
372a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
373a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
3753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
3763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
3773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
3783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
3793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
3803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
3813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
3823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
3833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
3843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
3853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
3863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
3873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
3883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
3893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
3903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
3913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
3923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
3933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
3943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
3953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
3963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
3973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
3983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
3995880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
4005880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
4015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
4025880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
4035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
4043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
4055880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion1(hwc_layer_1_t* layer)
4065880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
4073e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
4093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
4103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
4123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
4133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
414ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
415ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
416ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
417ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
418ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
419dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
420dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
421dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
4223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
4243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->compositionType = HWC_FRAMEBUFFER;
4253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->hints = 0;
4263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->flags = HWC_SKIP_LAYER;
4273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = 0;
4283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = HWC_BLENDING_NONE;
4293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.numRects = 0;
4303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects = NULL;
4315880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->acquireFenceFd = -1;
4325880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->releaseFenceFd = -1;
4333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
4353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
4363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
4373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
4383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
4393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
4403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
4423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
4433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
4453e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
4463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
4483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
4493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
4513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
4523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
4543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects =
4553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                reinterpret_cast<hwc_rect_t const *>(
4563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
4573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
4593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
4603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
4613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
4623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
4633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
4643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
4653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
4663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
4683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
4713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4725f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
473e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id))
4745f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
4755f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
476e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // FIXME: handle multiple displays
47730bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (!mHwc || index > mLists[0]->numHwLayers)
4783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
47930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
48030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    return LayerListIterator(new HWCLayerVersion1(mLists[0]->hwLayers), index);
4813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
4823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
4853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4865f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
4871e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
488a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
489a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
4903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
4923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4935f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
4941e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, getNumLayers(id));
4953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
4963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
49722da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
49822da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
499b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
5008372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
501e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n",
502e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                mDebugForceFakeVSync);
503e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  numHwLayers=%u, flags=%08x\n",
50430bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian                mLists[0]->numHwLayers, mLists[0]->flags);
505fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        result.append(
506aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
507aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
508aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
50930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        for (size_t i=0 ; i<mLists[0]->numHwLayers ; i++) {
51030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian            hwc_layer_1_t const* lp= &mLists[0]->hwLayers[i];
511fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
512fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            int32_t format = -1;
513fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            if (layer->getLayer() != NULL) {
514fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
515fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                if (buffer != NULL) {
516fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    format = buffer->getPixelFormat();
517fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
518fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
5191e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            const hwc_layer_1_t& l(*lp);
520e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            result.appendFormat(
521aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
5228372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
523aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
5248372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
52522da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
526fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    layer->getName().string());
5278372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
5281d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
52930bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian
53030bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian    if (mHwc && mHwc->dump) {
53130bcc61431d8e3bef779472dd52a7b156dcaba09Mathias Agopian        mHwc->dump(mHwc, buffer, SIZE);
5321d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
5338372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
5348372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
5358372785879d329f592f6883620b5a32d80d74691Mathias Agopian
536a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
5372965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5382965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
5392965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
5402965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
541e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mRefreshPeriod(hwc.getRefreshPeriod())
5422965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
5432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5442965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
5462965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
5472965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mEnabled = enabled;
5482965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mCondition.signal();
5492965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5502965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5512965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
5522965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
5532965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5542965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5552965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
5562965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
5572965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
5582965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
5592965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
5602965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
5612965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5622965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5632965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
5642965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
5652965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
5662965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
5672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
5682965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
5692965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
5702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
5712965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
5732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
5752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
5762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
5772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
5792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
5802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
5812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
5822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
5842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
5852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
5862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
5882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
5892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
5902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
591a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
592