blob: b87e191da75119eb05e7aca08c867da7c0621cfe [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
Mathias Agopian2965b262012-04-08 15:13:32 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopiana350ff92010-08-10 17:14:02 -070019#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070023#include <sys/types.h>
24
25#include <utils/Errors.h>
Mathias Agopian83727852010-09-23 18:13:21 -070026#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070027#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070028#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070029#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070030
31#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070032#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070033
34#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070035#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070036
37#include <EGL/egl.h>
38
Mathias Agopian22da60c2011-09-09 00:49:11 -070039#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070040#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070041#include "SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070042
43namespace android {
44// ---------------------------------------------------------------------------
45
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070046HWComposer::HWComposer(
47 const sp<SurfaceFlinger>& flinger,
48 EventHandler& handler,
49 nsecs_t refreshPeriod)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070050 : mFlinger(flinger),
51 mModule(0), mHwc(0), mList(0), mCapacity(0),
Mathias Agopian9c6e2972011-09-20 17:21:56 -070052 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070053 mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE),
54 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070055 mRefreshPeriod(refreshPeriod),
56 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -070057{
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070058 char value[PROPERTY_VALUE_MAX];
59 property_get("debug.sf.no_hw_vsync", value, "0");
60 mDebugForceFakeVSync = atoi(value);
61
Mathias Agopian3a778712012-04-09 14:16:47 -070062 bool needVSyncThread = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -070063 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +000064 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -070065 if (err == 0) {
66 err = hwc_open(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +000067 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -070068 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070069 if (err == 0) {
70 if (mHwc->registerProcs) {
71 mCBContext.hwc = this;
72 mCBContext.procs.invalidate = &hook_invalidate;
Mathias Agopian31d28432012-04-03 16:31:39 -070073 mCBContext.procs.vsync = &hook_vsync;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070074 mHwc->registerProcs(mHwc, &mCBContext.procs);
Mathias Agopian31d28432012-04-03 16:31:39 -070075 memset(mCBContext.procs.zero, 0, sizeof(mCBContext.procs.zero));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070076 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070077 if (mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
78 if (mDebugForceFakeVSync) {
79 // make sure to turn h/w vsync off in "fake vsync" mode
80 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
81 }
82 } else {
Mathias Agopian3a778712012-04-09 14:16:47 -070083 needVSyncThread = true;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070084 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -070085 }
Mathias Agopian3a778712012-04-09 14:16:47 -070086 } else {
87 needVSyncThread = true;
88 }
89
90 if (needVSyncThread) {
91 // we don't have VSYNC support, we need to fake it
92 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -070093 }
94}
95
96HWComposer::~HWComposer() {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070097 eventControl(EVENT_VSYNC, 0);
Mathias Agopiana350ff92010-08-10 17:14:02 -070098 free(mList);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070099 if (mVSyncThread != NULL) {
100 mVSyncThread->requestExitAndWait();
101 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700102 if (mHwc) {
103 hwc_close(mHwc);
104 }
105}
106
107status_t HWComposer::initCheck() const {
108 return mHwc ? NO_ERROR : NO_INIT;
109}
110
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700111void HWComposer::hook_invalidate(struct hwc_procs* procs) {
112 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
113}
114
Mathias Agopian31d28432012-04-03 16:31:39 -0700115void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
116 reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
117}
118
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700119void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700120 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700121}
122
Mathias Agopian31d28432012-04-03 16:31:39 -0700123void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700124 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700125 mEventHandler.onVSyncReceived(dpy, timestamp);
126}
127
128status_t HWComposer::eventControl(int event, int enabled) {
129 status_t err = NO_ERROR;
Erik Gilling1a3bf412012-04-06 14:13:32 -0700130 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700131 if (!mDebugForceFakeVSync) {
132 err = mHwc->methods->eventControl(mHwc, event, enabled);
133 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700134 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700135
136 if (err == NO_ERROR && mVSyncThread != NULL) {
137 mVSyncThread->setEnabled(enabled);
138 }
139
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700140 return err;
Mathias Agopian31d28432012-04-03 16:31:39 -0700141}
142
Mathias Agopiana350ff92010-08-10 17:14:02 -0700143void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
144 mDpy = (hwc_display_t)dpy;
145 mSur = (hwc_surface_t)sur;
146}
147
148status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopian45721772010-08-12 15:03:26 -0700149 if (mHwc) {
150 if (!mList || mCapacity < numLayers) {
151 free(mList);
152 size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
153 mList = (hwc_layer_list_t*)malloc(size);
154 mCapacity = numLayers;
155 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700156 mList->flags = HWC_GEOMETRY_CHANGED;
157 mList->numHwLayers = numLayers;
158 }
159 return NO_ERROR;
160}
161
162status_t HWComposer::prepare() const {
163 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700164 if (err == NO_ERROR) {
165 size_t numOVLayers = 0;
166 size_t numFBLayers = 0;
167 size_t count = mList->numHwLayers;
168 for (size_t i=0 ; i<count ; i++) {
169 hwc_layer& l(mList->hwLayers[i]);
170 if (l.flags & HWC_SKIP_LAYER) {
171 l.compositionType = HWC_FRAMEBUFFER;
172 }
173 switch (l.compositionType) {
174 case HWC_OVERLAY:
175 numOVLayers++;
176 break;
177 case HWC_FRAMEBUFFER:
178 numFBLayers++;
179 break;
180 }
181 }
182 mNumOVLayers = numOVLayers;
183 mNumFBLayers = numFBLayers;
184 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700185 return (status_t)err;
186}
187
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700188size_t HWComposer::getLayerCount(int type) const {
189 switch (type) {
190 case HWC_OVERLAY:
191 return mNumOVLayers;
192 case HWC_FRAMEBUFFER:
193 return mNumFBLayers;
194 }
195 return 0;
196}
197
Mathias Agopiana350ff92010-08-10 17:14:02 -0700198status_t HWComposer::commit() const {
199 int err = mHwc->set(mHwc, mDpy, mSur, mList);
Mathias Agopian58959342010-10-07 14:57:04 -0700200 if (mList) {
201 mList->flags &= ~HWC_GEOMETRY_CHANGED;
202 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700203 return (status_t)err;
204}
205
Antti Hatalaf5f27122010-09-09 02:33:05 -0700206status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700207 if (mHwc) {
Mathias Agopian22ffb112012-04-10 21:04:02 -0700208 if (mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
209 mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0);
210 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700211 int err = mHwc->set(mHwc, NULL, NULL, NULL);
212 return (status_t)err;
213 }
214 return NO_ERROR;
215}
216
217status_t HWComposer::disable() {
218 if (mHwc) {
219 free(mList);
220 mList = NULL;
221 int err = mHwc->prepare(mHwc, NULL);
222 return (status_t)err;
223 }
224 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700225}
226
Mathias Agopian45721772010-08-12 15:03:26 -0700227size_t HWComposer::getNumLayers() const {
228 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229}
230
Mathias Agopian45721772010-08-12 15:03:26 -0700231hwc_layer_t* HWComposer::getLayers() const {
232 return mList ? mList->hwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700233}
234
Mathias Agopian22da60c2011-09-09 00:49:11 -0700235void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
236 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700237 if (mHwc && mList) {
238 result.append("Hardware Composer state:\n");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700239 result.appendFormat(" mDebugForceFakeVSync=%d\n",
240 mDebugForceFakeVSync);
241 result.appendFormat(" numHwLayers=%u, flags=%08x\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700242 mList->numHwLayers, mList->flags);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700243 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700244 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
245 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
246 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700247 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
248 const hwc_layer_t& l(mList->hwLayers[i]);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700249 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
250 int32_t format = -1;
251 if (layer->getLayer() != NULL) {
252 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
253 if (buffer != NULL) {
254 format = buffer->getPixelFormat();
255 }
256 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700257 result.appendFormat(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700258 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700259 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700260 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700261 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700262 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700263 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700264 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800265 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700266 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_1 && mHwc->dump) {
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800267 mHwc->dump(mHwc, buffer, SIZE);
268 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700269 }
270}
271
Mathias Agopiana350ff92010-08-10 17:14:02 -0700272// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700273
274HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
275 : mHwc(hwc), mEnabled(false),
276 mNextFakeVSync(0),
277 mRefreshPeriod(hwc.mRefreshPeriod)
278{
279}
280
281void HWComposer::VSyncThread::setEnabled(bool enabled) {
282 Mutex::Autolock _l(mLock);
283 mEnabled = enabled;
284 mCondition.signal();
285}
286
287void HWComposer::VSyncThread::onFirstRef() {
288 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
289}
290
291bool HWComposer::VSyncThread::threadLoop() {
292 { // scope for lock
293 Mutex::Autolock _l(mLock);
294 while (!mEnabled) {
295 mCondition.wait(mLock);
296 }
297 }
298
299 const nsecs_t period = mRefreshPeriod;
300 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
301 nsecs_t next_vsync = mNextFakeVSync;
302 nsecs_t sleep = next_vsync - now;
303 if (sleep < 0) {
304 // we missed, find where the next vsync should be
305 sleep = (period - ((now - next_vsync) % period));
306 next_vsync = now + sleep;
307 }
308 mNextFakeVSync = next_vsync + period;
309
310 struct timespec spec;
311 spec.tv_sec = next_vsync / 1000000000;
312 spec.tv_nsec = next_vsync % 1000000000;
313
314 int err;
315 do {
316 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
317 } while (err<0 && errno == EINTR);
318
319 if (err == 0) {
320 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
321 }
322
323 return true;
324}
325
326// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700327}; // namespace android