blob: 113c26ca350196e94d555113c5842ec74389e918 [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 Hall9eb1eb52012-08-29 10:39:38 -070049#define MIN_HWC_HEADER_VERSION 0
50
51static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
52 uint32_t hwcVersion = hwc->common.version;
53 if (MIN_HWC_HEADER_VERSION == 0 &&
54 (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
55 // legacy version encoding
56 hwcVersion <<= 16;
57 }
58 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
59}
60
61static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
62 uint32_t hwcVersion = hwc->common.version;
63 if (MIN_HWC_HEADER_VERSION == 0 &&
64 (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
65 // legacy version encoding
66 hwcVersion <<= 16;
67 }
68 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
69}
70
71static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
72 uint32_t version) {
73 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070074}
75
Mathias Agopiana350ff92010-08-10 17:14:02 -070076// ---------------------------------------------------------------------------
77
Mathias Agopian3e8b8532012-05-13 20:42:01 -070078struct HWComposer::cb_context {
79 struct callbacks : public hwc_procs_t {
80 // these are here to facilitate the transition when adding
81 // new callbacks (an implementation can check for NULL before
82 // calling a new callback).
83 void (*zero[4])(void);
84 };
85 callbacks procs;
86 HWComposer* hwc;
87};
88
89// ---------------------------------------------------------------------------
90
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070091HWComposer::HWComposer(
92 const sp<SurfaceFlinger>& flinger,
Mathias Agopian8b736f12012-08-13 17:54:26 -070093 EventHandler& handler,
94 framebuffer_device_t const* fbDev)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070095 : mFlinger(flinger),
Mathias Agopianf4358632012-08-22 17:16:19 -070096 mModule(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070097 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070098 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070099 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700100{
Mathias Agopiane60b0682012-08-21 23:34:09 -0700101 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
102 mLists[i] = 0;
103 }
Jesse Hallb685c542012-07-31 14:32:56 -0700104
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.no_hw_vsync", value, "0");
107 mDebugForceFakeVSync = atoi(value);
108
Mathias Agopian028508c2012-07-25 21:12:12 -0700109 bool needVSyncThread = true;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700110 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule);
Steve Block32397c12012-01-05 23:22:43 +0000111 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700112 if (err == 0) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700113 err = hwc_open_1(mModule, &mHwc);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000114 ALOGE_IF(err, "%s device failed to initialize (%s)",
Mathias Agopiana350ff92010-08-10 17:14:02 -0700115 HWC_HARDWARE_COMPOSER, strerror(-err));
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700116 if (err == 0) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700117 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
118 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
119 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
120 ALOGE("%s device version %#x unsupported, will not be used",
Jesse Hall5880cc52012-06-05 23:40:32 -0700121 HWC_HARDWARE_COMPOSER, mHwc->common.version);
122 hwc_close_1(mHwc);
123 mHwc = NULL;
124 }
125 }
126
127 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700128 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
129 (hwcApiVersion(mHwc) >> 24) & 0xff,
130 (hwcApiVersion(mHwc) >> 16) & 0xff);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700131 if (mHwc->registerProcs) {
132 mCBContext->hwc = this;
133 mCBContext->procs.invalidate = &hook_invalidate;
134 mCBContext->procs.vsync = &hook_vsync;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700135 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
136 mCBContext->procs.hotplug = &hook_hotplug;
137 else
138 mCBContext->procs.hotplug = NULL;
Jesse Hallbbd164a2012-08-21 12:05:09 -0700139 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
140 mHwc->registerProcs(mHwc, &mCBContext->procs);
141 }
142
Jesse Hallb685c542012-07-31 14:32:56 -0700143 // always turn vsync off when we start
144 needVSyncThread = false;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700145 mHwc->eventControl(mHwc, HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallb685c542012-07-31 14:32:56 -0700146
Mathias Agopianf4358632012-08-22 17:16:19 -0700147 // these IDs are always reserved
148 for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
149 mAllocatedDisplayIDs.markBit(i);
Mathias Agopianf4358632012-08-22 17:16:19 -0700150 }
Jesse Hall8f971ff2012-08-22 11:50:00 -0700151
Mathias Agopianf4358632012-08-22 17:16:19 -0700152 // the number of displays we actually have depends on the
153 // hw composer version
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700154 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700155 // 1.2 adds support for virtual displays
156 mNumDisplays = MAX_DISPLAYS;
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700157 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
158 // 1.1 adds support for multiple displays
159 mNumDisplays = HWC_NUM_DISPLAY_TYPES;
160 } else {
161 mNumDisplays = 1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700162 }
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700163 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700164 }
165
Mathias Agopian8b736f12012-08-13 17:54:26 -0700166 if (fbDev) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700167 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
168 "should only have fbdev if no hwc or hwc is 1.0");
169
Mathias Agopianf4358632012-08-22 17:16:19 -0700170 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
171 disp.xdpi = fbDev->xdpi;
172 disp.ydpi = fbDev->ydpi;
173 if (disp.refresh == 0) {
174 disp.refresh = nsecs_t(1e9 / fbDev->fps);
175 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700176 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700177 if (disp.refresh == 0) {
178 disp.refresh = nsecs_t(1e9 / 60.0);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700179 ALOGW("getting VSYNC period from thin air: %lld",
180 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopianf4358632012-08-22 17:16:19 -0700181 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700182 } else if (mHwc) {
183 queryDisplayProperties(HWC_DISPLAY_PRIMARY);
Mathias Agopian888c8222012-08-04 21:10:38 -0700184 }
185
Mathias Agopian3a778712012-04-09 14:16:47 -0700186 if (needVSyncThread) {
187 // we don't have VSYNC support, we need to fake it
188 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700189 }
190}
191
192HWComposer::~HWComposer() {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700193 mHwc->eventControl(mHwc, 0, EVENT_VSYNC, 0);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700194 if (mVSyncThread != NULL) {
195 mVSyncThread->requestExitAndWait();
196 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700197 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700198 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700199 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700200 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700201}
202
203status_t HWComposer::initCheck() const {
204 return mHwc ? NO_ERROR : NO_INIT;
205}
206
Jesse Hallbbd164a2012-08-21 12:05:09 -0700207void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
208 cb_context* ctx = reinterpret_cast<cb_context*>(
209 const_cast<hwc_procs_t*>(procs));
210 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700211}
212
Jesse Hall1bd20e02012-08-29 10:47:52 -0700213void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700214 int64_t timestamp) {
215 cb_context* ctx = reinterpret_cast<cb_context*>(
216 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700217 ctx->hwc->vsync(disp, timestamp);
218}
219
220void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
221 int connected) {
222 cb_context* ctx = reinterpret_cast<cb_context*>(
223 const_cast<hwc_procs_t*>(procs));
224 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700225}
226
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700227void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700228 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700229}
230
Jesse Hall1bd20e02012-08-29 10:47:52 -0700231void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700232 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700233 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700234 Mutex::Autolock _l(mLock);
235 mLastHwVSync = timestamp;
236}
237
Jesse Hall1bd20e02012-08-29 10:47:52 -0700238void HWComposer::hotplug(int disp, int connected) {
239 if (disp == HWC_DISPLAY_PRIMARY || disp >= HWC_NUM_DISPLAY_TYPES) {
240 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
241 disp, connected);
242 return;
243 }
244
245 if (connected)
246 queryDisplayProperties(disp);
247
248 // TODO: tell someone else about this
249}
250
251static const uint32_t DISPLAY_ATTRIBUTES[] = {
252 HWC_DISPLAY_VSYNC_PERIOD,
253 HWC_DISPLAY_RESOLUTION_X,
254 HWC_DISPLAY_RESOLUTION_Y,
255 HWC_DISPLAY_DPI_X,
256 HWC_DISPLAY_DPI_Y,
257 HWC_DISPLAY_NO_ATTRIBUTE,
258};
259#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
260
261// http://developer.android.com/reference/android/util/DisplayMetrics.html
262#define ANDROID_DENSITY_TV 213
263#define ANDROID_DENSITY_XHIGH 320
264
265void HWComposer::queryDisplayProperties(int disp) {
266 ALOG_ASSERT(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
267
268 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
269 memset(values, 0, sizeof(values));
270
271 uint32_t config;
272 size_t numConfigs = 1;
273 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
274 if (err == NO_ERROR) {
275 mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES,
276 values);
277 }
278
279 int32_t w = 0, h = 0;
280 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
281 switch (DISPLAY_ATTRIBUTES[i]) {
282 case HWC_DISPLAY_VSYNC_PERIOD:
283 mDisplayData[disp].refresh = nsecs_t(values[i]);
284 break;
285 case HWC_DISPLAY_RESOLUTION_X:
286 // TODO: we'll probably want to remember this eventually
287 w = values[i];
288 break;
289 case HWC_DISPLAY_RESOLUTION_Y:
290 // TODO: we'll probably want to remember this eventually
291 h = values[i];
292 break;
293 case HWC_DISPLAY_DPI_X:
294 mDisplayData[disp].xdpi = values[i] / 1000.0f;
295 break;
296 case HWC_DISPLAY_DPI_Y:
297 mDisplayData[disp].ydpi = values[i] / 1000.0f;
298 break;
299 default:
300 ALOG_ASSERT(false, "unknown display attribute %#x",
301 DISPLAY_ATTRIBUTES[i]);
302 break;
303 }
304 }
305
306 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
307 // is there anything smarter we can do?
308 if (h >= 1080) {
309 mDisplayData[disp].xdpi = ANDROID_DENSITY_XHIGH;
310 mDisplayData[disp].ydpi = ANDROID_DENSITY_XHIGH;
311 } else {
312 mDisplayData[disp].xdpi = ANDROID_DENSITY_TV;
313 mDisplayData[disp].ydpi = ANDROID_DENSITY_TV;
314 }
315 }
316}
317
Mathias Agopiane60b0682012-08-21 23:34:09 -0700318int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700319 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700320 return NO_MEMORY;
321 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700322 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
323 mAllocatedDisplayIDs.markBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700324 return id;
325}
326
327status_t HWComposer::freeDisplayId(int32_t id) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700328 if (id < HWC_NUM_DISPLAY_TYPES) {
329 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700330 return BAD_VALUE;
331 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700332 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700333 return BAD_INDEX;
334 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700335 mAllocatedDisplayIDs.clearBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700336 return NO_ERROR;
337}
338
Mathias Agopian888c8222012-08-04 21:10:38 -0700339nsecs_t HWComposer::getRefreshPeriod() const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700340 return mDisplayData[HWC_DISPLAY_PRIMARY].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700341}
342
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700343nsecs_t HWComposer::getRefreshTimestamp() const {
344 // this returns the last refresh timestamp.
345 // if the last one is not available, we estimate it based on
346 // the refresh period and whatever closest timestamp we have.
347 Mutex::Autolock _l(mLock);
348 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Mathias Agopianf4358632012-08-22 17:16:19 -0700349 return now - ((now - mLastHwVSync) % mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700350}
351
Mathias Agopian8b736f12012-08-13 17:54:26 -0700352float HWComposer::getDpiX() const {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700353 return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700354}
355
356float HWComposer::getDpiY() const {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700357 return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700358}
359
Mathias Agopian03e40722012-04-26 16:11:59 -0700360void HWComposer::eventControl(int event, int enabled) {
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700361 status_t err = NO_ERROR;
Mathias Agopian30bcc612012-08-22 15:39:48 -0700362 if (mHwc) {
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700363 if (!mDebugForceFakeVSync) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700364 err = mHwc->eventControl(mHwc, 0, event, enabled);
Mathias Agopian03e40722012-04-26 16:11:59 -0700365 // error here should not happen -- not sure what we should
366 // do if it does.
367 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
368 event, enabled, strerror(-err));
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700369 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700370 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700371
372 if (err == NO_ERROR && mVSyncThread != NULL) {
373 mVSyncThread->setEnabled(enabled);
374 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700375}
376
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700377status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700378 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700379 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700380 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700381
Mathias Agopian45721772010-08-12 15:03:26 -0700382 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700383 DisplayData& disp(mDisplayData[id]);
Andy McFadden13a082e2012-08-24 10:16:42 -0700384 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700385 const size_t size = sizeof(hwc_display_contents_1_t)
386 + numLayers * sizeof(hwc_layer_1_t);
387 free(disp.list);
388 disp.list = (hwc_display_contents_1_t*)malloc(size);
389 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700390 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700391 disp.list->flags = HWC_GEOMETRY_CHANGED;
392 disp.list->numHwLayers = numLayers;
393 disp.list->flipFenceFd = -1;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700394 }
395 return NO_ERROR;
396}
397
Mathias Agopiane60b0682012-08-21 23:34:09 -0700398status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700399 for (size_t i=0 ; i<mNumDisplays ; i++) {
400 mLists[i] = mDisplayData[i].list;
401 if (mLists[i]) {
402 mLists[i]->dpy = EGL_NO_DISPLAY;
403 mLists[i]->sur = EGL_NO_SURFACE;
404 }
405 }
406 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700407 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700408 // here we're just making sure that "skip" layers are set
409 // to HWC_FRAMEBUFFER and we're also counting how many layers
410 // we have of each type.
Mathias Agopianf4358632012-08-22 17:16:19 -0700411 for (size_t i=0 ; i<mNumDisplays ; i++) {
412 DisplayData& disp(mDisplayData[i]);
413 disp.hasFbComp = false;
414 disp.hasOvComp = false;
415 if (disp.list) {
416 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
417 hwc_layer_1_t& l = disp.list->hwLayers[i];
418 if (l.flags & HWC_SKIP_LAYER) {
419 l.compositionType = HWC_FRAMEBUFFER;
420 }
421 if (l.compositionType == HWC_FRAMEBUFFER) {
422 disp.hasFbComp = true;
423 }
424 if (l.compositionType == HWC_OVERLAY) {
425 disp.hasOvComp = true;
426 }
427 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700428 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700429 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700430 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700431 return (status_t)err;
432}
433
Mathias Agopiane60b0682012-08-21 23:34:09 -0700434bool HWComposer::hasHwcComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700435 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700436 return false;
437 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700438}
439
Mathias Agopiane60b0682012-08-21 23:34:09 -0700440bool HWComposer::hasGlesComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700441 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700442 return false;
443 return mDisplayData[id].hasFbComp;
444}
445
Mathias Agopian30bcc612012-08-22 15:39:48 -0700446status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700447 int err = NO_ERROR;
448 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700449 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700450 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700451 // by the (dpy, sur) fields and we are guaranteed to have only
452 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700453 mLists[0]->dpy = eglGetCurrentDisplay();
454 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700455 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700456
Mathias Agopian30bcc612012-08-22 15:39:48 -0700457 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700458
459 for (size_t i=0 ; i<mNumDisplays ; i++) {
460 DisplayData& disp(mDisplayData[i]);
461 if (disp.list) {
462 if (disp.list->flipFenceFd != -1) {
463 close(disp.list->flipFenceFd);
464 disp.list->flipFenceFd = -1;
465 }
466 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
467 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700468 }
Mathias Agopian58959342010-10-07 14:57:04 -0700469 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700470 return (status_t)err;
471}
472
Antti Hatalaf5f27122010-09-09 02:33:05 -0700473status_t HWComposer::release() const {
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700474 if (mHwc) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700475 mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0);
476 return (status_t)mHwc->blank(mHwc, 0, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700477 }
478 return NO_ERROR;
479}
480
Colin Cross10fbdb62012-07-12 17:56:34 -0700481status_t HWComposer::acquire() const {
482 if (mHwc) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700483 return (status_t)mHwc->blank(mHwc, 0, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700484 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700485 return NO_ERROR;
486}
487
Mathias Agopianf4358632012-08-22 17:16:19 -0700488size_t HWComposer::getNumLayers(int32_t id) const {
489 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
490 return 0;
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700491 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700492 return (mHwc && mDisplayData[id].list) ?
493 mDisplayData[id].list->numHwLayers : 0;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700494}
495
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700496/*
497 * Helper template to implement a concrete HWCLayer
498 * This holds the pointer to the concrete hwc layer type
499 * and implements the "iterable" side of HWCLayer.
500 */
501template<typename CONCRETE, typename HWCTYPE>
502class Iterable : public HWComposer::HWCLayer {
503protected:
504 HWCTYPE* const mLayerList;
505 HWCTYPE* mCurrentLayer;
506 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
507 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
508 inline HWCTYPE* getLayer() { return mCurrentLayer; }
509 virtual ~Iterable() { }
510private:
511 // returns a copy of ourselves
512 virtual HWComposer::HWCLayer* dup() {
513 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
514 }
515 virtual status_t setLayer(size_t index) {
516 mCurrentLayer = &mLayerList[index];
517 return NO_ERROR;
518 }
519};
520
Jesse Hall5880cc52012-06-05 23:40:32 -0700521/*
522 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
523 * This implements the HWCLayer side of HWCIterableLayer.
524 */
525class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
526public:
527 HWCLayerVersion1(hwc_layer_1_t* layer)
528 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
529
530 virtual int32_t getCompositionType() const {
531 return getLayer()->compositionType;
532 }
533 virtual uint32_t getHints() const {
534 return getLayer()->hints;
535 }
Jesse Hallef194142012-06-14 14:45:17 -0700536 virtual int getAndResetReleaseFenceFd() {
537 int fd = getLayer()->releaseFenceFd;
538 getLayer()->releaseFenceFd = -1;
539 return fd;
540 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700541 virtual void setAcquireFenceFd(int fenceFd) {
542 getLayer()->acquireFenceFd = fenceFd;
543 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700544
545 virtual void setDefaultState() {
546 getLayer()->compositionType = HWC_FRAMEBUFFER;
547 getLayer()->hints = 0;
548 getLayer()->flags = HWC_SKIP_LAYER;
549 getLayer()->transform = 0;
550 getLayer()->blending = HWC_BLENDING_NONE;
551 getLayer()->visibleRegionScreen.numRects = 0;
552 getLayer()->visibleRegionScreen.rects = NULL;
553 getLayer()->acquireFenceFd = -1;
554 getLayer()->releaseFenceFd = -1;
555 }
556 virtual void setSkip(bool skip) {
557 if (skip) {
558 getLayer()->flags |= HWC_SKIP_LAYER;
559 } else {
560 getLayer()->flags &= ~HWC_SKIP_LAYER;
561 }
562 }
563 virtual void setBlending(uint32_t blending) {
564 getLayer()->blending = blending;
565 }
566 virtual void setTransform(uint32_t transform) {
567 getLayer()->transform = transform;
568 }
569 virtual void setFrame(const Rect& frame) {
570 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
571 }
572 virtual void setCrop(const Rect& crop) {
573 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
574 }
575 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700576 // Region::getSharedBuffer creates a reference to the underlying
577 // SharedBuffer of this Region, this reference is freed
578 // in onDisplayed()
579 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
580 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
581 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700582 }
583 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
584 if (buffer == 0 || buffer->handle == 0) {
585 getLayer()->compositionType = HWC_FRAMEBUFFER;
586 getLayer()->flags |= HWC_SKIP_LAYER;
587 getLayer()->handle = 0;
588 } else {
589 getLayer()->handle = buffer->handle;
590 }
591 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700592 virtual void onDisplayed() {
593 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
594 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
595 if (sb) {
596 sb->release();
597 // not technically needed but safer
598 visibleRegion.numRects = 0;
599 visibleRegion.rects = NULL;
600 }
Jesse Halle25d0052012-09-05 13:03:10 -0700601
602 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700603 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700604};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700605
606/*
607 * returns an iterator initialized at a given index in the layer list
608 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700609HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700610 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700611 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700612 }
613 const DisplayData& disp(mDisplayData[id]);
614 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700615 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700616 }
617 return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700618}
619
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700620/*
621 * returns an iterator on the beginning of the layer list
622 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700623HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700624 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700625}
626
627/*
628 * returns an iterator on the end of the layer list
629 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700630HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700631 return getLayerIterator(id, getNumLayers(id));
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700632}
633
Mathias Agopian22da60c2011-09-09 00:49:11 -0700634void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
635 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700636 if (mHwc) {
Mathias Agopian83727852010-09-23 18:13:21 -0700637 result.append("Hardware Composer state:\n");
Mathias Agopianf4358632012-08-22 17:16:19 -0700638 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
639 for (size_t i=0 ; i<mNumDisplays ; i++) {
640 const DisplayData& disp(mDisplayData[i]);
641 if (disp.list) {
642 result.appendFormat(" id=%d, numHwLayers=%u, flags=%08x\n",
643 i, disp.list->numHwLayers, disp.list->flags);
644 result.append(
645 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
646 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
647 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
648 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
649 const hwc_layer_1_t&l = disp.list->hwLayers[i];
650 const sp<LayerBase> layer(visibleLayersSortedByZ[i]);
651 int32_t format = -1;
652 if (layer->getLayer() != NULL) {
653 const sp<GraphicBuffer>& buffer(
654 layer->getLayer()->getActiveBuffer());
655 if (buffer != NULL) {
656 format = buffer->getPixelFormat();
657 }
658 }
659 result.appendFormat(
660 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
661 l.compositionType ? "OVERLAY" : "FB",
662 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
663 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
664 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
665 layer->getName().string());
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700666 }
667 }
Mathias Agopian83727852010-09-23 18:13:21 -0700668 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800669 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700670
671 if (mHwc && mHwc->dump) {
672 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800673 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700674 }
675}
676
Mathias Agopiana350ff92010-08-10 17:14:02 -0700677// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700678
679HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
680 : mHwc(hwc), mEnabled(false),
681 mNextFakeVSync(0),
Mathias Agopiane60b0682012-08-21 23:34:09 -0700682 mRefreshPeriod(hwc.getRefreshPeriod())
Mathias Agopian2965b262012-04-08 15:13:32 -0700683{
684}
685
686void HWComposer::VSyncThread::setEnabled(bool enabled) {
687 Mutex::Autolock _l(mLock);
688 mEnabled = enabled;
689 mCondition.signal();
690}
691
692void HWComposer::VSyncThread::onFirstRef() {
693 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
694}
695
696bool HWComposer::VSyncThread::threadLoop() {
697 { // scope for lock
698 Mutex::Autolock _l(mLock);
699 while (!mEnabled) {
700 mCondition.wait(mLock);
701 }
702 }
703
704 const nsecs_t period = mRefreshPeriod;
705 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
706 nsecs_t next_vsync = mNextFakeVSync;
707 nsecs_t sleep = next_vsync - now;
708 if (sleep < 0) {
709 // we missed, find where the next vsync should be
710 sleep = (period - ((now - next_vsync) % period));
711 next_vsync = now + sleep;
712 }
713 mNextFakeVSync = next_vsync + period;
714
715 struct timespec spec;
716 spec.tv_sec = next_vsync / 1000000000;
717 spec.tv_nsec = next_vsync % 1000000000;
718
719 int err;
720 do {
721 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
722 } while (err<0 && errno == EINTR);
723
724 if (err == 0) {
725 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
726 }
727
728 return true;
729}
730
731// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -0700732}; // namespace android