HWComposer.cpp revision 2965b26022f95051f65b09d7eac47cbe923855c9
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
19a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <stdint.h>
20f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdio.h>
21f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdlib.h>
22f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <string.h>
23a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <sys/types.h>
24a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
25a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <utils/Errors.h>
268372785879d329f592f6883620b5a32d80d74691Mathias Agopian#include <utils/String8.h>
273eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <utils/Thread.h>
282965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian#include <utils/Trace.h>
2922da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include <utils/Vector.h>
30a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
31a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
323eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian#include <hardware/hwcomposer.h>
33a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
34a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
35a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
36a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <EGL/egl.h>
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3822da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include "LayerBase.h"
39a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
40c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
41a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
42a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
43a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
453eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
463eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
473eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        EventHandler& handler,
483eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        nsecs_t refreshPeriod)
49c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
50c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian      mModule(0), mHwc(0), mList(0), mCapacity(0),
519c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian      mNumOVLayers(0), mNumFBLayers(0),
523eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian      mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE),
533eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian      mEventHandler(handler),
542965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mRefreshPeriod(refreshPeriod), mVSyncCount(0)
55a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
56a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
5732397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
58a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
59a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        err = hwc_open(mModule, &mHwc);
60e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err, "%s device failed to initialize (%s)",
61a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
62c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
63c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            if (mHwc->registerProcs) {
64c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.hwc = this;
65c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.procs.invalidate = &hook_invalidate;
6631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian                mCBContext.procs.vsync = &hook_vsync;
67c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mHwc->registerProcs(mHwc, &mCBContext.procs);
6831d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian                memset(mCBContext.procs.zero, 0, sizeof(mCBContext.procs.zero));
69c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            }
703eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
713eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian            if (mHwc->common.version < HWC_DEVICE_API_VERSION_0_3) {
723eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian                // we don't have VSYNC support, we need to fake it
733eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian                mVSyncThread = new VSyncThread(*this);
743eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian            }
75c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
76a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
77a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
78a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
79a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
80a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    free(mList);
813eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
823eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
833eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
84a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
85a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        hwc_close(mHwc);
86a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
87a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
88a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
89a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
90a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
91a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
92a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
93c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::hook_invalidate(struct hwc_procs* procs) {
94c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
95c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
96c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
9731d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
9831d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
9931d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
10031d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
101c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
102e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
103c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
104c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
10531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::vsync(int dpy, int64_t timestamp) {
1062965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
1073eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    mEventHandler.onVSyncReceived(dpy, timestamp);
1083eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
1093eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
1103eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopianstatus_t HWComposer::eventControl(int event, int enabled) {
1113eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
1121a3bf41b7165ba294af46bc32483eaad61e707eaErik Gilling    if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
1133eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        err = mHwc->methods->eventControl(mHwc, event, enabled);
1143eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    } else {
1153eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        if (mVSyncThread != NULL) {
1163eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian            mVSyncThread->setEnabled(enabled);
1173eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        } else {
1183eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian            err = BAD_VALUE;
1193eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        }
1203eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
1213eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    return err;
12231d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
12331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
124a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianvoid HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
125a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mDpy = (hwc_display_t)dpy;
126a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mSur = (hwc_surface_t)sur;
127a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
128a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
129a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::createWorkList(size_t numLayers) {
13045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
13145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        if (!mList || mCapacity < numLayers) {
13245721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            free(mList);
13345721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
13445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mList = (hwc_layer_list_t*)malloc(size);
13545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
13645721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
137a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->flags = HWC_GEOMETRY_CHANGED;
138a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->numHwLayers = numLayers;
139a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
140a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
141a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
142a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
143a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::prepare() const {
144a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->prepare(mHwc, mList);
1459c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
1469c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numOVLayers = 0;
1479c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numFBLayers = 0;
1489c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t count = mList->numHwLayers;
1499c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        for (size_t i=0 ; i<count ; i++) {
1509c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            hwc_layer& l(mList->hwLayers[i]);
1519c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            if (l.flags & HWC_SKIP_LAYER) {
1529c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                l.compositionType = HWC_FRAMEBUFFER;
1539c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
1549c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            switch (l.compositionType) {
1559c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_OVERLAY:
1569c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numOVLayers++;
1579c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
1589c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_FRAMEBUFFER:
1599c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numFBLayers++;
1609c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
1619c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
1629c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
1639c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumOVLayers = numOVLayers;
1649c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumFBLayers = numFBLayers;
1659c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
166a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
167a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
168a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1699c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopiansize_t HWComposer::getLayerCount(int type) const {
1709c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    switch (type) {
1719c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_OVERLAY:
1729c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumOVLayers;
1739c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_FRAMEBUFFER:
1749c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumFBLayers;
1759c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
1769c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    return 0;
1779c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
1789c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
179a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::commit() const {
180a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->set(mHwc, mDpy, mSur, mList);
18158959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    if (mList) {
18258959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian        mList->flags &= ~HWC_GEOMETRY_CHANGED;
18358959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
184a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
185a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
186a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
187f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
1887ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
1897ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->set(mHwc, NULL, NULL, NULL);
1907ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
1917ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
1927ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
1937ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
1947ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
1957ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
1967ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
1977ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        free(mList);
1987ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        mList = NULL;
1997ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->prepare(mHwc, NULL);
2007ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
2017ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
2027ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
203f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
204f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
20545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopiansize_t HWComposer::getNumLayers() const {
20645721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->numHwLayers : 0;
207a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
208a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
20945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopianhwc_layer_t* HWComposer::getLayers() const {
21045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->hwLayers : 0;
211a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
212a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
21322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
21422da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
2158372785879d329f592f6883620b5a32d80d74691Mathias Agopian    if (mHwc && mList) {
2168372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
2178372785879d329f592f6883620b5a32d80d74691Mathias Agopian
2188372785879d329f592f6883620b5a32d80d74691Mathias Agopian        snprintf(buffer, SIZE, "  numHwLayers=%u, flags=%08x\n",
2198372785879d329f592f6883620b5a32d80d74691Mathias Agopian                mList->numHwLayers, mList->flags);
2208372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append(buffer);
221fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        result.append(
222aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
223aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
224aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
2258372785879d329f592f6883620b5a32d80d74691Mathias Agopian        for (size_t i=0 ; i<mList->numHwLayers ; i++) {
2268372785879d329f592f6883620b5a32d80d74691Mathias Agopian            const hwc_layer_t& l(mList->hwLayers[i]);
227fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
228fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            int32_t format = -1;
229fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            if (layer->getLayer() != NULL) {
230fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
231fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                if (buffer != NULL) {
232fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    format = buffer->getPixelFormat();
233fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
234fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
235fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            snprintf(buffer, SIZE,
236aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
2378372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
238aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
2398372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
24022da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
241fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    layer->getName().string());
2428372785879d329f592f6883620b5a32d80d74691Mathias Agopian            result.append(buffer);
2438372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
2441d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
2451d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
2461d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mHwc->dump(mHwc, buffer, SIZE);
2471d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
2488372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
2498372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
2508372785879d329f592f6883620b5a32d80d74691Mathias Agopian
251a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
2522965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2532965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
2542965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
2552965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
2562965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mRefreshPeriod(hwc.mRefreshPeriod)
2572965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
2582965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
2592965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2602965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
2612965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
2622965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mEnabled = enabled;
2632965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mCondition.signal();
2642965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
2652965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2662965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
2672965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
2682965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
2692965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
2712965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
2722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
2732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
2742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
2752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
2762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
2772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
2792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
2802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
2812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
2822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
2832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
2842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
2852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
2862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
2872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
2882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
2902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
2912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
2922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
2942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
2952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
2962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
2972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
2992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
3002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
3012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
3022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
3032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
3042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
3052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
306a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
307