blob: be9b2265c7dbc10de8ae4b77aa87908c0394969d [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 Agopian88cde072011-09-20 17:21:56 -070043 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070044 mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE)
45{
46 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
47 LOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
48 if (err == 0) {
49 err = hwc_open(mModule, &mHwc);
50 LOGE_IF(err, "%s device failed to initialize (%s)",
51 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070052 if (err == 0) {
53 if (mHwc->registerProcs) {
54 mCBContext.hwc = this;
55 mCBContext.procs.invalidate = &hook_invalidate;
56 mHwc->registerProcs(mHwc, &mCBContext.procs);
57 }
58 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070059 }
60}
61
62HWComposer::~HWComposer() {
63 free(mList);
64 if (mHwc) {
65 hwc_close(mHwc);
66 }
67}
68
69status_t HWComposer::initCheck() const {
70 return mHwc ? NO_ERROR : NO_INIT;
71}
72
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070073void HWComposer::hook_invalidate(struct hwc_procs* procs) {
74 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
75}
76
77void HWComposer::invalidate() {
Mathias Agopianedaab942011-10-05 15:00:22 -070078 mFlinger->repaintEverything();
Mathias Agopiana0bd0d82011-08-01 16:32:21 -070079}
80
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070081void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
82 mDpy = (hwc_display_t)dpy;
83 mSur = (hwc_surface_t)sur;
84}
85
86status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -070087 if (mHwc) {
88 if (!mList || mCapacity < numLayers) {
89 free(mList);
90 size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
91 mList = (hwc_layer_list_t*)malloc(size);
92 mCapacity = numLayers;
93 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070094 mList->flags = HWC_GEOMETRY_CHANGED;
95 mList->numHwLayers = numLayers;
96 }
97 return NO_ERROR;
98}
99
100status_t HWComposer::prepare() const {
101 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian88cde072011-09-20 17:21:56 -0700102 if (err == NO_ERROR) {
103 size_t numOVLayers = 0;
104 size_t numFBLayers = 0;
105 size_t count = mList->numHwLayers;
106 for (size_t i=0 ; i<count ; i++) {
107 hwc_layer& l(mList->hwLayers[i]);
108 if (l.flags & HWC_SKIP_LAYER) {
109 l.compositionType = HWC_FRAMEBUFFER;
110 }
111 switch (l.compositionType) {
112 case HWC_OVERLAY:
113 numOVLayers++;
114 break;
115 case HWC_FRAMEBUFFER:
116 numFBLayers++;
117 break;
118 }
119 }
120 mNumOVLayers = numOVLayers;
121 mNumFBLayers = numFBLayers;
122 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700123 return (status_t)err;
124}
125
Mathias Agopian88cde072011-09-20 17:21:56 -0700126size_t HWComposer::getLayerCount(int type) const {
127 switch (type) {
128 case HWC_OVERLAY:
129 return mNumOVLayers;
130 case HWC_FRAMEBUFFER:
131 return mNumFBLayers;
132 }
133 return 0;
134}
135
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700136status_t HWComposer::commit() const {
137 int err = mHwc->set(mHwc, mDpy, mSur, mList);
Mathias Agopian3e1a18a2010-10-07 14:57:04 -0700138 if (mList) {
139 mList->flags &= ~HWC_GEOMETRY_CHANGED;
140 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700141 return (status_t)err;
142}
143
Antti Hatala11042a42010-09-09 02:33:05 -0700144status_t HWComposer::release() const {
Mathias Agopian7f972582011-09-02 12:22:39 -0700145 if (mHwc) {
146 int err = mHwc->set(mHwc, NULL, NULL, NULL);
147 return (status_t)err;
148 }
149 return NO_ERROR;
150}
151
152status_t HWComposer::disable() {
153 if (mHwc) {
154 free(mList);
155 mList = NULL;
156 int err = mHwc->prepare(mHwc, NULL);
157 return (status_t)err;
158 }
159 return NO_ERROR;
Antti Hatala11042a42010-09-09 02:33:05 -0700160}
161
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700162size_t HWComposer::getNumLayers() const {
163 return mList ? mList->numHwLayers : 0;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700164}
165
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700166hwc_layer_t* HWComposer::getLayers() const {
167 return mList ? mList->hwLayers : 0;
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700168}
169
Mathias Agopian5bd1b272011-09-09 00:49:11 -0700170void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
171 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700172 if (mHwc && mList) {
173 result.append("Hardware Composer state:\n");
174
175 snprintf(buffer, SIZE, " numHwLayers=%u, flags=%08x\n",
176 mList->numHwLayers, mList->flags);
177 result.append(buffer);
Mathias Agopian880ac7a2011-09-20 15:13:14 -0700178 result.append(
Mathias Agopian1f29cbe2011-09-29 18:07:08 -0700179 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
180 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
181 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700182 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
183 const hwc_layer_t& l(mList->hwLayers[i]);
Mathias Agopian880ac7a2011-09-20 15:13:14 -0700184 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
185 int32_t format = -1;
186 if (layer->getLayer() != NULL) {
187 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
188 if (buffer != NULL) {
189 format = buffer->getPixelFormat();
190 }
191 }
192 snprintf(buffer, SIZE,
Mathias Agopian1f29cbe2011-09-29 18:07:08 -0700193 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700194 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopian1f29cbe2011-09-29 18:07:08 -0700195 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700196 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian5bd1b272011-09-09 00:49:11 -0700197 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopian880ac7a2011-09-20 15:13:14 -0700198 layer->getName().string());
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700199 result.append(buffer);
200 }
Erik Gilling94720d72010-12-01 16:38:01 -0800201 }
202 if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
203 mHwc->dump(mHwc, buffer, SIZE);
204 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -0700205 }
206}
207
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700208// ---------------------------------------------------------------------------
209}; // namespace android