blob: 1678fe1318800f908286438a9e7fbc4edb9a6b86 [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
Jesse Hall5880cc52012-06-05 23:40:32 -070019// Uncomment this to remove support for HWC_DEVICE_API_VERSION_0_3 and older
Mathias Agopian30bcc612012-08-22 15:39:48 -070020#define HWC_REMOVE_DEPRECATED_VERSIONS 1
Jesse Hall5880cc52012-06-05 23:40:32 -070021
Mathias Agopiana350ff92010-08-10 17:14:02 -070022#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070026#include <sys/types.h>
27
28#include <utils/Errors.h>
Mathias Agopian83727852010-09-23 18:13:21 -070029#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070030#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070031#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070032#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070033
Mathias Agopian921e6ac2012-07-23 23:11:29 -070034#include <ui/GraphicBuffer.h>
35
Mathias Agopiana350ff92010-08-10 17:14:02 -070036#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070037#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
39#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070040#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070041
Mathias Agopian921e6ac2012-07-23 23:11:29 -070042#include "Layer.h" // needed only for debugging
Mathias Agopian22da60c2011-09-09 00:49:11 -070043#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070044#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070045#include "SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070046
47namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070048
Jesse Hall5880cc52012-06-05 23:40:32 -070049static bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
Mathias Agopian30bcc612012-08-22 15:39:48 -070050 return hwc->common.version >= version;
Jesse Hallbbd164a2012-08-21 12:05:09 -070051}
52
Mathias Agopiana350ff92010-08-10 17:14:02 -070053// ---------------------------------------------------------------------------
54
Mathias Agopian3e8b8532012-05-13 20:42:01 -070055struct HWComposer::cb_context {
56 struct callbacks : public hwc_procs_t {
57 // these are here to facilitate the transition when adding
58 // new callbacks (an implementation can check for NULL before
59 // calling a new callback).
60 void (*zero[4])(void);
61 };
62 callbacks procs;
63 HWComposer* hwc;
64};
65
66// ---------------------------------------------------------------------------
67
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070068HWComposer::HWComposer(
69 const sp<SurfaceFlinger>& flinger,
Mathias Agopian8b736f12012-08-13 17:54:26 -070070 EventHandler& handler,
71 framebuffer_device_t const* fbDev)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070072 : mFlinger(flinger),
Mathias Agopianf4358632012-08-22 17:16:19 -070073 mModule(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070074 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070075 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070076 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -070077{
Mathias Agopiane60b0682012-08-21 23:34:09 -070078 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
79 mLists[i] = 0;
80 }
Jesse Hallb685c542012-07-31 14:32:56 -070081
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070082 char value[PROPERTY_VALUE_MAX];
83 property_get("debug.sf.no_hw_vsync", value, "0");
84 mDebugForceFakeVSync = atoi(value);
85
Mathias Agopian028508c2012-07-25 21:12:12 -070086 bool needVSyncThread = true;
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +000088 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -070090 err = hwc_open_1(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +000091 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -070092 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -070093 if (err == 0) {
Mathias Agopian30bcc612012-08-22 15:39:48 -070094 if (mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
Jesse Hall5880cc52012-06-05 23:40:32 -070095 ALOGE("%s device version %#x too old, will not be used",
96 HWC_HARDWARE_COMPOSER, mHwc->common.version);
97 hwc_close_1(mHwc);
98 mHwc = NULL;
99 }
100 }
101
102 if (mHwc) {
Jesse Hallbbd164a2012-08-21 12:05:09 -0700103 if (mHwc->registerProcs) {
104 mCBContext->hwc = this;
105 mCBContext->procs.invalidate = &hook_invalidate;
106 mCBContext->procs.vsync = &hook_vsync;
107 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
108 mHwc->registerProcs(mHwc, &mCBContext->procs);
109 }
110
Jesse Hallb685c542012-07-31 14:32:56 -0700111 // always turn vsync off when we start
112 needVSyncThread = false;
Mathias Agopian30bcc612012-08-22 15:39:48 -0700113 mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
114
115 int period;
116 if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700117 mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(period);
Mathias Agopian028508c2012-07-25 21:12:12 -0700118 }
Jesse Hallb685c542012-07-31 14:32:56 -0700119
Mathias Agopianf4358632012-08-22 17:16:19 -0700120 // these IDs are always reserved
121 for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
122 mAllocatedDisplayIDs.markBit(i);
123 // TODO: we query xdpi / ydpi / refresh
124 }
Jesse Hall8f971ff2012-08-22 11:50:00 -0700125
Mathias Agopianf4358632012-08-22 17:16:19 -0700126 // the number of displays we actually have depends on the
127 // hw composer version
128 if (mHwc->common.version == HWC_DEVICE_API_VERSION_1_1) {
129 // 1.1 adds support for multiple displays
130 mNumDisplays = HWC_NUM_DISPLAY_TYPES;
131 } else if (mHwc->common.version > HWC_DEVICE_API_VERSION_1_1) {
132 // 1.2 adds support for virtual displays
133 mNumDisplays = MAX_DISPLAYS;
134 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700135 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700136 }
137
Mathias Agopian8b736f12012-08-13 17:54:26 -0700138 if (fbDev) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700139 // if we're here it means we are on version 1.0
140 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
141 disp.xdpi = fbDev->xdpi;
142 disp.ydpi = fbDev->ydpi;
143 if (disp.refresh == 0) {
144 disp.refresh = nsecs_t(1e9 / fbDev->fps);
145 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700146 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700147 if (disp.refresh == 0) {
148 disp.refresh = nsecs_t(1e9 / 60.0);
149 ALOGW("getting VSYNC period thin air: %lld", mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
150 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700151 }
152
Mathias Agopian3a778712012-04-09 14:16:47 -0700153 if (needVSyncThread) {
154 // we don't have VSYNC support, we need to fake it
155 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700156 }
157}
158
159HWComposer::~HWComposer() {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700160 mHwc->eventControl(mHwc, 0, EVENT_VSYNC, 0);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700161 if (mVSyncThread != NULL) {
162 mVSyncThread->requestExitAndWait();
163 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700164 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700165 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700166 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700167 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700168}
169
170status_t HWComposer::initCheck() const {
171 return mHwc ? NO_ERROR : NO_INIT;
172}
173
Jesse Hallbbd164a2012-08-21 12:05:09 -0700174void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
175 cb_context* ctx = reinterpret_cast<cb_context*>(
176 const_cast<hwc_procs_t*>(procs));
177 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700178}
179
Jesse Hallbbd164a2012-08-21 12:05:09 -0700180void HWComposer::hook_vsync(const struct hwc_procs* procs, int dpy,
181 int64_t timestamp) {
182 cb_context* ctx = reinterpret_cast<cb_context*>(
183 const_cast<hwc_procs_t*>(procs));
184 ctx->hwc->vsync(dpy, timestamp);
Mathias Agopian31d28432012-04-03 16:31:39 -0700185}
186
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700187void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700188 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700189}
190
Mathias Agopian31d28432012-04-03 16:31:39 -0700191void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700192 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700193 mEventHandler.onVSyncReceived(dpy, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700194 Mutex::Autolock _l(mLock);
195 mLastHwVSync = timestamp;
196}
197
Mathias Agopiane60b0682012-08-21 23:34:09 -0700198int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700199 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700200 return NO_MEMORY;
201 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700202 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
203 mAllocatedDisplayIDs.markBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700204 return id;
205}
206
207status_t HWComposer::freeDisplayId(int32_t id) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700208 if (id < HWC_NUM_DISPLAY_TYPES) {
209 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700210 return BAD_VALUE;
211 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700212 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700213 return BAD_INDEX;
214 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700215 mAllocatedDisplayIDs.clearBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700216 return NO_ERROR;
217}
218
Mathias Agopian888c8222012-08-04 21:10:38 -0700219nsecs_t HWComposer::getRefreshPeriod() const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700220 return mDisplayData[HWC_DISPLAY_PRIMARY].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700221}
222
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700223nsecs_t HWComposer::getRefreshTimestamp() const {
224 // this returns the last refresh timestamp.
225 // if the last one is not available, we estimate it based on
226 // the refresh period and whatever closest timestamp we have.
227 Mutex::Autolock _l(mLock);
228 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopianf4358632012-08-22 17:16:19 -0700229 return now - ((now - mLastHwVSync) % mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700230}
231
Mathias Agopian8b736f12012-08-13 17:54:26 -0700232float HWComposer::getDpiX() const {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700233 return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700234}
235
236float HWComposer::getDpiY() const {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700237 return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700238}
239
Mathias Agopian03e40722012-04-26 16:11:59 -0700240void HWComposer::eventControl(int event, int enabled) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700241 status_t err = NO_ERROR;
Mathias Agopian30bcc612012-08-22 15:39:48 -0700242 if (mHwc) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700243 if (!mDebugForceFakeVSync) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700244 err = mHwc->eventControl(mHwc, 0, event, enabled);
Mathias Agopian03e40722012-04-26 16:11:59 -0700245 // error here should not happen -- not sure what we should
246 // do if it does.
247 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
248 event, enabled, strerror(-err));
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700249 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700250 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700251
252 if (err == NO_ERROR && mVSyncThread != NULL) {
253 mVSyncThread->setEnabled(enabled);
254 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700255}
256
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700257status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700258 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700259 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700260 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700261
Mathias Agopian45721772010-08-12 15:03:26 -0700262 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700263 DisplayData& disp(mDisplayData[id]);
Andy McFadden13a082e2012-08-24 10:16:42 -0700264 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700265 const size_t size = sizeof(hwc_display_contents_1_t)
266 + numLayers * sizeof(hwc_layer_1_t);
267 free(disp.list);
268 disp.list = (hwc_display_contents_1_t*)malloc(size);
269 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700270 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700271 disp.list->flags = HWC_GEOMETRY_CHANGED;
272 disp.list->numHwLayers = numLayers;
273 disp.list->flipFenceFd = -1;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700274 }
275 return NO_ERROR;
276}
277
Mathias Agopiane60b0682012-08-21 23:34:09 -0700278status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700279 for (size_t i=0 ; i<mNumDisplays ; i++) {
280 mLists[i] = mDisplayData[i].list;
281 if (mLists[i]) {
282 mLists[i]->dpy = EGL_NO_DISPLAY;
283 mLists[i]->sur = EGL_NO_SURFACE;
284 }
285 }
286 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700287 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700288 // here we're just making sure that "skip" layers are set
289 // to HWC_FRAMEBUFFER and we're also counting how many layers
290 // we have of each type.
Mathias Agopianf4358632012-08-22 17:16:19 -0700291 for (size_t i=0 ; i<mNumDisplays ; i++) {
292 DisplayData& disp(mDisplayData[i]);
293 disp.hasFbComp = false;
294 disp.hasOvComp = false;
295 if (disp.list) {
296 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
297 hwc_layer_1_t& l = disp.list->hwLayers[i];
298 if (l.flags & HWC_SKIP_LAYER) {
299 l.compositionType = HWC_FRAMEBUFFER;
300 }
301 if (l.compositionType == HWC_FRAMEBUFFER) {
302 disp.hasFbComp = true;
303 }
304 if (l.compositionType == HWC_OVERLAY) {
305 disp.hasOvComp = true;
306 }
307 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700308 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700309 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700310 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700311 return (status_t)err;
312}
313
Mathias Agopiane60b0682012-08-21 23:34:09 -0700314bool HWComposer::hasHwcComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700315 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700316 return false;
317 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700318}
319
Mathias Agopiane60b0682012-08-21 23:34:09 -0700320bool HWComposer::hasGlesComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700321 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700322 return false;
323 return mDisplayData[id].hasFbComp;
324}
325
Mathias Agopian30bcc612012-08-22 15:39:48 -0700326status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700327 int err = NO_ERROR;
328 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700329 if (mHwc->common.version == HWC_DEVICE_API_VERSION_1_0) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700330 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700331 // by the (dpy, sur) fields and we are guaranteed to have only
332 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700333 mLists[0]->dpy = eglGetCurrentDisplay();
334 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700335 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700336
Mathias Agopian30bcc612012-08-22 15:39:48 -0700337 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700338
339 for (size_t i=0 ; i<mNumDisplays ; i++) {
340 DisplayData& disp(mDisplayData[i]);
341 if (disp.list) {
342 if (disp.list->flipFenceFd != -1) {
343 close(disp.list->flipFenceFd);
344 disp.list->flipFenceFd = -1;
345 }
346 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
347 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700348 }
Mathias Agopian58959342010-10-07 14:57:04 -0700349 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700350 return (status_t)err;
351}
352
Antti Hatalaf5f27122010-09-09 02:33:05 -0700353status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700354 if (mHwc) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700355 mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
356 return (status_t)mHwc->blank(mHwc, 0, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700357 }
358 return NO_ERROR;
359}
360
Colin Cross10fbdb62012-07-12 17:56:34 -0700361status_t HWComposer::acquire() const {
362 if (mHwc) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700363 return (status_t)mHwc->blank(mHwc, 0, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700364 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700365 return NO_ERROR;
366}
367
Mathias Agopianf4358632012-08-22 17:16:19 -0700368size_t HWComposer::getNumLayers(int32_t id) const {
369 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
370 return 0;
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700371 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700372 return (mHwc && mDisplayData[id].list) ?
373 mDisplayData[id].list->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700374}
375
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700376/*
377 * Helper template to implement a concrete HWCLayer
378 * This holds the pointer to the concrete hwc layer type
379 * and implements the "iterable" side of HWCLayer.
380 */
381template<typename CONCRETE, typename HWCTYPE>
382class Iterable : public HWComposer::HWCLayer {
383protected:
384 HWCTYPE* const mLayerList;
385 HWCTYPE* mCurrentLayer;
386 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
387 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
388 inline HWCTYPE* getLayer() { return mCurrentLayer; }
389 virtual ~Iterable() { }
390private:
391 // returns a copy of ourselves
392 virtual HWComposer::HWCLayer* dup() {
393 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
394 }
395 virtual status_t setLayer(size_t index) {
396 mCurrentLayer = &mLayerList[index];
397 return NO_ERROR;
398 }
399};
400
Jesse Hall5880cc52012-06-05 23:40:32 -0700401/*
402 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
403 * This implements the HWCLayer side of HWCIterableLayer.
404 */
405class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
406public:
407 HWCLayerVersion1(hwc_layer_1_t* layer)
408 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
409
410 virtual int32_t getCompositionType() const {
411 return getLayer()->compositionType;
412 }
413 virtual uint32_t getHints() const {
414 return getLayer()->hints;
415 }
Jesse Hallef194142012-06-14 14:45:17 -0700416 virtual int getAndResetReleaseFenceFd() {
417 int fd = getLayer()->releaseFenceFd;
418 getLayer()->releaseFenceFd = -1;
419 return fd;
420 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700421 virtual void setAcquireFenceFd(int fenceFd) {
422 getLayer()->acquireFenceFd = fenceFd;
423 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700424
425 virtual void setDefaultState() {
426 getLayer()->compositionType = HWC_FRAMEBUFFER;
427 getLayer()->hints = 0;
428 getLayer()->flags = HWC_SKIP_LAYER;
429 getLayer()->transform = 0;
430 getLayer()->blending = HWC_BLENDING_NONE;
431 getLayer()->visibleRegionScreen.numRects = 0;
432 getLayer()->visibleRegionScreen.rects = NULL;
433 getLayer()->acquireFenceFd = -1;
434 getLayer()->releaseFenceFd = -1;
435 }
436 virtual void setSkip(bool skip) {
437 if (skip) {
438 getLayer()->flags |= HWC_SKIP_LAYER;
439 } else {
440 getLayer()->flags &= ~HWC_SKIP_LAYER;
441 }
442 }
443 virtual void setBlending(uint32_t blending) {
444 getLayer()->blending = blending;
445 }
446 virtual void setTransform(uint32_t transform) {
447 getLayer()->transform = transform;
448 }
449 virtual void setFrame(const Rect& frame) {
450 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
451 }
452 virtual void setCrop(const Rect& crop) {
453 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
454 }
455 virtual void setVisibleRegionScreen(const Region& reg) {
456 getLayer()->visibleRegionScreen.rects =
457 reinterpret_cast<hwc_rect_t const *>(
458 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
459 }
460 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
461 if (buffer == 0 || buffer->handle == 0) {
462 getLayer()->compositionType = HWC_FRAMEBUFFER;
463 getLayer()->flags |= HWC_SKIP_LAYER;
464 getLayer()->handle = 0;
465 } else {
466 getLayer()->handle = buffer->handle;
467 }
468 }
469};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700470
471/*
472 * returns an iterator initialized at a given index in the layer list
473 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700474HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700475 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700476 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700477 }
478 const DisplayData& disp(mDisplayData[id]);
479 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700480 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700481 }
482 return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700483}
484
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700485/*
486 * returns an iterator on the beginning of the layer list
487 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700488HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700489 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700490}
491
492/*
493 * returns an iterator on the end of the layer list
494 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700495HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700496 return getLayerIterator(id, getNumLayers(id));
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700497}
498
Mathias Agopian22da60c2011-09-09 00:49:11 -0700499void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
500 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700501 if (mHwc) {
Mathias Agopian83727852010-09-23 18:13:21 -0700502 result.append("Hardware Composer state:\n");
Mathias Agopianf4358632012-08-22 17:16:19 -0700503 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
504 for (size_t i=0 ; i<mNumDisplays ; i++) {
505 const DisplayData& disp(mDisplayData[i]);
506 if (disp.list) {
507 result.appendFormat(" id=%d, numHwLayers=%u, flags=%08x\n",
508 i, disp.list->numHwLayers, disp.list->flags);
509 result.append(
510 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
511 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
512 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
513 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
514 const hwc_layer_1_t&l = disp.list->hwLayers[i];
515 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
516 int32_t format = -1;
517 if (layer->getLayer() != NULL) {
518 const sp<GraphicBuffer>& buffer(
519 layer->getLayer()->getActiveBuffer());
520 if (buffer != NULL) {
521 format = buffer->getPixelFormat();
522 }
523 }
524 result.appendFormat(
525 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
526 l.compositionType ? "OVERLAY" : "FB",
527 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
528 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
529 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
530 layer->getName().string());
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700531 }
532 }
Mathias Agopian83727852010-09-23 18:13:21 -0700533 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800534 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700535
536 if (mHwc && mHwc->dump) {
537 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800538 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700539 }
540}
541
Mathias Agopiana350ff92010-08-10 17:14:02 -0700542// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700543
544HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
545 : mHwc(hwc), mEnabled(false),
546 mNextFakeVSync(0),
Mathias Agopiane60b0682012-08-21 23:34:09 -0700547 mRefreshPeriod(hwc.getRefreshPeriod())
Mathias Agopian2965b262012-04-08 15:13:32 -0700548{
549}
550
551void HWComposer::VSyncThread::setEnabled(bool enabled) {
552 Mutex::Autolock _l(mLock);
553 mEnabled = enabled;
554 mCondition.signal();
555}
556
557void HWComposer::VSyncThread::onFirstRef() {
558 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
559}
560
561bool HWComposer::VSyncThread::threadLoop() {
562 { // scope for lock
563 Mutex::Autolock _l(mLock);
564 while (!mEnabled) {
565 mCondition.wait(mLock);
566 }
567 }
568
569 const nsecs_t period = mRefreshPeriod;
570 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
571 nsecs_t next_vsync = mNextFakeVSync;
572 nsecs_t sleep = next_vsync - now;
573 if (sleep < 0) {
574 // we missed, find where the next vsync should be
575 sleep = (period - ((now - next_vsync) % period));
576 next_vsync = now + sleep;
577 }
578 mNextFakeVSync = next_vsync + period;
579
580 struct timespec spec;
581 spec.tv_sec = next_vsync / 1000000000;
582 spec.tv_nsec = next_vsync % 1000000000;
583
584 int err;
585 do {
586 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
587 } while (err<0 && errno == EINTR);
588
589 if (err == 0) {
590 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
591 }
592
593 return true;
594}
595
596// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700597}; // namespace android