HWComposer.cpp revision 7ee4cd5556cef1878e1d4729f1b389f186311027
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
17a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <stdint.h>
18f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdio.h>
19f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <stdlib.h>
20f1352df47fe20aed23c216a78923c7d248f2bb91Mathias Agopian#include <string.h>
21a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <sys/types.h>
22a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
23a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <utils/Errors.h>
248372785879d329f592f6883620b5a32d80d74691Mathias Agopian#include <utils/String8.h>
25a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
26a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
27a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
28a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
29a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
30a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <EGL/egl.h>
31a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
32a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
33c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
34a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
35a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
36a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
38c7d14e247117392fbd44aa454622778a25c076aeMathias AgopianHWComposer::HWComposer(const sp<SurfaceFlinger>& flinger)
39c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
40c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian      mModule(0), mHwc(0), mList(0), mCapacity(0),
41a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian      mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE)
42a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
43a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
46a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        err = hwc_open(mModule, &mHwc);
47a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        LOGE_IF(err, "%s device failed to initialize (%s)",
48a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
49c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
50c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            if (mHwc->registerProcs) {
51c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.hwc = this;
52c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.procs.invalidate = &hook_invalidate;
53c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mHwc->registerProcs(mHwc, &mCBContext.procs);
54c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            }
55c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
56a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
57a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
58a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
59a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
60a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    free(mList);
61a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
62a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        hwc_close(mHwc);
63a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
64a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
65a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
66a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
67a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
68a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
69a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
70c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::hook_invalidate(struct hwc_procs* procs) {
71c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
72c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
73c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
74c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
75c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    mFlinger->signalEvent();
76c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
77c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
78a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianvoid HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
79a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mDpy = (hwc_display_t)dpy;
80a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mSur = (hwc_surface_t)sur;
81a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
82a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
83a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::createWorkList(size_t numLayers) {
8445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
8545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        if (!mList || mCapacity < numLayers) {
8645721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            free(mList);
8745721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
8845721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mList = (hwc_layer_list_t*)malloc(size);
8945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
9045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
91a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->flags = HWC_GEOMETRY_CHANGED;
92a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->numHwLayers = numLayers;
93a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
94a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
95a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
96a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
97a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::prepare() const {
98a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->prepare(mHwc, mList);
99a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
100a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
101a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
102a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::commit() const {
103a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->set(mHwc, mDpy, mSur, mList);
10458959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    if (mList) {
10558959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian        mList->flags &= ~HWC_GEOMETRY_CHANGED;
10658959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
107a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
108a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
109a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
110f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
1117ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
1127ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->set(mHwc, NULL, NULL, NULL);
1137ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
1147ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
1157ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
1167ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
1177ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
1187ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
1197ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
1207ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        free(mList);
1217ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        mList = NULL;
1227ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->prepare(mHwc, NULL);
1237ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
1247ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
1257ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
126f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
127f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
12845721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopiansize_t HWComposer::getNumLayers() const {
12945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->numHwLayers : 0;
130a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
131a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
13245721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopianhwc_layer_t* HWComposer::getLayers() const {
13345721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->hwLayers : 0;
134a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
135a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
1368372785879d329f592f6883620b5a32d80d74691Mathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE) const {
1378372785879d329f592f6883620b5a32d80d74691Mathias Agopian    if (mHwc && mList) {
1388372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
1398372785879d329f592f6883620b5a32d80d74691Mathias Agopian
1408372785879d329f592f6883620b5a32d80d74691Mathias Agopian        snprintf(buffer, SIZE, "  numHwLayers=%u, flags=%08x\n",
1418372785879d329f592f6883620b5a32d80d74691Mathias Agopian                mList->numHwLayers, mList->flags);
1428372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append(buffer);
1438372785879d329f592f6883620b5a32d80d74691Mathias Agopian
1448372785879d329f592f6883620b5a32d80d74691Mathias Agopian        for (size_t i=0 ; i<mList->numHwLayers ; i++) {
1458372785879d329f592f6883620b5a32d80d74691Mathias Agopian            const hwc_layer_t& l(mList->hwLayers[i]);
1468372785879d329f592f6883620b5a32d80d74691Mathias Agopian            snprintf(buffer, SIZE, "  %8s | %08x | %08x | %02x | %04x | [%5d,%5d,%5d,%5d] |  [%5d,%5d,%5d,%5d]\n",
1478372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
1488372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.hints, l.flags, l.transform, l.blending,
1498372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1508372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom);
1518372785879d329f592f6883620b5a32d80d74691Mathias Agopian            result.append(buffer);
1528372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
1531d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling
1541d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
1551d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
1561d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mHwc->dump(mHwc, buffer, SIZE);
1571d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
1588372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
1598372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
1608372785879d329f592f6883620b5a32d80d74691Mathias Agopian
161a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
162a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
163