HWComposer.cpp revision fb4d5d5726c172adbe62341d99a2148685a98379
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>
2522da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include <utils/Vector.h>
26a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
27a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <hardware/hardware.h>
28a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
29a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <cutils/log.h>
30a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
31a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include <EGL/egl.h>
32a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
3322da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian#include "LayerBase.h"
34a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian#include "HWComposer.h"
35c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian#include "SurfaceFlinger.h"
36a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
37a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopiannamespace android {
38a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
39a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
40c7d14e247117392fbd44aa454622778a25c076aeMathias AgopianHWComposer::HWComposer(const sp<SurfaceFlinger>& flinger)
41c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    : mFlinger(flinger),
42c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian      mModule(0), mHwc(0), mList(0), mCapacity(0),
43a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian      mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE)
44a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
45a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
46a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
47a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (err == 0) {
48a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        err = hwc_open(mModule, &mHwc);
49a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        LOGE_IF(err, "%s device failed to initialize (%s)",
50a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian                HWC_HARDWARE_COMPOSER, strerror(-err));
51c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        if (err == 0) {
52c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            if (mHwc->registerProcs) {
53c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.hwc = this;
54c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mCBContext.procs.invalidate = &hook_invalidate;
55c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian                mHwc->registerProcs(mHwc, &mCBContext.procs);
56c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian            }
57c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian        }
58a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
59a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
60a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
61a350ff98692b3a50cad5cc93f9f83221242ca86aMathias AgopianHWComposer::~HWComposer() {
62a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    free(mList);
63a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    if (mHwc) {
64a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        hwc_close(mHwc);
65a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
66a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
67a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
68a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::initCheck() const {
69a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return mHwc ? NO_ERROR : NO_INIT;
70a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
71a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
72c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::hook_invalidate(struct hwc_procs* procs) {
73c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
74c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
75c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
76c7d14e247117392fbd44aa454622778a25c076aeMathias Agopianvoid HWComposer::invalidate() {
77c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian    mFlinger->signalEvent();
78c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian}
79c7d14e247117392fbd44aa454622778a25c076aeMathias Agopian
80a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianvoid HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
81a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mDpy = (hwc_display_t)dpy;
82a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    mSur = (hwc_surface_t)sur;
83a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
84a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
85a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::createWorkList(size_t numLayers) {
8645721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    if (mHwc) {
8745721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        if (!mList || mCapacity < numLayers) {
8845721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            free(mList);
8945721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
9045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mList = (hwc_layer_list_t*)malloc(size);
9145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian            mCapacity = numLayers;
9245721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian        }
93a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->flags = HWC_GEOMETRY_CHANGED;
94a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian        mList->numHwLayers = numLayers;
95a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    }
96a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return NO_ERROR;
97a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
98a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
99a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::prepare() const {
100a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->prepare(mHwc, mList);
101a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
102a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
103a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
104a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopianstatus_t HWComposer::commit() const {
105a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    int err = mHwc->set(mHwc, mDpy, mSur, mList);
10658959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    if (mList) {
10758959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian        mList->flags &= ~HWC_GEOMETRY_CHANGED;
10858959343dbdb6157fa5f5463262d4842b8954353Mathias Agopian    }
109a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian    return (status_t)err;
110a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
111a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
112f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatalastatus_t HWComposer::release() const {
1137ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
1147ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->set(mHwc, NULL, NULL, NULL);
1157ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
1167ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
1177ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
1187ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian}
1197ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian
1207ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopianstatus_t HWComposer::disable() {
1217ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    if (mHwc) {
1227ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        free(mList);
1237ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        mList = NULL;
1247ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        int err = mHwc->prepare(mHwc, NULL);
1257ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian        return (status_t)err;
1267ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    }
1277ee4cd5556cef1878e1d4729f1b389f186311027Mathias Agopian    return NO_ERROR;
128f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala}
129f5f2712854599b4970643c6000fe6ae950a08ba9Antti Hatala
13045721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopiansize_t HWComposer::getNumLayers() const {
13145721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->numHwLayers : 0;
132a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
133a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
13445721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopianhwc_layer_t* HWComposer::getLayers() const {
13545721773e1a68e96da4b6cc04cef276bae7ca3e9Mathias Agopian    return mList ? mList->hwLayers : 0;
136a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
137a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
13822da60c3e64cd57535cbba063c07127814a2b52fMathias Agopianvoid HWComposer::dump(String8& result, char* buffer, size_t SIZE,
13922da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian        const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
1408372785879d329f592f6883620b5a32d80d74691Mathias Agopian    if (mHwc && mList) {
1418372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append("Hardware Composer state:\n");
1428372785879d329f592f6883620b5a32d80d74691Mathias Agopian
1438372785879d329f592f6883620b5a32d80d74691Mathias Agopian        snprintf(buffer, SIZE, "  numHwLayers=%u, flags=%08x\n",
1448372785879d329f592f6883620b5a32d80d74691Mathias Agopian                mList->numHwLayers, mList->flags);
1458372785879d329f592f6883620b5a32d80d74691Mathias Agopian        result.append(buffer);
146fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        result.append(
147fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                "    type   |   hints  |   flags  | tr | blend |  format  |     source rectangle      |      crop rectangle       name \n"
148fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                "-----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
149fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian        //      "  ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
1508372785879d329f592f6883620b5a32d80d74691Mathias Agopian        for (size_t i=0 ; i<mList->numHwLayers ; i++) {
1518372785879d329f592f6883620b5a32d80d74691Mathias Agopian            const hwc_layer_t& l(mList->hwLayers[i]);
152fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
153fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            int32_t format = -1;
154fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            if (layer->getLayer() != NULL) {
155fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
156fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                if (buffer != NULL) {
157fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    format = buffer->getPixelFormat();
158fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                }
159fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            }
160fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian            snprintf(buffer, SIZE,
161fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    "  %8s | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
1628372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.compositionType ? "OVERLAY" : "FB",
163fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    l.hints, l.flags, l.transform, l.blending, format,
1648372785879d329f592f6883620b5a32d80d74691Mathias Agopian                    l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
16522da60c3e64cd57535cbba063c07127814a2b52fMathias Agopian                    l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
166fb4d5d5726c172adbe62341d99a2148685a98379Mathias Agopian                    layer->getName().string());
1678372785879d329f592f6883620b5a32d80d74691Mathias Agopian            result.append(buffer);
1688372785879d329f592f6883620b5a32d80d74691Mathias Agopian        }
1691d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
1701d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
1711d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mHwc->dump(mHwc, buffer, SIZE);
1721d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
1738372785879d329f592f6883620b5a32d80d74691Mathias Agopian    }
1748372785879d329f592f6883620b5a32d80d74691Mathias Agopian}
1758372785879d329f592f6883620b5a32d80d74691Mathias Agopian
176a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian// ---------------------------------------------------------------------------
177a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}; // namespace android
178