blob: ca98133b5f46a6b90a7c4f43b6eebc0d50c5fd9e [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
Mathias Agopiana350ff92010-08-10 17:14:02 -070019#include <stdint.h>
Mathias Agopianf1352df2010-08-11 17:31:33 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070023#include <sys/types.h>
24
Mathias Agopian33ceeb32013-04-01 16:54:58 -070025#include <utils/CallStack.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070026#include <utils/Errors.h>
Mathias Agopianda27af92012-09-13 18:17:13 -070027#include <utils/misc.h>
Mathias Agopian83727852010-09-23 18:13:21 -070028#include <utils/String8.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070029#include <utils/Thread.h>
Mathias Agopian2965b262012-04-08 15:13:32 -070030#include <utils/Trace.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070031#include <utils/Vector.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070032
Mathias Agopian921e6ac2012-07-23 23:11:29 -070033#include <ui/GraphicBuffer.h>
34
Mathias Agopiana350ff92010-08-10 17:14:02 -070035#include <hardware/hardware.h>
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070036#include <hardware/hwcomposer.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070037
Jesse Hall1c569c42013-04-05 13:44:52 -070038#include <android/configuration.h>
39
Mathias Agopiana350ff92010-08-10 17:14:02 -070040#include <cutils/log.h>
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070041#include <cutils/properties.h>
Mathias Agopiana350ff92010-08-10 17:14:02 -070042
Mathias Agopiana350ff92010-08-10 17:14:02 -070043#include "HWComposer.h"
Mathias Agopian33ceeb32013-04-01 16:54:58 -070044
45#include "../Layer.h" // needed only for debugging
46#include "../SurfaceFlinger.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070047
48namespace android {
Jesse Hall5880cc52012-06-05 23:40:32 -070049
Jesse Hall38efe862013-04-06 23:12:29 -070050#ifndef HWC_DEVICE_API_VERSION_1_3
51#define HWC_DEVICE_API_VERSION_1_3 HARDWARE_DEVICE_API_VERSION_2(1, 3, HWC_HEADER_VERSION)
52#endif
Jesse Halle737c112013-05-07 11:58:47 -070053
Jesse Hall72960512013-01-10 16:19:56 -080054#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
Jesse Hall9eb1eb52012-08-29 10:39:38 -070055
Jesse Hallafaf14b2013-03-20 13:42:29 -070056#define NUM_PHYSICAL_DISPLAYS HWC_NUM_DISPLAY_TYPES
57#define VIRTUAL_DISPLAY_ID_BASE HWC_NUM_DISPLAY_TYPES
58
Jesse Hall9eb1eb52012-08-29 10:39:38 -070059static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
60 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070061 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
62}
63
64static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
65 uint32_t hwcVersion = hwc->common.version;
Jesse Hall9eb1eb52012-08-29 10:39:38 -070066 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
67}
68
69static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
70 uint32_t version) {
71 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
Jesse Hallbbd164a2012-08-21 12:05:09 -070072}
73
Mathias Agopiana350ff92010-08-10 17:14:02 -070074// ---------------------------------------------------------------------------
75
Mathias Agopian3e8b8532012-05-13 20:42:01 -070076struct HWComposer::cb_context {
77 struct callbacks : public hwc_procs_t {
78 // these are here to facilitate the transition when adding
79 // new callbacks (an implementation can check for NULL before
80 // calling a new callback).
81 void (*zero[4])(void);
82 };
83 callbacks procs;
84 HWComposer* hwc;
85};
86
87// ---------------------------------------------------------------------------
88
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070089HWComposer::HWComposer(
90 const sp<SurfaceFlinger>& flinger,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070091 EventHandler& handler)
Mathias Agopianc7d14e22011-08-01 16:32:21 -070092 : mFlinger(flinger),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070093 mFbDev(0), mHwc(0), mNumDisplays(1),
Mathias Agopian3e8b8532012-05-13 20:42:01 -070094 mCBContext(new cb_context),
Mathias Agopiane60b0682012-08-21 23:34:09 -070095 mEventHandler(handler),
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -070096 mVSyncCount(0), mDebugForceFakeVSync(false)
Mathias Agopiana350ff92010-08-10 17:14:02 -070097{
Mathias Agopiane60b0682012-08-21 23:34:09 -070098 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
99 mLists[i] = 0;
100 }
Jesse Hallb685c542012-07-31 14:32:56 -0700101
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700102 char value[PROPERTY_VALUE_MAX];
103 property_get("debug.sf.no_hw_vsync", value, "0");
104 mDebugForceFakeVSync = atoi(value);
105
Mathias Agopian028508c2012-07-25 21:12:12 -0700106 bool needVSyncThread = true;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700107
108 // Note: some devices may insist that the FB HAL be opened before HWC.
Jesse Hallef64b752013-03-18 11:28:50 -0700109 int fberr = loadFbHalModule();
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700110 loadHwcModule();
111
Mathias Agopianda27af92012-09-13 18:17:13 -0700112 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
113 // close FB HAL if we don't needed it.
114 // FIXME: this is temporary until we're not forced to open FB HAL
115 // before HWC.
116 framebuffer_close(mFbDev);
117 mFbDev = NULL;
118 }
119
Andy McFaddenbabba182012-09-12 13:14:51 -0700120 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
121 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
122 && !mFbDev) {
Jesse Hallef64b752013-03-18 11:28:50 -0700123 ALOGE("ERROR: failed to open framebuffer (%s), aborting",
124 strerror(-fberr));
Andy McFadden43601a22012-09-11 15:15:13 -0700125 abort();
126 }
127
Andy McFadden620685c2012-10-19 12:53:46 -0700128 // these display IDs are always reserved
Jesse Hallafaf14b2013-03-20 13:42:29 -0700129 for (size_t i=0 ; i<NUM_PHYSICAL_DISPLAYS ; i++) {
Andy McFadden620685c2012-10-19 12:53:46 -0700130 mAllocatedDisplayIDs.markBit(i);
131 }
132
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700133 if (mHwc) {
134 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
135 (hwcApiVersion(mHwc) >> 24) & 0xff,
136 (hwcApiVersion(mHwc) >> 16) & 0xff);
137 if (mHwc->registerProcs) {
138 mCBContext->hwc = this;
139 mCBContext->procs.invalidate = &hook_invalidate;
140 mCBContext->procs.vsync = &hook_vsync;
141 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
142 mCBContext->procs.hotplug = &hook_hotplug;
143 else
144 mCBContext->procs.hotplug = NULL;
145 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
146 mHwc->registerProcs(mHwc, &mCBContext->procs);
Jesse Hall5880cc52012-06-05 23:40:32 -0700147 }
148
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700149 // don't need a vsync thread if we have a hardware composer
150 needVSyncThread = false;
151 // always turn vsync off when we start
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700152 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Jesse Hallbbd164a2012-08-21 12:05:09 -0700153
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700154 // the number of displays we actually have depends on the
155 // hw composer version
Jesse Hall38efe862013-04-06 23:12:29 -0700156 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
157 // 1.3 adds support for virtual displays
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700158 mNumDisplays = MAX_DISPLAYS;
159 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
160 // 1.1 adds support for multiple displays
Jesse Hallafaf14b2013-03-20 13:42:29 -0700161 mNumDisplays = NUM_PHYSICAL_DISPLAYS;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700162 } else {
163 mNumDisplays = 1;
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700164 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700165 }
166
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700167 if (mFbDev) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700168 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
169 "should only have fbdev if no hwc or hwc is 1.0");
170
Mathias Agopianf4358632012-08-22 17:16:19 -0700171 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
Mathias Agopian38e623b2012-09-20 19:27:07 -0700172 disp.connected = true;
Jesse Halldb276212012-09-07 11:20:56 -0700173 disp.width = mFbDev->width;
174 disp.height = mFbDev->height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700175 disp.format = mFbDev->format;
176 disp.xdpi = mFbDev->xdpi;
177 disp.ydpi = mFbDev->ydpi;
Mathias Agopianf4358632012-08-22 17:16:19 -0700178 if (disp.refresh == 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700179 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
Mathias Agopianf4358632012-08-22 17:16:19 -0700180 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
Mathias Agopian888c8222012-08-04 21:10:38 -0700181 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700182 if (disp.refresh == 0) {
183 disp.refresh = nsecs_t(1e9 / 60.0);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700184 ALOGW("getting VSYNC period from thin air: %lld",
185 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
Mathias Agopianf4358632012-08-22 17:16:19 -0700186 }
Jesse Hall1bd20e02012-08-29 10:47:52 -0700187 } else if (mHwc) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700188 // here we're guaranteed to have at least HWC 1.1
Jesse Hallafaf14b2013-03-20 13:42:29 -0700189 for (size_t i =0 ; i<NUM_PHYSICAL_DISPLAYS ; i++) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700190 queryDisplayProperties(i);
191 }
Mathias Agopian888c8222012-08-04 21:10:38 -0700192 }
193
Mathias Agopian3a778712012-04-09 14:16:47 -0700194 if (needVSyncThread) {
195 // we don't have VSYNC support, we need to fake it
196 mVSyncThread = new VSyncThread(*this);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700197 }
198}
199
200HWComposer::~HWComposer() {
Andy McFaddenbabba182012-09-12 13:14:51 -0700201 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700202 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
Andy McFaddenbabba182012-09-12 13:14:51 -0700203 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700204 if (mVSyncThread != NULL) {
205 mVSyncThread->requestExitAndWait();
206 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700207 if (mHwc) {
Jesse Hall5880cc52012-06-05 23:40:32 -0700208 hwc_close_1(mHwc);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700209 }
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700210 if (mFbDev) {
211 framebuffer_close(mFbDev);
212 }
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700213 delete mCBContext;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700214}
215
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700216// Load and prepare the hardware composer module. Sets mHwc.
217void HWComposer::loadHwcModule()
218{
219 hw_module_t const* module;
220
221 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
222 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
223 return;
224 }
225
226 int err = hwc_open_1(module, &mHwc);
227 if (err) {
228 ALOGE("%s device failed to initialize (%s)",
229 HWC_HARDWARE_COMPOSER, strerror(-err));
230 return;
231 }
232
233 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
234 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
235 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
236 ALOGE("%s device version %#x unsupported, will not be used",
237 HWC_HARDWARE_COMPOSER, mHwc->common.version);
238 hwc_close_1(mHwc);
239 mHwc = NULL;
240 return;
241 }
242}
243
244// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
Jesse Hallef64b752013-03-18 11:28:50 -0700245int HWComposer::loadFbHalModule()
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700246{
247 hw_module_t const* module;
248
Jesse Hallef64b752013-03-18 11:28:50 -0700249 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
250 if (err != 0) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700251 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
Jesse Hallef64b752013-03-18 11:28:50 -0700252 return err;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700253 }
254
Jesse Hallef64b752013-03-18 11:28:50 -0700255 return framebuffer_open(module, &mFbDev);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700256}
257
Mathias Agopiana350ff92010-08-10 17:14:02 -0700258status_t HWComposer::initCheck() const {
259 return mHwc ? NO_ERROR : NO_INIT;
260}
261
Jesse Hallbbd164a2012-08-21 12:05:09 -0700262void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
263 cb_context* ctx = reinterpret_cast<cb_context*>(
264 const_cast<hwc_procs_t*>(procs));
265 ctx->hwc->invalidate();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700266}
267
Jesse Hall1bd20e02012-08-29 10:47:52 -0700268void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700269 int64_t timestamp) {
270 cb_context* ctx = reinterpret_cast<cb_context*>(
271 const_cast<hwc_procs_t*>(procs));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700272 ctx->hwc->vsync(disp, timestamp);
273}
274
275void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
276 int connected) {
277 cb_context* ctx = reinterpret_cast<cb_context*>(
278 const_cast<hwc_procs_t*>(procs));
279 ctx->hwc->hotplug(disp, connected);
Mathias Agopian31d28432012-04-03 16:31:39 -0700280}
281
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700282void HWComposer::invalidate() {
Mathias Agopiane2c2f922011-10-05 15:00:22 -0700283 mFlinger->repaintEverything();
Mathias Agopianc7d14e22011-08-01 16:32:21 -0700284}
285
Jesse Hall1bd20e02012-08-29 10:47:52 -0700286void HWComposer::vsync(int disp, int64_t timestamp) {
Mathias Agopian2965b262012-04-08 15:13:32 -0700287 ATRACE_INT("VSYNC", ++mVSyncCount&1);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700288 mEventHandler.onVSyncReceived(disp, timestamp);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700289 Mutex::Autolock _l(mLock);
290 mLastHwVSync = timestamp;
291}
292
Jesse Hall1bd20e02012-08-29 10:47:52 -0700293void HWComposer::hotplug(int disp, int connected) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700294 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
Jesse Hall1bd20e02012-08-29 10:47:52 -0700295 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
296 disp, connected);
297 return;
298 }
Mathias Agopianf5a33922012-09-19 18:16:22 -0700299 queryDisplayProperties(disp);
Mathias Agopian148994e2012-09-19 17:31:36 -0700300 mEventHandler.onHotplugReceived(disp, bool(connected));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700301}
302
Jesse Hall1c569c42013-04-05 13:44:52 -0700303static float getDefaultDensity(uint32_t height) {
304 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
305 else return ACONFIGURATION_DENSITY_TV;
306}
307
Jesse Hall1bd20e02012-08-29 10:47:52 -0700308static const uint32_t DISPLAY_ATTRIBUTES[] = {
309 HWC_DISPLAY_VSYNC_PERIOD,
Jesse Halldb276212012-09-07 11:20:56 -0700310 HWC_DISPLAY_WIDTH,
311 HWC_DISPLAY_HEIGHT,
Jesse Hall1bd20e02012-08-29 10:47:52 -0700312 HWC_DISPLAY_DPI_X,
313 HWC_DISPLAY_DPI_Y,
314 HWC_DISPLAY_NO_ATTRIBUTE,
315};
316#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
317
Mathias Agopian1604f772012-09-18 21:54:42 -0700318status_t HWComposer::queryDisplayProperties(int disp) {
319
Mathias Agopianda27af92012-09-13 18:17:13 -0700320 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
Jesse Hall1bd20e02012-08-29 10:47:52 -0700321
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700322 // use zero as default value for unspecified attributes
Jesse Hall1bd20e02012-08-29 10:47:52 -0700323 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
324 memset(values, 0, sizeof(values));
325
326 uint32_t config;
327 size_t numConfigs = 1;
328 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
Mathias Agopian1604f772012-09-18 21:54:42 -0700329 if (err != NO_ERROR) {
330 // this can happen if an unpluggable display is not connected
Mathias Agopianf5a33922012-09-19 18:16:22 -0700331 mDisplayData[disp].connected = false;
Mathias Agopian1604f772012-09-18 21:54:42 -0700332 return err;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700333 }
334
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700335 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
336 if (err != NO_ERROR) {
337 // we can't get this display's info. turn it off.
338 mDisplayData[disp].connected = false;
339 return err;
340 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700341
Jesse Hall1bd20e02012-08-29 10:47:52 -0700342 int32_t w = 0, h = 0;
343 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
344 switch (DISPLAY_ATTRIBUTES[i]) {
345 case HWC_DISPLAY_VSYNC_PERIOD:
346 mDisplayData[disp].refresh = nsecs_t(values[i]);
347 break;
Jesse Halldb276212012-09-07 11:20:56 -0700348 case HWC_DISPLAY_WIDTH:
349 mDisplayData[disp].width = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700350 break;
Jesse Halldb276212012-09-07 11:20:56 -0700351 case HWC_DISPLAY_HEIGHT:
352 mDisplayData[disp].height = values[i];
Jesse Hall1bd20e02012-08-29 10:47:52 -0700353 break;
354 case HWC_DISPLAY_DPI_X:
355 mDisplayData[disp].xdpi = values[i] / 1000.0f;
356 break;
357 case HWC_DISPLAY_DPI_Y:
358 mDisplayData[disp].ydpi = values[i] / 1000.0f;
359 break;
360 default:
Mathias Agopianda27af92012-09-13 18:17:13 -0700361 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
362 i, DISPLAY_ATTRIBUTES[i]);
Jesse Hall1bd20e02012-08-29 10:47:52 -0700363 break;
364 }
365 }
366
Mathias Agopianf5a33922012-09-19 18:16:22 -0700367 // FIXME: what should we set the format to?
368 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
369 mDisplayData[disp].connected = true;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700370 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
Jesse Hall1c569c42013-04-05 13:44:52 -0700371 float dpi = getDefaultDensity(h);
372 mDisplayData[disp].xdpi = dpi;
373 mDisplayData[disp].ydpi = dpi;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700374 }
Mathias Agopian1604f772012-09-18 21:54:42 -0700375 return NO_ERROR;
Jesse Hall1bd20e02012-08-29 10:47:52 -0700376}
377
Jesse Hall1c569c42013-04-05 13:44:52 -0700378status_t HWComposer::setVirtualDisplayProperties(int32_t id,
379 uint32_t w, uint32_t h, uint32_t format) {
380 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
381 !mAllocatedDisplayIDs.hasBit(id)) {
382 return BAD_INDEX;
383 }
384 mDisplayData[id].width = w;
385 mDisplayData[id].height = h;
386 mDisplayData[id].format = format;
387 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h);
388 return NO_ERROR;
389}
390
Mathias Agopiane60b0682012-08-21 23:34:09 -0700391int32_t HWComposer::allocateDisplayId() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700392 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700393 return NO_MEMORY;
394 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700395 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
396 mAllocatedDisplayIDs.markBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800397 mDisplayData[id].connected = true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700398 return id;
399}
400
401status_t HWComposer::freeDisplayId(int32_t id) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700402 if (id < NUM_PHYSICAL_DISPLAYS) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700403 // cannot free the reserved IDs
Mathias Agopiane60b0682012-08-21 23:34:09 -0700404 return BAD_VALUE;
405 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700406 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopiane60b0682012-08-21 23:34:09 -0700407 return BAD_INDEX;
408 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700409 mAllocatedDisplayIDs.clearBit(id);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800410 mDisplayData[id].connected = false;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700411 return NO_ERROR;
412}
413
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700414nsecs_t HWComposer::getRefreshPeriod(int disp) const {
415 return mDisplayData[disp].refresh;
Mathias Agopian888c8222012-08-04 21:10:38 -0700416}
417
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700418nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700419 // this returns the last refresh timestamp.
420 // if the last one is not available, we estimate it based on
421 // the refresh period and whatever closest timestamp we have.
422 Mutex::Autolock _l(mLock);
423 nsecs_t now = systemTime(CLOCK_MONOTONIC);
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700424 return now - ((now - mLastHwVSync) % mDisplayData[disp].refresh);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700425}
426
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800427sp<Fence> HWComposer::getDisplayFence(int disp) const {
428 return mDisplayData[disp].lastDisplayFence;
429}
430
Jesse Halldb276212012-09-07 11:20:56 -0700431uint32_t HWComposer::getWidth(int disp) const {
432 return mDisplayData[disp].width;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700433}
434
Jesse Halldb276212012-09-07 11:20:56 -0700435uint32_t HWComposer::getHeight(int disp) const {
436 return mDisplayData[disp].height;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700437}
438
439uint32_t HWComposer::getFormat(int disp) const {
440 return mDisplayData[disp].format;
441}
442
443float HWComposer::getDpiX(int disp) const {
444 return mDisplayData[disp].xdpi;
445}
446
447float HWComposer::getDpiY(int disp) const {
448 return mDisplayData[disp].ydpi;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700449}
450
Mathias Agopianf5a33922012-09-19 18:16:22 -0700451bool HWComposer::isConnected(int disp) const {
452 return mDisplayData[disp].connected;
453}
454
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700455void HWComposer::eventControl(int disp, int event, int enabled) {
456 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
Andy McFadden620685c2012-10-19 12:53:46 -0700457 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
458 event, disp, enabled);
459 return;
460 }
461 if (event != EVENT_VSYNC) {
462 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
463 event, disp, enabled);
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700464 return;
465 }
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700466 status_t err = NO_ERROR;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700467 if (mHwc && !mDebugForceFakeVSync) {
468 // NOTE: we use our own internal lock here because we have to call
469 // into the HWC with the lock held, and we want to make sure
470 // that even if HWC blocks (which it shouldn't), it won't
471 // affect other threads.
472 Mutex::Autolock _l(mEventControlLock);
473 const int32_t eventBit = 1UL << event;
474 const int32_t newValue = enabled ? eventBit : 0;
475 const int32_t oldValue = mDisplayData[disp].events & eventBit;
476 if (newValue != oldValue) {
477 ATRACE_CALL();
478 err = mHwc->eventControl(mHwc, disp, event, enabled);
479 if (!err) {
480 int32_t& events(mDisplayData[disp].events);
481 events = (events & ~eventBit) | newValue;
482 }
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700483 }
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700484 // error here should not happen -- not sure what we should
485 // do if it does.
486 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
487 event, enabled, strerror(-err));
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700488 }
Mathias Agopian3a778712012-04-09 14:16:47 -0700489
490 if (err == NO_ERROR && mVSyncThread != NULL) {
491 mVSyncThread->setEnabled(enabled);
492 }
Mathias Agopian31d28432012-04-03 16:31:39 -0700493}
494
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700495status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700496 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700497 return BAD_INDEX;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700498 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700499
Mathias Agopian45721772010-08-12 15:03:26 -0700500 if (mHwc) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700501 DisplayData& disp(mDisplayData[id]);
Mathias Agopianda27af92012-09-13 18:17:13 -0700502 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
503 // we need space for the HWC_FRAMEBUFFER_TARGET
504 numLayers++;
505 }
Andy McFadden13a082e2012-08-24 10:16:42 -0700506 if (disp.capacity < numLayers || disp.list == NULL) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700507 size_t size = sizeof(hwc_display_contents_1_t)
Mathias Agopianf4358632012-08-22 17:16:19 -0700508 + numLayers * sizeof(hwc_layer_1_t);
509 free(disp.list);
510 disp.list = (hwc_display_contents_1_t*)malloc(size);
511 disp.capacity = numLayers;
Mathias Agopian45721772010-08-12 15:03:26 -0700512 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700513 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
514 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
515 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800516 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
Mathias Agopianda27af92012-09-13 18:17:13 -0700517 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
518 disp.framebufferTarget->hints = 0;
519 disp.framebufferTarget->flags = 0;
520 disp.framebufferTarget->handle = disp.fbTargetHandle;
521 disp.framebufferTarget->transform = 0;
522 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
523 disp.framebufferTarget->sourceCrop = r;
524 disp.framebufferTarget->displayFrame = r;
525 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
526 disp.framebufferTarget->visibleRegionScreen.rects =
527 &disp.framebufferTarget->displayFrame;
528 disp.framebufferTarget->acquireFenceFd = -1;
529 disp.framebufferTarget->releaseFenceFd = -1;
Mathias Agopian70a6e882013-03-21 16:25:12 -0700530 disp.framebufferTarget->planeAlpha = 0xFF;
Mathias Agopianda27af92012-09-13 18:17:13 -0700531 }
Jesse Halldb276212012-09-07 11:20:56 -0700532 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700533 disp.list->flags = HWC_GEOMETRY_CHANGED;
534 disp.list->numHwLayers = numLayers;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700535 }
536 return NO_ERROR;
537}
538
Mathias Agopianda27af92012-09-13 18:17:13 -0700539status_t HWComposer::setFramebufferTarget(int32_t id,
540 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
541 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
542 return BAD_INDEX;
543 }
544 DisplayData& disp(mDisplayData[id]);
545 if (!disp.framebufferTarget) {
546 // this should never happen, but apparently eglCreateWindowSurface()
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800547 // triggers a Surface::queueBuffer() on some
Mathias Agopianda27af92012-09-13 18:17:13 -0700548 // devices (!?) -- log and ignore.
549 ALOGE("HWComposer: framebufferTarget is null");
Mathias Agopianda27af92012-09-13 18:17:13 -0700550 return NO_ERROR;
551 }
552
553 int acquireFenceFd = -1;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800554 if (acquireFence->isValid()) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700555 acquireFenceFd = acquireFence->dup();
556 }
557
558 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
559 disp.fbTargetHandle = buf->handle;
560 disp.framebufferTarget->handle = disp.fbTargetHandle;
561 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
562 return NO_ERROR;
563}
564
Mathias Agopiane60b0682012-08-21 23:34:09 -0700565status_t HWComposer::prepare() {
Mathias Agopianf4358632012-08-22 17:16:19 -0700566 for (size_t i=0 ; i<mNumDisplays ; i++) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700567 DisplayData& disp(mDisplayData[i]);
568 if (disp.framebufferTarget) {
569 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
570 // DO NOT reset the handle field to NULL, because it's possible
571 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
572 // in which case, we should continue to use the same buffer.
Andy McFadden27ec5732012-10-02 19:04:45 -0700573 LOG_FATAL_IF(disp.list == NULL);
Mathias Agopianda27af92012-09-13 18:17:13 -0700574 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
575 }
Andy McFadden27ec5732012-10-02 19:04:45 -0700576 if (!disp.connected && disp.list != NULL) {
577 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
578 i, disp.list->numHwLayers);
579 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700580 mLists[i] = disp.list;
Mathias Agopianf4358632012-08-22 17:16:19 -0700581 if (mLists[i]) {
Jesse Hall38efe862013-04-06 23:12:29 -0700582 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
Jesse Halldb276212012-09-07 11:20:56 -0700583 mLists[i]->outbuf = NULL;
584 mLists[i]->outbufAcquireFenceFd = -1;
585 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
586 // garbage data to catch improper use
587 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
588 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
589 } else {
590 mLists[i]->dpy = EGL_NO_DISPLAY;
591 mLists[i]->sur = EGL_NO_SURFACE;
592 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700593 }
594 }
Jesse Halldb276212012-09-07 11:20:56 -0700595
Mathias Agopianf4358632012-08-22 17:16:19 -0700596 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
Mathias Agopianda27af92012-09-13 18:17:13 -0700597 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
598
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700599 if (err == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700600 // here we're just making sure that "skip" layers are set
601 // to HWC_FRAMEBUFFER and we're also counting how many layers
602 // we have of each type.
Mathias Agopianf4358632012-08-22 17:16:19 -0700603 for (size_t i=0 ; i<mNumDisplays ; i++) {
604 DisplayData& disp(mDisplayData[i]);
605 disp.hasFbComp = false;
606 disp.hasOvComp = false;
607 if (disp.list) {
608 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
609 hwc_layer_1_t& l = disp.list->hwLayers[i];
Mathias Agopianda27af92012-09-13 18:17:13 -0700610
611 //ALOGD("prepare: %d, type=%d, handle=%p",
612 // i, l.compositionType, l.handle);
613
Mathias Agopianf4358632012-08-22 17:16:19 -0700614 if (l.flags & HWC_SKIP_LAYER) {
615 l.compositionType = HWC_FRAMEBUFFER;
616 }
617 if (l.compositionType == HWC_FRAMEBUFFER) {
618 disp.hasFbComp = true;
619 }
620 if (l.compositionType == HWC_OVERLAY) {
621 disp.hasOvComp = true;
622 }
623 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700624 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700625 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700626 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700627 return (status_t)err;
628}
629
Mathias Agopiane60b0682012-08-21 23:34:09 -0700630bool HWComposer::hasHwcComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700631 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Mathias Agopiane60b0682012-08-21 23:34:09 -0700632 return false;
633 return mDisplayData[id].hasOvComp;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700634}
635
Mathias Agopiane60b0682012-08-21 23:34:09 -0700636bool HWComposer::hasGlesComposition(int32_t id) const {
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700637 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
638 return true;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700639 return mDisplayData[id].hasFbComp;
640}
641
Jesse Hall13f01cb2013-03-20 11:37:21 -0700642sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700643 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
Jesse Hall13f01cb2013-03-20 11:37:21 -0700644 return Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700645
646 int fd = INVALID_OPERATION;
647 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
648 const DisplayData& disp(mDisplayData[id]);
649 if (disp.framebufferTarget) {
650 fd = disp.framebufferTarget->releaseFenceFd;
Jamie Gennisd30b36d2012-09-30 20:02:03 -0700651 disp.framebufferTarget->acquireFenceFd = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700652 disp.framebufferTarget->releaseFenceFd = -1;
653 }
654 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700655 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Mathias Agopianda27af92012-09-13 18:17:13 -0700656}
657
Mathias Agopian30bcc612012-08-22 15:39:48 -0700658status_t HWComposer::commit() {
Mathias Agopian86303202012-07-24 22:46:10 -0700659 int err = NO_ERROR;
660 if (mHwc) {
Jesse Hall9eb1eb52012-08-29 10:39:38 -0700661 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopian30bcc612012-08-22 15:39:48 -0700662 // On version 1.0, the OpenGL ES target surface is communicated
Mathias Agopianf4358632012-08-22 17:16:19 -0700663 // by the (dpy, sur) fields and we are guaranteed to have only
664 // a single display.
Mathias Agopian30bcc612012-08-22 15:39:48 -0700665 mLists[0]->dpy = eglGetCurrentDisplay();
666 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
Mathias Agopian86303202012-07-24 22:46:10 -0700667 }
Mathias Agopianf4358632012-08-22 17:16:19 -0700668
Jesse Hallafaf14b2013-03-20 13:42:29 -0700669 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
Jesse Hall80e0a392013-03-15 12:32:10 -0700670 DisplayData& disp(mDisplayData[i]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700671 if (disp.outbufHandle) {
672 mLists[i]->outbuf = disp.outbufHandle;
Jesse Hall80e0a392013-03-15 12:32:10 -0700673 mLists[i]->outbufAcquireFenceFd =
Jesse Hall851cfe82013-03-20 13:44:00 -0700674 disp.outbufAcquireFence->dup();
Jesse Hall80e0a392013-03-15 12:32:10 -0700675 }
676 }
677
Mathias Agopian30bcc612012-08-22 15:39:48 -0700678 err = mHwc->set(mHwc, mNumDisplays, mLists);
Mathias Agopianf4358632012-08-22 17:16:19 -0700679
680 for (size_t i=0 ; i<mNumDisplays ; i++) {
681 DisplayData& disp(mDisplayData[i]);
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800682 disp.lastDisplayFence = disp.lastRetireFence;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800683 disp.lastRetireFence = Fence::NO_FENCE;
Mathias Agopianf4358632012-08-22 17:16:19 -0700684 if (disp.list) {
Jesse Halldb276212012-09-07 11:20:56 -0700685 if (disp.list->retireFenceFd != -1) {
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800686 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
Jesse Halldb276212012-09-07 11:20:56 -0700687 disp.list->retireFenceFd = -1;
Mathias Agopianf4358632012-08-22 17:16:19 -0700688 }
689 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
690 }
Mathias Agopian30bcc612012-08-22 15:39:48 -0700691 }
Mathias Agopian58959342010-10-07 14:57:04 -0700692 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700693 return (status_t)err;
694}
695
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700696status_t HWComposer::release(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700697 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700698 if (mHwc) {
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700699 eventControl(disp, HWC_EVENT_VSYNC, 0);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700700 return (status_t)mHwc->blank(mHwc, disp, 1);
Mathias Agopian7ee4cd52011-09-02 12:22:39 -0700701 }
702 return NO_ERROR;
703}
704
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700705status_t HWComposer::acquire(int disp) {
Jesse Hallafaf14b2013-03-20 13:42:29 -0700706 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
Colin Cross10fbdb62012-07-12 17:56:34 -0700707 if (mHwc) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700708 return (status_t)mHwc->blank(mHwc, disp, 0);
Colin Cross10fbdb62012-07-12 17:56:34 -0700709 }
Colin Cross10fbdb62012-07-12 17:56:34 -0700710 return NO_ERROR;
711}
712
Andy McFadden27ec5732012-10-02 19:04:45 -0700713void HWComposer::disconnectDisplay(int disp) {
Andy McFadden5a8f9012012-10-04 19:09:45 -0700714 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
Andy McFadden27ec5732012-10-02 19:04:45 -0700715 DisplayData& dd(mDisplayData[disp]);
Jesse Hall851cfe82013-03-20 13:44:00 -0700716 free(dd.list);
717 dd.list = NULL;
718 dd.framebufferTarget = NULL; // points into dd.list
719 dd.fbTargetHandle = NULL;
720 dd.outbufHandle = NULL;
721 dd.lastRetireFence = Fence::NO_FENCE;
722 dd.lastDisplayFence = Fence::NO_FENCE;
723 dd.outbufAcquireFence = Fence::NO_FENCE;
Andy McFadden27ec5732012-10-02 19:04:45 -0700724}
725
Mathias Agopiancde87a32012-09-13 14:09:01 -0700726int HWComposer::getVisualID() const {
Jesse Halld3d35f12012-09-18 11:39:40 -0700727 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700728 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
729 // is supported by the implementation. we can only be in this case
730 // if we have HWC 1.1
731 return HAL_PIXEL_FORMAT_RGBA_8888;
732 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700733 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700734 return mFbDev->format;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700735 }
736}
737
Mathias Agopianda27af92012-09-13 18:17:13 -0700738bool HWComposer::supportsFramebufferTarget() const {
739 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
740}
741
742int HWComposer::fbPost(int32_t id,
743 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
Jesse Halld3d35f12012-09-18 11:39:40 -0700744 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700745 return setFramebufferTarget(id, acquireFence, buffer);
746 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700747 acquireFence->waitForever("HWComposer::fbPost");
Mathias Agopianda27af92012-09-13 18:17:13 -0700748 return mFbDev->post(mFbDev, buffer->handle);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700749 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700750}
751
752int HWComposer::fbCompositionComplete() {
Jesse Halld3d35f12012-09-18 11:39:40 -0700753 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
Mathias Agopianda27af92012-09-13 18:17:13 -0700754 return NO_ERROR;
755
756 if (mFbDev->compositionComplete) {
757 return mFbDev->compositionComplete(mFbDev);
758 } else {
759 return INVALID_OPERATION;
Mathias Agopiancde87a32012-09-13 14:09:01 -0700760 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700761}
762
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700763void HWComposer::fbDump(String8& result) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700764 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700765 const size_t SIZE = 4096;
766 char buffer[SIZE];
767 mFbDev->dump(mFbDev, buffer, SIZE);
768 result.append(buffer);
769 }
770}
771
Jesse Hall851cfe82013-03-20 13:44:00 -0700772status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
773 const sp<GraphicBuffer>& buf) {
774 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
775 return BAD_INDEX;
776 if (id < VIRTUAL_DISPLAY_ID_BASE)
777 return INVALID_OPERATION;
778
779 DisplayData& disp(mDisplayData[id]);
780 disp.outbufHandle = buf->handle;
781 disp.outbufAcquireFence = acquireFence;
782 return NO_ERROR;
783}
784
785sp<Fence> HWComposer::getLastRetireFence(int32_t id) {
786 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
787 return Fence::NO_FENCE;
788 return mDisplayData[id].lastRetireFence;
789}
790
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700791/*
792 * Helper template to implement a concrete HWCLayer
793 * This holds the pointer to the concrete hwc layer type
794 * and implements the "iterable" side of HWCLayer.
795 */
796template<typename CONCRETE, typename HWCTYPE>
797class Iterable : public HWComposer::HWCLayer {
798protected:
799 HWCTYPE* const mLayerList;
800 HWCTYPE* mCurrentLayer;
801 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
802 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
803 inline HWCTYPE* getLayer() { return mCurrentLayer; }
804 virtual ~Iterable() { }
805private:
806 // returns a copy of ourselves
807 virtual HWComposer::HWCLayer* dup() {
808 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
809 }
810 virtual status_t setLayer(size_t index) {
811 mCurrentLayer = &mLayerList[index];
812 return NO_ERROR;
813 }
814};
815
Jesse Hall5880cc52012-06-05 23:40:32 -0700816/*
817 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
818 * This implements the HWCLayer side of HWCIterableLayer.
819 */
820class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800821 struct hwc_composer_device_1* mHwc;
Jesse Hall5880cc52012-06-05 23:40:32 -0700822public:
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800823 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
824 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
Jesse Hall5880cc52012-06-05 23:40:32 -0700825
826 virtual int32_t getCompositionType() const {
827 return getLayer()->compositionType;
828 }
829 virtual uint32_t getHints() const {
830 return getLayer()->hints;
831 }
Jesse Hall13f01cb2013-03-20 11:37:21 -0700832 virtual sp<Fence> getAndResetReleaseFence() {
Jesse Hallef194142012-06-14 14:45:17 -0700833 int fd = getLayer()->releaseFenceFd;
834 getLayer()->releaseFenceFd = -1;
Jesse Hall13f01cb2013-03-20 11:37:21 -0700835 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
Jesse Hallef194142012-06-14 14:45:17 -0700836 }
Jesse Halldc5b4852012-06-29 15:21:18 -0700837 virtual void setAcquireFenceFd(int fenceFd) {
838 getLayer()->acquireFenceFd = fenceFd;
839 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800840 virtual void setPerFrameDefaultState() {
841 //getLayer()->compositionType = HWC_FRAMEBUFFER;
842 }
843 virtual void setPlaneAlpha(uint8_t alpha) {
844 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
845 getLayer()->planeAlpha = alpha;
846 } else {
Mathias Agopian5fe58b82013-02-07 16:22:50 -0800847 if (alpha < 0xFF) {
848 getLayer()->flags |= HWC_SKIP_LAYER;
849 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800850 }
851 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700852 virtual void setDefaultState() {
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800853 hwc_layer_1_t* const l = getLayer();
854 l->compositionType = HWC_FRAMEBUFFER;
855 l->hints = 0;
856 l->flags = HWC_SKIP_LAYER;
857 l->handle = 0;
858 l->transform = 0;
859 l->blending = HWC_BLENDING_NONE;
860 l->visibleRegionScreen.numRects = 0;
861 l->visibleRegionScreen.rects = NULL;
862 l->acquireFenceFd = -1;
863 l->releaseFenceFd = -1;
864 l->planeAlpha = 0xFF;
Jesse Hall5880cc52012-06-05 23:40:32 -0700865 }
866 virtual void setSkip(bool skip) {
867 if (skip) {
868 getLayer()->flags |= HWC_SKIP_LAYER;
869 } else {
870 getLayer()->flags &= ~HWC_SKIP_LAYER;
871 }
872 }
873 virtual void setBlending(uint32_t blending) {
874 getLayer()->blending = blending;
875 }
876 virtual void setTransform(uint32_t transform) {
877 getLayer()->transform = transform;
878 }
879 virtual void setFrame(const Rect& frame) {
880 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
881 }
882 virtual void setCrop(const Rect& crop) {
883 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
884 }
885 virtual void setVisibleRegionScreen(const Region& reg) {
Mathias Agopianc3973602012-08-31 17:51:25 -0700886 // Region::getSharedBuffer creates a reference to the underlying
887 // SharedBuffer of this Region, this reference is freed
888 // in onDisplayed()
889 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
890 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
891 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
Jesse Hall5880cc52012-06-05 23:40:32 -0700892 }
893 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
894 if (buffer == 0 || buffer->handle == 0) {
895 getLayer()->compositionType = HWC_FRAMEBUFFER;
896 getLayer()->flags |= HWC_SKIP_LAYER;
897 getLayer()->handle = 0;
898 } else {
899 getLayer()->handle = buffer->handle;
900 }
901 }
Mathias Agopianc3973602012-08-31 17:51:25 -0700902 virtual void onDisplayed() {
903 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
904 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
905 if (sb) {
906 sb->release();
907 // not technically needed but safer
908 visibleRegion.numRects = 0;
909 visibleRegion.rects = NULL;
910 }
Jesse Halle25d0052012-09-05 13:03:10 -0700911
912 getLayer()->acquireFenceFd = -1;
Mathias Agopianc3973602012-08-31 17:51:25 -0700913 }
Jesse Hall5880cc52012-06-05 23:40:32 -0700914};
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700915
916/*
917 * returns an iterator initialized at a given index in the layer list
918 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700919HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
Mathias Agopianf4358632012-08-22 17:16:19 -0700920 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700921 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700922 }
923 const DisplayData& disp(mDisplayData[id]);
924 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700925 return LayerListIterator();
Mathias Agopianf4358632012-08-22 17:16:19 -0700926 }
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800927 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700928}
929
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700930/*
931 * returns an iterator on the beginning of the layer list
932 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700933HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700934 return getLayerIterator(id, 0);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700935}
936
937/*
938 * returns an iterator on the end of the layer list
939 */
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700940HWComposer::LayerListIterator HWComposer::end(int32_t id) {
Mathias Agopianda27af92012-09-13 18:17:13 -0700941 size_t numLayers = 0;
942 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
943 const DisplayData& disp(mDisplayData[id]);
944 if (mHwc && disp.list) {
945 numLayers = disp.list->numHwLayers;
946 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
947 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
948 // which we ignore when iterating through the layer list.
949 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
950 if (numLayers) {
951 numLayers--;
952 }
953 }
954 }
955 }
956 return getLayerIterator(id, numLayers);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700957}
958
Mathias Agopian74d211a2013-04-22 16:55:35 +0200959void HWComposer::dump(String8& result) const {
Jesse Hallb685c542012-07-31 14:32:56 -0700960 if (mHwc) {
Mathias Agopiancde87a32012-09-13 14:09:01 -0700961 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
Mathias Agopianf4358632012-08-22 17:16:19 -0700962 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
963 for (size_t i=0 ; i<mNumDisplays ; i++) {
964 const DisplayData& disp(mDisplayData[i]);
Jesse Hall02d86562013-03-25 14:43:23 -0700965 if (!disp.connected)
966 continue;
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700967
Mathias Agopian13127d82013-03-05 17:47:11 -0800968 const Vector< sp<Layer> >& visibleLayersSortedByZ =
Mathias Agopiancb558572012-10-04 15:58:54 -0700969 mFlinger->getLayerSortedByZForHwcDisplay(i);
970
Jesse Hall02d86562013-03-25 14:43:23 -0700971 result.appendFormat(
972 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
973 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700974
Jesse Hall02d86562013-03-25 14:43:23 -0700975 if (disp.list) {
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700976 result.appendFormat(
977 " numHwLayers=%u, flags=%08x\n",
978 disp.list->numHwLayers, disp.list->flags);
979
Mathias Agopianf4358632012-08-22 17:16:19 -0700980 result.append(
Mathias Agopianda27af92012-09-13 18:17:13 -0700981 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
982 "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
983 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
Mathias Agopianf4358632012-08-22 17:16:19 -0700984 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
985 const hwc_layer_1_t&l = disp.list->hwLayers[i];
Mathias Agopianf4358632012-08-22 17:16:19 -0700986 int32_t format = -1;
Mathias Agopianda27af92012-09-13 18:17:13 -0700987 String8 name("unknown");
Mathias Agopiancb558572012-10-04 15:58:54 -0700988
Mathias Agopianda27af92012-09-13 18:17:13 -0700989 if (i < visibleLayersSortedByZ.size()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800990 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
991 const sp<GraphicBuffer>& buffer(
992 layer->getActiveBuffer());
993 if (buffer != NULL) {
994 format = buffer->getPixelFormat();
Mathias Agopianf4358632012-08-22 17:16:19 -0700995 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700996 name = layer->getName();
Mathias Agopianf4358632012-08-22 17:16:19 -0700997 }
Mathias Agopianda27af92012-09-13 18:17:13 -0700998
999 int type = l.compositionType;
1000 if (type == HWC_FRAMEBUFFER_TARGET) {
1001 name = "HWC_FRAMEBUFFER_TARGET";
1002 format = disp.format;
1003 }
1004
1005 static char const* compositionTypeName[] = {
1006 "GLES",
1007 "HWC",
1008 "BACKGROUND",
1009 "FB TARGET",
1010 "UNKNOWN"};
1011 if (type >= NELEM(compositionTypeName))
1012 type = NELEM(compositionTypeName) - 1;
1013
Mathias Agopianf4358632012-08-22 17:16:19 -07001014 result.appendFormat(
Mathias Agopianda27af92012-09-13 18:17:13 -07001015 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
1016 compositionTypeName[type],
Mathias Agopianf4358632012-08-22 17:16:19 -07001017 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1018 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1019 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
Mathias Agopianda27af92012-09-13 18:17:13 -07001020 name.string());
Mathias Agopianfb4d5d52011-09-20 15:13:14 -07001021 }
1022 }
Mathias Agopian83727852010-09-23 18:13:21 -07001023 }
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001024 }
Mathias Agopian30bcc612012-08-22 15:39:48 -07001025
1026 if (mHwc && mHwc->dump) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02001027 const size_t SIZE = 4096;
1028 char buffer[SIZE];
Mathias Agopian30bcc612012-08-22 15:39:48 -07001029 mHwc->dump(mHwc, buffer, SIZE);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001030 result.append(buffer);
Mathias Agopian83727852010-09-23 18:13:21 -07001031 }
1032}
1033
Mathias Agopiana350ff92010-08-10 17:14:02 -07001034// ---------------------------------------------------------------------------
Mathias Agopian2965b262012-04-08 15:13:32 -07001035
1036HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1037 : mHwc(hwc), mEnabled(false),
1038 mNextFakeVSync(0),
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07001039 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
Mathias Agopian2965b262012-04-08 15:13:32 -07001040{
1041}
1042
1043void HWComposer::VSyncThread::setEnabled(bool enabled) {
1044 Mutex::Autolock _l(mLock);
Mathias Agopian81cd5d32012-10-04 02:34:38 -07001045 if (mEnabled != enabled) {
1046 mEnabled = enabled;
1047 mCondition.signal();
1048 }
Mathias Agopian2965b262012-04-08 15:13:32 -07001049}
1050
1051void HWComposer::VSyncThread::onFirstRef() {
1052 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1053}
1054
1055bool HWComposer::VSyncThread::threadLoop() {
1056 { // scope for lock
1057 Mutex::Autolock _l(mLock);
1058 while (!mEnabled) {
1059 mCondition.wait(mLock);
1060 }
1061 }
1062
1063 const nsecs_t period = mRefreshPeriod;
1064 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1065 nsecs_t next_vsync = mNextFakeVSync;
1066 nsecs_t sleep = next_vsync - now;
1067 if (sleep < 0) {
1068 // we missed, find where the next vsync should be
1069 sleep = (period - ((now - next_vsync) % period));
1070 next_vsync = now + sleep;
1071 }
1072 mNextFakeVSync = next_vsync + period;
1073
1074 struct timespec spec;
1075 spec.tv_sec = next_vsync / 1000000000;
1076 spec.tv_nsec = next_vsync % 1000000000;
1077
1078 int err;
1079 do {
1080 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1081 } while (err<0 && errno == EINTR);
1082
1083 if (err == 0) {
1084 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1085 }
1086
1087 return true;
1088}
1089
Jesse Halla9a1b002013-02-27 16:39:25 -08001090HWComposer::DisplayData::DisplayData()
1091: width(0), height(0), format(0),
1092 xdpi(0.0f), ydpi(0.0f),
1093 refresh(0),
1094 connected(false),
1095 hasFbComp(false), hasOvComp(false),
1096 capacity(0), list(NULL),
1097 framebufferTarget(NULL), fbTargetHandle(0),
1098 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
Jesse Hall851cfe82013-03-20 13:44:00 -07001099 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
Jesse Halla9a1b002013-02-27 16:39:25 -08001100 events(0)
1101{}
1102
1103HWComposer::DisplayData::~DisplayData() {
1104 free(list);
1105}
1106
Mathias Agopian2965b262012-04-08 15:13:32 -07001107// ---------------------------------------------------------------------------
Mathias Agopiana350ff92010-08-10 17:14:02 -07001108}; // namespace android