HWComposer.cpp revision 1e26087493ac0e7d7dc6dea8ad85cfef08b3271f
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
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 Hall// ---------------------------------------------------------------------------
505880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// Support for HWC_DEVICE_API_VERSION_0_3 and older:
515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// Since v0.3 is deprecated and support will be dropped soon, as much as
525880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// possible the code is written to target v1.0. When using a v0.3 HWC, we
53b685c542836b93c99cd85053e07696406ea37adbJesse Hall// allocate v0.3 structures, but assign them to v1.0 pointers.
545880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
555880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall#if HWC_REMOVE_DEPRECATED_VERSIONS
56b685c542836b93c99cd85053e07696406ea37adbJesse Hall// We need complete types to satisfy semantic checks, even though the code
57b685c542836b93c99cd85053e07696406ea37adbJesse Hall// paths that use these won't get executed at runtime (and will likely be dead-
58b685c542836b93c99cd85053e07696406ea37adbJesse Hall// code-eliminated). When we remove the code to support v0.3 we can remove
595880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// these as well.
605880cc573823148237eac9ab7bc586b8e4eb7160Jesse Halltypedef hwc_layer_1_t hwc_layer_t;
61b685c542836b93c99cd85053e07696406ea37adbJesse Halltypedef hwc_display_contents_1_t hwc_layer_list_t;
625880cc573823148237eac9ab7bc586b8e4eb7160Jesse Halltypedef hwc_composer_device_1_t hwc_composer_device_t;
635880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall#endif
645880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
655880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// This function assumes we've already rejected HWC's with lower-than-required
665880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// versions. Don't use it for the initial "does HWC meet requirements" check!
675880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallstatic bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
685880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (HWC_REMOVE_DEPRECATED_VERSIONS &&
695880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            version <= HWC_DEVICE_API_VERSION_1_0) {
705880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return true;
715880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    } else {
725880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return hwc->common.version >= version;
735880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
745880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall}
755880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
76b685c542836b93c99cd85053e07696406ea37adbJesse Hallstatic bool hwcHasVsyncEvent(const hwc_composer_device_1_t* hwc) {
77b685c542836b93c99cd85053e07696406ea37adbJesse Hall    return hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_0_3);
78b685c542836b93c99cd85053e07696406ea37adbJesse Hall}
79b685c542836b93c99cd85053e07696406ea37adbJesse Hall
805880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallstatic size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc,
815880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        size_t numLayers) {
825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
83b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return sizeof(hwc_display_contents_1_t) + numLayers*sizeof(hwc_layer_1_t);
845880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    } else {
855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t);
865880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
875880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall}
885880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
89b685c542836b93c99cd85053e07696406ea37adbJesse Hallstatic int hwcEventControl(hwc_composer_device_1_t* hwc, int dpy,
90b685c542836b93c99cd85053e07696406ea37adbJesse Hall        int event, int enabled) {
91b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
92b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return hwc->methods->eventControl(hwc, dpy, event, enabled);
93b685c542836b93c99cd85053e07696406ea37adbJesse Hall    } else {
941e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
95b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return hwc0->methods->eventControl(hwc0, event, enabled);
96b685c542836b93c99cd85053e07696406ea37adbJesse Hall    }
97b685c542836b93c99cd85053e07696406ea37adbJesse Hall}
98b685c542836b93c99cd85053e07696406ea37adbJesse Hall
99b685c542836b93c99cd85053e07696406ea37adbJesse Hallstatic int hwcBlank(hwc_composer_device_1_t* hwc, int dpy, int blank) {
100b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
101b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return hwc->methods->blank(hwc, dpy, blank);
102b685c542836b93c99cd85053e07696406ea37adbJesse Hall    } else {
103b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (blank) {
1041e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
105b685c542836b93c99cd85053e07696406ea37adbJesse Hall            return hwc0->set(hwc0, NULL, NULL, NULL);
106b685c542836b93c99cd85053e07696406ea37adbJesse Hall        } else {
107b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // HWC 0.x turns the screen on at the next set()
108b685c542836b93c99cd85053e07696406ea37adbJesse Hall            return NO_ERROR;
109b685c542836b93c99cd85053e07696406ea37adbJesse Hall        }
110b685c542836b93c99cd85053e07696406ea37adbJesse Hall    }
111b685c542836b93c99cd85053e07696406ea37adbJesse Hall}
112b685c542836b93c99cd85053e07696406ea37adbJesse Hall
113b685c542836b93c99cd85053e07696406ea37adbJesse Hallstatic int hwcPrepare(hwc_composer_device_1_t* hwc,
114b685c542836b93c99cd85053e07696406ea37adbJesse Hall        size_t numDisplays, hwc_display_contents_1_t** displays) {
115b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
116b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return hwc->prepare(hwc, numDisplays, displays);
117b685c542836b93c99cd85053e07696406ea37adbJesse Hall    } else {
1181e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
1191e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
120b685c542836b93c99cd85053e07696406ea37adbJesse Hall        // In the past, SurfaceFlinger would pass a NULL list when doing full
121b685c542836b93c99cd85053e07696406ea37adbJesse Hall        // OpenGL ES composition. I don't know what, if any, dependencies there
122b685c542836b93c99cd85053e07696406ea37adbJesse Hall        // are on this behavior, so I'm playing it safe and preserving it.
123b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (list0->numHwLayers == 0)
124b685c542836b93c99cd85053e07696406ea37adbJesse Hall            return hwc0->prepare(hwc0, NULL);
125b685c542836b93c99cd85053e07696406ea37adbJesse Hall        else
126b685c542836b93c99cd85053e07696406ea37adbJesse Hall            return hwc0->prepare(hwc0, list0);
127b685c542836b93c99cd85053e07696406ea37adbJesse Hall    }
128b685c542836b93c99cd85053e07696406ea37adbJesse Hall}
129b685c542836b93c99cd85053e07696406ea37adbJesse Hall
130b685c542836b93c99cd85053e07696406ea37adbJesse Hallstatic int hwcSet(hwc_composer_device_1_t* hwc, EGLDisplay dpy, EGLSurface sur,
131b685c542836b93c99cd85053e07696406ea37adbJesse Hall        size_t numDisplays, hwc_display_contents_1_t** displays) {
132b685c542836b93c99cd85053e07696406ea37adbJesse Hall    int err;
133b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
134b685c542836b93c99cd85053e07696406ea37adbJesse Hall        displays[0]->dpy = dpy;
135b685c542836b93c99cd85053e07696406ea37adbJesse Hall        displays[0]->sur = sur;
136b685c542836b93c99cd85053e07696406ea37adbJesse Hall        err = hwc->set(hwc, numDisplays, displays);
137b685c542836b93c99cd85053e07696406ea37adbJesse Hall    } else {
1381e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
1391e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
140b685c542836b93c99cd85053e07696406ea37adbJesse Hall        err = hwc0->set(hwc0, dpy, sur, list0);
141b685c542836b93c99cd85053e07696406ea37adbJesse Hall    }
142b685c542836b93c99cd85053e07696406ea37adbJesse Hall    return err;
143b685c542836b93c99cd85053e07696406ea37adbJesse Hall}
144b685c542836b93c99cd85053e07696406ea37adbJesse Hall
145b685c542836b93c99cd85053e07696406ea37adbJesse Hallstatic uint32_t& hwcFlags(hwc_composer_device_1_t* hwc,
146b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwc_display_contents_1_t* display) {
147b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
148b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return display->flags;
149b685c542836b93c99cd85053e07696406ea37adbJesse Hall    } else {
1501e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
151b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return list0->flags;
152b685c542836b93c99cd85053e07696406ea37adbJesse Hall    }
153b685c542836b93c99cd85053e07696406ea37adbJesse Hall}
154b685c542836b93c99cd85053e07696406ea37adbJesse Hall
155b685c542836b93c99cd85053e07696406ea37adbJesse Hallstatic size_t& hwcNumHwLayers(hwc_composer_device_1_t* hwc,
156b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwc_display_contents_1_t* display) {
157b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
158b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return display->numHwLayers;
159b685c542836b93c99cd85053e07696406ea37adbJesse Hall    } else {
1601e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
161b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return list0->numHwLayers;
162b685c542836b93c99cd85053e07696406ea37adbJesse Hall    }
163b685c542836b93c99cd85053e07696406ea37adbJesse Hall}
164b685c542836b93c99cd85053e07696406ea37adbJesse Hall
165a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
166a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
1683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
1693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
1703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
1713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
1723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
1733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
1743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
1753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
1763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
1773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
1783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
1793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
1803eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
1813eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
182888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        EventHandler& handler)
183c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
184b685c542836b93c99cd85053e07696406ea37adbJesse Hall      mModule(0), mHwc(0), mCapacity(0),
1859c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian      mNumOVLayers(0), mNumFBLayers(0),
1863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
187888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian      mEventHandler(handler), mRefreshPeriod(0),
188e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
189a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
190b685c542836b93c99cd85053e07696406ea37adbJesse Hall    for (size_t i = 0; i < MAX_DISPLAYS; i++)
191b685c542836b93c99cd85053e07696406ea37adbJesse Hall        mLists[i] = NULL;
192b685c542836b93c99cd85053e07696406ea37adbJesse Hall
193e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
194e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
195e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
196e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
197028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
198a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
19932397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
200a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
2015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        err = hwc_open_1(mModule, &mHwc);
202e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err, "%s device failed to initialize (%s)",
203a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
204c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
2055880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (HWC_REMOVE_DEPRECATED_VERSIONS &&
2065880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                    mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
2075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                ALOGE("%s device version %#x too old, will not be used",
2085880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                        HWC_HARDWARE_COMPOSER, mHwc->common.version);
2095880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                hwc_close_1(mHwc);
2105880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                mHwc = NULL;
2115880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
2125880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
2135880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
2145880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (mHwc) {
215b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // always turn vsync off when we start
216b685c542836b93c99cd85053e07696406ea37adbJesse Hall            needVSyncThread = false;
217b685c542836b93c99cd85053e07696406ea37adbJesse Hall            if (hwcHasVsyncEvent(mHwc)) {
218b685c542836b93c99cd85053e07696406ea37adbJesse Hall                hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
219888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
220888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                int period;
221888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
222888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                    mRefreshPeriod = nsecs_t(period);
223888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                }
224b685c542836b93c99cd85053e07696406ea37adbJesse Hall            } else {
225b685c542836b93c99cd85053e07696406ea37adbJesse Hall                needVSyncThread = true;
226028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian            }
227b685c542836b93c99cd85053e07696406ea37adbJesse Hall
228c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            if (mHwc->registerProcs) {
2293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mCBContext->hwc = this;
2303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mCBContext->procs.invalidate = &hook_invalidate;
2313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mCBContext->procs.vsync = &hook_vsync;
2323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                mHwc->registerProcs(mHwc, &mCBContext->procs);
2333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
234c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            }
235b685c542836b93c99cd85053e07696406ea37adbJesse Hall
236b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // create initial empty display contents for display 0
2371e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            createWorkList(MAIN, 0);
238c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
2393a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
2403a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
241888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    if (mRefreshPeriod == 0) {
242888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        // for compatibility, we attempt to get the refresh rate from
243888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        // the FB HAL if we couldn't get it from the HWC HAL.
244888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        hw_module_t const* module;
245888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
246888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian            framebuffer_device_t* fbDev;
247888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian            int err = framebuffer_open(module, &fbDev);
248888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian            if (!err && fbDev) {
249888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                mRefreshPeriod = nsecs_t(1e9 / fbDev->fps);
250888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                framebuffer_close(fbDev);
251888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian            }
252888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        }
253888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        ALOGW("getting VSYNC period from fb HAL: %lld", mRefreshPeriod);
254888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
255888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
256888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    if (mRefreshPeriod == 0) {
257888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        mRefreshPeriod = nsecs_t(1e9 / 60.0);
258888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        ALOGW("getting VSYNC period thin air: %lld", mRefreshPeriod);
259888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
260888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
2613a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
2623a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
2633a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
264a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
265a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
266a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
267a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
268b685c542836b93c99cd85053e07696406ea37adbJesse Hall    hwcEventControl(mHwc, 0, EVENT_VSYNC, 0);
269b685c542836b93c99cd85053e07696406ea37adbJesse Hall    for (size_t i = 0; i < MAX_DISPLAYS; i++)
270b685c542836b93c99cd85053e07696406ea37adbJesse Hall        free(mLists[i]);
2713eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
2723eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
2733eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
274a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2755880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
276a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
2773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
278a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
279a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
280a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
281a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
282a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
283a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
284c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::hook_invalidate(struct hwc_procs* procs) {
285c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
286c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
287c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
28831d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
28931d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
29031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
29131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
292c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
293e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
294c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
295c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
29631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::vsync(int dpy, int64_t timestamp) {
2972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
2983eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    mEventHandler.onVSyncReceived(dpy, timestamp);
299d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
300d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mLastHwVSync = timestamp;
301d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
302d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
303888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopiannsecs_t HWComposer::getRefreshPeriod() const {
304888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    return mRefreshPeriod;
305888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian}
306888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
307d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopiannsecs_t HWComposer::getRefreshTimestamp() const {
308d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
309d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
310d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
311d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
312d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
313d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    return now - ((now - mLastHwVSync) %  mRefreshPeriod);
3143eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
3153eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
31603e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopianvoid HWComposer::eventControl(int event, int enabled) {
3173eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
3181a3bf41b7165ba294af46bc32483eaad61e707eaErik Gilling    if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
319e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        if (!mDebugForceFakeVSync) {
320b685c542836b93c99cd85053e07696406ea37adbJesse Hall            err = hwcEventControl(mHwc, 0, event, enabled);
32103e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // error here should not happen -- not sure what we should
32203e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // do if it does.
32303e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            ALOGE_IF(err, "eventControl(%d, %d) failed %s",
32403e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian                    event, enabled, strerror(-err));
325e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
3263eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
3273a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
3283a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
3293a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
3303a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
33131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
33231d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
3331e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) { // FIXME: handle multiple displays
3341e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    if (uint32_t(id) >= MAX_DISPLAYS)
3351e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
3361e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
33745721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
3381e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        // TODO: must handle multiple displays here
339b685c542836b93c99cd85053e07696406ea37adbJesse Hall        // mLists[0] is NULL only when this is called from the constructor
340b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (!mLists[0] || mCapacity < numLayers) {
341b685c542836b93c99cd85053e07696406ea37adbJesse Hall            free(mLists[0]);
3425880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            size_t size = sizeofHwcLayerList(mHwc, numLayers);
343b685c542836b93c99cd85053e07696406ea37adbJesse Hall            mLists[0] = (hwc_display_contents_1_t*)malloc(size);
34445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
34545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
346b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcFlags(mHwc, mLists[0]) = HWC_GEOMETRY_CHANGED;
347b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcNumHwLayers(mHwc, mLists[0]) = numLayers;
348b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
349b685c542836b93c99cd85053e07696406ea37adbJesse Hall            mLists[0]->flipFenceFd = -1;
350b685c542836b93c99cd85053e07696406ea37adbJesse Hall        }
351a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
352a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
353a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
354a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
355a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::prepare() const {
356b685c542836b93c99cd85053e07696406ea37adbJesse Hall    int err = hwcPrepare(mHwc, 1,
357b685c542836b93c99cd85053e07696406ea37adbJesse Hall            const_cast<hwc_display_contents_1_t**>(mLists));
3589c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
3599c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numOVLayers = 0;
3609c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numFBLayers = 0;
3611e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        size_t count = getNumLayers(0);
3621e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
3639c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        for (size_t i=0 ; i<count ; i++) {
3641e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            int compositionType;
3655880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
3661e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                hwc_layer_1_t* l = &mLists[0]->hwLayers[i];
3671e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                if (l->flags & HWC_SKIP_LAYER) {
3681e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                    l->compositionType = HWC_FRAMEBUFFER;
3691e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                }
3701e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                compositionType = l->compositionType;
3715880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            } else {
3725880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                // mList really has hwc_layer_list_t memory layout
3731e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
3741e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                hwc_layer_t* l = &list0->hwLayers[i];
3751e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                if (l->flags & HWC_SKIP_LAYER) {
3761e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                    l->compositionType = HWC_FRAMEBUFFER;
3771e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                }
3781e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                compositionType = l->compositionType;
3795880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
3801e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
3811e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            switch (compositionType) {
3829c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_OVERLAY:
3839c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numOVLayers++;
3849c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
3859c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_FRAMEBUFFER:
3869c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numFBLayers++;
3879c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
3889c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
3899c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
3909c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumOVLayers = numOVLayers;
3919c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumFBLayers = numFBLayers;
3929c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
393a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
394a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
395a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3961e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopiansize_t HWComposer::getLayerCount(int32_t id, int type) const { // FIXME: handle multiple displays
3979c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    switch (type) {
3989c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_OVERLAY:
3999c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumOVLayers;
4009c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_FRAMEBUFFER:
4019c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumFBLayers;
4029c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
4039c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    return 0;
4049c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
4059c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
40634a09ba1efd706323a15633da5044b352988eb5fJesse Hallstatus_t HWComposer::commit(void* fbDisplay, void* fbSurface) const {
4078630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
4088630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
409b685c542836b93c99cd85053e07696406ea37adbJesse Hall        err = hwcSet(mHwc, fbDisplay, fbSurface, 1,
410b685c542836b93c99cd85053e07696406ea37adbJesse Hall                const_cast<hwc_display_contents_1_t**>(mLists));
411b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
412b685c542836b93c99cd85053e07696406ea37adbJesse Hall            if (mLists[0]->flipFenceFd != -1) {
413b685c542836b93c99cd85053e07696406ea37adbJesse Hall                close(mLists[0]->flipFenceFd);
414b685c542836b93c99cd85053e07696406ea37adbJesse Hall                mLists[0]->flipFenceFd = -1;
415b685c542836b93c99cd85053e07696406ea37adbJesse Hall            }
4168630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
417b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcFlags(mHwc, mLists[0]) &= ~HWC_GEOMETRY_CHANGED;
41858959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
419a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
420a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
421a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
422f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
4237ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
424b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (hwcHasVsyncEvent(mHwc)) {
425b685c542836b93c99cd85053e07696406ea37adbJesse Hall            hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
42610fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross        }
427b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return (status_t)hwcBlank(mHwc, 0, 1);
4287ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
4297ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
4307ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
4317ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
43210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Crossstatus_t HWComposer::acquire() const {
43310fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
434b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return (status_t)hwcBlank(mHwc, 0, 0);
43510fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
43610fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
43710fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
43810fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
4397ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
4407ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
441b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcNumHwLayers(mHwc, mLists[0]) = 0;
442b685c542836b93c99cd85053e07696406ea37adbJesse Hall        int err = hwcPrepare(mHwc, 1, mLists);
4437ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
4447ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
4457ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
446f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
447f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
4481e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopiansize_t HWComposer::getNumLayers(int32_t id) const { // FIXME: handle multiple displays
449b685c542836b93c99cd85053e07696406ea37adbJesse Hall    return mHwc ? hwcNumHwLayers(mHwc, mLists[0]) : 0;
450a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
451a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
4523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
4543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
4553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
4563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
4583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
4593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
4603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
4613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
4623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
4633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
4643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
4653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
4663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
4673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
4683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
4693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
4703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
4723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
4733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
4743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
4753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
4763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
4775880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// #if !HWC_REMOVE_DEPRECATED_VERSIONS
4783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
4793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
4803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This implements the HWCLayer side of HWCIterableLayer.
4813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
4825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
4835880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallpublic:
4845880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion0(hwc_layer_t* layer)
4855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
4865880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
4875880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual int32_t getCompositionType() const {
4885880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return getLayer()->compositionType;
4895880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
4905880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual uint32_t getHints() const {
4915880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return getLayer()->hints;
4925880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
493ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
494ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        // not supported on VERSION_03
495ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return -1;
496ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
497dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
498dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        if (fenceFd != -1) {
499dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            ALOGE("HWC 0.x can't handle acquire fences");
500dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            close(fenceFd);
501dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        }
502dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
5035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
5045880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setDefaultState() {
5055880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->compositionType = HWC_FRAMEBUFFER;
5065880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->hints = 0;
5075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->flags = HWC_SKIP_LAYER;
5085880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->transform = 0;
5095880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->blending = HWC_BLENDING_NONE;
5105880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.numRects = 0;
5115880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.rects = NULL;
5125880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5135880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setSkip(bool skip) {
5145880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (skip) {
5155880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags |= HWC_SKIP_LAYER;
5165880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        } else {
5175880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags &= ~HWC_SKIP_LAYER;
5185880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
5195880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5205880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setBlending(uint32_t blending) {
5215880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->blending = blending;
5225880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5235880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setTransform(uint32_t transform) {
5245880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->transform = transform;
5255880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5265880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setFrame(const Rect& frame) {
5275880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
5285880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5295880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setCrop(const Rect& crop) {
5305880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
5315880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5325880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setVisibleRegionScreen(const Region& reg) {
5335880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.rects =
5345880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                reinterpret_cast<hwc_rect_t const *>(
5355880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
5365880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5375880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
5385880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (buffer == 0 || buffer->handle == 0) {
5395880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->compositionType = HWC_FRAMEBUFFER;
5405880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags |= HWC_SKIP_LAYER;
5415880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->handle = 0;
5425880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        } else {
5435880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->handle = buffer->handle;
5445880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
5455880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5465880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall};
5475880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
5485880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
5495880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
5505880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
5515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
5525880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
5535880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
5543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
5555880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion1(hwc_layer_1_t* layer)
5565880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
5573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
5583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
5593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
5603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
5613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
5623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
5633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
564ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
565ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
566ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
567ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
568ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
569dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
570dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
571dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
5723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
5733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
5743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->compositionType = HWC_FRAMEBUFFER;
5753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->hints = 0;
5763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->flags = HWC_SKIP_LAYER;
5773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = 0;
5783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = HWC_BLENDING_NONE;
5793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.numRects = 0;
5803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects = NULL;
5815880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->acquireFenceFd = -1;
5825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->releaseFenceFd = -1;
5833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
5843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
5853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
5863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
5873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
5883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
5893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
5903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
5913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
5923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
5933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
5943e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
5953e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
5963e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
5973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
5983e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
5993e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6003e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
6013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
6023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
6043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects =
6053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                reinterpret_cast<hwc_rect_t const *>(
6063e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
6073e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6083e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
6093e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
6103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
6113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
6123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
6133e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
6143e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
6153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
6163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
6183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
6193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
6203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
6213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
6221e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) { // FIXME: handle multiple displays
623b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (index > hwcNumHwLayers(mHwc, mLists[0]))
6243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
6255880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
626b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return LayerListIterator(new HWCLayerVersion1(mLists[0]->hwLayers),
627b685c542836b93c99cd85053e07696406ea37adbJesse Hall                index);
6285880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    } else {
6291e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
6305880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
6315880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
6323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
6333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
6343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
6353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
6363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
6371e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) { // FIXME: handle multiple displays
6381e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
639a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
640a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
6413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
6423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
6433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
6441e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) { // FIXME: handle multiple displays
6451e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, getNumLayers(id));
6463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
6473e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
64822da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
64922da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
650b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
6511e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
652b685c542836b93c99cd85053e07696406ea37adbJesse Hall
6538372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
654e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n",
655e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                mDebugForceFakeVSync);
656e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  numHwLayers=%u, flags=%08x\n",
657b685c542836b93c99cd85053e07696406ea37adbJesse Hall                hwcNumHwLayers(mHwc, mLists[0]), hwcFlags(mHwc, mLists[0]));
658fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        result.append(
659aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
660aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
661aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
662b685c542836b93c99cd85053e07696406ea37adbJesse Hall        for (size_t i=0 ; i<hwcNumHwLayers(mHwc, mLists[0]) ; i++) {
6631e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            hwc_layer_1_t const* lp;
6645880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
6651e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                lp = &mLists[0]->hwLayers[i];
6665880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            } else {
6671e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                // FIXME: here we rely on hwc_layer_1_t and hwc_layer_t having the same layout
6681e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                lp = reinterpret_cast<hwc_layer_1_t const*>(&list0->hwLayers[i]);
6695880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
670fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
671fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            int32_t format = -1;
672fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            if (layer->getLayer() != NULL) {
673fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
674fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                if (buffer != NULL) {
675fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    format = buffer->getPixelFormat();
676fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
677fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
6781e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            const hwc_layer_1_t& l(*lp);
679e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            result.appendFormat(
680aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
6818372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
682aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
6838372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
68422da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
685fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    layer->getName().string());
6868372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
6871d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
6885880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) {
6891d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mHwc->dump(mHwc, buffer, SIZE);
6901d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
6918372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
6928372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
6938372785879d329f592f6883620b5a32d80d74691Mathias Agopian
694a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
6952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
6962965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
6972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
6982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
6992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mRefreshPeriod(hwc.mRefreshPeriod)
7002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
7012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
7022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
7042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
7052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mEnabled = enabled;
7062965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mCondition.signal();
7072965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
7082965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7092965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
7102965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
7112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
7122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
7142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
7152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
7162965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
7172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
7182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
7192965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
7202965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
7222965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
7232965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
7242965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
7252965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
7262965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
7272965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
7282965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
7292965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
7302965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
7312965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7322965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
7332965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
7342965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
7352965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7362965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
7372965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
7382965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
7392965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
7402965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7412965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
7422965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
7432965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
7442965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7452965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
7462965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
7472965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7482965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
749a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
750