blob: a3ec3522b35032e36a72d65a8df08ddf0349ac84 [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
20// #define HWC_REMOVE_DEPRECATED_VERSIONS 1
21
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
49// ---------------------------------------------------------------------------
50// Support for HWC_DEVICE_API_VERSION_0_3 and older:
51// Since v0.3 is deprecated and support will be dropped soon, as much as
52// possible the code is written to target v1.0. When using a v0.3 HWC, we
Jesse Hallb685c542012-07-31 14:32:56 -070053// allocate v0.3 structures, but assign them to v1.0 pointers.
Jesse Hall5880cc52012-06-05 23:40:32 -070054
55#if HWC_REMOVE_DEPRECATED_VERSIONS
Jesse Hallb685c542012-07-31 14:32:56 -070056// We need complete types to satisfy semantic checks, even though the code
57// paths that use these won't get executed at runtime (and will likely be dead-
58// code-eliminated). When we remove the code to support v0.3 we can remove
Jesse Hall5880cc52012-06-05 23:40:32 -070059// these as well.
60typedef hwc_layer_1_t hwc_layer_t;
Jesse Hallb685c542012-07-31 14:32:56 -070061typedef hwc_display_contents_1_t hwc_layer_list_t;
Jesse Hall5880cc52012-06-05 23:40:32 -070062typedef hwc_composer_device_1_t hwc_composer_device_t;
63#endif
64
65// This function assumes we've already rejected HWC's with lower-than-required
66// versions. Don't use it for the initial "does HWC meet requirements" check!
67static bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) {
68 if (HWC_REMOVE_DEPRECATED_VERSIONS &&
69 version <= HWC_DEVICE_API_VERSION_1_0) {
70 return true;
71 } else {
72 return hwc->common.version >= version;
73 }
74}
75
Jesse Hallb685c542012-07-31 14:32:56 -070076static bool hwcHasVsyncEvent(const hwc_composer_device_1_t* hwc) {
77 return hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_0_3);
78}
79
Jesse Hall5880cc52012-06-05 23:40:32 -070080static size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc,
81 size_t numLayers) {
82 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
Jesse Hallb685c542012-07-31 14:32:56 -070083 return sizeof(hwc_display_contents_1_t) + numLayers*sizeof(hwc_layer_1_t);
Jesse Hall5880cc52012-06-05 23:40:32 -070084 } else {
85 return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t);
86 }
87}
88
Jesse Hallb685c542012-07-31 14:32:56 -070089static int hwcEventControl(hwc_composer_device_1_t* hwc, int dpy,
90 int event, int enabled) {
91 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
92 return hwc->methods->eventControl(hwc, dpy, event, enabled);
93 } else {
Mathias Agopian1e260872012-08-08 18:35:12 -070094 hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
Jesse Hallb685c542012-07-31 14:32:56 -070095 return hwc0->methods->eventControl(hwc0, event, enabled);
96 }
97}
98
99static int hwcBlank(hwc_composer_device_1_t* hwc, int dpy, int blank) {
100 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
101 return hwc->methods->blank(hwc, dpy, blank);
102 } else {
103 if (blank) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700104 hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
Jesse Hallb685c542012-07-31 14:32:56 -0700105 return hwc0->set(hwc0, NULL, NULL, NULL);
106 } else {
107 // HWC 0.x turns the screen on at the next set()
108 return NO_ERROR;
109 }
110 }
111}
112
113static int hwcPrepare(hwc_composer_device_1_t* hwc,
114 size_t numDisplays, hwc_display_contents_1_t** displays) {
115 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
116 return hwc->prepare(hwc, numDisplays, displays);
117 } else {
Mathias Agopian1e260872012-08-08 18:35:12 -0700118 hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
119 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
Jesse Hallb685c542012-07-31 14:32:56 -0700120 // In the past, SurfaceFlinger would pass a NULL list when doing full
121 // OpenGL ES composition. I don't know what, if any, dependencies there
122 // are on this behavior, so I'm playing it safe and preserving it.
123 if (list0->numHwLayers == 0)
124 return hwc0->prepare(hwc0, NULL);
125 else
126 return hwc0->prepare(hwc0, list0);
127 }
128}
129
130static int hwcSet(hwc_composer_device_1_t* hwc, EGLDisplay dpy, EGLSurface sur,
131 size_t numDisplays, hwc_display_contents_1_t** displays) {
132 int err;
133 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
134 displays[0]->dpy = dpy;
135 displays[0]->sur = sur;
136 err = hwc->set(hwc, numDisplays, displays);
137 } else {
Mathias Agopian1e260872012-08-08 18:35:12 -0700138 hwc_composer_device_t* hwc0 = reinterpret_cast<hwc_composer_device_t*>(hwc);
139 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(displays[0]);
Jesse Hallb685c542012-07-31 14:32:56 -0700140 err = hwc0->set(hwc0, dpy, sur, list0);
141 }
142 return err;
143}
144
145static uint32_t& hwcFlags(hwc_composer_device_1_t* hwc,
146 hwc_display_contents_1_t* display) {
147 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
148 return display->flags;
149 } else {
Mathias Agopian1e260872012-08-08 18:35:12 -0700150 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
Jesse Hallb685c542012-07-31 14:32:56 -0700151 return list0->flags;
152 }
153}
154
155static size_t& hwcNumHwLayers(hwc_composer_device_1_t* hwc,
156 hwc_display_contents_1_t* display) {
157 if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) {
158 return display->numHwLayers;
159 } else {
Mathias Agopian1e260872012-08-08 18:35:12 -0700160 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(display);
Jesse Hallb685c542012-07-31 14:32:56 -0700161 return list0->numHwLayers;
162 }
163}
164
Mathias Agopiana350ff92010-08-10 17:14:02 -0700165// ---------------------------------------------------------------------------
166
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700167struct HWComposer::cb_context {
168 struct callbacks : public hwc_procs_t {
169 // these are here to facilitate the transition when adding
170 // new callbacks (an implementation can check for NULL before
171 // calling a new callback).
172 void (*zero[4])(void);
173 };
174 callbacks procs;
175 HWComposer* hwc;
176};
177
178// ---------------------------------------------------------------------------
179
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700180HWComposer::HWComposer(
181 const sp<SurfaceFlinger>& flinger,
Mathias Agopian8b736f12012-08-13 17:54:26 -0700182 EventHandler& handler,
183 framebuffer_device_t const* fbDev)
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700184 : mFlinger(flinger),
Jesse Hallb685c542012-07-31 14:32:56 -0700185 mModule(0), mHwc(0), mCapacity(0),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700186 mNumOVLayers(0), mNumFBLayers(0),
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700187 mCBContext(new cb_context),
Mathias Agopian888c8222012-08-04 21:10:38 -0700188 mEventHandler(handler), mRefreshPeriod(0),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700189 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700190{
Jesse Hallb685c542012-07-31 14:32:56 -0700191 for (size_t i = 0; i < MAX_DISPLAYS; i++)
192 mLists[i] = NULL;
193
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700194 char value[PROPERTY_VALUE_MAX];
195 property_get("debug.sf.no_hw_vsync", value, "0");
196 mDebugForceFakeVSync = atoi(value);
197
Mathias Agopian028508c2012-07-25 21:12:12 -0700198 bool needVSyncThread = true;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700199 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +0000200 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700201 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700202 err = hwc_open_1(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000203 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -0700204 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700205 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700206 if (HWC_REMOVE_DEPRECATED_VERSIONS &&
207 mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) {
208 ALOGE("%s device version %#x too old, will not be used",
209 HWC_HARDWARE_COMPOSER, mHwc->common.version);
210 hwc_close_1(mHwc);
211 mHwc = NULL;
212 }
213 }
214
215 if (mHwc) {
Jesse Hallb685c542012-07-31 14:32:56 -0700216 // always turn vsync off when we start
217 needVSyncThread = false;
218 if (hwcHasVsyncEvent(mHwc)) {
219 hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
Mathias Agopian888c8222012-08-04 21:10:38 -0700220
221 int period;
222 if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) {
223 mRefreshPeriod = nsecs_t(period);
224 }
Jesse Hallb685c542012-07-31 14:32:56 -0700225 } else {
226 needVSyncThread = true;
Mathias Agopian028508c2012-07-25 21:12:12 -0700227 }
Jesse Hallb685c542012-07-31 14:32:56 -0700228
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700229 if (mHwc->registerProcs) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700230 mCBContext->hwc = this;
231 mCBContext->procs.invalidate = &hook_invalidate;
232 mCBContext->procs.vsync = &hook_vsync;
233 mHwc->registerProcs(mHwc, &mCBContext->procs);
234 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700235 }
Jesse Hallb685c542012-07-31 14:32:56 -0700236
237 // create initial empty display contents for display 0
Mathias Agopian1e260872012-08-08 18:35:12 -0700238 createWorkList(MAIN, 0);
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700239 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700240 }
241
Mathias Agopian8b736f12012-08-13 17:54:26 -0700242
243 if (fbDev) {
244 if (mRefreshPeriod == 0) {
245 mRefreshPeriod = nsecs_t(1e9 / fbDev->fps);
246 ALOGW("getting VSYNC period from fb HAL: %lld", mRefreshPeriod);
Mathias Agopian888c8222012-08-04 21:10:38 -0700247 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700248 mDpiX = fbDev->xdpi;
249 mDpiY = fbDev->ydpi;
Mathias Agopian888c8222012-08-04 21:10:38 -0700250 }
251
252 if (mRefreshPeriod == 0) {
253 mRefreshPeriod = nsecs_t(1e9 / 60.0);
254 ALOGW("getting VSYNC period thin air: %lld", mRefreshPeriod);
255 }
256
Mathias Agopian3a778712012-04-09 14:16:47 -0700257 if (needVSyncThread) {
258 // we don't have VSYNC support, we need to fake it
259 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700260 }
261}
262
263HWComposer::~HWComposer() {
Jesse Hallb685c542012-07-31 14:32:56 -0700264 hwcEventControl(mHwc, 0, EVENT_VSYNC, 0);
265 for (size_t i = 0; i < MAX_DISPLAYS; i++)
266 free(mLists[i]);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700267 if (mVSyncThread != NULL) {
268 mVSyncThread->requestExitAndWait();
269 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700270 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700271 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700272 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700273 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700274}
275
276status_t HWComposer::initCheck() const {
277 return mHwc ? NO_ERROR : NO_INIT;
278}
279
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700280void HWComposer::hook_invalidate(struct hwc_procs* procs) {
281 reinterpret_cast<cb_context *>(procs)->hwc->invalidate();
282}
283
Mathias Agopian31d28432012-04-03 16:31:39 -0700284void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) {
285 reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp);
286}
287
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700288void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700289 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700290}
291
Mathias Agopian31d28432012-04-03 16:31:39 -0700292void HWComposer::vsync(int dpy, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700293 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700294 mEventHandler.onVSyncReceived(dpy, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700295 Mutex::Autolock _l(mLock);
296 mLastHwVSync = timestamp;
297}
298
Mathias Agopian888c8222012-08-04 21:10:38 -0700299nsecs_t HWComposer::getRefreshPeriod() const {
300 return mRefreshPeriod;
301}
302
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700303nsecs_t HWComposer::getRefreshTimestamp() const {
304 // this returns the last refresh timestamp.
305 // if the last one is not available, we estimate it based on
306 // the refresh period and whatever closest timestamp we have.
307 Mutex::Autolock _l(mLock);
308 nsecs_t now = systemTime(CLOCK_MONOTONIC);
309 return now - ((now - mLastHwVSync) % mRefreshPeriod);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700310}
311
Mathias Agopian8b736f12012-08-13 17:54:26 -0700312float HWComposer::getDpiX() const {
313 return mDpiX;
314}
315
316float HWComposer::getDpiY() const {
317 return mDpiY;
318}
319
Mathias Agopian03e40722012-04-26 16:11:59 -0700320void HWComposer::eventControl(int event, int enabled) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700321 status_t err = NO_ERROR;
Erik Gilling1a3bf412012-04-06 14:13:32 -0700322 if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700323 if (!mDebugForceFakeVSync) {
Jesse Hallb685c542012-07-31 14:32:56 -0700324 err = hwcEventControl(mHwc, 0, event, enabled);
Mathias Agopian03e40722012-04-26 16:11:59 -0700325 // error here should not happen -- not sure what we should
326 // do if it does.
327 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
328 event, enabled, strerror(-err));
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700329 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700330 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700331
332 if (err == NO_ERROR && mVSyncThread != NULL) {
333 mVSyncThread->setEnabled(enabled);
334 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700335}
336
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700337status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
338 // FIXME: handle multiple displays
Mathias Agopian1e260872012-08-08 18:35:12 -0700339 if (uint32_t(id) >= MAX_DISPLAYS)
340 return BAD_INDEX;
341
Mathias Agopian45721772010-08-12 15:03:26 -0700342 if (mHwc) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700343 // TODO: must handle multiple displays here
Jesse Hallb685c542012-07-31 14:32:56 -0700344 // mLists[0] is NULL only when this is called from the constructor
345 if (!mLists[0] || mCapacity < numLayers) {
346 free(mLists[0]);
Jesse Hall5880cc52012-06-05 23:40:32 -0700347 size_t size = sizeofHwcLayerList(mHwc, numLayers);
Jesse Hallb685c542012-07-31 14:32:56 -0700348 mLists[0] = (hwc_display_contents_1_t*)malloc(size);
Mathias Agopian45721772010-08-12 15:03:26 -0700349 mCapacity = numLayers;
350 }
Jesse Hallb685c542012-07-31 14:32:56 -0700351 hwcFlags(mHwc, mLists[0]) = HWC_GEOMETRY_CHANGED;
352 hwcNumHwLayers(mHwc, mLists[0]) = numLayers;
353 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
354 mLists[0]->flipFenceFd = -1;
355 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700356 }
357 return NO_ERROR;
358}
359
360status_t HWComposer::prepare() const {
Jesse Hallb685c542012-07-31 14:32:56 -0700361 int err = hwcPrepare(mHwc, 1,
362 const_cast<hwc_display_contents_1_t**>(mLists));
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700363 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700364
365 // here we're just making sure that "skip" layers are set
366 // to HWC_FRAMEBUFFER and we're also counting how many layers
367 // we have of each type.
368 // It would be nice if we could get rid of this entirely, which I
369 // think is almost possible.
370
371 // TODO: must handle multiple displays here
372
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700373 size_t numOVLayers = 0;
374 size_t numFBLayers = 0;
Mathias Agopian1e260872012-08-08 18:35:12 -0700375 size_t count = getNumLayers(0);
376
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700377 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700378 int compositionType;
Jesse Hall5880cc52012-06-05 23:40:32 -0700379 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700380 hwc_layer_1_t* l = &mLists[0]->hwLayers[i];
381 if (l->flags & HWC_SKIP_LAYER) {
382 l->compositionType = HWC_FRAMEBUFFER;
383 }
384 compositionType = l->compositionType;
Jesse Hall5880cc52012-06-05 23:40:32 -0700385 } else {
386 // mList really has hwc_layer_list_t memory layout
Mathias Agopian1e260872012-08-08 18:35:12 -0700387 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
388 hwc_layer_t* l = &list0->hwLayers[i];
389 if (l->flags & HWC_SKIP_LAYER) {
390 l->compositionType = HWC_FRAMEBUFFER;
391 }
392 compositionType = l->compositionType;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700393 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700394
395 switch (compositionType) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700396 case HWC_OVERLAY:
397 numOVLayers++;
398 break;
399 case HWC_FRAMEBUFFER:
400 numFBLayers++;
401 break;
402 }
403 }
404 mNumOVLayers = numOVLayers;
405 mNumFBLayers = numFBLayers;
406 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700407 return (status_t)err;
408}
409
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700410size_t HWComposer::getLayerCount(int32_t id, int type) const {
411 // FIXME: handle multiple displays
412 if (uint32_t(id) >= MAX_DISPLAYS) {
413 // FIXME: in practice this is only use to know
414 // if we have at least one layer of type.
415 return (type == HWC_FRAMEBUFFER) ? 1 : 0;
416 }
417
418
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700419 switch (type) {
420 case HWC_OVERLAY:
421 return mNumOVLayers;
422 case HWC_FRAMEBUFFER:
423 return mNumFBLayers;
424 }
425 return 0;
426}
427
Jesse Hall34a09ba2012-07-29 22:35:34 -0700428status_t HWComposer::commit(void* fbDisplay, void* fbSurface) const {
Mathias Agopian86303202012-07-24 22:46:10 -0700429 int err = NO_ERROR;
430 if (mHwc) {
Jesse Hallb685c542012-07-31 14:32:56 -0700431 err = hwcSet(mHwc, fbDisplay, fbSurface, 1,
432 const_cast<hwc_display_contents_1_t**>(mLists));
433 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
434 if (mLists[0]->flipFenceFd != -1) {
435 close(mLists[0]->flipFenceFd);
436 mLists[0]->flipFenceFd = -1;
437 }
Mathias Agopian86303202012-07-24 22:46:10 -0700438 }
Jesse Hallb685c542012-07-31 14:32:56 -0700439 hwcFlags(mHwc, mLists[0]) &= ~HWC_GEOMETRY_CHANGED;
Mathias Agopian58959342010-10-07 14:57:04 -0700440 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700441 return (status_t)err;
442}
443
Antti Hatalaf5f27122010-09-09 02:33:05 -0700444status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700445 if (mHwc) {
Jesse Hallb685c542012-07-31 14:32:56 -0700446 if (hwcHasVsyncEvent(mHwc)) {
447 hwcEventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
Mathias Agopian22ffb112012-04-10 21:04:02 -0700448 }
Jesse Hallb685c542012-07-31 14:32:56 -0700449 return (status_t)hwcBlank(mHwc, 0, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700450 }
451 return NO_ERROR;
452}
453
Colin Cross10fbdb62012-07-12 17:56:34 -0700454status_t HWComposer::acquire() const {
455 if (mHwc) {
Jesse Hallb685c542012-07-31 14:32:56 -0700456 return (status_t)hwcBlank(mHwc, 0, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700457 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700458 return NO_ERROR;
459}
460
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700461status_t HWComposer::disable() {
462 if (mHwc) {
Jesse Hallb685c542012-07-31 14:32:56 -0700463 hwcNumHwLayers(mHwc, mLists[0]) = 0;
464 int err = hwcPrepare(mHwc, 1, mLists);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700465 return (status_t)err;
466 }
467 return NO_ERROR;
Antti Hatalaf5f27122010-09-09 02:33:05 -0700468}
469
Mathias Agopian1e260872012-08-08 18:35:12 -0700470size_t HWComposer::getNumLayers(int32_t id) const { // FIXME: handle multiple displays
Jesse Hallb685c542012-07-31 14:32:56 -0700471 return mHwc ? hwcNumHwLayers(mHwc, mLists[0]) : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700472}
473
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700474/*
475 * Helper template to implement a concrete HWCLayer
476 * This holds the pointer to the concrete hwc layer type
477 * and implements the "iterable" side of HWCLayer.
478 */
479template<typename CONCRETE, typename HWCTYPE>
480class Iterable : public HWComposer::HWCLayer {
481protected:
482 HWCTYPE* const mLayerList;
483 HWCTYPE* mCurrentLayer;
484 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
485 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
486 inline HWCTYPE* getLayer() { return mCurrentLayer; }
487 virtual ~Iterable() { }
488private:
489 // returns a copy of ourselves
490 virtual HWComposer::HWCLayer* dup() {
491 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
492 }
493 virtual status_t setLayer(size_t index) {
494 mCurrentLayer = &mLayerList[index];
495 return NO_ERROR;
496 }
497};
498
Jesse Hall5880cc52012-06-05 23:40:32 -0700499// #if !HWC_REMOVE_DEPRECATED_VERSIONS
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700500/*
501 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3
502 * This implements the HWCLayer side of HWCIterableLayer.
503 */
Jesse Hall5880cc52012-06-05 23:40:32 -0700504class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700505public:
Jesse Hall5880cc52012-06-05 23:40:32 -0700506 HWCLayerVersion0(hwc_layer_t* layer)
507 : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700508
509 virtual int32_t getCompositionType() const {
510 return getLayer()->compositionType;
511 }
512 virtual uint32_t getHints() const {
513 return getLayer()->hints;
514 }
Jesse Hallef194142012-06-14 14:45:17 -0700515 virtual int getAndResetReleaseFenceFd() {
516 // not supported on VERSION_03
517 return -1;
518 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700519 virtual void setAcquireFenceFd(int fenceFd) {
520 if (fenceFd != -1) {
521 ALOGE("HWC 0.x can't handle acquire fences");
522 close(fenceFd);
523 }
524 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700525
526 virtual void setDefaultState() {
527 getLayer()->compositionType = HWC_FRAMEBUFFER;
528 getLayer()->hints = 0;
529 getLayer()->flags = HWC_SKIP_LAYER;
530 getLayer()->transform = 0;
531 getLayer()->blending = HWC_BLENDING_NONE;
532 getLayer()->visibleRegionScreen.numRects = 0;
533 getLayer()->visibleRegionScreen.rects = NULL;
534 }
535 virtual void setSkip(bool skip) {
536 if (skip) {
537 getLayer()->flags |= HWC_SKIP_LAYER;
538 } else {
539 getLayer()->flags &= ~HWC_SKIP_LAYER;
540 }
541 }
542 virtual void setBlending(uint32_t blending) {
543 getLayer()->blending = blending;
544 }
545 virtual void setTransform(uint32_t transform) {
546 getLayer()->transform = transform;
547 }
548 virtual void setFrame(const Rect& frame) {
549 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
550 }
551 virtual void setCrop(const Rect& crop) {
552 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
553 }
554 virtual void setVisibleRegionScreen(const Region& reg) {
555 getLayer()->visibleRegionScreen.rects =
556 reinterpret_cast<hwc_rect_t const *>(
557 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
558 }
559 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
560 if (buffer == 0 || buffer->handle == 0) {
561 getLayer()->compositionType = HWC_FRAMEBUFFER;
562 getLayer()->flags |= HWC_SKIP_LAYER;
563 getLayer()->handle = 0;
564 } else {
565 getLayer()->handle = buffer->handle;
566 }
567 }
568};
Jesse Hall5880cc52012-06-05 23:40:32 -0700569// #endif // !HWC_REMOVE_DEPRECATED_VERSIONS
570
571/*
572 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
573 * This implements the HWCLayer side of HWCIterableLayer.
574 */
575class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
576public:
577 HWCLayerVersion1(hwc_layer_1_t* layer)
578 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
579
580 virtual int32_t getCompositionType() const {
581 return getLayer()->compositionType;
582 }
583 virtual uint32_t getHints() const {
584 return getLayer()->hints;
585 }
Jesse Hallef194142012-06-14 14:45:17 -0700586 virtual int getAndResetReleaseFenceFd() {
587 int fd = getLayer()->releaseFenceFd;
588 getLayer()->releaseFenceFd = -1;
589 return fd;
590 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700591 virtual void setAcquireFenceFd(int fenceFd) {
592 getLayer()->acquireFenceFd = fenceFd;
593 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700594
595 virtual void setDefaultState() {
596 getLayer()->compositionType = HWC_FRAMEBUFFER;
597 getLayer()->hints = 0;
598 getLayer()->flags = HWC_SKIP_LAYER;
599 getLayer()->transform = 0;
600 getLayer()->blending = HWC_BLENDING_NONE;
601 getLayer()->visibleRegionScreen.numRects = 0;
602 getLayer()->visibleRegionScreen.rects = NULL;
603 getLayer()->acquireFenceFd = -1;
604 getLayer()->releaseFenceFd = -1;
605 }
606 virtual void setSkip(bool skip) {
607 if (skip) {
608 getLayer()->flags |= HWC_SKIP_LAYER;
609 } else {
610 getLayer()->flags &= ~HWC_SKIP_LAYER;
611 }
612 }
613 virtual void setBlending(uint32_t blending) {
614 getLayer()->blending = blending;
615 }
616 virtual void setTransform(uint32_t transform) {
617 getLayer()->transform = transform;
618 }
619 virtual void setFrame(const Rect& frame) {
620 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
621 }
622 virtual void setCrop(const Rect& crop) {
623 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
624 }
625 virtual void setVisibleRegionScreen(const Region& reg) {
626 getLayer()->visibleRegionScreen.rects =
627 reinterpret_cast<hwc_rect_t const *>(
628 reg.getArray(&getLayer()->visibleRegionScreen.numRects));
629 }
630 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
631 if (buffer == 0 || buffer->handle == 0) {
632 getLayer()->compositionType = HWC_FRAMEBUFFER;
633 getLayer()->flags |= HWC_SKIP_LAYER;
634 getLayer()->handle = 0;
635 } else {
636 getLayer()->handle = buffer->handle;
637 }
638 }
639};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700640
641/*
642 * returns an iterator initialized at a given index in the layer list
643 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700644HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
645 // FIXME: handle multiple displays
646 if (uint32_t(id) >= MAX_DISPLAYS)
647 return LayerListIterator();
648
Mathias Agopian748f3df2012-08-10 14:36:40 -0700649 if (!mHwc || index > hwcNumHwLayers(mHwc, mLists[0]))
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700650 return LayerListIterator();
Jesse Hall5880cc52012-06-05 23:40:32 -0700651 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
Jesse Hallb685c542012-07-31 14:32:56 -0700652 return LayerListIterator(new HWCLayerVersion1(mLists[0]->hwLayers),
653 index);
Jesse Hall5880cc52012-06-05 23:40:32 -0700654 } else {
Mathias Agopian1e260872012-08-08 18:35:12 -0700655 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
Jesse Hall5880cc52012-06-05 23:40:32 -0700656 return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index);
657 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700658}
659
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700660/*
661 * returns an iterator on the beginning of the layer list
662 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700663HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700664 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700665}
666
667/*
668 * returns an iterator on the end of the layer list
669 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700670HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700671 return getLayerIterator(id, getNumLayers(id));
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700672}
673
Mathias Agopian22da60c2011-09-09 00:49:11 -0700674void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
675 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700676 if (mHwc) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700677 hwc_layer_list_t* list0 = reinterpret_cast<hwc_layer_list_t*>(mLists[0]);
Jesse Hallb685c542012-07-31 14:32:56 -0700678
Mathias Agopian83727852010-09-23 18:13:21 -0700679 result.append("Hardware Composer state:\n");
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700680 result.appendFormat(" mDebugForceFakeVSync=%d\n",
681 mDebugForceFakeVSync);
682 result.appendFormat(" numHwLayers=%u, flags=%08x\n",
Jesse Hallb685c542012-07-31 14:32:56 -0700683 hwcNumHwLayers(mHwc, mLists[0]), hwcFlags(mHwc, mLists[0]));
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700684 result.append(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700685 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
686 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
687 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Jesse Hallb685c542012-07-31 14:32:56 -0700688 for (size_t i=0 ; i<hwcNumHwLayers(mHwc, mLists[0]) ; i++) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700689 hwc_layer_1_t const* lp;
Jesse Hall5880cc52012-06-05 23:40:32 -0700690 if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700691 lp = &mLists[0]->hwLayers[i];
Jesse Hall5880cc52012-06-05 23:40:32 -0700692 } else {
Mathias Agopian1e260872012-08-08 18:35:12 -0700693 // FIXME: here we rely on hwc_layer_1_t and hwc_layer_t having the same layout
694 lp = reinterpret_cast<hwc_layer_1_t const*>(&list0->hwLayers[i]);
Jesse Hall5880cc52012-06-05 23:40:32 -0700695 }
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700696 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
697 int32_t format = -1;
698 if (layer->getLayer() != NULL) {
699 const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer());
700 if (buffer != NULL) {
701 format = buffer->getPixelFormat();
702 }
703 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700704 const hwc_layer_1_t& l(*lp);
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700705 result.appendFormat(
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700706 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
Mathias Agopian83727852010-09-23 18:13:21 -0700707 l.compositionType ? "OVERLAY" : "FB",
Mathias Agopianaebac5f2011-09-29 18:07:08 -0700708 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
Mathias Agopian83727852010-09-23 18:13:21 -0700709 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
Mathias Agopian22da60c2011-09-09 00:49:11 -0700710 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700711 layer->getName().string());
Mathias Agopian83727852010-09-23 18:13:21 -0700712 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800713 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700714 if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) {
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800715 mHwc->dump(mHwc, buffer, SIZE);
716 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700717 }
718}
719
Mathias Agopiana350ff92010-08-10 17:14:02 -0700720// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700721
722HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
723 : mHwc(hwc), mEnabled(false),
724 mNextFakeVSync(0),
725 mRefreshPeriod(hwc.mRefreshPeriod)
726{
727}
728
729void HWComposer::VSyncThread::setEnabled(bool enabled) {
730 Mutex::Autolock _l(mLock);
731 mEnabled = enabled;
732 mCondition.signal();
733}
734
735void HWComposer::VSyncThread::onFirstRef() {
736 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
737}
738
739bool HWComposer::VSyncThread::threadLoop() {
740 { // scope for lock
741 Mutex::Autolock _l(mLock);
742 while (!mEnabled) {
743 mCondition.wait(mLock);
744 }
745 }
746
747 const nsecs_t period = mRefreshPeriod;
748 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
749 nsecs_t next_vsync = mNextFakeVSync;
750 nsecs_t sleep = next_vsync - now;
751 if (sleep < 0) {
752 // we missed, find where the next vsync should be
753 sleep = (period - ((now - next_vsync) % period));
754 next_vsync = now + sleep;
755 }
756 mNextFakeVSync = next_vsync + period;
757
758 struct timespec spec;
759 spec.tv_sec = next_vsync / 1000000000;
760 spec.tv_nsec = next_vsync % 1000000000;
761
762 int err;
763 do {
764 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
765 } while (err<0 && errno == EINTR);
766
767 if (err == 0) {
768 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
769 }
770
771 return true;
772}
773
774// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700775}; // namespace android