HWComposer.cpp revision e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8
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>
35e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian#include <cutils/properties.h>
36a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <EGL/egl.h>
38a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3922da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include "LayerBase.h"
40a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
41c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
42a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
43a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
463eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias AgopianHWComposer::HWComposer(
473eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        const sp<SurfaceFlinger>& flinger,
483eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        EventHandler& handler,
493eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        nsecs_t refreshPeriod)
50c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
51c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian      mModule(0), mHwc(0), mList(0), mCapacity(0),
529c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian      mNumOVLayers(0), mNumFBLayers(0),
533eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian      mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE),
543eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian      mEventHandler(handler),
55e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mRefreshPeriod(refreshPeriod),
56e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian      mVSyncCount(0), mDebugForceFakeVSync(false)
57a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
58e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    char value[PROPERTY_VALUE_MAX];
59e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    property_get("debug.sf.no_hw_vsync", value, "0");
60e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    mDebugForceFakeVSync = atoi(value);
61e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian
623a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    bool needVSyncThread = false;
63a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
6432397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
65a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
66a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        err = hwc_open(mModule, &mHwc);
67e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(err, "%s device failed to initialize (%s)",
68a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
69c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
70c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            if (mHwc->registerProcs) {
71c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.hwc = this;
72c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.procs.invalidate = &hook_invalidate;
7331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian                mCBContext.procs.vsync = &hook_vsync;
74c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mHwc->registerProcs(mHwc, &mCBContext.procs);
7531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian                memset(mCBContext.procs.zero, 0, sizeof(mCBContext.procs.zero));
76c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            }
77e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            if (mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
78e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                if (mDebugForceFakeVSync) {
79e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                    // make sure to turn h/w vsync off in "fake vsync" mode
80e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                    mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
81e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                }
82e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            } else {
833a77871383bc1a03cc866686d81628493d14de7cMathias Agopian                needVSyncThread = true;
843eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian            }
85c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
863a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    } else {
873a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        needVSyncThread = true;
883a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
893a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
903a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (needVSyncThread) {
913a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        // we don't have VSYNC support, we need to fake it
923a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread = new VSyncThread(*this);
93a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
94a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
95a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
96a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
97e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    eventControl(EVENT_VSYNC, 0);
98a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    free(mList);
993eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    if (mVSyncThread != NULL) {
1003eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian        mVSyncThread->requestExitAndWait();
1013eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
102a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
103a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        hwc_close(mHwc);
104a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
105a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
106a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
107a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
108a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
109a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
110a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
111c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::hook_invalidate(struct hwc_procs* procs) {
112c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
113c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
114c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
11531d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
11631d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
11731d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
11831d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
119c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
120e2c2f9213f936f98db604dc9c126ff22f725a824Mathias Agopian    mFlinger->repaintEverything();
121c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
122c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
12331d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopianvoid HWComposer::vsync(int dpy, int64_t timestamp) {
1242965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    ATRACE_INT("VSYNC", ++mVSyncCount&1);
1253eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    mEventHandler.onVSyncReceived(dpy, timestamp);
1263eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian}
1273eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian
1283eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopianstatus_t HWComposer::eventControl(int event, int enabled) {
1293eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    status_t err = NO_ERROR;
1301a3bf41b7165ba294af46bc32483eaad61e707eaErik Gilling    if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
131e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        if (!mDebugForceFakeVSync) {
132e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            err = mHwc->methods->eventControl(mHwc, event, enabled);
133e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        }
1343eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    }
1353a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
1363a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    if (err == NO_ERROR && mVSyncThread != NULL) {
1373a77871383bc1a03cc866686d81628493d14de7cMathias Agopian        mVSyncThread->setEnabled(enabled);
1383a77871383bc1a03cc866686d81628493d14de7cMathias Agopian    }
1393a77871383bc1a03cc866686d81628493d14de7cMathias Agopian
1403eb38cb33e41ce40dd1094bdec850f0fca9f8a53Mathias Agopian    return err;
14131d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian}
14231d2843b45ebdb69ec3355111b7567363fd2a6b7Mathias Agopian
143a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianvoid HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
144a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mDpy = (hwc_display_t)dpy;
145a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mSur = (hwc_surface_t)sur;
146a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
147a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
148a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::createWorkList(size_t numLayers) {
14945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
15045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        if (!mList || mCapacity < numLayers) {
15145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            free(mList);
15245721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
15345721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mList = (hwc_layer_list_t*)malloc(size);
15445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
15545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
156a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->flags = HWC_GEOMETRY_CHANGED;
157a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->numHwLayers = numLayers;
158a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
159a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
160a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
161a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
162a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::prepare() const {
163a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->prepare(mHwc, mList);
1649c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    if (err == NO_ERROR) {
1659c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numOVLayers = 0;
1669c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t numFBLayers = 0;
1679c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        size_t count = mList->numHwLayers;
1689c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        for (size_t i=0 ; i<count ; i++) {
1699c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            hwc_layer& l(mList->hwLayers[i]);
1709c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            if (l.flags & HWC_SKIP_LAYER) {
1719c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                l.compositionType = HWC_FRAMEBUFFER;
1729c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
1739c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            switch (l.compositionType) {
1749c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_OVERLAY:
1759c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numOVLayers++;
1769c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
1779c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                case HWC_FRAMEBUFFER:
1789c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    numFBLayers++;
1799c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian                    break;
1809c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            }
1819c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        }
1829c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumOVLayers = numOVLayers;
1839c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        mNumFBLayers = numFBLayers;
1849c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
185a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
186a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
187a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1889c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopiansize_t HWComposer::getLayerCount(int type) const {
1899c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    switch (type) {
1909c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_OVERLAY:
1919c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumOVLayers;
1929c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian        case HWC_FRAMEBUFFER:
1939c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian            return mNumFBLayers;
1949c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    }
1959c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian    return 0;
1969c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian}
1979c6e297271ec9af9d974242d89cfa08cb6ceaa0aMathias Agopian
198a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::commit() const {
199a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->set(mHwc, mDpy, mSur, mList);
20058959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    if (mList) {
20158959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian        mList->flags &= ~HWC_GEOMETRY_CHANGED;
20258959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
203a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
204a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
205a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
206f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
2077ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
2087ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->set(mHwc, NULL, NULL, NULL);
2097ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
2107ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
2117ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
2127ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
2137ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
2147ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
2157ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
2167ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        free(mList);
2177ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        mList = NULL;
2187ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->prepare(mHwc, NULL);
2197ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
2207ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
2217ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
222f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
223f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
22445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopiansize_t HWComposer::getNumLayers() const {
22545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->numHwLayers : 0;
226a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
227a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
22845721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopianhwc_layer_t* HWComposer::getLayers() const {
22945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->hwLayers : 0;
230a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
231a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
23222da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
23322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
2348372785879d329f592f6883620b5a32d80d74691Mathias Agopian    if (mHwc && mList) {
2358372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
236e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  mDebugForceFakeVSync=%d\n",
237e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian                mDebugForceFakeVSync);
238e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian        result.appendFormat("  numHwLayers=%u, flags=%08x\n",
2398372785879d329f592f6883620b5a32d80d74691Mathias Agopian                mList->numHwLayers, mList->flags);
240fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        result.append(
241aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "   type   |  handle  |   hints  |   flags  | tr | blend |  format  |       source crop         |           frame           name \n"
242aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
243aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian        //      " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
2448372785879d329f592f6883620b5a32d80d74691Mathias Agopian        for (size_t i=0 ; i<mList->numHwLayers ; i++) {
2458372785879d329f592f6883620b5a32d80d74691Mathias Agopian            const hwc_layer_t& l(mList->hwLayers[i]);
246fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
247fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            int32_t format = -1;
248fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            if (layer->getLayer() != NULL) {
249fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
250fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                if (buffer != NULL) {
251fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    format = buffer->getPixelFormat();
252fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
253fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
254e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian            result.appendFormat(
255aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
2568372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
257aebac5f34a098f733f887c993bd617a393e10db8Mathias Agopian                    intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
2588372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
25922da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
260fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    layer->getName().string());
2618372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
2621d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
263e2c4f4ec23b735dd2a03f4ea8b08b288a1bb04e8Mathias Agopian    if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_1 && mHwc->dump) {
2641d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mHwc->dump(mHwc, buffer, SIZE);
2651d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
2668372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
2678372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
2688372785879d329f592f6883620b5a32d80d74691Mathias Agopian
269a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
2702965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2712965b26022f95051f65b09d7eac47cbe923855c9Mathias AgopianHWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
2722965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    : mHwc(hwc), mEnabled(false),
2732965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mNextFakeVSync(0),
2742965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian      mRefreshPeriod(hwc.mRefreshPeriod)
2752965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian{
2762965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
2772965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2782965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::setEnabled(bool enabled) {
2792965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    Mutex::Autolock _l(mLock);
2802965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mEnabled = enabled;
2812965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mCondition.signal();
2822965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
2832965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2842965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianvoid HWComposer::VSyncThread::onFirstRef() {
2852965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
2862965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
2872965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2882965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopianbool HWComposer::VSyncThread::threadLoop() {
2892965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    { // scope for lock
2902965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        Mutex::Autolock _l(mLock);
2912965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        while (!mEnabled) {
2922965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian            mCondition.wait(mLock);
2932965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        }
2942965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
2952965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
2962965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t period = mRefreshPeriod;
2972965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    const nsecs_t now = systemTime(CLOCK_MONOTONIC);
2982965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t next_vsync = mNextFakeVSync;
2992965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    nsecs_t sleep = next_vsync - now;
3002965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (sleep < 0) {
3012965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        // we missed, find where the next vsync should be
3022965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        sleep = (period - ((now - next_vsync) % period));
3032965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        next_vsync = now + sleep;
3042965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
3052965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    mNextFakeVSync = next_vsync + period;
3062965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
3072965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    struct timespec spec;
3082965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_sec  = next_vsync / 1000000000;
3092965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    spec.tv_nsec = next_vsync % 1000000000;
3102965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
3112965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    int err;
3122965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    do {
3132965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
3142965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    } while (err<0 && errno == EINTR);
3152965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
3162965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    if (err == 0) {
3172965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian        mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
3182965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    }
3192965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
3202965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian    return true;
3212965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian}
3222965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian
3232965b26022f95051f65b09d7eac47cbe923855c9Mathias Agopian// ---------------------------------------------------------------------------
324a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
325