HWComposer.cpp revision e60b0687c8d49871d0c8786cabe6851f7a7783b5
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)) {
92bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        return hwc->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)) {
101bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        return hwc->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
165bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallstatic void hwcDump(hwc_composer_device_1_t* hwc, char* buff, int buff_len) {
166bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
167bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        if (hwc->dump)
168bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            hwc->dump(hwc, buff, buff_len);
169bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    } else if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_0_1)) {
170bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
171bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        if (hwc0->dump)
172bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            hwc0->dump(hwc0, buff, buff_len);
173bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    }
174bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall}
175bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
176a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
177a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianstruct HWComposer::cb_context {
1793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    struct callbacks : public hwc_procs_t {
1803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // these are here to facilitate the transition when adding
1813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // new callbacks (an implementation can check for NULL before
1823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        // calling a new callback).
1833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        void (*zero[4])(void);
1843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    };
1853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    callbacks procs;
1863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWComposer* hwc;
1873e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
1883e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
1893e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian// ---------------------------------------------------------------------------
1903e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
1913eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
1923eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
1938b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        EventHandler& handler,
1948b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian        framebuffer_device_t const* fbDev)
195c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
1968f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall      mModule(0), mHwc(0), mNumDisplays(1), mCapacity(0),
1973e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian      mCBContext(new cb_context),
198e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mEventHandler(handler),
199e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
200a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
201e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
202e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mLists[i] = 0;
203e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
204b685c542836b93c99cd85053e07696406ea37adbJesse Hall
205e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
206e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
207e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
208e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
209028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian    bool needVSyncThread = true;
210a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
21132397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
212a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
2135880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        err = hwc_open_1(mModule, &mHwc);
214e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err, "%s device failed to initialize (%s)",
215a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
216c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
2175880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (HWC_REMOVE_DEPRECATED_VERSIONS &&
2185880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                    mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
2195880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                ALOGE("%s device version %#x too old, will not be used",
2205880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                        HWC_HARDWARE_COMPOSER, mHwc->common.version);
2215880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                hwc_close_1(mHwc);
2225880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                mHwc = NULL;
2235880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
2245880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
2255880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
2265880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (mHwc) {
227bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            if (mHwc->registerProcs) {
228bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mCBContext->hwc = this;
229bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mCBContext->procs.invalidate = &hook_invalidate;
230bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mCBContext->procs.vsync = &hook_vsync;
231bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
232bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall                mHwc->registerProcs(mHwc, &mCBContext->procs);
233bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            }
234bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall
235e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            // these IDs are always reserved
236e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mTokens.markBit(HWC_DISPLAY_PRIMARY);
237e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mTokens.markBit(HWC_DISPLAY_EXTERNAL);
238e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
239b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // always turn vsync off when we start
240b685c542836b93c99cd85053e07696406ea37adbJesse Hall            needVSyncThread = false;
241b685c542836b93c99cd85053e07696406ea37adbJesse Hall            if (hwcHasVsyncEvent(mHwc)) {
242b685c542836b93c99cd85053e07696406ea37adbJesse Hall                hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
243888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
244888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                int period;
245888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
246e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                    mDisplayData[0].refresh = nsecs_t(period);
247888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian                }
248b685c542836b93c99cd85053e07696406ea37adbJesse Hall            } else {
249b685c542836b93c99cd85053e07696406ea37adbJesse Hall                needVSyncThread = true;
250028508cad5ef63ef9fbd42c14e76658e4fd9ebf2Mathias Agopian            }
251b685c542836b93c99cd85053e07696406ea37adbJesse Hall
2528f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
2538f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall                mNumDisplays = HWC_NUM_DISPLAY_TYPES;
2548f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall
255b685c542836b93c99cd85053e07696406ea37adbJesse Hall            // create initial empty display contents for display 0
2568f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall            createWorkList(HWC_DISPLAY_PRIMARY, 0);
257c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
2583a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
2593a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
2608b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
2618b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian    if (fbDev) {
262e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        if (mDisplayData[HWC_DISPLAY_PRIMARY].refresh == 0) {
263e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(1e9 / fbDev->fps);
264e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            ALOGW("getting VSYNC period from fb HAL: %lld", mDisplayData[0].refresh);
265888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian        }
266e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mDisplayData[HWC_DISPLAY_PRIMARY].xdpi = fbDev->xdpi;
267e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mDisplayData[HWC_DISPLAY_PRIMARY].ydpi = fbDev->ydpi;
268888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
269888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
270e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (mDisplayData[HWC_DISPLAY_PRIMARY].refresh == 0) {
271e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(1e9 / 60.0);
272e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        ALOGW("getting VSYNC period thin air: %lld", mDisplayData[0].refresh);
273888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian    }
274888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
2753a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
2763a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
2773a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
278a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
279a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
280a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
281a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
282b685c542836b93c99cd85053e07696406ea37adbJesse Hall    hwcEventControl(mHwc, 0, EVENT_VSYNC, 0);
283e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    for (size_t i = 0; i < MAX_DISPLAYS; i++) {
284b685c542836b93c99cd85053e07696406ea37adbJesse Hall        free(mLists[i]);
285e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
2863eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
2873eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
2883eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
289a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
2905880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        hwc_close_1(mHwc);
291a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
2923e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    delete mCBContext;
293a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
294a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
295a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
296a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
297a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
298a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
299bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_invalidate(const struct hwc_procs* procs) {
300bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
301bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
302bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->invalidate();
303c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
304c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
305bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hallvoid HWComposer::hook_vsync(const struct hwc_procs* procs, int dpy,
306bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        int64_t timestamp) {
307bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    cb_context* ctx = reinterpret_cast<cb_context*>(
308bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall            const_cast<hwc_procs_t*>(procs));
309bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    ctx->hwc->vsync(dpy, timestamp);
31031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
31131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
312c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
313e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
314c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
315c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
31631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::vsync(int dpy, int64_t timestamp) {
3172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
3183eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    mEventHandler.onVSyncReceived(dpy, timestamp);
319d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
320d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    mLastHwVSync = timestamp;
321d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian}
322d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian
323e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianint32_t HWComposer::allocateDisplayId() {
324e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (mTokens.isFull()) {
325e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return NO_MEMORY;
326e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
327e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
328e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // FIXME: for now we don't support h/w composition wifi displays
329e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return -1;
330e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
331e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    int32_t id = mTokens.firstUnmarkedBit();
332e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    mTokens.markBit(id);
333e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return id;
334e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
335e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
336e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::freeDisplayId(int32_t id) {
337e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (id < MAX_DISPLAYS) {
338e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_VALUE;
339e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
340e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id)) {
341e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return BAD_INDEX;
342e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
343e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    mTokens.clearBit(id);
344e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return NO_ERROR;
345e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
346e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
347888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopiannsecs_t HWComposer::getRefreshPeriod() const {
348e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[0].refresh;
349888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian}
350888c822c4cb6976aab9256c58bae9e17e3e55c5cMathias Agopian
351d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopiannsecs_t HWComposer::getRefreshTimestamp() const {
352d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // this returns the last refresh timestamp.
353d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // if the last one is not available, we estimate it based on
354d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    // the refresh period and whatever closest timestamp we have.
355d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    Mutex::Autolock _l(mLock);
356d3ee231eddce0b69ec5e35188dbd0f4a2c3b9ac3Mathias Agopian    nsecs_t now = systemTime(CLOCK_MONOTONIC);
357e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return now - ((now - mLastHwVSync) %  mDisplayData[0].refresh);
3583eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
3593eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
3608b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopianfloat HWComposer::getDpiX() const {
361e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi;
3628b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
3638b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
3648b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopianfloat HWComposer::getDpiY() const {
365e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi;
3668b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian}
3678b736f138cfd9b239a2c7073347a13c489534ae1Mathias Agopian
36803e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopianvoid HWComposer::eventControl(int event, int enabled) {
3693eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
3701a3bf41b7165ba294af46bc32483eaad61e707eaErik Gilling    if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
371e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        if (!mDebugForceFakeVSync) {
372b685c542836b93c99cd85053e07696406ea37adbJesse Hall            err = hwcEventControl(mHwc, 0, event, enabled);
37303e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // error here should not happen -- not sure what we should
37403e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            // do if it does.
37503e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian            ALOGE_IF(err, "eventControl(%d, %d) failed %s",
37603e407270c7ad76632f982c886d0776bed9e9b4cMathias Agopian                    event, enabled, strerror(-err));
377e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
3783eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
3793a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
3803a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
3813a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
3823a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
38331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
38431d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
3855f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopianstatus_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
386e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id)) {
3871e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        return BAD_INDEX;
388e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    }
3891e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian
390e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // FIXME: handle multiple displays
39145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
3921e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        // TODO: must handle multiple displays here
393b685c542836b93c99cd85053e07696406ea37adbJesse Hall        // mLists[0] is NULL only when this is called from the constructor
394b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (!mLists[0] || mCapacity < numLayers) {
395b685c542836b93c99cd85053e07696406ea37adbJesse Hall            free(mLists[0]);
3965880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            size_t size = sizeofHwcLayerList(mHwc, numLayers);
397b685c542836b93c99cd85053e07696406ea37adbJesse Hall            mLists[0] = (hwc_display_contents_1_t*)malloc(size);
39845721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
39945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
400b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcFlags(mHwc, mLists[0]) = HWC_GEOMETRY_CHANGED;
401b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcNumHwLayers(mHwc, mLists[0]) = numLayers;
402b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
403b685c542836b93c99cd85053e07696406ea37adbJesse Hall            mLists[0]->flipFenceFd = -1;
404b685c542836b93c99cd85053e07696406ea37adbJesse Hall        }
405a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
406a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
407a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
408a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
409e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::prepare() {
4108f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall    int err = hwcPrepare(mHwc, mNumDisplays,
411b685c542836b93c99cd85053e07696406ea37adbJesse Hall            const_cast<hwc_display_contents_1_t**>(mLists));
412e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian
4139c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
4145f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
4155f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // here we're just making sure that "skip" layers are set
4165f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // to HWC_FRAMEBUFFER and we're also counting how many layers
4175f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // we have of each type.
4185f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // It would be nice if we could get rid of this entirely, which I
4195f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // think is almost possible.
4205f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
4215f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        // TODO: must handle multiple displays here
422e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
423e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            size_t count = getNumLayers(0);
424e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            struct hwc_display_contents_1* disp = mLists[0];
425e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mDisplayData[0].hasFbComp = false;
426e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mDisplayData[0].hasOvComp = false;
427e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            for (size_t i=0 ; i<count ; i++) {
428e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                hwc_layer_1_t* l = &disp->hwLayers[i];
4291e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                if (l->flags & HWC_SKIP_LAYER) {
4301e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                    l->compositionType = HWC_FRAMEBUFFER;
4311e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                }
432e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                if (l->compositionType == HWC_FRAMEBUFFER)
433e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                    mDisplayData[HWC_DISPLAY_PRIMARY].hasFbComp = true;
434e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                if (l->compositionType == HWC_OVERLAY)
435e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                    mDisplayData[HWC_DISPLAY_PRIMARY].hasOvComp = true;
436e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            }
437e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        } else {
438e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            size_t count = getNumLayers(0);
439e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            hwc_layer_list_t* disp = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
440e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mDisplayData[0].hasFbComp = false;
441e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            mDisplayData[0].hasOvComp = false;
442e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian            for (size_t i=0 ; i<count ; i++) {
443e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                hwc_layer_t* l = &disp->hwLayers[i];
4441e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                if (l->flags & HWC_SKIP_LAYER) {
4451e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                    l->compositionType = HWC_FRAMEBUFFER;
4461e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                }
447e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                if (l->compositionType == HWC_FRAMEBUFFER)
448e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                    mDisplayData[HWC_DISPLAY_PRIMARY].hasFbComp = true;
449e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                if (l->compositionType == HWC_OVERLAY)
450e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian                    mDisplayData[HWC_DISPLAY_PRIMARY].hasOvComp = true;
4519c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
4529c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
4539c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
454a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
455a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
456a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
457e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasHwcComposition(int32_t id) const {
458e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id))
459e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
460e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasOvComp;
461e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian}
4625f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
463e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianbool HWComposer::hasGlesComposition(int32_t id) const {
464e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id))
465e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        return false;
466e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    return mDisplayData[id].hasFbComp;
4679c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
4689c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
469e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopianstatus_t HWComposer::commit() const {
4708630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    int err = NO_ERROR;
4718630320433bd15aca239522e54e711ef6372ab07Mathias Agopian    if (mHwc) {
472e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        void* fbDisplay = eglGetCurrentDisplay();
473e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian        void* fbSurface = eglGetCurrentSurface(EGL_DRAW);
4748f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall        err = hwcSet(mHwc, fbDisplay, fbSurface, mNumDisplays,
475b685c542836b93c99cd85053e07696406ea37adbJesse Hall                const_cast<hwc_display_contents_1_t**>(mLists));
476b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
477b685c542836b93c99cd85053e07696406ea37adbJesse Hall            if (mLists[0]->flipFenceFd != -1) {
478b685c542836b93c99cd85053e07696406ea37adbJesse Hall                close(mLists[0]->flipFenceFd);
479b685c542836b93c99cd85053e07696406ea37adbJesse Hall                mLists[0]->flipFenceFd = -1;
480b685c542836b93c99cd85053e07696406ea37adbJesse Hall            }
4818630320433bd15aca239522e54e711ef6372ab07Mathias Agopian        }
482b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcFlags(mHwc, mLists[0]) &= ~HWC_GEOMETRY_CHANGED;
48358959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
484a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
485a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
486a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
487f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
4887ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
489b685c542836b93c99cd85053e07696406ea37adbJesse Hall        if (hwcHasVsyncEvent(mHwc)) {
490b685c542836b93c99cd85053e07696406ea37adbJesse Hall            hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
49110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross        }
492b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return (status_t)hwcBlank(mHwc, 0, 1);
4937ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
4947ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
4957ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
4967ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
49710fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Crossstatus_t HWComposer::acquire() const {
49810fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    if (mHwc) {
499b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return (status_t)hwcBlank(mHwc, 0, 0);
50010fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    }
50110fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross    return NO_ERROR;
50210fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross}
50310fbdb6059755f009e02f0ccc2b9d289beb6b086Colin Cross
5047ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
5057ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
506b685c542836b93c99cd85053e07696406ea37adbJesse Hall        hwcNumHwLayers(mHwc, mLists[0]) = 0;
5078f971ff6661c875e7adb3f14731e1579c3c80c62Jesse Hall        int err = hwcPrepare(mHwc, mNumDisplays, mLists);
5087ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
5097ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
5107ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
511f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
512f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
5131e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopiansize_t HWComposer::getNumLayers(int32_t id) const { // FIXME: handle multiple displays
514b685c542836b93c99cd85053e07696406ea37adbJesse Hall    return mHwc ? hwcNumHwLayers(mHwc, mLists[0]) : 0;
515a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
516a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
5173e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
5183e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Helper template to implement a concrete HWCLayer
5193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This holds the pointer to the concrete hwc layer type
5203e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * and implements the "iterable" side of HWCLayer.
5213e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
5223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopiantemplate<typename CONCRETE, typename HWCTYPE>
5233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianclass Iterable : public HWComposer::HWCLayer {
5243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprotected:
5253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* const mLayerList;
5263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    HWCTYPE* mCurrentLayer;
5273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
5283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
5293e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    inline HWCTYPE* getLayer() { return mCurrentLayer; }
5303e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual ~Iterable() { }
5313e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianprivate:
5323e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // returns a copy of ourselves
5333e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual HWComposer::HWCLayer* dup() {
5343e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return new CONCRETE( static_cast<const CONCRETE&>(*this) );
5353e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
5363e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual status_t setLayer(size_t index) {
5373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        mCurrentLayer = &mLayerList[index];
5383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return NO_ERROR;
5393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
5403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
5413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
5425880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// #if !HWC_REMOVE_DEPRECATED_VERSIONS
5433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
5443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
5453e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * This implements the HWCLayer side of HWCIterableLayer.
5463e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
5475880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
5485880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallpublic:
5495880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion0(hwc_layer_t* layer)
5505880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
5515880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
5525880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual int32_t getCompositionType() const {
5535880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return getLayer()->compositionType;
5545880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5555880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual uint32_t getHints() const {
5565880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return getLayer()->hints;
5575880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
558ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
559ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        // not supported on VERSION_03
560ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return -1;
561ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
562dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
563dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        if (fenceFd != -1) {
564dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            ALOGE("HWC 0.x can't handle acquire fences");
565dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall            close(fenceFd);
566dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        }
567dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
5685880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
5695880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setDefaultState() {
5705880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->compositionType = HWC_FRAMEBUFFER;
5715880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->hints = 0;
5725880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->flags = HWC_SKIP_LAYER;
5735880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->transform = 0;
5745880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->blending = HWC_BLENDING_NONE;
5755880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.numRects = 0;
5765880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.rects = NULL;
5775880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5785880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setSkip(bool skip) {
5795880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (skip) {
5805880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags |= HWC_SKIP_LAYER;
5815880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        } else {
5825880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags &= ~HWC_SKIP_LAYER;
5835880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
5845880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5855880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setBlending(uint32_t blending) {
5865880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->blending = blending;
5875880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5885880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setTransform(uint32_t transform) {
5895880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->transform = transform;
5905880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5915880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setFrame(const Rect& frame) {
5925880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
5935880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5945880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setCrop(const Rect& crop) {
5955880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
5965880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
5975880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setVisibleRegionScreen(const Region& reg) {
5985880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->visibleRegionScreen.rects =
5995880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                reinterpret_cast<hwc_rect_t const *>(
6005880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
6015880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
6025880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
6035880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        if (buffer == 0 || buffer->handle == 0) {
6045880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->compositionType = HWC_FRAMEBUFFER;
6055880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->flags |= HWC_SKIP_LAYER;
6065880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->handle = 0;
6075880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        } else {
6085880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            getLayer()->handle = buffer->handle;
6095880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        }
6105880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
6115880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall};
6125880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
6135880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall
6145880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall/*
6155880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
6165880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall * This implements the HWCLayer side of HWCIterableLayer.
6175880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall */
6185880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hallclass HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
6193e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopianpublic:
6205880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    HWCLayerVersion1(hwc_layer_1_t* layer)
6215880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
6223e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
6233e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual int32_t getCompositionType() const {
6243e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->compositionType;
6253e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6263e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual uint32_t getHints() const {
6273e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return getLayer()->hints;
6283e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
629ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    virtual int getAndResetReleaseFenceFd() {
630ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        int fd = getLayer()->releaseFenceFd;
631ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        getLayer()->releaseFenceFd = -1;
632ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall        return fd;
633ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall    }
634dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    virtual void setAcquireFenceFd(int fenceFd) {
635dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall        getLayer()->acquireFenceFd = fenceFd;
636dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    }
6373e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
6383e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setDefaultState() {
6393e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->compositionType = HWC_FRAMEBUFFER;
6403e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->hints = 0;
6413e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->flags = HWC_SKIP_LAYER;
6423e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = 0;
6433e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = HWC_BLENDING_NONE;
6443e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.numRects = 0;
6453e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects = NULL;
6465880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->acquireFenceFd = -1;
6475880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        getLayer()->releaseFenceFd = -1;
6483e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6493e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setSkip(bool skip) {
6503e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (skip) {
6513e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
6523e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
6533e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags &= ~HWC_SKIP_LAYER;
6543e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
6553e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6563e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBlending(uint32_t blending) {
6573e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->blending = blending;
6583e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6593e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setTransform(uint32_t transform) {
6603e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->transform = transform;
6613e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6623e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setFrame(const Rect& frame) {
6633e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
6643e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6653e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setCrop(const Rect& crop) {
6663e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
6673e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6683e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setVisibleRegionScreen(const Region& reg) {
6693e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        getLayer()->visibleRegionScreen.rects =
6703e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                reinterpret_cast<hwc_rect_t const *>(
6713e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian                        reg.getArray(&getLayer()->visibleRegionScreen.numRects));
6723e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6733e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
6743e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        if (buffer == 0 || buffer->handle == 0) {
6753e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->compositionType = HWC_FRAMEBUFFER;
6763e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->flags |= HWC_SKIP_LAYER;
6773e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = 0;
6783e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        } else {
6793e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian            getLayer()->handle = buffer->handle;
6803e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        }
6813e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    }
6823e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian};
6833e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
6843e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
6853e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator initialized at a given index in the layer list
6863e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
6875f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
688e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    if (!mTokens.hasBit(id))
6895f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian        return LayerListIterator();
6905f20e2d4462da3471f59152b32cd8640fa4a21daMathias Agopian
691e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian    // FIXME: handle multiple displays
692748f3df66f6695681cde44461e2548e6e1dbc1bbMathias Agopian    if (!mHwc || index > hwcNumHwLayers(mHwc, mLists[0]))
6933e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian        return LayerListIterator();
6945880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
695b685c542836b93c99cd85053e07696406ea37adbJesse Hall        return LayerListIterator(new HWCLayerVersion1(mLists[0]->hwLayers),
696b685c542836b93c99cd85053e07696406ea37adbJesse Hall                index);
6975880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    } else {
6981e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
6995880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall        return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
7005880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall    }
7013e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
7023e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
7033e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
7043e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the beginning of the layer list
7053e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
7065f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::begin(int32_t id) {
7071e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, 0);
708a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
709a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
7103e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian/*
7113e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian * returns an iterator on the end of the layer list
7123e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian */
7135f20e2d4462da3471f59152b32cd8640fa4a21daMathias AgopianHWComposer::LayerListIterator HWComposer::end(int32_t id) {
7141e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian    return getLayerIterator(id, getNumLayers(id));
7153e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian}
7163e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian
71722da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
71822da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
719b685c542836b93c99cd85053e07696406ea37adbJesse Hall    if (mHwc) {
7201e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian        hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
721b685c542836b93c99cd85053e07696406ea37adbJesse Hall
7228372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
723e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n",
724e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                mDebugForceFakeVSync);
725e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  numHwLayers=%u, flags=%08x\n",
726b685c542836b93c99cd85053e07696406ea37adbJesse Hall                hwcNumHwLayers(mHwc, mLists[0]), hwcFlags(mHwc, mLists[0]));
727fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        result.append(
728aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
729aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
730aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
731b685c542836b93c99cd85053e07696406ea37adbJesse Hall        for (size_t i=0 ; i<hwcNumHwLayers(mHwc, mLists[0]) ; i++) {
7321e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            hwc_layer_1_t const* lp;
7335880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
7341e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                lp = &mLists[0]->hwLayers[i];
7355880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            } else {
7361e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                // FIXME: here we rely on hwc_layer_1_t and hwc_layer_t having the same layout
7371e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian                lp = reinterpret_cast<hwc_layer_1_t const*>(&list0->hwLayers[i]);
7385880cc573823148237eac9ab7bc586b8e4eb7160Jesse Hall            }
739fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
740fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            int32_t format = -1;
741fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            if (layer->getLayer() != NULL) {
742fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
743fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                if (buffer != NULL) {
744fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    format = buffer->getPixelFormat();
745fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
746fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
7471e26087493ac0e7d7dc6dea8ad85cfef08b3271fMathias Agopian            const hwc_layer_1_t& l(*lp);
748e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            result.appendFormat(
749aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
7508372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
751aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
7528372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
75322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
754fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    layer->getName().string());
7558372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
7561d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
757bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall    if (mHwc) {
758bbd164a3c790a0649dffd2f015e6f47692c72e1cJesse Hall        hwcDump(mHwc, buffer, SIZE);
7591d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
7608372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
7618372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
7628372785879d329f592f6883620b5a32d80d74691Mathias Agopian
763a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
7642965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7652965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
7662965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
7672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
768e60b0687c8d49871d0c8786cabe6851f7a7783b5Mathias Agopian      mRefreshPeriod(hwc.getRefreshPeriod())
7692965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
7702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
7712965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
7732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
7742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mEnabled = enabled;
7752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mCondition.signal();
7762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
7772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
7792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
7802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
7812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
7832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
7842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
7852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
7862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
7872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
7882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
7892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
7902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
7912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
7922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
7932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
7942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
7952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
7962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
7972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
7982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
7992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
8002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
8012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
8022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
8032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
8042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
8052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
8062965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
8072965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
8082965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
8092965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
8102965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
8112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
8122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
8132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
8142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
8152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
8162965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
8172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
818a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
819