blob: 4e13377202e53a0e2df3d12bcf20c6082107c5a9 [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>
35
36#include <EGL/egl.h>
37
Mathias Agopian22da60c2011-09-09 00:49:11 -070038#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070039#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070040#include "SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070041
42namespace android {
43// ---------------------------------------------------------------------------
44
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070045HWComposer::HWComposer(
46 const sp<SurfaceFlinger>& flinger,
47 EventHandler& handler,
48 nsecs_t refreshPeriod)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070049 : mFlinger(flinger),
50 mModule(0), mHwc(0), mList(0), mCapacity(0),
Mathias Agopian9c6e2972011-09-20 17:21:56 -070051 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070052 mDpy(EGL_NO_DISPLAY), mSur(EGL_NO_SURFACE),
53 mEventHandler(handler),
Mathias Agopian2965b262012-04-08 15:13:32 -070054 mRefreshPeriod(refreshPeriod), mVSyncCount(0)
Mathias Agopiana350ff92010-08-10 17:14:02 -070055{
Mathias Agopian3a778712012-04-09 14:16:47 -070056 bool needVSyncThread = false;
Mathias Agopiana350ff92010-08-10 17:14:02 -070057 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +000058 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -070059 if (err == 0) {
60 err = hwc_open(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +000061 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -070062 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070063 if (err == 0) {
64 if (mHwc->registerProcs) {
65 mCBContext.hwc = this;
66 mCBContext.procs.invalidate = &hook_invalidate;
Mathias Agopian31d28432012-04-03 16:31:39 -070067 mCBContext.procs.vsync = &hook_vsync;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070068 mHwc->registerProcs(mHwc, &mCBContext.procs);
Mathias Agopian31d28432012-04-03 16:31:39 -070069 memset(mCBContext.procs.zero, 0, sizeof(mCBContext.procs.zero));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070070 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070071 if (mHwc->common.version < HWC_DEVICE_API_VERSION_0_3) {
Mathias Agopian3a778712012-04-09 14:16:47 -070072 needVSyncThread = true;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070073 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -070074 }
Mathias Agopian3a778712012-04-09 14:16:47 -070075 } else {
76 needVSyncThread = true;
77 }
78
79 if (needVSyncThread) {
80 // we don't have VSYNC support, we need to fake it
81 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -070082 }
83}
84
85HWComposer::~HWComposer() {
86 free(mList);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070087 if (mVSyncThread != NULL) {
88 mVSyncThread->requestExitAndWait();
89 }
Mathias Agopiana350ff92010-08-10 17:14:02 -070090 if (mHwc) {
91 hwc_close(mHwc);
92 }
93}
94
95status_t HWComposer::initCheck() const {
96 return mHwc ? NO_ERROR : NO_INIT;
97}
98
Mathias Agopianc7d14e22011-08-01 16:32:21 -070099void HWComposer::hook_invalidate(struct hwc_procs* procs) {
100 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
101}
102
Mathias Agopian31d28432012-04-03 16:31:39 -0700103void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
104 reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
105}
106
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700107void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700108 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700109}
110
Mathias Agopian31d28432012-04-03 16:31:39 -0700111void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700112 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700113 mEventHandler.onVSyncReceived(dpy, timestamp);
114}
115
116status_t HWComposer::eventControl(int event, int enabled) {
117 status_t err = NO_ERROR;
Erik Gilling1a3bf412012-04-06 14:13:32 -0700118 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700119 err = mHwc->methods->eventControl(mHwc, event, enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700120 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700121
122 if (err == NO_ERROR && mVSyncThread != NULL) {
123 mVSyncThread->setEnabled(enabled);
124 }
125
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700126 return err;
Mathias Agopian31d28432012-04-03 16:31:39 -0700127}
128
Mathias Agopiana350ff92010-08-10 17:14:02 -0700129void HWComposer::setFrameBuffer(EGLDisplay dpy, EGLSurface sur) {
130 mDpy = (hwc_display_t)dpy;
131 mSur = (hwc_surface_t)sur;
132}
133
134status_t HWComposer::createWorkList(size_t numLayers) {
Mathias Agopian45721772010-08-12 15:03:26 -0700135 if (mHwc) {
136 if (!mList || mCapacity < numLayers) {
137 free(mList);
138 size_t size = sizeof(hwc_layer_list) + numLayers*sizeof(hwc_layer_t);
139 mList = (hwc_layer_list_t*)malloc(size);
140 mCapacity = numLayers;
141 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700142 mList->flags = HWC_GEOMETRY_CHANGED;
143 mList->numHwLayers = numLayers;
144 }
145 return NO_ERROR;
146}
147
148status_t HWComposer::prepare() const {
149 int err = mHwc->prepare(mHwc, mList);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700150 if (err == NO_ERROR) {
151 size_t numOVLayers = 0;
152 size_t numFBLayers = 0;
153 size_t count = mList->numHwLayers;
154 for (size_t i=0 ; i<count ; i++) {
155 hwc_layer& l(mList->hwLayers[i]);
156 if (l.flags & HWC_SKIP_LAYER) {
157 l.compositionType = HWC_FRAMEBUFFER;
158 }
159 switch (l.compositionType) {
160 case HWC_OVERLAY:
161 numOVLayers++;
162 break;
163 case HWC_FRAMEBUFFER:
164 numFBLayers++;
165 break;
166 }
167 }
168 mNumOVLayers = numOVLayers;
169 mNumFBLayers = numFBLayers;
170 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700171 return (status_t)err;
172}
173
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700174size_t HWComposer::getLayerCount(int type) const {
175 switch (type) {
176 case HWC_OVERLAY:
177 return mNumOVLayers;
178 case HWC_FRAMEBUFFER:
179 return mNumFBLayers;
180 }
181 return 0;
182}
183
Mathias Agopiana350ff92010-08-10 17:14:02 -0700184status_t HWComposer::commit() const {
185 int err = mHwc->set(mHwc, mDpy, mSur, mList);
Mathias Agopian58959342010-10-07 14:57:04 -0700186 if (mList) {
187 mList->flags &= ~HWC_GEOMETRY_CHANGED;
188 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700189 return (status_t)err;
190}
191
Antti Hatalaf5f27122010-09-09 02:33:05 -0700192status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700193 if (mHwc) {
194 int err = mHwc->set(mHwc, NULL, NULL, NULL);
195 return (status_t)err;
196 }
197 return NO_ERROR;
198}
199
200status_t HWComposer::disable() {
201 if (mHwc) {
202 free(mList);
203 mList = NULL;
204 int err = mHwc->prepare(mHwc, NULL);
205 return (status_t)err;
206 }
207 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700208}
209
Mathias Agopian45721772010-08-12 15:03:26 -0700210size_t HWComposer::getNumLayers() const {
211 return mList ? mList->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700212}
213
Mathias Agopian45721772010-08-12 15:03:26 -0700214hwc_layer_t* HWComposer::getLayers() const {
215 return mList ? mList->hwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700216}
217
Mathias Agopian22da60c2011-09-09 00:49:11 -0700218void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
219 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Mathias Agopian83727852010-09-23 18:13:21 -0700220 if (mHwc && mList) {
221 result.append("Hardware Composer state:\n");
222
223 snprintf(buffer, SIZE, " numHwLayers=%u, flags=%08x\n",
224 mList->numHwLayers, mList->flags);
225 result.append(buffer);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700226 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700227 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
228 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
229 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopian83727852010-09-23 18:13:21 -0700230 for (size_t i=0 ; i<mList->numHwLayers ; i++) {
231 const hwc_layer_t& l(mList->hwLayers[i]);
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700232 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
233 int32_t format = -1;
234 if (layer->getLayer() != NULL) {
235 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
236 if (buffer != NULL) {
237 format = buffer->getPixelFormat();
238 }
239 }
240 snprintf(buffer, SIZE,
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700241 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700242 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700243 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700244 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700245 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700246 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700247 result.append(buffer);
248 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800249 }
250 if (mHwc && mHwc->common.version >= 1 && mHwc->dump) {
251 mHwc->dump(mHwc, buffer, SIZE);
252 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700253 }
254}
255
Mathias Agopiana350ff92010-08-10 17:14:02 -0700256// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700257
258HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
259 : mHwc(hwc), mEnabled(false),
260 mNextFakeVSync(0),
261 mRefreshPeriod(hwc.mRefreshPeriod)
262{
263}
264
265void HWComposer::VSyncThread::setEnabled(bool enabled) {
266 Mutex::Autolock _l(mLock);
267 mEnabled = enabled;
268 mCondition.signal();
269}
270
271void HWComposer::VSyncThread::onFirstRef() {
272 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
273}
274
275bool HWComposer::VSyncThread::threadLoop() {
276 { // scope for lock
277 Mutex::Autolock _l(mLock);
278 while (!mEnabled) {
279 mCondition.wait(mLock);
280 }
281 }
282
283 const nsecs_t period = mRefreshPeriod;
284 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
285 nsecs_t next_vsync = mNextFakeVSync;
286 nsecs_t sleep = next_vsync - now;
287 if (sleep < 0) {
288 // we missed, find where the next vsync should be
289 sleep = (period - ((now - next_vsync) % period));
290 next_vsync = now + sleep;
291 }
292 mNextFakeVSync = next_vsync + period;
293
294 struct timespec spec;
295 spec.tv_sec = next_vsync / 1000000000;
296 spec.tv_nsec = next_vsync % 1000000000;
297
298 int err;
299 do {
300 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
301 } while (err<0 && errno == EINTR);
302
303 if (err == 0) {
304 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
305 }
306
307 return true;
308}
309
310// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700311}; // namespace android