blob: 597e5d63058e72e205df8cc354820d24f5a1ccc6 [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 Agopianda27af92012-09-13 18:17:13 -070029#include <utils/misc.h>
Mathias Agopian83727852010-09-23 18:13:21 -070030#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070031#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070032#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070033#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070034
Mathias Agopian921e6ac2012-07-23 23:11:29 -070035#include <ui/GraphicBuffer.h>
36
Mathias Agopiana350ff92010-08-10 17:14:02 -070037#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070038#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070039
40#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070041#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070042
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043#include "Layer.h" // needed only for debugging
Mathias Agopian22da60c2011-09-09 00:49:11 -070044#include "LayerBase.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070045#include "HWComposer.h"
Mathias Agopianc7d14e22011-08-01 16:32:21 -070046#include "SurfaceFlinger.h"
Mathias Agopianda27af92012-09-13 18:17:13 -070047#include <utils/CallStack.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070048
49namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070050
Jesse Hall9eb1eb52012-08-29 10:39:38 -070051#define MIN_HWC_HEADER_VERSION 0
52
53static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
54 uint32_t hwcVersion = hwc->common.version;
55 if (MIN_HWC_HEADER_VERSION == 0 &&
56 (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
57 // legacy version encoding
58 hwcVersion <<= 16;
59 }
60 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
61}
62
63static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
64 uint32_t hwcVersion = hwc->common.version;
65 if (MIN_HWC_HEADER_VERSION == 0 &&
66 (hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK) == 0) {
67 // legacy version encoding
68 hwcVersion <<= 16;
69 }
70 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
71}
72
73static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
74 uint32_t version) {
75 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070076}
77
Mathias Agopiana350ff92010-08-10 17:14:02 -070078// ---------------------------------------------------------------------------
79
Mathias Agopian3e8b8532012-05-13 20:42:01 -070080struct HWComposer::cb_context {
81 struct callbacks : public hwc_procs_t {
82 // these are here to facilitate the transition when adding
83 // new callbacks (an implementation can check for NULL before
84 // calling a new callback).
85 void (*zero[4])(void);
86 };
87 callbacks procs;
88 HWComposer* hwc;
89};
90
91// ---------------------------------------------------------------------------
92
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070093HWComposer::HWComposer(
94 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070095 EventHandler& handler)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070096 : mFlinger(flinger),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070097 mFbDev(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070098 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070099 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700100 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700101{
Mathias Agopiane60b0682012-08-21 23:34:09 -0700102 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
103 mLists[i] = 0;
104 }
Jesse Hallb685c542012-07-31 14:32:56 -0700105
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700106 char value[PROPERTY_VALUE_MAX];
107 property_get("debug.sf.no_hw_vsync", value, "0");
108 mDebugForceFakeVSync = atoi(value);
109
Mathias Agopian028508c2012-07-25 21:12:12 -0700110 bool needVSyncThread = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700111
112 // Note: some devices may insist that the FB HAL be opened before HWC.
113 loadFbHalModule();
114 loadHwcModule();
115
Mathias Agopianda27af92012-09-13 18:17:13 -0700116 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
117 // close FB HAL if we don't needed it.
118 // FIXME: this is temporary until we're not forced to open FB HAL
119 // before HWC.
120 framebuffer_close(mFbDev);
121 mFbDev = NULL;
122 }
123
Andy McFaddenbabba182012-09-12 13:14:51 -0700124 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
125 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
126 && !mFbDev) {
Andy McFadden43601a22012-09-11 15:15:13 -0700127 ALOGE("ERROR: failed to open framebuffer, aborting");
Andy McFadden43601a22012-09-11 15:15:13 -0700128 abort();
129 }
130
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700131 if (mHwc) {
132 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
133 (hwcApiVersion(mHwc) >> 24) & 0xff,
134 (hwcApiVersion(mHwc) >> 16) & 0xff);
135 if (mHwc->registerProcs) {
136 mCBContext->hwc = this;
137 mCBContext->procs.invalidate = &hook_invalidate;
138 mCBContext->procs.vsync = &hook_vsync;
139 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
140 mCBContext->procs.hotplug = &hook_hotplug;
141 else
142 mCBContext->procs.hotplug = NULL;
143 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
144 mHwc->registerProcs(mHwc, &mCBContext->procs);
Jesse Hall5880cc52012-06-05 23:40:32 -0700145 }
146
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700147 // don't need a vsync thread if we have a hardware composer
148 needVSyncThread = false;
149 // always turn vsync off when we start
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700150 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700151
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700152 // these IDs are always reserved
153 for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
154 mAllocatedDisplayIDs.markBit(i);
155 }
Jesse Hallb685c542012-07-31 14:32:56 -0700156
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700157 // the number of displays we actually have depends on the
158 // hw composer version
159 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
160 // 1.2 adds support for virtual displays
161 mNumDisplays = MAX_DISPLAYS;
162 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
163 // 1.1 adds support for multiple displays
164 mNumDisplays = HWC_NUM_DISPLAY_TYPES;
165 } else {
166 mNumDisplays = 1;
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700167 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700168 }
169
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700170 if (mFbDev) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700171 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
172 "should only have fbdev if no hwc or hwc is 1.0");
173
Mathias Agopianf4358632012-08-22 17:16:19 -0700174 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
Mathias Agopian38e623b2012-09-20 19:27:07 -0700175 disp.connected = true;
Jesse Halldb276212012-09-07 11:20:56 -0700176 disp.width = mFbDev->width;
177 disp.height = mFbDev->height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700178 disp.format = mFbDev->format;
179 disp.xdpi = mFbDev->xdpi;
180 disp.ydpi = mFbDev->ydpi;
Mathias Agopianf4358632012-08-22 17:16:19 -0700181 if (disp.refresh == 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700182 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
Mathias Agopianf4358632012-08-22 17:16:19 -0700183 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700184 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700185 if (disp.refresh == 0) {
186 disp.refresh = nsecs_t(1e9 / 60.0);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700187 ALOGW("getting VSYNC period from thin air: %lld",
188 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopianf4358632012-08-22 17:16:19 -0700189 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700190 } else if (mHwc) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700191 // here we're guaranteed to have at least HWC 1.1
192 for (size_t i =0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
193 queryDisplayProperties(i);
194 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700195 }
196
Mathias Agopian3a778712012-04-09 14:16:47 -0700197 if (needVSyncThread) {
198 // we don't have VSYNC support, we need to fake it
199 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700200 }
201}
202
203HWComposer::~HWComposer() {
Andy McFaddenbabba182012-09-12 13:14:51 -0700204 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700205 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Andy McFaddenbabba182012-09-12 13:14:51 -0700206 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700207 if (mVSyncThread != NULL) {
208 mVSyncThread->requestExitAndWait();
209 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700210 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700211 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700212 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700213 if (mFbDev) {
214 framebuffer_close(mFbDev);
215 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700216 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700217}
218
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700219// Load and prepare the hardware composer module. Sets mHwc.
220void HWComposer::loadHwcModule()
221{
222 hw_module_t const* module;
223
224 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
225 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
226 return;
227 }
228
229 int err = hwc_open_1(module, &mHwc);
230 if (err) {
231 ALOGE("%s device failed to initialize (%s)",
232 HWC_HARDWARE_COMPOSER, strerror(-err));
233 return;
234 }
235
236 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
237 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
238 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
239 ALOGE("%s device version %#x unsupported, will not be used",
240 HWC_HARDWARE_COMPOSER, mHwc->common.version);
241 hwc_close_1(mHwc);
242 mHwc = NULL;
243 return;
244 }
245}
246
247// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
248void HWComposer::loadFbHalModule()
249{
250 hw_module_t const* module;
251
252 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) != 0) {
253 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
254 return;
255 }
256
257 int err = framebuffer_open(module, &mFbDev);
258 if (err) {
259 ALOGE("framebuffer_open failed (%s)", strerror(-err));
260 return;
261 }
262}
263
Mathias Agopiana350ff92010-08-10 17:14:02 -0700264status_t HWComposer::initCheck() const {
265 return mHwc ? NO_ERROR : NO_INIT;
266}
267
Jesse Hallbbd164a2012-08-21 12:05:09 -0700268void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
269 cb_context* ctx = reinterpret_cast<cb_context*>(
270 const_cast<hwc_procs_t*>(procs));
271 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700272}
273
Jesse Hall1bd20e02012-08-29 10:47:52 -0700274void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700275 int64_t timestamp) {
276 cb_context* ctx = reinterpret_cast<cb_context*>(
277 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700278 ctx->hwc->vsync(disp, timestamp);
279}
280
281void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
282 int connected) {
283 cb_context* ctx = reinterpret_cast<cb_context*>(
284 const_cast<hwc_procs_t*>(procs));
285 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700286}
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
Jesse Hall1bd20e02012-08-29 10:47:52 -0700292void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700293 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700294 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700295 Mutex::Autolock _l(mLock);
296 mLastHwVSync = timestamp;
297}
298
Jesse Hall1bd20e02012-08-29 10:47:52 -0700299void HWComposer::hotplug(int disp, int connected) {
300 if (disp == HWC_DISPLAY_PRIMARY || disp >= HWC_NUM_DISPLAY_TYPES) {
301 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
302 disp, connected);
303 return;
304 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700305 queryDisplayProperties(disp);
Mathias Agopian148994e2012-09-19 17:31:36 -0700306 mEventHandler.onHotplugReceived(disp, bool(connected));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700307}
308
309static const uint32_t DISPLAY_ATTRIBUTES[] = {
310 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700311 HWC_DISPLAY_WIDTH,
312 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700313 HWC_DISPLAY_DPI_X,
314 HWC_DISPLAY_DPI_Y,
315 HWC_DISPLAY_NO_ATTRIBUTE,
316};
317#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
318
319// http://developer.android.com/reference/android/util/DisplayMetrics.html
320#define ANDROID_DENSITY_TV 213
321#define ANDROID_DENSITY_XHIGH 320
322
Mathias Agopian1604f772012-09-18 21:54:42 -0700323status_t HWComposer::queryDisplayProperties(int disp) {
324
Mathias Agopianda27af92012-09-13 18:17:13 -0700325 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700326
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700327 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700328 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
329 memset(values, 0, sizeof(values));
330
331 uint32_t config;
332 size_t numConfigs = 1;
333 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700334 if (err != NO_ERROR) {
335 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700336 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700337 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700338 }
339
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700340 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
341 if (err != NO_ERROR) {
342 // we can't get this display's info. turn it off.
343 mDisplayData[disp].connected = false;
344 return err;
345 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700346
Jesse Hall1bd20e02012-08-29 10:47:52 -0700347 int32_t w = 0, h = 0;
348 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
349 switch (DISPLAY_ATTRIBUTES[i]) {
350 case HWC_DISPLAY_VSYNC_PERIOD:
351 mDisplayData[disp].refresh = nsecs_t(values[i]);
352 break;
Jesse Halldb276212012-09-07 11:20:56 -0700353 case HWC_DISPLAY_WIDTH:
354 mDisplayData[disp].width = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700355 break;
Jesse Halldb276212012-09-07 11:20:56 -0700356 case HWC_DISPLAY_HEIGHT:
357 mDisplayData[disp].height = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700358 break;
359 case HWC_DISPLAY_DPI_X:
360 mDisplayData[disp].xdpi = values[i] / 1000.0f;
361 break;
362 case HWC_DISPLAY_DPI_Y:
363 mDisplayData[disp].ydpi = values[i] / 1000.0f;
364 break;
365 default:
Mathias Agopianda27af92012-09-13 18:17:13 -0700366 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
367 i, DISPLAY_ATTRIBUTES[i]);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700368 break;
369 }
370 }
371
Mathias Agopianf5a33922012-09-19 18:16:22 -0700372 // FIXME: what should we set the format to?
373 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
374 mDisplayData[disp].connected = true;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700375 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
376 // is there anything smarter we can do?
377 if (h >= 1080) {
378 mDisplayData[disp].xdpi = ANDROID_DENSITY_XHIGH;
379 mDisplayData[disp].ydpi = ANDROID_DENSITY_XHIGH;
380 } else {
381 mDisplayData[disp].xdpi = ANDROID_DENSITY_TV;
382 mDisplayData[disp].ydpi = ANDROID_DENSITY_TV;
383 }
384 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700385 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700386}
387
Mathias Agopiane60b0682012-08-21 23:34:09 -0700388int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700389 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700390 return NO_MEMORY;
391 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700392 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
393 mAllocatedDisplayIDs.markBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700394 return id;
395}
396
397status_t HWComposer::freeDisplayId(int32_t id) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700398 if (id < HWC_NUM_DISPLAY_TYPES) {
399 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700400 return BAD_VALUE;
401 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700402 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700403 return BAD_INDEX;
404 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700405 mAllocatedDisplayIDs.clearBit(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700406 return NO_ERROR;
407}
408
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700409nsecs_t HWComposer::getRefreshPeriod(int disp) const {
410 return mDisplayData[disp].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700411}
412
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700413nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700414 // this returns the last refresh timestamp.
415 // if the last one is not available, we estimate it based on
416 // the refresh period and whatever closest timestamp we have.
417 Mutex::Autolock _l(mLock);
418 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700419 return now - ((now - mLastHwVSync) % mDisplayData[disp].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700420}
421
Jesse Halldb276212012-09-07 11:20:56 -0700422uint32_t HWComposer::getWidth(int disp) const {
423 return mDisplayData[disp].width;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700424}
425
Jesse Halldb276212012-09-07 11:20:56 -0700426uint32_t HWComposer::getHeight(int disp) const {
427 return mDisplayData[disp].height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700428}
429
430uint32_t HWComposer::getFormat(int disp) const {
431 return mDisplayData[disp].format;
432}
433
434float HWComposer::getDpiX(int disp) const {
435 return mDisplayData[disp].xdpi;
436}
437
438float HWComposer::getDpiY(int disp) const {
439 return mDisplayData[disp].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700440}
441
Mathias Agopianf5a33922012-09-19 18:16:22 -0700442bool HWComposer::isConnected(int disp) const {
443 return mDisplayData[disp].connected;
444}
445
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700446void HWComposer::eventControl(int disp, int event, int enabled) {
447 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
448 return;
449 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700450 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700451 if (mHwc && !mDebugForceFakeVSync) {
452 // NOTE: we use our own internal lock here because we have to call
453 // into the HWC with the lock held, and we want to make sure
454 // that even if HWC blocks (which it shouldn't), it won't
455 // affect other threads.
456 Mutex::Autolock _l(mEventControlLock);
457 const int32_t eventBit = 1UL << event;
458 const int32_t newValue = enabled ? eventBit : 0;
459 const int32_t oldValue = mDisplayData[disp].events & eventBit;
460 if (newValue != oldValue) {
461 ATRACE_CALL();
462 err = mHwc->eventControl(mHwc, disp, event, enabled);
463 if (!err) {
464 int32_t& events(mDisplayData[disp].events);
465 events = (events & ~eventBit) | newValue;
466 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700467 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700468 // error here should not happen -- not sure what we should
469 // do if it does.
470 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
471 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700472 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700473
474 if (err == NO_ERROR && mVSyncThread != NULL) {
475 mVSyncThread->setEnabled(enabled);
476 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700477}
478
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700479status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700480 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700481 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700482 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700483
Mathias Agopian45721772010-08-12 15:03:26 -0700484 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700485 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700486 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
487 // we need space for the HWC_FRAMEBUFFER_TARGET
488 numLayers++;
489 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700490 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700491 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700492 + numLayers * sizeof(hwc_layer_1_t);
493 free(disp.list);
494 disp.list = (hwc_display_contents_1_t*)malloc(size);
495 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700496 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700497 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
498 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
499 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
500 const hwc_rect_t r = { 0, 0, disp.width, disp.height };
501 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
502 disp.framebufferTarget->hints = 0;
503 disp.framebufferTarget->flags = 0;
504 disp.framebufferTarget->handle = disp.fbTargetHandle;
505 disp.framebufferTarget->transform = 0;
506 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
507 disp.framebufferTarget->sourceCrop = r;
508 disp.framebufferTarget->displayFrame = r;
509 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
510 disp.framebufferTarget->visibleRegionScreen.rects =
511 &disp.framebufferTarget->displayFrame;
512 disp.framebufferTarget->acquireFenceFd = -1;
513 disp.framebufferTarget->releaseFenceFd = -1;
514 }
Jesse Halldb276212012-09-07 11:20:56 -0700515 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700516 disp.list->flags = HWC_GEOMETRY_CHANGED;
517 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700518 }
519 return NO_ERROR;
520}
521
Mathias Agopianda27af92012-09-13 18:17:13 -0700522status_t HWComposer::setFramebufferTarget(int32_t id,
523 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
524 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
525 return BAD_INDEX;
526 }
527 DisplayData& disp(mDisplayData[id]);
528 if (!disp.framebufferTarget) {
529 // this should never happen, but apparently eglCreateWindowSurface()
530 // triggers a SurfaceTextureClient::queueBuffer() on some
531 // devices (!?) -- log and ignore.
532 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianf5a33922012-09-19 18:16:22 -0700533// CallStack stack;
534// stack.update();
535// stack.dump("");
Mathias Agopianda27af92012-09-13 18:17:13 -0700536 return NO_ERROR;
537 }
538
539 int acquireFenceFd = -1;
540 if (acquireFence != NULL) {
541 acquireFenceFd = acquireFence->dup();
542 }
543
544 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
545 disp.fbTargetHandle = buf->handle;
546 disp.framebufferTarget->handle = disp.fbTargetHandle;
547 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
548 return NO_ERROR;
549}
550
Mathias Agopiane60b0682012-08-21 23:34:09 -0700551status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700552 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700553 DisplayData& disp(mDisplayData[i]);
554 if (disp.framebufferTarget) {
555 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
556 // DO NOT reset the handle field to NULL, because it's possible
557 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
558 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700559 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700560 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
561 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700562 if (!disp.connected && disp.list != NULL) {
563 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
564 i, disp.list->numHwLayers);
565 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700566 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700567 if (mLists[i]) {
Jesse Halldb276212012-09-07 11:20:56 -0700568 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
569 mLists[i]->outbuf = NULL;
570 mLists[i]->outbufAcquireFenceFd = -1;
571 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
572 // garbage data to catch improper use
573 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
574 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
575 } else {
576 mLists[i]->dpy = EGL_NO_DISPLAY;
577 mLists[i]->sur = EGL_NO_SURFACE;
578 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700579 }
580 }
Jesse Halldb276212012-09-07 11:20:56 -0700581
Mathias Agopianf4358632012-08-22 17:16:19 -0700582 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700583 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
584
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700585 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700586 // here we're just making sure that "skip" layers are set
587 // to HWC_FRAMEBUFFER and we're also counting how many layers
588 // we have of each type.
Mathias Agopianf4358632012-08-22 17:16:19 -0700589 for (size_t i=0 ; i<mNumDisplays ; i++) {
590 DisplayData& disp(mDisplayData[i]);
591 disp.hasFbComp = false;
592 disp.hasOvComp = false;
593 if (disp.list) {
594 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
595 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700596
597 //ALOGD("prepare: %d, type=%d, handle=%p",
598 // i, l.compositionType, l.handle);
599
Mathias Agopianf4358632012-08-22 17:16:19 -0700600 if (l.flags & HWC_SKIP_LAYER) {
601 l.compositionType = HWC_FRAMEBUFFER;
602 }
603 if (l.compositionType == HWC_FRAMEBUFFER) {
604 disp.hasFbComp = true;
605 }
606 if (l.compositionType == HWC_OVERLAY) {
607 disp.hasOvComp = true;
608 }
609 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700610 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700611 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700612 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700613 return (status_t)err;
614}
615
Mathias Agopiane60b0682012-08-21 23:34:09 -0700616bool HWComposer::hasHwcComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700617 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700618 return false;
619 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700620}
621
Mathias Agopiane60b0682012-08-21 23:34:09 -0700622bool HWComposer::hasGlesComposition(int32_t id) const {
Mathias Agopianf4358632012-08-22 17:16:19 -0700623 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700624 return false;
625 return mDisplayData[id].hasFbComp;
626}
627
Mathias Agopianda27af92012-09-13 18:17:13 -0700628int HWComposer::getAndResetReleaseFenceFd(int32_t id) {
629 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
630 return BAD_INDEX;
631
632 int fd = INVALID_OPERATION;
633 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
634 const DisplayData& disp(mDisplayData[id]);
635 if (disp.framebufferTarget) {
636 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700637 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700638 disp.framebufferTarget->releaseFenceFd = -1;
639 }
640 }
641 return fd;
642}
643
Mathias Agopian30bcc612012-08-22 15:39:48 -0700644status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700645 int err = NO_ERROR;
646 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700647 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700648 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700649 // by the (dpy, sur) fields and we are guaranteed to have only
650 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700651 mLists[0]->dpy = eglGetCurrentDisplay();
652 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700653 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700654
Mathias Agopian30bcc612012-08-22 15:39:48 -0700655 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700656
657 for (size_t i=0 ; i<mNumDisplays ; i++) {
658 DisplayData& disp(mDisplayData[i]);
659 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700660 if (disp.list->retireFenceFd != -1) {
661 close(disp.list->retireFenceFd);
662 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700663 }
664 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
665 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700666 }
Mathias Agopian58959342010-10-07 14:57:04 -0700667 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700668 return (status_t)err;
669}
670
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700671status_t HWComposer::release(int disp) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700672 LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700673 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700674 eventControl(disp, HWC_EVENT_VSYNC, 0);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700675 return (status_t)mHwc->blank(mHwc, disp, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700676 }
677 return NO_ERROR;
678}
679
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700680status_t HWComposer::acquire(int disp) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700681 LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
Colin Cross10fbdb62012-07-12 17:56:34 -0700682 if (mHwc) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700683 return (status_t)mHwc->blank(mHwc, disp, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700684 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700685 return NO_ERROR;
686}
687
Andy McFadden27ec5732012-10-02 19:04:45 -0700688void HWComposer::disconnectDisplay(int disp) {
689 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY ||
690 disp >= HWC_NUM_DISPLAY_TYPES);
691 DisplayData& dd(mDisplayData[disp]);
692 if (dd.list != NULL) {
693 free(dd.list);
694 dd.list = NULL;
695 dd.framebufferTarget = NULL; // points into dd.list
696 }
697}
698
Mathias Agopiancde87a32012-09-13 14:09:01 -0700699int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700700 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700701 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
702 // is supported by the implementation. we can only be in this case
703 // if we have HWC 1.1
704 return HAL_PIXEL_FORMAT_RGBA_8888;
705 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700706 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700707 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700708 }
709}
710
Mathias Agopianda27af92012-09-13 18:17:13 -0700711bool HWComposer::supportsFramebufferTarget() const {
712 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
713}
714
715int HWComposer::fbPost(int32_t id,
716 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700717 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700718 return setFramebufferTarget(id, acquireFence, buffer);
719 } else {
720 if (acquireFence != NULL) {
Jesse Hallba607d52012-10-01 14:05:20 -0700721 acquireFence->waitForever(1000, "HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700722 }
723 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700724 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700725}
726
727int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700728 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700729 return NO_ERROR;
730
731 if (mFbDev->compositionComplete) {
732 return mFbDev->compositionComplete(mFbDev);
733 } else {
734 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700735 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700736}
737
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700738void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700739 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700740 const size_t SIZE = 4096;
741 char buffer[SIZE];
742 mFbDev->dump(mFbDev, buffer, SIZE);
743 result.append(buffer);
744 }
745}
746
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700747/*
748 * Helper template to implement a concrete HWCLayer
749 * This holds the pointer to the concrete hwc layer type
750 * and implements the "iterable" side of HWCLayer.
751 */
752template<typename CONCRETE, typename HWCTYPE>
753class Iterable : public HWComposer::HWCLayer {
754protected:
755 HWCTYPE* const mLayerList;
756 HWCTYPE* mCurrentLayer;
757 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
758 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
759 inline HWCTYPE* getLayer() { return mCurrentLayer; }
760 virtual ~Iterable() { }
761private:
762 // returns a copy of ourselves
763 virtual HWComposer::HWCLayer* dup() {
764 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
765 }
766 virtual status_t setLayer(size_t index) {
767 mCurrentLayer = &mLayerList[index];
768 return NO_ERROR;
769 }
770};
771
Jesse Hall5880cc52012-06-05 23:40:32 -0700772/*
773 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
774 * This implements the HWCLayer side of HWCIterableLayer.
775 */
776class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
777public:
778 HWCLayerVersion1(hwc_layer_1_t* layer)
779 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { }
780
781 virtual int32_t getCompositionType() const {
782 return getLayer()->compositionType;
783 }
784 virtual uint32_t getHints() const {
785 return getLayer()->hints;
786 }
Jesse Hallef194142012-06-14 14:45:17 -0700787 virtual int getAndResetReleaseFenceFd() {
788 int fd = getLayer()->releaseFenceFd;
789 getLayer()->releaseFenceFd = -1;
790 return fd;
791 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700792 virtual void setAcquireFenceFd(int fenceFd) {
793 getLayer()->acquireFenceFd = fenceFd;
794 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700795
796 virtual void setDefaultState() {
797 getLayer()->compositionType = HWC_FRAMEBUFFER;
798 getLayer()->hints = 0;
799 getLayer()->flags = HWC_SKIP_LAYER;
Mathias Agopian55882de2012-09-05 16:00:56 -0700800 getLayer()->handle = 0;
Jesse Hall5880cc52012-06-05 23:40:32 -0700801 getLayer()->transform = 0;
802 getLayer()->blending = HWC_BLENDING_NONE;
803 getLayer()->visibleRegionScreen.numRects = 0;
804 getLayer()->visibleRegionScreen.rects = NULL;
805 getLayer()->acquireFenceFd = -1;
806 getLayer()->releaseFenceFd = -1;
807 }
808 virtual void setSkip(bool skip) {
809 if (skip) {
810 getLayer()->flags |= HWC_SKIP_LAYER;
811 } else {
812 getLayer()->flags &= ~HWC_SKIP_LAYER;
813 }
814 }
815 virtual void setBlending(uint32_t blending) {
816 getLayer()->blending = blending;
817 }
818 virtual void setTransform(uint32_t transform) {
819 getLayer()->transform = transform;
820 }
821 virtual void setFrame(const Rect& frame) {
822 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
823 }
824 virtual void setCrop(const Rect& crop) {
825 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
826 }
827 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700828 // Region::getSharedBuffer creates a reference to the underlying
829 // SharedBuffer of this Region, this reference is freed
830 // in onDisplayed()
831 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
832 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
833 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700834 }
835 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
836 if (buffer == 0 || buffer->handle == 0) {
837 getLayer()->compositionType = HWC_FRAMEBUFFER;
838 getLayer()->flags |= HWC_SKIP_LAYER;
839 getLayer()->handle = 0;
840 } else {
841 getLayer()->handle = buffer->handle;
842 }
843 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700844 virtual void onDisplayed() {
845 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
846 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
847 if (sb) {
848 sb->release();
849 // not technically needed but safer
850 visibleRegion.numRects = 0;
851 visibleRegion.rects = NULL;
852 }
Jesse Halle25d0052012-09-05 13:03:10 -0700853
854 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700855 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700856};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700857
858/*
859 * returns an iterator initialized at a given index in the layer list
860 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700861HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700862 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700863 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700864 }
865 const DisplayData& disp(mDisplayData[id]);
866 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700867 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700868 }
869 return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700870}
871
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700872/*
873 * returns an iterator on the beginning of the layer list
874 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700875HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700876 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700877}
878
879/*
880 * returns an iterator on the end of the layer list
881 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700882HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700883 size_t numLayers = 0;
884 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
885 const DisplayData& disp(mDisplayData[id]);
886 if (mHwc && disp.list) {
887 numLayers = disp.list->numHwLayers;
888 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
889 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
890 // which we ignore when iterating through the layer list.
891 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
892 if (numLayers) {
893 numLayers--;
894 }
895 }
896 }
897 }
898 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700899}
900
Mathias Agopian22da60c2011-09-09 00:49:11 -0700901void HWComposer::dump(String8& result, char* buffer, size_t SIZE,
902 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700903 if (mHwc) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700904 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -0700905 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
906 for (size_t i=0 ; i<mNumDisplays ; i++) {
907 const DisplayData& disp(mDisplayData[i]);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700908
909 if (disp.connected) {
910 result.appendFormat(
911 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
912 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
913 }
914
915 if (disp.list && disp.connected) {
916 result.appendFormat(
917 " numHwLayers=%u, flags=%08x\n",
918 disp.list->numHwLayers, disp.list->flags);
919
Mathias Agopianf4358632012-08-22 17:16:19 -0700920 result.append(
Mathias Agopianda27af92012-09-13 18:17:13 -0700921 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
922 "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
923 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopianf4358632012-08-22 17:16:19 -0700924 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
925 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -0700926 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700927 String8 name("unknown");
928 if (i < visibleLayersSortedByZ.size()) {
929 const sp<LayerBase>& layer(visibleLayersSortedByZ[i]);
930 if (layer->getLayer() != NULL) {
931 const sp<GraphicBuffer>& buffer(
Mathias Agopianf4358632012-08-22 17:16:19 -0700932 layer->getLayer()->getActiveBuffer());
Mathias Agopianda27af92012-09-13 18:17:13 -0700933 if (buffer != NULL) {
934 format = buffer->getPixelFormat();
935 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700936 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700937 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -0700938 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700939
940 int type = l.compositionType;
941 if (type == HWC_FRAMEBUFFER_TARGET) {
942 name = "HWC_FRAMEBUFFER_TARGET";
943 format = disp.format;
944 }
945
946 static char const* compositionTypeName[] = {
947 "GLES",
948 "HWC",
949 "BACKGROUND",
950 "FB TARGET",
951 "UNKNOWN"};
952 if (type >= NELEM(compositionTypeName))
953 type = NELEM(compositionTypeName) - 1;
954
Mathias Agopianf4358632012-08-22 17:16:19 -0700955 result.appendFormat(
Mathias Agopianda27af92012-09-13 18:17:13 -0700956 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
957 compositionTypeName[type],
Mathias Agopianf4358632012-08-22 17:16:19 -0700958 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
959 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
960 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianda27af92012-09-13 18:17:13 -0700961 name.string());
Mathias Agopianfb4d5d52011-09-20 15:13:14 -0700962 }
963 }
Mathias Agopian83727852010-09-23 18:13:21 -0700964 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800965 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700966
967 if (mHwc && mHwc->dump) {
968 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -0800969 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -0700970 }
971}
972
Mathias Agopiana350ff92010-08-10 17:14:02 -0700973// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -0700974
975HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
976 : mHwc(hwc), mEnabled(false),
977 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700978 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -0700979{
980}
981
982void HWComposer::VSyncThread::setEnabled(bool enabled) {
983 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700984 if (mEnabled != enabled) {
985 mEnabled = enabled;
986 mCondition.signal();
987 }
Mathias Agopian2965b262012-04-08 15:13:32 -0700988}
989
990void HWComposer::VSyncThread::onFirstRef() {
991 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
992}
993
994bool HWComposer::VSyncThread::threadLoop() {
995 { // scope for lock
996 Mutex::Autolock _l(mLock);
997 while (!mEnabled) {
998 mCondition.wait(mLock);
999 }
1000 }
1001
1002 const nsecs_t period = mRefreshPeriod;
1003 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1004 nsecs_t next_vsync = mNextFakeVSync;
1005 nsecs_t sleep = next_vsync - now;
1006 if (sleep < 0) {
1007 // we missed, find where the next vsync should be
1008 sleep = (period - ((now - next_vsync) % period));
1009 next_vsync = now + sleep;
1010 }
1011 mNextFakeVSync = next_vsync + period;
1012
1013 struct timespec spec;
1014 spec.tv_sec = next_vsync / 1000000000;
1015 spec.tv_nsec = next_vsync % 1000000000;
1016
1017 int err;
1018 do {
1019 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1020 } while (err<0 && errno == EINTR);
1021
1022 if (err == 0) {
1023 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1024 }
1025
1026 return true;
1027}
1028
1029// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001030}; // namespace android