blob: 6bdccb931888e90b6de130897e9c8afc940eafdb [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
Mathias Agopian33ceeb32013-04-01 16:54:58 -070028#include <utils/CallStack.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070029#include <utils/Errors.h>
Mathias Agopianda27af92012-09-13 18:17:13 -070030#include <utils/misc.h>
Mathias Agopian83727852010-09-23 18:13:21 -070031#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070032#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070033#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070034#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070035
Mathias Agopian921e6ac2012-07-23 23:11:29 -070036#include <ui/GraphicBuffer.h>
37
Mathias Agopiana350ff92010-08-10 17:14:02 -070038#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070039#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070040
Jesse Hall1c569c42013-04-05 13:44:52 -070041#include <android/configuration.h>
42
Mathias Agopiana350ff92010-08-10 17:14:02 -070043#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070044#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070045
Mathias Agopiana350ff92010-08-10 17:14:02 -070046#include "HWComposer.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070047
48#include "../Layer.h" // needed only for debugging
49#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070050
51namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070052
Jesse Hall72960512013-01-10 16:19:56 -080053#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070054
Jesse Hallafaf14b2013-03-20 13:42:29 -070055#define NUM_PHYSICAL_DISPLAYS HWC_NUM_DISPLAY_TYPES
56#define VIRTUAL_DISPLAY_ID_BASE HWC_NUM_DISPLAY_TYPES
57
Jesse Hall9eb1eb52012-08-29 10:39:38 -070058static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
59 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070060 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;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070065 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
66}
67
68static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
69 uint32_t version) {
70 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070071}
72
Mathias Agopiana350ff92010-08-10 17:14:02 -070073// ---------------------------------------------------------------------------
74
Mathias Agopian3e8b8532012-05-13 20:42:01 -070075struct HWComposer::cb_context {
76 struct callbacks : public hwc_procs_t {
77 // these are here to facilitate the transition when adding
78 // new callbacks (an implementation can check for NULL before
79 // calling a new callback).
80 void (*zero[4])(void);
81 };
82 callbacks procs;
83 HWComposer* hwc;
84};
85
86// ---------------------------------------------------------------------------
87
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070088HWComposer::HWComposer(
89 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070090 EventHandler& handler)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070091 : mFlinger(flinger),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070092 mFbDev(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070093 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070094 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070095 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -070096{
Mathias Agopiane60b0682012-08-21 23:34:09 -070097 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
98 mLists[i] = 0;
99 }
Jesse Hallb685c542012-07-31 14:32:56 -0700100
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700101 char value[PROPERTY_VALUE_MAX];
102 property_get("debug.sf.no_hw_vsync", value, "0");
103 mDebugForceFakeVSync = atoi(value);
104
Mathias Agopian028508c2012-07-25 21:12:12 -0700105 bool needVSyncThread = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700106
107 // Note: some devices may insist that the FB HAL be opened before HWC.
Jesse Hallef64b752013-03-18 11:28:50 -0700108 int fberr = loadFbHalModule();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700109 loadHwcModule();
110
Mathias Agopianda27af92012-09-13 18:17:13 -0700111 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
112 // close FB HAL if we don't needed it.
113 // FIXME: this is temporary until we're not forced to open FB HAL
114 // before HWC.
115 framebuffer_close(mFbDev);
116 mFbDev = NULL;
117 }
118
Andy McFaddenbabba182012-09-12 13:14:51 -0700119 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
120 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
121 && !mFbDev) {
Jesse Hallef64b752013-03-18 11:28:50 -0700122 ALOGE("ERROR: failed to open framebuffer (%s), aborting",
123 strerror(-fberr));
Andy McFadden43601a22012-09-11 15:15:13 -0700124 abort();
125 }
126
Andy McFadden620685c2012-10-19 12:53:46 -0700127 // these display IDs are always reserved
Jesse Hallafaf14b2013-03-20 13:42:29 -0700128 for (size_t i=0 ; i<NUM_PHYSICAL_DISPLAYS ; i++) {
Andy McFadden620685c2012-10-19 12:53:46 -0700129 mAllocatedDisplayIDs.markBit(i);
130 }
131
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700132 if (mHwc) {
133 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
134 (hwcApiVersion(mHwc) >> 24) & 0xff,
135 (hwcApiVersion(mHwc) >> 16) & 0xff);
136 if (mHwc->registerProcs) {
137 mCBContext->hwc = this;
138 mCBContext->procs.invalidate = &hook_invalidate;
139 mCBContext->procs.vsync = &hook_vsync;
140 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
141 mCBContext->procs.hotplug = &hook_hotplug;
142 else
143 mCBContext->procs.hotplug = NULL;
144 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
145 mHwc->registerProcs(mHwc, &mCBContext->procs);
Jesse Hall5880cc52012-06-05 23:40:32 -0700146 }
147
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700148 // don't need a vsync thread if we have a hardware composer
149 needVSyncThread = false;
150 // always turn vsync off when we start
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700151 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700152
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700153 // the number of displays we actually have depends on the
154 // hw composer version
155 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
156 // 1.2 adds support for virtual displays
157 mNumDisplays = MAX_DISPLAYS;
158 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
159 // 1.1 adds support for multiple displays
Jesse Hallafaf14b2013-03-20 13:42:29 -0700160 mNumDisplays = NUM_PHYSICAL_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700161 } else {
162 mNumDisplays = 1;
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700163 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700164 }
165
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700166 if (mFbDev) {
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]);
Mathias Agopian38e623b2012-09-20 19:27:07 -0700171 disp.connected = true;
Jesse Halldb276212012-09-07 11:20:56 -0700172 disp.width = mFbDev->width;
173 disp.height = mFbDev->height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700174 disp.format = mFbDev->format;
175 disp.xdpi = mFbDev->xdpi;
176 disp.ydpi = mFbDev->ydpi;
Mathias Agopianf4358632012-08-22 17:16:19 -0700177 if (disp.refresh == 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700178 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
Mathias Agopianf4358632012-08-22 17:16:19 -0700179 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700180 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700181 if (disp.refresh == 0) {
182 disp.refresh = nsecs_t(1e9 / 60.0);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700183 ALOGW("getting VSYNC period from thin air: %lld",
184 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopianf4358632012-08-22 17:16:19 -0700185 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700186 } else if (mHwc) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700187 // here we're guaranteed to have at least HWC 1.1
Jesse Hallafaf14b2013-03-20 13:42:29 -0700188 for (size_t i =0 ; i<NUM_PHYSICAL_DISPLAYS ; i++) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700189 queryDisplayProperties(i);
190 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700191 }
192
Mathias Agopian3a778712012-04-09 14:16:47 -0700193 if (needVSyncThread) {
194 // we don't have VSYNC support, we need to fake it
195 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700196 }
197}
198
199HWComposer::~HWComposer() {
Andy McFaddenbabba182012-09-12 13:14:51 -0700200 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700201 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Andy McFaddenbabba182012-09-12 13:14:51 -0700202 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700203 if (mVSyncThread != NULL) {
204 mVSyncThread->requestExitAndWait();
205 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700206 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700207 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700208 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700209 if (mFbDev) {
210 framebuffer_close(mFbDev);
211 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700212 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700213}
214
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700215// Load and prepare the hardware composer module. Sets mHwc.
216void HWComposer::loadHwcModule()
217{
218 hw_module_t const* module;
219
220 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
221 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
222 return;
223 }
224
225 int err = hwc_open_1(module, &mHwc);
226 if (err) {
227 ALOGE("%s device failed to initialize (%s)",
228 HWC_HARDWARE_COMPOSER, strerror(-err));
229 return;
230 }
231
232 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
233 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
234 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
235 ALOGE("%s device version %#x unsupported, will not be used",
236 HWC_HARDWARE_COMPOSER, mHwc->common.version);
237 hwc_close_1(mHwc);
238 mHwc = NULL;
239 return;
240 }
241}
242
243// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
Jesse Hallef64b752013-03-18 11:28:50 -0700244int HWComposer::loadFbHalModule()
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700245{
246 hw_module_t const* module;
247
Jesse Hallef64b752013-03-18 11:28:50 -0700248 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
249 if (err != 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700250 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
Jesse Hallef64b752013-03-18 11:28:50 -0700251 return err;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700252 }
253
Jesse Hallef64b752013-03-18 11:28:50 -0700254 return framebuffer_open(module, &mFbDev);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700255}
256
Mathias Agopiana350ff92010-08-10 17:14:02 -0700257status_t HWComposer::initCheck() const {
258 return mHwc ? NO_ERROR : NO_INIT;
259}
260
Jesse Hallbbd164a2012-08-21 12:05:09 -0700261void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
262 cb_context* ctx = reinterpret_cast<cb_context*>(
263 const_cast<hwc_procs_t*>(procs));
264 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700265}
266
Jesse Hall1bd20e02012-08-29 10:47:52 -0700267void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700268 int64_t timestamp) {
269 cb_context* ctx = reinterpret_cast<cb_context*>(
270 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700271 ctx->hwc->vsync(disp, timestamp);
272}
273
274void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
275 int connected) {
276 cb_context* ctx = reinterpret_cast<cb_context*>(
277 const_cast<hwc_procs_t*>(procs));
278 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700279}
280
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700281void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700282 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700283}
284
Jesse Hall1bd20e02012-08-29 10:47:52 -0700285void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700286 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700287 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700288 Mutex::Autolock _l(mLock);
289 mLastHwVSync = timestamp;
290}
291
Jesse Hall1bd20e02012-08-29 10:47:52 -0700292void HWComposer::hotplug(int disp, int connected) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700293 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700294 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
295 disp, connected);
296 return;
297 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700298 queryDisplayProperties(disp);
Mathias Agopian148994e2012-09-19 17:31:36 -0700299 mEventHandler.onHotplugReceived(disp, bool(connected));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700300}
301
Jesse Hall1c569c42013-04-05 13:44:52 -0700302static float getDefaultDensity(uint32_t height) {
303 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
304 else return ACONFIGURATION_DENSITY_TV;
305}
306
Jesse Hall1bd20e02012-08-29 10:47:52 -0700307static const uint32_t DISPLAY_ATTRIBUTES[] = {
308 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700309 HWC_DISPLAY_WIDTH,
310 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700311 HWC_DISPLAY_DPI_X,
312 HWC_DISPLAY_DPI_Y,
313 HWC_DISPLAY_NO_ATTRIBUTE,
314};
315#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
316
Mathias Agopian1604f772012-09-18 21:54:42 -0700317status_t HWComposer::queryDisplayProperties(int disp) {
318
Mathias Agopianda27af92012-09-13 18:17:13 -0700319 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700320
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700321 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700322 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
323 memset(values, 0, sizeof(values));
324
325 uint32_t config;
326 size_t numConfigs = 1;
327 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700328 if (err != NO_ERROR) {
329 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700330 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700331 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700332 }
333
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700334 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
335 if (err != NO_ERROR) {
336 // we can't get this display's info. turn it off.
337 mDisplayData[disp].connected = false;
338 return err;
339 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700340
Jesse Hall1bd20e02012-08-29 10:47:52 -0700341 int32_t w = 0, h = 0;
342 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
343 switch (DISPLAY_ATTRIBUTES[i]) {
344 case HWC_DISPLAY_VSYNC_PERIOD:
345 mDisplayData[disp].refresh = nsecs_t(values[i]);
346 break;
Jesse Halldb276212012-09-07 11:20:56 -0700347 case HWC_DISPLAY_WIDTH:
348 mDisplayData[disp].width = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700349 break;
Jesse Halldb276212012-09-07 11:20:56 -0700350 case HWC_DISPLAY_HEIGHT:
351 mDisplayData[disp].height = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700352 break;
353 case HWC_DISPLAY_DPI_X:
354 mDisplayData[disp].xdpi = values[i] / 1000.0f;
355 break;
356 case HWC_DISPLAY_DPI_Y:
357 mDisplayData[disp].ydpi = values[i] / 1000.0f;
358 break;
359 default:
Mathias Agopianda27af92012-09-13 18:17:13 -0700360 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
361 i, DISPLAY_ATTRIBUTES[i]);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700362 break;
363 }
364 }
365
Mathias Agopianf5a33922012-09-19 18:16:22 -0700366 // FIXME: what should we set the format to?
367 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
368 mDisplayData[disp].connected = true;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700369 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
Jesse Hall1c569c42013-04-05 13:44:52 -0700370 float dpi = getDefaultDensity(h);
371 mDisplayData[disp].xdpi = dpi;
372 mDisplayData[disp].ydpi = dpi;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700373 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700374 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700375}
376
Jesse Hall1c569c42013-04-05 13:44:52 -0700377status_t HWComposer::setVirtualDisplayProperties(int32_t id,
378 uint32_t w, uint32_t h, uint32_t format) {
379 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
380 !mAllocatedDisplayIDs.hasBit(id)) {
381 return BAD_INDEX;
382 }
383 mDisplayData[id].width = w;
384 mDisplayData[id].height = h;
385 mDisplayData[id].format = format;
386 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h);
387 return NO_ERROR;
388}
389
Mathias Agopiane60b0682012-08-21 23:34:09 -0700390int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700391 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700392 return NO_MEMORY;
393 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700394 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
395 mAllocatedDisplayIDs.markBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800396 mDisplayData[id].connected = true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700397 return id;
398}
399
400status_t HWComposer::freeDisplayId(int32_t id) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700401 if (id < NUM_PHYSICAL_DISPLAYS) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700402 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700403 return BAD_VALUE;
404 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700405 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700406 return BAD_INDEX;
407 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700408 mAllocatedDisplayIDs.clearBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800409 mDisplayData[id].connected = false;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700410 return NO_ERROR;
411}
412
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700413nsecs_t HWComposer::getRefreshPeriod(int disp) const {
414 return mDisplayData[disp].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700415}
416
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700417nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700418 // this returns the last refresh timestamp.
419 // if the last one is not available, we estimate it based on
420 // the refresh period and whatever closest timestamp we have.
421 Mutex::Autolock _l(mLock);
422 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700423 return now - ((now - mLastHwVSync) % mDisplayData[disp].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700424}
425
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800426sp<Fence> HWComposer::getDisplayFence(int disp) const {
427 return mDisplayData[disp].lastDisplayFence;
428}
429
Jesse Halldb276212012-09-07 11:20:56 -0700430uint32_t HWComposer::getWidth(int disp) const {
431 return mDisplayData[disp].width;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700432}
433
Jesse Halldb276212012-09-07 11:20:56 -0700434uint32_t HWComposer::getHeight(int disp) const {
435 return mDisplayData[disp].height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700436}
437
438uint32_t HWComposer::getFormat(int disp) const {
439 return mDisplayData[disp].format;
440}
441
442float HWComposer::getDpiX(int disp) const {
443 return mDisplayData[disp].xdpi;
444}
445
446float HWComposer::getDpiY(int disp) const {
447 return mDisplayData[disp].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700448}
449
Mathias Agopianf5a33922012-09-19 18:16:22 -0700450bool HWComposer::isConnected(int disp) const {
451 return mDisplayData[disp].connected;
452}
453
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700454void HWComposer::eventControl(int disp, int event, int enabled) {
455 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
Andy McFadden620685c2012-10-19 12:53:46 -0700456 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
457 event, disp, enabled);
458 return;
459 }
460 if (event != EVENT_VSYNC) {
461 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
462 event, disp, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700463 return;
464 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700465 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700466 if (mHwc && !mDebugForceFakeVSync) {
467 // NOTE: we use our own internal lock here because we have to call
468 // into the HWC with the lock held, and we want to make sure
469 // that even if HWC blocks (which it shouldn't), it won't
470 // affect other threads.
471 Mutex::Autolock _l(mEventControlLock);
472 const int32_t eventBit = 1UL << event;
473 const int32_t newValue = enabled ? eventBit : 0;
474 const int32_t oldValue = mDisplayData[disp].events & eventBit;
475 if (newValue != oldValue) {
476 ATRACE_CALL();
477 err = mHwc->eventControl(mHwc, disp, event, enabled);
478 if (!err) {
479 int32_t& events(mDisplayData[disp].events);
480 events = (events & ~eventBit) | newValue;
481 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700482 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700483 // error here should not happen -- not sure what we should
484 // do if it does.
485 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
486 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700487 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700488
489 if (err == NO_ERROR && mVSyncThread != NULL) {
490 mVSyncThread->setEnabled(enabled);
491 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700492}
493
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700494status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700495 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700496 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700497 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700498
Mathias Agopian45721772010-08-12 15:03:26 -0700499 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700500 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700501 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
502 // we need space for the HWC_FRAMEBUFFER_TARGET
503 numLayers++;
504 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700505 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700506 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700507 + numLayers * sizeof(hwc_layer_1_t);
508 free(disp.list);
509 disp.list = (hwc_display_contents_1_t*)malloc(size);
510 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700511 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700512 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
513 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
514 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800515 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
Mathias Agopianda27af92012-09-13 18:17:13 -0700516 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
517 disp.framebufferTarget->hints = 0;
518 disp.framebufferTarget->flags = 0;
519 disp.framebufferTarget->handle = disp.fbTargetHandle;
520 disp.framebufferTarget->transform = 0;
521 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
522 disp.framebufferTarget->sourceCrop = r;
523 disp.framebufferTarget->displayFrame = r;
524 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
525 disp.framebufferTarget->visibleRegionScreen.rects =
526 &disp.framebufferTarget->displayFrame;
527 disp.framebufferTarget->acquireFenceFd = -1;
528 disp.framebufferTarget->releaseFenceFd = -1;
Mathias Agopian70a6e882013-03-21 16:25:12 -0700529 disp.framebufferTarget->planeAlpha = 0xFF;
Mathias Agopianda27af92012-09-13 18:17:13 -0700530 }
Jesse Halldb276212012-09-07 11:20:56 -0700531 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700532 disp.list->flags = HWC_GEOMETRY_CHANGED;
533 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700534 }
535 return NO_ERROR;
536}
537
Mathias Agopianda27af92012-09-13 18:17:13 -0700538status_t HWComposer::setFramebufferTarget(int32_t id,
539 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
540 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
541 return BAD_INDEX;
542 }
543 DisplayData& disp(mDisplayData[id]);
544 if (!disp.framebufferTarget) {
545 // this should never happen, but apparently eglCreateWindowSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800546 // triggers a Surface::queueBuffer() on some
Mathias Agopianda27af92012-09-13 18:17:13 -0700547 // devices (!?) -- log and ignore.
548 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianda27af92012-09-13 18:17:13 -0700549 return NO_ERROR;
550 }
551
552 int acquireFenceFd = -1;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800553 if (acquireFence->isValid()) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700554 acquireFenceFd = acquireFence->dup();
555 }
556
557 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
558 disp.fbTargetHandle = buf->handle;
559 disp.framebufferTarget->handle = disp.fbTargetHandle;
560 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
561 return NO_ERROR;
562}
563
Mathias Agopiane60b0682012-08-21 23:34:09 -0700564status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700565 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700566 DisplayData& disp(mDisplayData[i]);
567 if (disp.framebufferTarget) {
568 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
569 // DO NOT reset the handle field to NULL, because it's possible
570 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
571 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700572 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700573 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
574 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700575 if (!disp.connected && disp.list != NULL) {
576 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
577 i, disp.list->numHwLayers);
578 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700579 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700580 if (mLists[i]) {
Jesse Halldb276212012-09-07 11:20:56 -0700581 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
582 mLists[i]->outbuf = NULL;
583 mLists[i]->outbufAcquireFenceFd = -1;
584 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
585 // garbage data to catch improper use
586 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
587 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
588 } else {
589 mLists[i]->dpy = EGL_NO_DISPLAY;
590 mLists[i]->sur = EGL_NO_SURFACE;
591 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700592 }
593 }
Jesse Halldb276212012-09-07 11:20:56 -0700594
Mathias Agopianf4358632012-08-22 17:16:19 -0700595 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700596 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
597
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700598 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700599 // here we're just making sure that "skip" layers are set
600 // to HWC_FRAMEBUFFER and we're also counting how many layers
601 // we have of each type.
Mathias Agopianf4358632012-08-22 17:16:19 -0700602 for (size_t i=0 ; i<mNumDisplays ; i++) {
603 DisplayData& disp(mDisplayData[i]);
604 disp.hasFbComp = false;
605 disp.hasOvComp = false;
606 if (disp.list) {
607 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
608 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700609
610 //ALOGD("prepare: %d, type=%d, handle=%p",
611 // i, l.compositionType, l.handle);
612
Mathias Agopianf4358632012-08-22 17:16:19 -0700613 if (l.flags & HWC_SKIP_LAYER) {
614 l.compositionType = HWC_FRAMEBUFFER;
615 }
616 if (l.compositionType == HWC_FRAMEBUFFER) {
617 disp.hasFbComp = true;
618 }
619 if (l.compositionType == HWC_OVERLAY) {
620 disp.hasOvComp = true;
621 }
622 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700623 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700624 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700625 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700626 return (status_t)err;
627}
628
Mathias Agopiane60b0682012-08-21 23:34:09 -0700629bool HWComposer::hasHwcComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700630 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700631 return false;
632 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700633}
634
Mathias Agopiane60b0682012-08-21 23:34:09 -0700635bool HWComposer::hasGlesComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700636 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
637 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700638 return mDisplayData[id].hasFbComp;
639}
640
Jesse Hall13f01cb2013-03-20 11:37:21 -0700641sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700642 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Jesse Hall13f01cb2013-03-20 11:37:21 -0700643 return Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700644
645 int fd = INVALID_OPERATION;
646 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
647 const DisplayData& disp(mDisplayData[id]);
648 if (disp.framebufferTarget) {
649 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700650 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700651 disp.framebufferTarget->releaseFenceFd = -1;
652 }
653 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700654 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700655}
656
Mathias Agopian30bcc612012-08-22 15:39:48 -0700657status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700658 int err = NO_ERROR;
659 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700660 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700661 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700662 // by the (dpy, sur) fields and we are guaranteed to have only
663 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700664 mLists[0]->dpy = eglGetCurrentDisplay();
665 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700666 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700667
Jesse Hallafaf14b2013-03-20 13:42:29 -0700668 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
Jesse Hall80e0a392013-03-15 12:32:10 -0700669 DisplayData& disp(mDisplayData[i]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700670 if (disp.outbufHandle) {
671 mLists[i]->outbuf = disp.outbufHandle;
Jesse Hall80e0a392013-03-15 12:32:10 -0700672 mLists[i]->outbufAcquireFenceFd =
Jesse Hall851cfe82013-03-20 13:44:00 -0700673 disp.outbufAcquireFence->dup();
Jesse Hall80e0a392013-03-15 12:32:10 -0700674 }
675 }
676
Mathias Agopian30bcc612012-08-22 15:39:48 -0700677 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700678
679 for (size_t i=0 ; i<mNumDisplays ; i++) {
680 DisplayData& disp(mDisplayData[i]);
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800681 disp.lastDisplayFence = disp.lastRetireFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800682 disp.lastRetireFence = Fence::NO_FENCE;
Mathias Agopianf4358632012-08-22 17:16:19 -0700683 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700684 if (disp.list->retireFenceFd != -1) {
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800685 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
Jesse Halldb276212012-09-07 11:20:56 -0700686 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700687 }
688 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
689 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700690 }
Mathias Agopian58959342010-10-07 14:57:04 -0700691 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700692 return (status_t)err;
693}
694
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700695status_t HWComposer::release(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700696 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700697 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700698 eventControl(disp, HWC_EVENT_VSYNC, 0);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700699 return (status_t)mHwc->blank(mHwc, disp, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700700 }
701 return NO_ERROR;
702}
703
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700704status_t HWComposer::acquire(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700705 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Colin Cross10fbdb62012-07-12 17:56:34 -0700706 if (mHwc) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700707 return (status_t)mHwc->blank(mHwc, disp, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700708 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700709 return NO_ERROR;
710}
711
Andy McFadden27ec5732012-10-02 19:04:45 -0700712void HWComposer::disconnectDisplay(int disp) {
Andy McFadden5a8f9012012-10-04 19:09:45 -0700713 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
Andy McFadden27ec5732012-10-02 19:04:45 -0700714 DisplayData& dd(mDisplayData[disp]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700715 free(dd.list);
716 dd.list = NULL;
717 dd.framebufferTarget = NULL; // points into dd.list
718 dd.fbTargetHandle = NULL;
719 dd.outbufHandle = NULL;
720 dd.lastRetireFence = Fence::NO_FENCE;
721 dd.lastDisplayFence = Fence::NO_FENCE;
722 dd.outbufAcquireFence = Fence::NO_FENCE;
Andy McFadden27ec5732012-10-02 19:04:45 -0700723}
724
Mathias Agopiancde87a32012-09-13 14:09:01 -0700725int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700726 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700727 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
728 // is supported by the implementation. we can only be in this case
729 // if we have HWC 1.1
730 return HAL_PIXEL_FORMAT_RGBA_8888;
731 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700732 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700733 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700734 }
735}
736
Mathias Agopianda27af92012-09-13 18:17:13 -0700737bool HWComposer::supportsFramebufferTarget() const {
738 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
739}
740
741int HWComposer::fbPost(int32_t id,
742 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700743 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700744 return setFramebufferTarget(id, acquireFence, buffer);
745 } else {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800746 acquireFence->waitForever(1000, "HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700747 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700748 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700749}
750
751int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700752 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700753 return NO_ERROR;
754
755 if (mFbDev->compositionComplete) {
756 return mFbDev->compositionComplete(mFbDev);
757 } else {
758 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700759 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700760}
761
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700762void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700763 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700764 const size_t SIZE = 4096;
765 char buffer[SIZE];
766 mFbDev->dump(mFbDev, buffer, SIZE);
767 result.append(buffer);
768 }
769}
770
Jesse Hall851cfe82013-03-20 13:44:00 -0700771status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
772 const sp<GraphicBuffer>& buf) {
773 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
774 return BAD_INDEX;
775 if (id < VIRTUAL_DISPLAY_ID_BASE)
776 return INVALID_OPERATION;
777
778 DisplayData& disp(mDisplayData[id]);
779 disp.outbufHandle = buf->handle;
780 disp.outbufAcquireFence = acquireFence;
781 return NO_ERROR;
782}
783
784sp<Fence> HWComposer::getLastRetireFence(int32_t id) {
785 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
786 return Fence::NO_FENCE;
787 return mDisplayData[id].lastRetireFence;
788}
789
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700790/*
791 * Helper template to implement a concrete HWCLayer
792 * This holds the pointer to the concrete hwc layer type
793 * and implements the "iterable" side of HWCLayer.
794 */
795template<typename CONCRETE, typename HWCTYPE>
796class Iterable : public HWComposer::HWCLayer {
797protected:
798 HWCTYPE* const mLayerList;
799 HWCTYPE* mCurrentLayer;
800 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
801 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
802 inline HWCTYPE* getLayer() { return mCurrentLayer; }
803 virtual ~Iterable() { }
804private:
805 // returns a copy of ourselves
806 virtual HWComposer::HWCLayer* dup() {
807 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
808 }
809 virtual status_t setLayer(size_t index) {
810 mCurrentLayer = &mLayerList[index];
811 return NO_ERROR;
812 }
813};
814
Jesse Hall5880cc52012-06-05 23:40:32 -0700815/*
816 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
817 * This implements the HWCLayer side of HWCIterableLayer.
818 */
819class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800820 struct hwc_composer_device_1* mHwc;
Jesse Hall5880cc52012-06-05 23:40:32 -0700821public:
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800822 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
823 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
Jesse Hall5880cc52012-06-05 23:40:32 -0700824
825 virtual int32_t getCompositionType() const {
826 return getLayer()->compositionType;
827 }
828 virtual uint32_t getHints() const {
829 return getLayer()->hints;
830 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700831 virtual sp<Fence> getAndResetReleaseFence() {
Jesse Hallef194142012-06-14 14:45:17 -0700832 int fd = getLayer()->releaseFenceFd;
833 getLayer()->releaseFenceFd = -1;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700834 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Jesse Hallef194142012-06-14 14:45:17 -0700835 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700836 virtual void setAcquireFenceFd(int fenceFd) {
837 getLayer()->acquireFenceFd = fenceFd;
838 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800839 virtual void setPerFrameDefaultState() {
840 //getLayer()->compositionType = HWC_FRAMEBUFFER;
841 }
842 virtual void setPlaneAlpha(uint8_t alpha) {
843 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
844 getLayer()->planeAlpha = alpha;
845 } else {
Mathias Agopian5fe58b82013-02-07 16:22:50 -0800846 if (alpha < 0xFF) {
847 getLayer()->flags |= HWC_SKIP_LAYER;
848 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800849 }
850 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700851 virtual void setDefaultState() {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800852 hwc_layer_1_t* const l = getLayer();
853 l->compositionType = HWC_FRAMEBUFFER;
854 l->hints = 0;
855 l->flags = HWC_SKIP_LAYER;
856 l->handle = 0;
857 l->transform = 0;
858 l->blending = HWC_BLENDING_NONE;
859 l->visibleRegionScreen.numRects = 0;
860 l->visibleRegionScreen.rects = NULL;
861 l->acquireFenceFd = -1;
862 l->releaseFenceFd = -1;
863 l->planeAlpha = 0xFF;
Jesse Hall5880cc52012-06-05 23:40:32 -0700864 }
865 virtual void setSkip(bool skip) {
866 if (skip) {
867 getLayer()->flags |= HWC_SKIP_LAYER;
868 } else {
869 getLayer()->flags &= ~HWC_SKIP_LAYER;
870 }
871 }
872 virtual void setBlending(uint32_t blending) {
873 getLayer()->blending = blending;
874 }
875 virtual void setTransform(uint32_t transform) {
876 getLayer()->transform = transform;
877 }
878 virtual void setFrame(const Rect& frame) {
879 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
880 }
881 virtual void setCrop(const Rect& crop) {
882 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
883 }
884 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700885 // Region::getSharedBuffer creates a reference to the underlying
886 // SharedBuffer of this Region, this reference is freed
887 // in onDisplayed()
888 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
889 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
890 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700891 }
892 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
893 if (buffer == 0 || buffer->handle == 0) {
894 getLayer()->compositionType = HWC_FRAMEBUFFER;
895 getLayer()->flags |= HWC_SKIP_LAYER;
896 getLayer()->handle = 0;
897 } else {
898 getLayer()->handle = buffer->handle;
899 }
900 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700901 virtual void onDisplayed() {
902 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
903 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
904 if (sb) {
905 sb->release();
906 // not technically needed but safer
907 visibleRegion.numRects = 0;
908 visibleRegion.rects = NULL;
909 }
Jesse Halle25d0052012-09-05 13:03:10 -0700910
911 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700912 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700913};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700914
915/*
916 * returns an iterator initialized at a given index in the layer list
917 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700918HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700919 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700920 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700921 }
922 const DisplayData& disp(mDisplayData[id]);
923 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700924 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700925 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800926 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700927}
928
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700929/*
930 * returns an iterator on the beginning of the layer list
931 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700932HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700933 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700934}
935
936/*
937 * returns an iterator on the end of the layer list
938 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700939HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700940 size_t numLayers = 0;
941 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
942 const DisplayData& disp(mDisplayData[id]);
943 if (mHwc && disp.list) {
944 numLayers = disp.list->numHwLayers;
945 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
946 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
947 // which we ignore when iterating through the layer list.
948 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
949 if (numLayers) {
950 numLayers--;
951 }
952 }
953 }
954 }
955 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700956}
957
Mathias Agopian74d211a2013-04-22 16:55:35 +0200958void HWComposer::dump(String8& result) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700959 if (mHwc) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700960 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -0700961 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
962 for (size_t i=0 ; i<mNumDisplays ; i++) {
963 const DisplayData& disp(mDisplayData[i]);
Jesse Hall02d86562013-03-25 14:43:23 -0700964 if (!disp.connected)
965 continue;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700966
Mathias Agopian13127d82013-03-05 17:47:11 -0800967 const Vector< sp<Layer> >& visibleLayersSortedByZ =
Mathias Agopiancb558572012-10-04 15:58:54 -0700968 mFlinger->getLayerSortedByZForHwcDisplay(i);
969
Jesse Hall02d86562013-03-25 14:43:23 -0700970 result.appendFormat(
971 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
972 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700973
Jesse Hall02d86562013-03-25 14:43:23 -0700974 if (disp.list) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700975 result.appendFormat(
976 " numHwLayers=%u, flags=%08x\n",
977 disp.list->numHwLayers, disp.list->flags);
978
Mathias Agopianf4358632012-08-22 17:16:19 -0700979 result.append(
Mathias Agopianda27af92012-09-13 18:17:13 -0700980 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
981 "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
982 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopianf4358632012-08-22 17:16:19 -0700983 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
984 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -0700985 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700986 String8 name("unknown");
Mathias Agopiancb558572012-10-04 15:58:54 -0700987
Mathias Agopianda27af92012-09-13 18:17:13 -0700988 if (i < visibleLayersSortedByZ.size()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800989 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
990 const sp<GraphicBuffer>& buffer(
991 layer->getActiveBuffer());
992 if (buffer != NULL) {
993 format = buffer->getPixelFormat();
Mathias Agopianf4358632012-08-22 17:16:19 -0700994 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700995 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -0700996 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700997
998 int type = l.compositionType;
999 if (type == HWC_FRAMEBUFFER_TARGET) {
1000 name = "HWC_FRAMEBUFFER_TARGET";
1001 format = disp.format;
1002 }
1003
1004 static char const* compositionTypeName[] = {
1005 "GLES",
1006 "HWC",
1007 "BACKGROUND",
1008 "FB TARGET",
1009 "UNKNOWN"};
1010 if (type >= NELEM(compositionTypeName))
1011 type = NELEM(compositionTypeName) - 1;
1012
Mathias Agopianf4358632012-08-22 17:16:19 -07001013 result.appendFormat(
Mathias Agopianda27af92012-09-13 18:17:13 -07001014 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
1015 compositionTypeName[type],
Mathias Agopianf4358632012-08-22 17:16:19 -07001016 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1017 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1018 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianda27af92012-09-13 18:17:13 -07001019 name.string());
Mathias Agopianfb4d5d52011-09-20 15:13:14 -07001020 }
1021 }
Mathias Agopian83727852010-09-23 18:13:21 -07001022 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001023 }
Mathias Agopian30bcc612012-08-22 15:39:48 -07001024
1025 if (mHwc && mHwc->dump) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02001026 const size_t SIZE = 4096;
1027 char buffer[SIZE];
Mathias Agopian30bcc612012-08-22 15:39:48 -07001028 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001029 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001030 }
1031}
1032
Mathias Agopiana350ff92010-08-10 17:14:02 -07001033// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -07001034
1035HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1036 : mHwc(hwc), mEnabled(false),
1037 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07001038 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -07001039{
1040}
1041
1042void HWComposer::VSyncThread::setEnabled(bool enabled) {
1043 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -07001044 if (mEnabled != enabled) {
1045 mEnabled = enabled;
1046 mCondition.signal();
1047 }
Mathias Agopian2965b262012-04-08 15:13:32 -07001048}
1049
1050void HWComposer::VSyncThread::onFirstRef() {
1051 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1052}
1053
1054bool HWComposer::VSyncThread::threadLoop() {
1055 { // scope for lock
1056 Mutex::Autolock _l(mLock);
1057 while (!mEnabled) {
1058 mCondition.wait(mLock);
1059 }
1060 }
1061
1062 const nsecs_t period = mRefreshPeriod;
1063 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1064 nsecs_t next_vsync = mNextFakeVSync;
1065 nsecs_t sleep = next_vsync - now;
1066 if (sleep < 0) {
1067 // we missed, find where the next vsync should be
1068 sleep = (period - ((now - next_vsync) % period));
1069 next_vsync = now + sleep;
1070 }
1071 mNextFakeVSync = next_vsync + period;
1072
1073 struct timespec spec;
1074 spec.tv_sec = next_vsync / 1000000000;
1075 spec.tv_nsec = next_vsync % 1000000000;
1076
1077 int err;
1078 do {
1079 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1080 } while (err<0 && errno == EINTR);
1081
1082 if (err == 0) {
1083 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1084 }
1085
1086 return true;
1087}
1088
Jesse Halla9a1b002013-02-27 16:39:25 -08001089HWComposer::DisplayData::DisplayData()
1090: width(0), height(0), format(0),
1091 xdpi(0.0f), ydpi(0.0f),
1092 refresh(0),
1093 connected(false),
1094 hasFbComp(false), hasOvComp(false),
1095 capacity(0), list(NULL),
1096 framebufferTarget(NULL), fbTargetHandle(0),
1097 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
Jesse Hall851cfe82013-03-20 13:44:00 -07001098 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
Jesse Halla9a1b002013-02-27 16:39:25 -08001099 events(0)
1100{}
1101
1102HWComposer::DisplayData::~DisplayData() {
1103 free(list);
1104}
1105
Mathias Agopian2965b262012-04-08 15:13:32 -07001106// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001107}; // namespace android