blob: e742d3ee66d237fd5af85dcb49e145d04d0d0cd0 [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 Agopian3eb38cb2012-04-03 22:09:52 -070025#include <utils/Thread.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070026#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070027
28#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070029#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070030
31#include <cutils/log.h>
32
33#include <EGL/egl.h>
34
Mathias Agopian22da60c2011-09-09 00:49:11 -070035#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070036#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070037#include "SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
39namespace android {
40// ---------------------------------------------------------------------------
41
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070042HWComposer::HWComposer(
43 const sp<SurfaceFlinger>& flinger,
44 EventHandler& handler,
45 nsecs_t refreshPeriod)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070046 : mFlinger(flinger),
47 mModule(0), mHwc(0), mList(0), mCapacity(0),
Mathias Agopian9c6e2972011-09-20 17:21:56 -070048 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070049 mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE),
50 mEventHandler(handler),
51 mRefreshPeriod(refreshPeriod)
Mathias Agopiana350ff92010-08-10 17:14:02 -070052{
53 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +000054 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -070055 if (err == 0) {
56 err = hwc_open(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +000057 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -070058 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070059 if (err == 0) {
60 if (mHwc->registerProcs) {
61 mCBContext.hwc = this;
62 mCBContext.procs.invalidate = &hook_invalidate;
Mathias Agopian31d28432012-04-03 16:31:39 -070063 mCBContext.procs.vsync = &hook_vsync;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070064 mHwc->registerProcs(mHwc, &mCBContext.procs);
Mathias Agopian31d28432012-04-03 16:31:39 -070065 memset(mCBContext.procs.zero, 0, sizeof(mCBContext.procs.zero));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070066 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070067
68 if (mHwc->common.version < HWC_DEVICE_API_VERSION_0_3) {
69 // we don't have VSYNC support, we need to fake it
70 mVSyncThread = new VSyncThread(*this);
71 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -070072 }
Mathias Agopiana350ff92010-08-10 17:14:02 -070073 }
74}
75
76HWComposer::~HWComposer() {
77 free(mList);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070078 if (mVSyncThread != NULL) {
79 mVSyncThread->requestExitAndWait();
80 }
Mathias Agopiana350ff92010-08-10 17:14:02 -070081 if (mHwc) {
82 hwc_close(mHwc);
83 }
84}
85
86status_t HWComposer::initCheck() const {
87 return mHwc ? NO_ERROR : NO_INIT;
88}
89
Mathias Agopianc7d14e22011-08-01 16:32:21 -070090void HWComposer::hook_invalidate(struct hwc_procs* procs) {
91 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
92}
93
Mathias Agopian31d28432012-04-03 16:31:39 -070094void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
95 reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
96}
97
Mathias Agopianc7d14e22011-08-01 16:32:21 -070098void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -070099 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700100}
101
Mathias Agopian31d28432012-04-03 16:31:39 -0700102void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700103 mEventHandler.onVSyncReceived(dpy, timestamp);
104}
105
106status_t HWComposer::eventControl(int event, int enabled) {
107 status_t err = NO_ERROR;
108 if (mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
109 err = mHwc->methods->eventControl(mHwc, event, enabled);
110 } else {
111 if (mVSyncThread != NULL) {
112 mVSyncThread->setEnabled(enabled);
113 } else {
114 err = BAD_VALUE;
115 }
116 }
117 return err;
Mathias Agopian31d28432012-04-03 16:31:39 -0700118}
119
Mathias Agopiana350ff92010-08-10 17:14:02 -0700120void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
121 mDpy = (hwc_display_t)dpy;
122 mSur = (hwc_surface_t)sur;
123}
124
125status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopian45721772010-08-12 15:03:26 -0700126 if (mHwc) {
127 if (!mList || mCapacity < numLayers) {
128 free(mList);
129 size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
130 mList = (hwc_layer_list_t*)malloc(size);
131 mCapacity = numLayers;
132 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700133 mList->flags = HWC_GEOMETRY_CHANGED;
134 mList->numHwLayers = numLayers;
135 }
136 return NO_ERROR;
137}
138
139status_t HWComposer::prepare() const {
140 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700141 if (err == NO_ERROR) {
142 size_t numOVLayers = 0;
143 size_t numFBLayers = 0;
144 size_t count = mList->numHwLayers;
145 for (size_t i=0 ; i<count ; i++) {
146 hwc_layer& l(mList->hwLayers[i]);
147 if (l.flags & HWC_SKIP_LAYER) {
148 l.compositionType = HWC_FRAMEBUFFER;
149 }
150 switch (l.compositionType) {
151 case HWC_OVERLAY:
152 numOVLayers++;
153 break;
154 case HWC_FRAMEBUFFER:
155 numFBLayers++;
156 break;
157 }
158 }
159 mNumOVLayers = numOVLayers;
160 mNumFBLayers = numFBLayers;
161 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700162 return (status_t)err;
163}
164
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700165size_t HWComposer::getLayerCount(int type) const {
166 switch (type) {
167 case HWC_OVERLAY:
168 return mNumOVLayers;
169 case HWC_FRAMEBUFFER:
170 return mNumFBLayers;
171 }
172 return 0;
173}
174
Mathias Agopiana350ff92010-08-10 17:14:02 -0700175status_t HWComposer::commit() const {
176 int err = mHwc->set(mHwc, mDpy, mSur, mList);
Mathias Agopian58959342010-10-07 14:57:04 -0700177 if (mList) {
178 mList->flags &= ~HWC_GEOMETRY_CHANGED;
179 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700180 return (status_t)err;
181}
182
Antti Hatalaf5f27122010-09-09 02:33:05 -0700183status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700184 if (mHwc) {
185 int err = mHwc->set(mHwc, NULL, NULL, NULL);
186 return (status_t)err;
187 }
188 return NO_ERROR;
189}
190
191status_t HWComposer::disable() {
192 if (mHwc) {
193 free(mList);
194 mList = NULL;
195 int err = mHwc->prepare(mHwc, NULL);
196 return (status_t)err;
197 }
198 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700199}
200
Mathias Agopian45721772010-08-12 15:03:26 -0700201size_t HWComposer::getNumLayers() const {
202 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700203}
204
Mathias Agopian45721772010-08-12 15:03:26 -0700205hwc_layer_t* HWComposer::getLayers() const {
206 return mList ? mList->hwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700207}
208
Mathias Agopian22da60c2011-09-09 00:49:11 -0700209void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
210 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700211 if (mHwc && mList) {
212 result.append("Hardware Composer state:\n");
213
214 snprintf(buffer, SIZE, " numHwLayers=%u, flags=%08x\n",
215 mList->numHwLayers, mList->flags);
216 result.append(buffer);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700217 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700218 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
219 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
220 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700221 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
222 const hwc_layer_t& l(mList->hwLayers[i]);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700223 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
224 int32_t format = -1;
225 if (layer->getLayer() != NULL) {
226 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
227 if (buffer != NULL) {
228 format = buffer->getPixelFormat();
229 }
230 }
231 snprintf(buffer, SIZE,
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700232 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700233 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700234 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700235 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700236 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700237 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700238 result.append(buffer);
239 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800240 }
241 if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
242 mHwc->dump(mHwc, buffer, SIZE);
243 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700244 }
245}
246
Mathias Agopiana350ff92010-08-10 17:14:02 -0700247// ---------------------------------------------------------------------------
248}; // namespace android