blob: 94d0021679f69c2e58eacbdb0d5c3926c8dc2f21 [file] [log] [blame]
Mathias Agopiana350ff92010-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 Agopianf1352df2010-08-11 17:31:33 -070018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070021#include <sys/types.h>
22
23#include <utils/Errors.h>
Mathias Agopian83727852010-09-23 18:13:21 -070024#include <utils/String8.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070025#include <utils/Vector.h>
Mathias Agopiana350ff92010-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 Agopian22da60c2011-09-09 00:49:11 -070033#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070034#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070035#include "SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070036
37namespace android {
38// ---------------------------------------------------------------------------
39
Mathias Agopianc7d14e22011-08-01 16:32:21 -070040HWComposer::HWComposer(const sp<SurfaceFlinger>& flinger)
41 : mFlinger(flinger),
42 mModule(0), mHwc(0), mList(0), mCapacity(0),
Mathias Agopian9c6e2972011-09-20 17:21:56 -070043 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopiana350ff92010-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);
Steve Block32397c12012-01-05 23:22:43 +000047 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -070048 if (err == 0) {
49 err = hwc_open(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +000050 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -070051 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070052 if (err == 0) {
53 if (mHwc->registerProcs) {
54 mCBContext.hwc = this;
55 mCBContext.procs.invalidate = &hook_invalidate;
Mathias Agopian31d28432012-04-03 16:31:39 -070056 mCBContext.procs.vsync = &hook_vsync;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070057 mHwc->registerProcs(mHwc, &mCBContext.procs);
Mathias Agopian31d28432012-04-03 16:31:39 -070058 memset(mCBContext.procs.zero, 0, sizeof(mCBContext.procs.zero));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070059 }
60 }
Mathias Agopiana350ff92010-08-10 17:14:02 -070061 }
62}
63
64HWComposer::~HWComposer() {
65 free(mList);
66 if (mHwc) {
67 hwc_close(mHwc);
68 }
69}
70
71status_t HWComposer::initCheck() const {
72 return mHwc ? NO_ERROR : NO_INIT;
73}
74
Mathias Agopianc7d14e22011-08-01 16:32:21 -070075void HWComposer::hook_invalidate(struct hwc_procs* procs) {
76 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
77}
78
Mathias Agopian31d28432012-04-03 16:31:39 -070079void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
80 reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
81}
82
Mathias Agopianc7d14e22011-08-01 16:32:21 -070083void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -070084 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -070085}
86
Mathias Agopian31d28432012-04-03 16:31:39 -070087void HWComposer::vsync(int dpy, int64_t timestamp) {
88}
89
Mathias Agopiana350ff92010-08-10 17:14:02 -070090void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
91 mDpy = (hwc_display_t)dpy;
92 mSur = (hwc_surface_t)sur;
93}
94
95status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopian45721772010-08-12 15:03:26 -070096 if (mHwc) {
97 if (!mList || mCapacity < numLayers) {
98 free(mList);
99 size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
100 mList = (hwc_layer_list_t*)malloc(size);
101 mCapacity = numLayers;
102 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700103 mList->flags = HWC_GEOMETRY_CHANGED;
104 mList->numHwLayers = numLayers;
105 }
106 return NO_ERROR;
107}
108
109status_t HWComposer::prepare() const {
110 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700111 if (err == NO_ERROR) {
112 size_t numOVLayers = 0;
113 size_t numFBLayers = 0;
114 size_t count = mList->numHwLayers;
115 for (size_t i=0 ; i<count ; i++) {
116 hwc_layer& l(mList->hwLayers[i]);
117 if (l.flags & HWC_SKIP_LAYER) {
118 l.compositionType = HWC_FRAMEBUFFER;
119 }
120 switch (l.compositionType) {
121 case HWC_OVERLAY:
122 numOVLayers++;
123 break;
124 case HWC_FRAMEBUFFER:
125 numFBLayers++;
126 break;
127 }
128 }
129 mNumOVLayers = numOVLayers;
130 mNumFBLayers = numFBLayers;
131 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700132 return (status_t)err;
133}
134
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700135size_t HWComposer::getLayerCount(int type) const {
136 switch (type) {
137 case HWC_OVERLAY:
138 return mNumOVLayers;
139 case HWC_FRAMEBUFFER:
140 return mNumFBLayers;
141 }
142 return 0;
143}
144
Mathias Agopiana350ff92010-08-10 17:14:02 -0700145status_t HWComposer::commit() const {
146 int err = mHwc->set(mHwc, mDpy, mSur, mList);
Mathias Agopian58959342010-10-07 14:57:04 -0700147 if (mList) {
148 mList->flags &= ~HWC_GEOMETRY_CHANGED;
149 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700150 return (status_t)err;
151}
152
Antti Hatalaf5f27122010-09-09 02:33:05 -0700153status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700154 if (mHwc) {
155 int err = mHwc->set(mHwc, NULL, NULL, NULL);
156 return (status_t)err;
157 }
158 return NO_ERROR;
159}
160
161status_t HWComposer::disable() {
162 if (mHwc) {
163 free(mList);
164 mList = NULL;
165 int err = mHwc->prepare(mHwc, NULL);
166 return (status_t)err;
167 }
168 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700169}
170
Mathias Agopian45721772010-08-12 15:03:26 -0700171size_t HWComposer::getNumLayers() const {
172 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700173}
174
Mathias Agopian45721772010-08-12 15:03:26 -0700175hwc_layer_t* HWComposer::getLayers() const {
176 return mList ? mList->hwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700177}
178
Mathias Agopian22da60c2011-09-09 00:49:11 -0700179void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
180 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700181 if (mHwc && mList) {
182 result.append("Hardware Composer state:\n");
183
184 snprintf(buffer, SIZE, " numHwLayers=%u, flags=%08x\n",
185 mList->numHwLayers, mList->flags);
186 result.append(buffer);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700187 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700188 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
189 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
190 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700191 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
192 const hwc_layer_t& l(mList->hwLayers[i]);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700193 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
194 int32_t format = -1;
195 if (layer->getLayer() != NULL) {
196 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
197 if (buffer != NULL) {
198 format = buffer->getPixelFormat();
199 }
200 }
201 snprintf(buffer, SIZE,
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700202 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700203 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700204 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700205 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700206 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700207 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700208 result.append(buffer);
209 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800210 }
211 if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
212 mHwc->dump(mHwc, buffer, SIZE);
213 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700214 }
215}
216
Mathias Agopiana350ff92010-08-10 17:14:02 -0700217// ---------------------------------------------------------------------------
218}; // namespace android