blob: 5082192cf69ce575e57c690171408eb0e667ef9b [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 Halle737c112013-05-07 11:58:47 -070053// This is not a real HWC version. It's used for in-development features that
54// haven't been committed to a specific real HWC version.
55#define HWC_DEVICE_API_VERSION_1_EXP HARDWARE_DEVICE_API_VERSION_2(1, 0xFF, HWC_HEADER_VERSION)
56
Jesse Hall72960512013-01-10 16:19:56 -080057#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070058
Jesse Hallafaf14b2013-03-20 13:42:29 -070059#define NUM_PHYSICAL_DISPLAYS HWC_NUM_DISPLAY_TYPES
60#define VIRTUAL_DISPLAY_ID_BASE HWC_NUM_DISPLAY_TYPES
61
Jesse Hall9eb1eb52012-08-29 10:39:38 -070062static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
63 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070064 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
65}
66
67static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
68 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070069 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
70}
71
72static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
73 uint32_t version) {
74 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070075}
76
Mathias Agopiana350ff92010-08-10 17:14:02 -070077// ---------------------------------------------------------------------------
78
Mathias Agopian3e8b8532012-05-13 20:42:01 -070079struct HWComposer::cb_context {
80 struct callbacks : public hwc_procs_t {
81 // these are here to facilitate the transition when adding
82 // new callbacks (an implementation can check for NULL before
83 // calling a new callback).
84 void (*zero[4])(void);
85 };
86 callbacks procs;
87 HWComposer* hwc;
88};
89
90// ---------------------------------------------------------------------------
91
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070092HWComposer::HWComposer(
93 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070094 EventHandler& handler)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070095 : mFlinger(flinger),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070096 mFbDev(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070097 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070098 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070099 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700100{
Mathias Agopiane60b0682012-08-21 23:34:09 -0700101 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
102 mLists[i] = 0;
103 }
Jesse Hallb685c542012-07-31 14:32:56 -0700104
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700105 char value[PROPERTY_VALUE_MAX];
106 property_get("debug.sf.no_hw_vsync", value, "0");
107 mDebugForceFakeVSync = atoi(value);
108
Mathias Agopian028508c2012-07-25 21:12:12 -0700109 bool needVSyncThread = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700110
111 // Note: some devices may insist that the FB HAL be opened before HWC.
Jesse Hallef64b752013-03-18 11:28:50 -0700112 int fberr = loadFbHalModule();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700113 loadHwcModule();
114
Mathias Agopianda27af92012-09-13 18:17:13 -0700115 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
116 // close FB HAL if we don't needed it.
117 // FIXME: this is temporary until we're not forced to open FB HAL
118 // before HWC.
119 framebuffer_close(mFbDev);
120 mFbDev = NULL;
121 }
122
Andy McFaddenbabba182012-09-12 13:14:51 -0700123 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
124 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
125 && !mFbDev) {
Jesse Hallef64b752013-03-18 11:28:50 -0700126 ALOGE("ERROR: failed to open framebuffer (%s), aborting",
127 strerror(-fberr));
Andy McFadden43601a22012-09-11 15:15:13 -0700128 abort();
129 }
130
Andy McFadden620685c2012-10-19 12:53:46 -0700131 // these display IDs are always reserved
Jesse Hallafaf14b2013-03-20 13:42:29 -0700132 for (size_t i=0 ; i<NUM_PHYSICAL_DISPLAYS ; i++) {
Andy McFadden620685c2012-10-19 12:53:46 -0700133 mAllocatedDisplayIDs.markBit(i);
134 }
135
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700136 if (mHwc) {
137 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
138 (hwcApiVersion(mHwc) >> 24) & 0xff,
139 (hwcApiVersion(mHwc) >> 16) & 0xff);
140 if (mHwc->registerProcs) {
141 mCBContext->hwc = this;
142 mCBContext->procs.invalidate = &hook_invalidate;
143 mCBContext->procs.vsync = &hook_vsync;
144 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
145 mCBContext->procs.hotplug = &hook_hotplug;
146 else
147 mCBContext->procs.hotplug = NULL;
148 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
149 mHwc->registerProcs(mHwc, &mCBContext->procs);
Jesse Hall5880cc52012-06-05 23:40:32 -0700150 }
151
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700152 // don't need a vsync thread if we have a hardware composer
153 needVSyncThread = false;
154 // always turn vsync off when we start
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700155 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700156
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700157 // the number of displays we actually have depends on the
158 // hw composer version
Jesse Halle737c112013-05-07 11:58:47 -0700159 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_EXP)) {
160 // 1.?? adds support for virtual displays
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700161 mNumDisplays = MAX_DISPLAYS;
162 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
163 // 1.1 adds support for multiple displays
Jesse Hallafaf14b2013-03-20 13:42:29 -0700164 mNumDisplays = NUM_PHYSICAL_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700165 } 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
Jesse Hallafaf14b2013-03-20 13:42:29 -0700192 for (size_t i =0 ; i<NUM_PHYSICAL_DISPLAYS ; i++) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700193 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.
Jesse Hallef64b752013-03-18 11:28:50 -0700248int HWComposer::loadFbHalModule()
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700249{
250 hw_module_t const* module;
251
Jesse Hallef64b752013-03-18 11:28:50 -0700252 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
253 if (err != 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700254 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
Jesse Hallef64b752013-03-18 11:28:50 -0700255 return err;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700256 }
257
Jesse Hallef64b752013-03-18 11:28:50 -0700258 return framebuffer_open(module, &mFbDev);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700259}
260
Mathias Agopiana350ff92010-08-10 17:14:02 -0700261status_t HWComposer::initCheck() const {
262 return mHwc ? NO_ERROR : NO_INIT;
263}
264
Jesse Hallbbd164a2012-08-21 12:05:09 -0700265void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
266 cb_context* ctx = reinterpret_cast<cb_context*>(
267 const_cast<hwc_procs_t*>(procs));
268 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700269}
270
Jesse Hall1bd20e02012-08-29 10:47:52 -0700271void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700272 int64_t timestamp) {
273 cb_context* ctx = reinterpret_cast<cb_context*>(
274 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700275 ctx->hwc->vsync(disp, timestamp);
276}
277
278void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
279 int connected) {
280 cb_context* ctx = reinterpret_cast<cb_context*>(
281 const_cast<hwc_procs_t*>(procs));
282 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700283}
284
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700285void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700286 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700287}
288
Jesse Hall1bd20e02012-08-29 10:47:52 -0700289void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700290 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700291 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700292 Mutex::Autolock _l(mLock);
293 mLastHwVSync = timestamp;
294}
295
Jesse Hall1bd20e02012-08-29 10:47:52 -0700296void HWComposer::hotplug(int disp, int connected) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700297 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700298 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
299 disp, connected);
300 return;
301 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700302 queryDisplayProperties(disp);
Mathias Agopian148994e2012-09-19 17:31:36 -0700303 mEventHandler.onHotplugReceived(disp, bool(connected));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700304}
305
Jesse Hall1c569c42013-04-05 13:44:52 -0700306static float getDefaultDensity(uint32_t height) {
307 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
308 else return ACONFIGURATION_DENSITY_TV;
309}
310
Jesse Hall1bd20e02012-08-29 10:47:52 -0700311static const uint32_t DISPLAY_ATTRIBUTES[] = {
312 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700313 HWC_DISPLAY_WIDTH,
314 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700315 HWC_DISPLAY_DPI_X,
316 HWC_DISPLAY_DPI_Y,
317 HWC_DISPLAY_NO_ATTRIBUTE,
318};
319#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
320
Mathias Agopian1604f772012-09-18 21:54:42 -0700321status_t HWComposer::queryDisplayProperties(int disp) {
322
Mathias Agopianda27af92012-09-13 18:17:13 -0700323 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700324
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700325 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700326 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
327 memset(values, 0, sizeof(values));
328
329 uint32_t config;
330 size_t numConfigs = 1;
331 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700332 if (err != NO_ERROR) {
333 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700334 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700335 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700336 }
337
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700338 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
339 if (err != NO_ERROR) {
340 // we can't get this display's info. turn it off.
341 mDisplayData[disp].connected = false;
342 return err;
343 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700344
Jesse Hall1bd20e02012-08-29 10:47:52 -0700345 int32_t w = 0, h = 0;
346 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
347 switch (DISPLAY_ATTRIBUTES[i]) {
348 case HWC_DISPLAY_VSYNC_PERIOD:
349 mDisplayData[disp].refresh = nsecs_t(values[i]);
350 break;
Jesse Halldb276212012-09-07 11:20:56 -0700351 case HWC_DISPLAY_WIDTH:
352 mDisplayData[disp].width = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700353 break;
Jesse Halldb276212012-09-07 11:20:56 -0700354 case HWC_DISPLAY_HEIGHT:
355 mDisplayData[disp].height = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700356 break;
357 case HWC_DISPLAY_DPI_X:
358 mDisplayData[disp].xdpi = values[i] / 1000.0f;
359 break;
360 case HWC_DISPLAY_DPI_Y:
361 mDisplayData[disp].ydpi = values[i] / 1000.0f;
362 break;
363 default:
Mathias Agopianda27af92012-09-13 18:17:13 -0700364 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
365 i, DISPLAY_ATTRIBUTES[i]);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700366 break;
367 }
368 }
369
Mathias Agopianf5a33922012-09-19 18:16:22 -0700370 // FIXME: what should we set the format to?
371 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
372 mDisplayData[disp].connected = true;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700373 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
Jesse Hall1c569c42013-04-05 13:44:52 -0700374 float dpi = getDefaultDensity(h);
375 mDisplayData[disp].xdpi = dpi;
376 mDisplayData[disp].ydpi = dpi;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700377 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700378 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700379}
380
Jesse Hall1c569c42013-04-05 13:44:52 -0700381status_t HWComposer::setVirtualDisplayProperties(int32_t id,
382 uint32_t w, uint32_t h, uint32_t format) {
383 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
384 !mAllocatedDisplayIDs.hasBit(id)) {
385 return BAD_INDEX;
386 }
387 mDisplayData[id].width = w;
388 mDisplayData[id].height = h;
389 mDisplayData[id].format = format;
390 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h);
391 return NO_ERROR;
392}
393
Mathias Agopiane60b0682012-08-21 23:34:09 -0700394int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700395 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700396 return NO_MEMORY;
397 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700398 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
399 mAllocatedDisplayIDs.markBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800400 mDisplayData[id].connected = true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700401 return id;
402}
403
404status_t HWComposer::freeDisplayId(int32_t id) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700405 if (id < NUM_PHYSICAL_DISPLAYS) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700406 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700407 return BAD_VALUE;
408 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700409 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700410 return BAD_INDEX;
411 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700412 mAllocatedDisplayIDs.clearBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800413 mDisplayData[id].connected = false;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700414 return NO_ERROR;
415}
416
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700417nsecs_t HWComposer::getRefreshPeriod(int disp) const {
418 return mDisplayData[disp].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700419}
420
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700421nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700422 // this returns the last refresh timestamp.
423 // if the last one is not available, we estimate it based on
424 // the refresh period and whatever closest timestamp we have.
425 Mutex::Autolock _l(mLock);
426 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700427 return now - ((now - mLastHwVSync) % mDisplayData[disp].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700428}
429
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800430sp<Fence> HWComposer::getDisplayFence(int disp) const {
431 return mDisplayData[disp].lastDisplayFence;
432}
433
Jesse Halldb276212012-09-07 11:20:56 -0700434uint32_t HWComposer::getWidth(int disp) const {
435 return mDisplayData[disp].width;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700436}
437
Jesse Halldb276212012-09-07 11:20:56 -0700438uint32_t HWComposer::getHeight(int disp) const {
439 return mDisplayData[disp].height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700440}
441
442uint32_t HWComposer::getFormat(int disp) const {
443 return mDisplayData[disp].format;
444}
445
446float HWComposer::getDpiX(int disp) const {
447 return mDisplayData[disp].xdpi;
448}
449
450float HWComposer::getDpiY(int disp) const {
451 return mDisplayData[disp].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700452}
453
Mathias Agopianf5a33922012-09-19 18:16:22 -0700454bool HWComposer::isConnected(int disp) const {
455 return mDisplayData[disp].connected;
456}
457
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700458void HWComposer::eventControl(int disp, int event, int enabled) {
459 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
Andy McFadden620685c2012-10-19 12:53:46 -0700460 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
461 event, disp, enabled);
462 return;
463 }
464 if (event != EVENT_VSYNC) {
465 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
466 event, disp, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700467 return;
468 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700469 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700470 if (mHwc && !mDebugForceFakeVSync) {
471 // NOTE: we use our own internal lock here because we have to call
472 // into the HWC with the lock held, and we want to make sure
473 // that even if HWC blocks (which it shouldn't), it won't
474 // affect other threads.
475 Mutex::Autolock _l(mEventControlLock);
476 const int32_t eventBit = 1UL << event;
477 const int32_t newValue = enabled ? eventBit : 0;
478 const int32_t oldValue = mDisplayData[disp].events & eventBit;
479 if (newValue != oldValue) {
480 ATRACE_CALL();
481 err = mHwc->eventControl(mHwc, disp, event, enabled);
482 if (!err) {
483 int32_t& events(mDisplayData[disp].events);
484 events = (events & ~eventBit) | newValue;
485 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700486 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700487 // error here should not happen -- not sure what we should
488 // do if it does.
489 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
490 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700491 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700492
493 if (err == NO_ERROR && mVSyncThread != NULL) {
494 mVSyncThread->setEnabled(enabled);
495 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700496}
497
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700498status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700499 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700500 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700501 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700502
Mathias Agopian45721772010-08-12 15:03:26 -0700503 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700504 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700505 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
506 // we need space for the HWC_FRAMEBUFFER_TARGET
507 numLayers++;
508 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700509 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700510 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700511 + numLayers * sizeof(hwc_layer_1_t);
512 free(disp.list);
513 disp.list = (hwc_display_contents_1_t*)malloc(size);
514 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700515 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700516 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
517 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
518 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800519 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
Mathias Agopianda27af92012-09-13 18:17:13 -0700520 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
521 disp.framebufferTarget->hints = 0;
522 disp.framebufferTarget->flags = 0;
523 disp.framebufferTarget->handle = disp.fbTargetHandle;
524 disp.framebufferTarget->transform = 0;
525 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
526 disp.framebufferTarget->sourceCrop = r;
527 disp.framebufferTarget->displayFrame = r;
528 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
529 disp.framebufferTarget->visibleRegionScreen.rects =
530 &disp.framebufferTarget->displayFrame;
531 disp.framebufferTarget->acquireFenceFd = -1;
532 disp.framebufferTarget->releaseFenceFd = -1;
Mathias Agopian70a6e882013-03-21 16:25:12 -0700533 disp.framebufferTarget->planeAlpha = 0xFF;
Mathias Agopianda27af92012-09-13 18:17:13 -0700534 }
Jesse Halldb276212012-09-07 11:20:56 -0700535 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700536 disp.list->flags = HWC_GEOMETRY_CHANGED;
537 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700538 }
539 return NO_ERROR;
540}
541
Mathias Agopianda27af92012-09-13 18:17:13 -0700542status_t HWComposer::setFramebufferTarget(int32_t id,
543 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
544 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
545 return BAD_INDEX;
546 }
547 DisplayData& disp(mDisplayData[id]);
548 if (!disp.framebufferTarget) {
549 // this should never happen, but apparently eglCreateWindowSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800550 // triggers a Surface::queueBuffer() on some
Mathias Agopianda27af92012-09-13 18:17:13 -0700551 // devices (!?) -- log and ignore.
552 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianda27af92012-09-13 18:17:13 -0700553 return NO_ERROR;
554 }
555
556 int acquireFenceFd = -1;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800557 if (acquireFence->isValid()) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700558 acquireFenceFd = acquireFence->dup();
559 }
560
561 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
562 disp.fbTargetHandle = buf->handle;
563 disp.framebufferTarget->handle = disp.fbTargetHandle;
564 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
565 return NO_ERROR;
566}
567
Mathias Agopiane60b0682012-08-21 23:34:09 -0700568status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700569 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700570 DisplayData& disp(mDisplayData[i]);
571 if (disp.framebufferTarget) {
572 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
573 // DO NOT reset the handle field to NULL, because it's possible
574 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
575 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700576 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700577 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
578 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700579 if (!disp.connected && disp.list != NULL) {
580 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
581 i, disp.list->numHwLayers);
582 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700583 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700584 if (mLists[i]) {
Jesse Halle737c112013-05-07 11:58:47 -0700585 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_EXP)) {
Jesse Halldb276212012-09-07 11:20:56 -0700586 mLists[i]->outbuf = NULL;
587 mLists[i]->outbufAcquireFenceFd = -1;
588 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
589 // garbage data to catch improper use
590 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
591 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
592 } else {
593 mLists[i]->dpy = EGL_NO_DISPLAY;
594 mLists[i]->sur = EGL_NO_SURFACE;
595 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700596 }
597 }
Jesse Halldb276212012-09-07 11:20:56 -0700598
Mathias Agopianf4358632012-08-22 17:16:19 -0700599 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700600 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
601
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700602 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700603 // here we're just making sure that "skip" layers are set
604 // to HWC_FRAMEBUFFER and we're also counting how many layers
605 // we have of each type.
Mathias Agopianf4358632012-08-22 17:16:19 -0700606 for (size_t i=0 ; i<mNumDisplays ; i++) {
607 DisplayData& disp(mDisplayData[i]);
608 disp.hasFbComp = false;
609 disp.hasOvComp = false;
610 if (disp.list) {
611 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
612 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700613
614 //ALOGD("prepare: %d, type=%d, handle=%p",
615 // i, l.compositionType, l.handle);
616
Mathias Agopianf4358632012-08-22 17:16:19 -0700617 if (l.flags & HWC_SKIP_LAYER) {
618 l.compositionType = HWC_FRAMEBUFFER;
619 }
620 if (l.compositionType == HWC_FRAMEBUFFER) {
621 disp.hasFbComp = true;
622 }
623 if (l.compositionType == HWC_OVERLAY) {
624 disp.hasOvComp = true;
625 }
626 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700627 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700628 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700629 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700630 return (status_t)err;
631}
632
Mathias Agopiane60b0682012-08-21 23:34:09 -0700633bool HWComposer::hasHwcComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700634 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700635 return false;
636 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700637}
638
Mathias Agopiane60b0682012-08-21 23:34:09 -0700639bool HWComposer::hasGlesComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700640 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
641 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700642 return mDisplayData[id].hasFbComp;
643}
644
Jesse Hall13f01cb2013-03-20 11:37:21 -0700645sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700646 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Jesse Hall13f01cb2013-03-20 11:37:21 -0700647 return Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700648
649 int fd = INVALID_OPERATION;
650 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
651 const DisplayData& disp(mDisplayData[id]);
652 if (disp.framebufferTarget) {
653 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700654 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700655 disp.framebufferTarget->releaseFenceFd = -1;
656 }
657 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700658 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700659}
660
Mathias Agopian30bcc612012-08-22 15:39:48 -0700661status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700662 int err = NO_ERROR;
663 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700664 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700665 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700666 // by the (dpy, sur) fields and we are guaranteed to have only
667 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700668 mLists[0]->dpy = eglGetCurrentDisplay();
669 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700670 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700671
Jesse Hallafaf14b2013-03-20 13:42:29 -0700672 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
Jesse Hall80e0a392013-03-15 12:32:10 -0700673 DisplayData& disp(mDisplayData[i]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700674 if (disp.outbufHandle) {
675 mLists[i]->outbuf = disp.outbufHandle;
Jesse Hall80e0a392013-03-15 12:32:10 -0700676 mLists[i]->outbufAcquireFenceFd =
Jesse Hall851cfe82013-03-20 13:44:00 -0700677 disp.outbufAcquireFence->dup();
Jesse Hall80e0a392013-03-15 12:32:10 -0700678 }
679 }
680
Mathias Agopian30bcc612012-08-22 15:39:48 -0700681 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700682
683 for (size_t i=0 ; i<mNumDisplays ; i++) {
684 DisplayData& disp(mDisplayData[i]);
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800685 disp.lastDisplayFence = disp.lastRetireFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800686 disp.lastRetireFence = Fence::NO_FENCE;
Mathias Agopianf4358632012-08-22 17:16:19 -0700687 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700688 if (disp.list->retireFenceFd != -1) {
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800689 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
Jesse Halldb276212012-09-07 11:20:56 -0700690 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700691 }
692 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
693 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700694 }
Mathias Agopian58959342010-10-07 14:57:04 -0700695 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700696 return (status_t)err;
697}
698
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700699status_t HWComposer::release(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700700 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700701 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700702 eventControl(disp, HWC_EVENT_VSYNC, 0);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700703 return (status_t)mHwc->blank(mHwc, disp, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700704 }
705 return NO_ERROR;
706}
707
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700708status_t HWComposer::acquire(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700709 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Colin Cross10fbdb62012-07-12 17:56:34 -0700710 if (mHwc) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700711 return (status_t)mHwc->blank(mHwc, disp, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700712 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700713 return NO_ERROR;
714}
715
Andy McFadden27ec5732012-10-02 19:04:45 -0700716void HWComposer::disconnectDisplay(int disp) {
Andy McFadden5a8f9012012-10-04 19:09:45 -0700717 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
Andy McFadden27ec5732012-10-02 19:04:45 -0700718 DisplayData& dd(mDisplayData[disp]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700719 free(dd.list);
720 dd.list = NULL;
721 dd.framebufferTarget = NULL; // points into dd.list
722 dd.fbTargetHandle = NULL;
723 dd.outbufHandle = NULL;
724 dd.lastRetireFence = Fence::NO_FENCE;
725 dd.lastDisplayFence = Fence::NO_FENCE;
726 dd.outbufAcquireFence = Fence::NO_FENCE;
Andy McFadden27ec5732012-10-02 19:04:45 -0700727}
728
Mathias Agopiancde87a32012-09-13 14:09:01 -0700729int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700730 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700731 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
732 // is supported by the implementation. we can only be in this case
733 // if we have HWC 1.1
734 return HAL_PIXEL_FORMAT_RGBA_8888;
735 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700736 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700737 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700738 }
739}
740
Mathias Agopianda27af92012-09-13 18:17:13 -0700741bool HWComposer::supportsFramebufferTarget() const {
742 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
743}
744
745int HWComposer::fbPost(int32_t id,
746 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700747 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700748 return setFramebufferTarget(id, acquireFence, buffer);
749 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700750 acquireFence->waitForever("HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700751 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700752 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700753}
754
755int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700756 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700757 return NO_ERROR;
758
759 if (mFbDev->compositionComplete) {
760 return mFbDev->compositionComplete(mFbDev);
761 } else {
762 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700763 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700764}
765
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700766void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700767 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700768 const size_t SIZE = 4096;
769 char buffer[SIZE];
770 mFbDev->dump(mFbDev, buffer, SIZE);
771 result.append(buffer);
772 }
773}
774
Jesse Hall851cfe82013-03-20 13:44:00 -0700775status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
776 const sp<GraphicBuffer>& buf) {
777 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
778 return BAD_INDEX;
779 if (id < VIRTUAL_DISPLAY_ID_BASE)
780 return INVALID_OPERATION;
781
782 DisplayData& disp(mDisplayData[id]);
783 disp.outbufHandle = buf->handle;
784 disp.outbufAcquireFence = acquireFence;
785 return NO_ERROR;
786}
787
788sp<Fence> HWComposer::getLastRetireFence(int32_t id) {
789 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
790 return Fence::NO_FENCE;
791 return mDisplayData[id].lastRetireFence;
792}
793
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700794/*
795 * Helper template to implement a concrete HWCLayer
796 * This holds the pointer to the concrete hwc layer type
797 * and implements the "iterable" side of HWCLayer.
798 */
799template<typename CONCRETE, typename HWCTYPE>
800class Iterable : public HWComposer::HWCLayer {
801protected:
802 HWCTYPE* const mLayerList;
803 HWCTYPE* mCurrentLayer;
804 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
805 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
806 inline HWCTYPE* getLayer() { return mCurrentLayer; }
807 virtual ~Iterable() { }
808private:
809 // returns a copy of ourselves
810 virtual HWComposer::HWCLayer* dup() {
811 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
812 }
813 virtual status_t setLayer(size_t index) {
814 mCurrentLayer = &mLayerList[index];
815 return NO_ERROR;
816 }
817};
818
Jesse Hall5880cc52012-06-05 23:40:32 -0700819/*
820 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
821 * This implements the HWCLayer side of HWCIterableLayer.
822 */
823class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800824 struct hwc_composer_device_1* mHwc;
Jesse Hall5880cc52012-06-05 23:40:32 -0700825public:
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800826 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
827 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
Jesse Hall5880cc52012-06-05 23:40:32 -0700828
829 virtual int32_t getCompositionType() const {
830 return getLayer()->compositionType;
831 }
832 virtual uint32_t getHints() const {
833 return getLayer()->hints;
834 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700835 virtual sp<Fence> getAndResetReleaseFence() {
Jesse Hallef194142012-06-14 14:45:17 -0700836 int fd = getLayer()->releaseFenceFd;
837 getLayer()->releaseFenceFd = -1;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700838 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Jesse Hallef194142012-06-14 14:45:17 -0700839 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700840 virtual void setAcquireFenceFd(int fenceFd) {
841 getLayer()->acquireFenceFd = fenceFd;
842 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800843 virtual void setPerFrameDefaultState() {
844 //getLayer()->compositionType = HWC_FRAMEBUFFER;
845 }
846 virtual void setPlaneAlpha(uint8_t alpha) {
847 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
848 getLayer()->planeAlpha = alpha;
849 } else {
Mathias Agopian5fe58b82013-02-07 16:22:50 -0800850 if (alpha < 0xFF) {
851 getLayer()->flags |= HWC_SKIP_LAYER;
852 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800853 }
854 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700855 virtual void setDefaultState() {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800856 hwc_layer_1_t* const l = getLayer();
857 l->compositionType = HWC_FRAMEBUFFER;
858 l->hints = 0;
859 l->flags = HWC_SKIP_LAYER;
860 l->handle = 0;
861 l->transform = 0;
862 l->blending = HWC_BLENDING_NONE;
863 l->visibleRegionScreen.numRects = 0;
864 l->visibleRegionScreen.rects = NULL;
865 l->acquireFenceFd = -1;
866 l->releaseFenceFd = -1;
867 l->planeAlpha = 0xFF;
Jesse Hall5880cc52012-06-05 23:40:32 -0700868 }
869 virtual void setSkip(bool skip) {
870 if (skip) {
871 getLayer()->flags |= HWC_SKIP_LAYER;
872 } else {
873 getLayer()->flags &= ~HWC_SKIP_LAYER;
874 }
875 }
876 virtual void setBlending(uint32_t blending) {
877 getLayer()->blending = blending;
878 }
879 virtual void setTransform(uint32_t transform) {
880 getLayer()->transform = transform;
881 }
882 virtual void setFrame(const Rect& frame) {
883 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
884 }
885 virtual void setCrop(const Rect& crop) {
886 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
887 }
888 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700889 // Region::getSharedBuffer creates a reference to the underlying
890 // SharedBuffer of this Region, this reference is freed
891 // in onDisplayed()
892 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
893 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
894 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700895 }
896 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
897 if (buffer == 0 || buffer->handle == 0) {
898 getLayer()->compositionType = HWC_FRAMEBUFFER;
899 getLayer()->flags |= HWC_SKIP_LAYER;
900 getLayer()->handle = 0;
901 } else {
902 getLayer()->handle = buffer->handle;
903 }
904 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700905 virtual void onDisplayed() {
906 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
907 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
908 if (sb) {
909 sb->release();
910 // not technically needed but safer
911 visibleRegion.numRects = 0;
912 visibleRegion.rects = NULL;
913 }
Jesse Halle25d0052012-09-05 13:03:10 -0700914
915 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700916 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700917};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700918
919/*
920 * returns an iterator initialized at a given index in the layer list
921 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700922HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700923 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700924 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700925 }
926 const DisplayData& disp(mDisplayData[id]);
927 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700928 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700929 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800930 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700931}
932
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700933/*
934 * returns an iterator on the beginning of the layer list
935 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700936HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700937 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700938}
939
940/*
941 * returns an iterator on the end of the layer list
942 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700943HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700944 size_t numLayers = 0;
945 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
946 const DisplayData& disp(mDisplayData[id]);
947 if (mHwc && disp.list) {
948 numLayers = disp.list->numHwLayers;
949 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
950 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
951 // which we ignore when iterating through the layer list.
952 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
953 if (numLayers) {
954 numLayers--;
955 }
956 }
957 }
958 }
959 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700960}
961
Mathias Agopian74d211a2013-04-22 16:55:35 +0200962void HWComposer::dump(String8& result) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700963 if (mHwc) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700964 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -0700965 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
966 for (size_t i=0 ; i<mNumDisplays ; i++) {
967 const DisplayData& disp(mDisplayData[i]);
Jesse Hall02d86562013-03-25 14:43:23 -0700968 if (!disp.connected)
969 continue;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700970
Mathias Agopian13127d82013-03-05 17:47:11 -0800971 const Vector< sp<Layer> >& visibleLayersSortedByZ =
Mathias Agopiancb558572012-10-04 15:58:54 -0700972 mFlinger->getLayerSortedByZForHwcDisplay(i);
973
Jesse Hall02d86562013-03-25 14:43:23 -0700974 result.appendFormat(
975 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
976 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700977
Jesse Hall02d86562013-03-25 14:43:23 -0700978 if (disp.list) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700979 result.appendFormat(
980 " numHwLayers=%u, flags=%08x\n",
981 disp.list->numHwLayers, disp.list->flags);
982
Mathias Agopianf4358632012-08-22 17:16:19 -0700983 result.append(
Mathias Agopianda27af92012-09-13 18:17:13 -0700984 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
985 "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
986 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopianf4358632012-08-22 17:16:19 -0700987 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
988 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -0700989 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700990 String8 name("unknown");
Mathias Agopiancb558572012-10-04 15:58:54 -0700991
Mathias Agopianda27af92012-09-13 18:17:13 -0700992 if (i < visibleLayersSortedByZ.size()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800993 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
994 const sp<GraphicBuffer>& buffer(
995 layer->getActiveBuffer());
996 if (buffer != NULL) {
997 format = buffer->getPixelFormat();
Mathias Agopianf4358632012-08-22 17:16:19 -0700998 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700999 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -07001000 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001001
1002 int type = l.compositionType;
1003 if (type == HWC_FRAMEBUFFER_TARGET) {
1004 name = "HWC_FRAMEBUFFER_TARGET";
1005 format = disp.format;
1006 }
1007
1008 static char const* compositionTypeName[] = {
1009 "GLES",
1010 "HWC",
1011 "BACKGROUND",
1012 "FB TARGET",
1013 "UNKNOWN"};
1014 if (type >= NELEM(compositionTypeName))
1015 type = NELEM(compositionTypeName) - 1;
1016
Mathias Agopianf4358632012-08-22 17:16:19 -07001017 result.appendFormat(
Mathias Agopianda27af92012-09-13 18:17:13 -07001018 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
1019 compositionTypeName[type],
Mathias Agopianf4358632012-08-22 17:16:19 -07001020 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1021 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1022 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianda27af92012-09-13 18:17:13 -07001023 name.string());
Mathias Agopianfb4d5d52011-09-20 15:13:14 -07001024 }
1025 }
Mathias Agopian83727852010-09-23 18:13:21 -07001026 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001027 }
Mathias Agopian30bcc612012-08-22 15:39:48 -07001028
1029 if (mHwc && mHwc->dump) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02001030 const size_t SIZE = 4096;
1031 char buffer[SIZE];
Mathias Agopian30bcc612012-08-22 15:39:48 -07001032 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001033 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001034 }
1035}
1036
Mathias Agopiana350ff92010-08-10 17:14:02 -07001037// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -07001038
1039HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1040 : mHwc(hwc), mEnabled(false),
1041 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07001042 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -07001043{
1044}
1045
1046void HWComposer::VSyncThread::setEnabled(bool enabled) {
1047 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -07001048 if (mEnabled != enabled) {
1049 mEnabled = enabled;
1050 mCondition.signal();
1051 }
Mathias Agopian2965b262012-04-08 15:13:32 -07001052}
1053
1054void HWComposer::VSyncThread::onFirstRef() {
1055 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1056}
1057
1058bool HWComposer::VSyncThread::threadLoop() {
1059 { // scope for lock
1060 Mutex::Autolock _l(mLock);
1061 while (!mEnabled) {
1062 mCondition.wait(mLock);
1063 }
1064 }
1065
1066 const nsecs_t period = mRefreshPeriod;
1067 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1068 nsecs_t next_vsync = mNextFakeVSync;
1069 nsecs_t sleep = next_vsync - now;
1070 if (sleep < 0) {
1071 // we missed, find where the next vsync should be
1072 sleep = (period - ((now - next_vsync) % period));
1073 next_vsync = now + sleep;
1074 }
1075 mNextFakeVSync = next_vsync + period;
1076
1077 struct timespec spec;
1078 spec.tv_sec = next_vsync / 1000000000;
1079 spec.tv_nsec = next_vsync % 1000000000;
1080
1081 int err;
1082 do {
1083 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1084 } while (err<0 && errno == EINTR);
1085
1086 if (err == 0) {
1087 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1088 }
1089
1090 return true;
1091}
1092
Jesse Halla9a1b002013-02-27 16:39:25 -08001093HWComposer::DisplayData::DisplayData()
1094: width(0), height(0), format(0),
1095 xdpi(0.0f), ydpi(0.0f),
1096 refresh(0),
1097 connected(false),
1098 hasFbComp(false), hasOvComp(false),
1099 capacity(0), list(NULL),
1100 framebufferTarget(NULL), fbTargetHandle(0),
1101 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
Jesse Hall851cfe82013-03-20 13:44:00 -07001102 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
Jesse Halla9a1b002013-02-27 16:39:25 -08001103 events(0)
1104{}
1105
1106HWComposer::DisplayData::~DisplayData() {
1107 free(list);
1108}
1109
Mathias Agopian2965b262012-04-08 15:13:32 -07001110// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001111}; // namespace android