HWComposer.cpp revision f5f2712854599b4970643c6000fe6ae950a08ba9
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24
25#include <hardware/hardware.h>
26
27#include <cutils/log.h>
28
29#include <EGL/egl.h>
30
31#include "HWComposer.h"
32
33namespace android {
34// ---------------------------------------------------------------------------
35
36HWComposer::HWComposer()
37    : mModule(0), mHwc(0), mList(0), mCapacity(0),
38      mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE)
39{
40    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
41    LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
42    if (err == 0) {
43        err = hwc_open(mModule, &mHwc);
44        LOGE_IF(err, "%s device failed to initialize (%s)",
45                HWC_HARDWARE_COMPOSER, strerror(-err));
46    }
47}
48
49HWComposer::~HWComposer() {
50    free(mList);
51    if (mHwc) {
52        hwc_close(mHwc);
53    }
54}
55
56status_t HWComposer::initCheck() const {
57    return mHwc ? NO_ERROR : NO_INIT;
58}
59
60void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
61    mDpy = (hwc_display_t)dpy;
62    mSur = (hwc_surface_t)sur;
63}
64
65status_t HWComposer::createWorkList(size_t numLayers) {
66    if (mHwc) {
67        if (!mList || mCapacity < numLayers) {
68            free(mList);
69            size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
70            mList = (hwc_layer_list_t*)malloc(size);
71            mCapacity = numLayers;
72        }
73        mList->flags = HWC_GEOMETRY_CHANGED;
74        mList->numHwLayers = numLayers;
75    }
76    return NO_ERROR;
77}
78
79status_t HWComposer::prepare() const {
80    int err = mHwc->prepare(mHwc, mList);
81    return (status_t)err;
82}
83
84status_t HWComposer::commit() const {
85    int err = mHwc->set(mHwc, mDpy, mSur, mList);
86    mList->flags &= ~HWC_GEOMETRY_CHANGED;
87    return (status_t)err;
88}
89
90status_t HWComposer::release() const {
91    int err = mHwc->set(mHwc, NULL, NULL, NULL);
92    return (status_t)err;
93}
94
95size_t HWComposer::getNumLayers() const {
96    return mList ? mList->numHwLayers : 0;
97}
98
99hwc_layer_t* HWComposer::getLayers() const {
100    return mList ? mList->hwLayers : 0;
101}
102
103// ---------------------------------------------------------------------------
104}; // namespace android
105