HWComposer.cpp revision a350ff98692b3a50cad5cc93f9f83221242ca86a
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 <sys/types.h>
19
20#include <utils/Errors.h>
21
22#include <hardware/hardware.h>
23
24#include <cutils/log.h>
25
26#include <EGL/egl.h>
27
28#include "HWComposer.h"
29
30namespace android {
31// ---------------------------------------------------------------------------
32
33HWComposer::HWComposer()
34    : mModule(0), mHwc(0), mList(0),
35      mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE)
36{
37    int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
38    LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
39    if (err == 0) {
40        err = hwc_open(mModule, &mHwc);
41        LOGE_IF(err, "%s device failed to initialize (%s)",
42                HWC_HARDWARE_COMPOSER, strerror(-err));
43    }
44}
45
46HWComposer::~HWComposer() {
47    free(mList);
48    if (mHwc) {
49        hwc_close(mHwc);
50    }
51}
52
53status_t HWComposer::initCheck() const {
54    return mHwc ? NO_ERROR : NO_INIT;
55}
56
57void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
58    mDpy = (hwc_display_t)dpy;
59    mSur = (hwc_surface_t)sur;
60}
61
62status_t HWComposer::createWorkList(size_t numLayers) {
63    if (mHwc && (!mList || mList->numHwLayers < numLayers)) {
64        free(mList);
65        size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
66        mList = (hwc_layer_list_t*)malloc(size);
67        mList->flags = HWC_GEOMETRY_CHANGED;
68        mList->numHwLayers = numLayers;
69    }
70    return NO_ERROR;
71}
72
73status_t HWComposer::prepare() const {
74    int err = mHwc->prepare(mHwc, mList);
75    return (status_t)err;
76}
77
78status_t HWComposer::commit() const {
79    int err = mHwc->set(mHwc, mDpy, mSur, mList);
80    mList->flags &= ~HWC_GEOMETRY_CHANGED;
81    return (status_t)err;
82}
83
84HWComposer::iterator HWComposer::begin() {
85    return mList ? &(mList->hwLayers[0]) : NULL;
86}
87
88HWComposer::iterator HWComposer::end() {
89    return mList ? &(mList->hwLayers[mList->numHwLayers]) : NULL;
90}
91
92// ---------------------------------------------------------------------------
93}; // namespace android
94