blob: 6ae03c95616f4cb7f1c7c56fc623df15a005d0ed [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) {
208 int err = mHwc->set(mHwc, NULL, NULL, NULL);
209 return (status_t)err;
210 }
211 return NO_ERROR;
212}
213
214status_t HWComposer::disable() {
215 if (mHwc) {
216 free(mList);
217 mList = NULL;
218 int err = mHwc->prepare(mHwc, NULL);
219 return (status_t)err;
220 }
221 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700222}
223
Mathias Agopian45721772010-08-12 15:03:26 -0700224size_t HWComposer::getNumLayers() const {
225 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700226}
227
Mathias Agopian45721772010-08-12 15:03:26 -0700228hwc_layer_t* HWComposer::getLayers() const {
229 return mList ? mList->hwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700230}
231
Mathias Agopian22da60c2011-09-09 00:49:11 -0700232void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
233 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700234 if (mHwc && mList) {
235 result.append("Hardware Composer state:\n");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700236 result.appendFormat(" mDebugForceFakeVSync=%d\n",
237 mDebugForceFakeVSync);
238 result.appendFormat(" numHwLayers=%u, flags=%08x\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700239 mList->numHwLayers, mList->flags);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700240 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700241 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
242 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
243 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700244 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
245 const hwc_layer_t& l(mList->hwLayers[i]);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700246 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
247 int32_t format = -1;
248 if (layer->getLayer() != NULL) {
249 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
250 if (buffer != NULL) {
251 format = buffer->getPixelFormat();
252 }
253 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700254 result.appendFormat(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700255 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700256 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700257 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700258 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700259 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700260 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700261 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800262 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700263 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_1 && mHwc->dump) {
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800264 mHwc->dump(mHwc, buffer, SIZE);
265 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700266 }
267}
268
Mathias Agopiana350ff92010-08-10 17:14:02 -0700269// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700270
271HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
272 : mHwc(hwc), mEnabled(false),
273 mNextFakeVSync(0),
274 mRefreshPeriod(hwc.mRefreshPeriod)
275{
276}
277
278void HWComposer::VSyncThread::setEnabled(bool enabled) {
279 Mutex::Autolock _l(mLock);
280 mEnabled = enabled;
281 mCondition.signal();
282}
283
284void HWComposer::VSyncThread::onFirstRef() {
285 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
286}
287
288bool HWComposer::VSyncThread::threadLoop() {
289 { // scope for lock
290 Mutex::Autolock _l(mLock);
291 while (!mEnabled) {
292 mCondition.wait(mLock);
293 }
294 }
295
296 const nsecs_t period = mRefreshPeriod;
297 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
298 nsecs_t next_vsync = mNextFakeVSync;
299 nsecs_t sleep = next_vsync - now;
300 if (sleep < 0) {
301 // we missed, find where the next vsync should be
302 sleep = (period - ((now - next_vsync) % period));
303 next_vsync = now + sleep;
304 }
305 mNextFakeVSync = next_vsync + period;
306
307 struct timespec spec;
308 spec.tv_sec = next_vsync / 1000000000;
309 spec.tv_nsec = next_vsync % 1000000000;
310
311 int err;
312 do {
313 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
314 } while (err<0 && errno == EINTR);
315
316 if (err == 0) {
317 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
318 }
319
320 return true;
321}
322
323// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700324}; // namespace android