blob: c9567d52700812d90e44e3922fd1697ebcea6ff7 [file] [log] [blame]
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -07001/*
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>
Mathias Agopianb59beb52010-08-11 17:31:33 -070018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070021#include <sys/types.h>
22
23#include <utils/Errors.h>
Mathias Agopian90da4dd2010-09-23 18:13:21 -070024#include <utils/String8.h>
Mathias Agopian5bd1b272011-09-09 00:49:11 -070025#include <utils/Vector.h>
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070026
27#include <hardware/hardware.h>
28
29#include <cutils/log.h>
30
31#include <EGL/egl.h>
32
Mathias Agopian5bd1b272011-09-09 00:49:11 -070033#include "LayerBase.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070034#include "HWComposer.h"
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070035#include "SurfaceFlinger.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070036
37namespace android {
38// ---------------------------------------------------------------------------
39
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070040HWComposer::HWComposer(const sp<SurfaceFlinger>& flinger)
41 : mFlinger(flinger),
42 mModule(0), mHwc(0), mList(0), mCapacity(0),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070043 mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE)
44{
45 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
46 LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
47 if (err == 0) {
48 err = hwc_open(mModule, &mHwc);
49 LOGE_IF(err, "%s device failed to initialize (%s)",
50 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070051 if (err == 0) {
52 if (mHwc->registerProcs) {
53 mCBContext.hwc = this;
54 mCBContext.procs.invalidate = &hook_invalidate;
55 mHwc->registerProcs(mHwc, &mCBContext.procs);
56 }
57 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070058 }
59}
60
61HWComposer::~HWComposer() {
62 free(mList);
63 if (mHwc) {
64 hwc_close(mHwc);
65 }
66}
67
68status_t HWComposer::initCheck() const {
69 return mHwc ? NO_ERROR : NO_INIT;
70}
71
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070072void HWComposer::hook_invalidate(struct hwc_procs* procs) {
73 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
74}
75
76void HWComposer::invalidate() {
77 mFlinger->signalEvent();
78}
79
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070080void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
81 mDpy = (hwc_display_t)dpy;
82 mSur = (hwc_surface_t)sur;
83}
84
85status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -070086 if (mHwc) {
87 if (!mList || mCapacity < numLayers) {
88 free(mList);
89 size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
90 mList = (hwc_layer_list_t*)malloc(size);
91 mCapacity = numLayers;
92 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070093 mList->flags = HWC_GEOMETRY_CHANGED;
94 mList->numHwLayers = numLayers;
95 }
96 return NO_ERROR;
97}
98
99status_t HWComposer::prepare() const {
100 int err = mHwc->prepare(mHwc, mList);
101 return (status_t)err;
102}
103
104status_t HWComposer::commit() const {
105 int err = mHwc->set(mHwc, mDpy, mSur, mList);
Mathias Agopian3e1a18a2010-10-07 14:57:04 -0700106 if (mList) {
107 mList->flags &= ~HWC_GEOMETRY_CHANGED;
108 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700109 return (status_t)err;
110}
111
Antti Hatala11042a42010-09-09 02:33:05 -0700112status_t HWComposer::release() const {
Mathias Agopian7f972582011-09-02 12:22:39 -0700113 if (mHwc) {
114 int err = mHwc->set(mHwc, NULL, NULL, NULL);
115 return (status_t)err;
116 }
117 return NO_ERROR;
118}
119
120status_t HWComposer::disable() {
121 if (mHwc) {
122 free(mList);
123 mList = NULL;
124 int err = mHwc->prepare(mHwc, NULL);
125 return (status_t)err;
126 }
127 return NO_ERROR;
Antti Hatala11042a42010-09-09 02:33:05 -0700128}
129
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700130size_t HWComposer::getNumLayers() const {
131 return mList ? mList->numHwLayers : 0;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700132}
133
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700134hwc_layer_t* HWComposer::getLayers() const {
135 return mList ? mList->hwLayers : 0;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700136}
137
Mathias Agopian5bd1b272011-09-09 00:49:11 -0700138void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
139 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700140 if (mHwc && mList) {
141 result.append("Hardware Composer state:\n");
142
143 snprintf(buffer, SIZE, " numHwLayers=%u, flags=%08x\n",
144 mList->numHwLayers, mList->flags);
145 result.append(buffer);
146
147 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
148 const hwc_layer_t& l(mList->hwLayers[i]);
Mathias Agopian5bd1b272011-09-09 00:49:11 -0700149 snprintf(buffer, SIZE, " %8s | %08x | %08x | %02x | %04x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700150 l.compositionType ? "OVERLAY" : "FB",
151 l.hints, l.flags, l.transform, l.blending,
152 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian5bd1b272011-09-09 00:49:11 -0700153 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
154 visibleLayersSortedByZ[i]->getName().string());
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700155 result.append(buffer);
156 }
Erik Gilling94720d72010-12-01 16:38:01 -0800157
158 }
159 if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
160 mHwc->dump(mHwc, buffer, SIZE);
161 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700162 }
163}
164
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700165// ---------------------------------------------------------------------------
166}; // namespace android