HWComposer.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  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. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  17. #include <inttypes.h>
  18. #include <math.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <utils/Errors.h>
  25. #include <utils/misc.h>
  26. #include <utils/NativeHandle.h>
  27. #include <utils/String8.h>
  28. #include <utils/Thread.h>
  29. #include <utils/Trace.h>
  30. #include <utils/Vector.h>
  31. #include <ui/GraphicBuffer.h>
  32. #include <hardware/hardware.h>
  33. #include <hardware/hwcomposer.h>
  34. #ifdef QTI_BSP
  35. #include <hardware/display_defs.h>
  36. #endif
  37. #include <android/configuration.h>
  38. #include <cutils/log.h>
  39. #include <cutils/properties.h>
  40. #include "HWComposer.h"
  41. #include "../Layer.h" // needed only for debugging
  42. #include "../SurfaceFlinger.h"
  43. namespace android {
  44. #define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
  45. static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
  46. uint32_t hwcVersion = hwc->common.version;
  47. return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
  48. }
  49. static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
  50. uint32_t hwcVersion = hwc->common.version;
  51. return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
  52. }
  53. static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
  54. uint32_t version) {
  55. return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
  56. }
  57. // ---------------------------------------------------------------------------
  58. struct HWComposer::cb_context {
  59. struct callbacks : public hwc_procs_t {
  60. // these are here to facilitate the transition when adding
  61. // new callbacks (an implementation can check for NULL before
  62. // calling a new callback).
  63. void (*zero[4])(void);
  64. };
  65. callbacks procs;
  66. HWComposer* hwc;
  67. };
  68. // ---------------------------------------------------------------------------
  69. HWComposer::HWComposer(
  70. const sp<SurfaceFlinger>& flinger,
  71. EventHandler& handler)
  72. : mFlinger(flinger),
  73. mFbDev(0), mHwc(0), mNumDisplays(1),
  74. mCBContext(new cb_context),
  75. mEventHandler(handler),
  76. mDebugForceFakeVSync(false)
  77. {
  78. for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
  79. mLists[i] = 0;
  80. }
  81. for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
  82. mLastHwVSync[i] = 0;
  83. mVSyncCounts[i] = 0;
  84. }
  85. char value[PROPERTY_VALUE_MAX];
  86. property_get("debug.sf.no_hw_vsync", value, "0");
  87. mDebugForceFakeVSync = atoi(value);
  88. bool needVSyncThread = true;
  89. // Note: some devices may insist that the FB HAL be opened before HWC.
  90. int fberr = loadFbHalModule();
  91. loadHwcModule();
  92. if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  93. // close FB HAL if we don't needed it.
  94. // FIXME: this is temporary until we're not forced to open FB HAL
  95. // before HWC.
  96. framebuffer_close(mFbDev);
  97. mFbDev = NULL;
  98. }
  99. // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
  100. if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
  101. && !mFbDev) {
  102. ALOGE("ERROR: failed to open framebuffer (%s), aborting",
  103. strerror(-fberr));
  104. abort();
  105. }
  106. // these display IDs are always reserved
  107. for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
  108. mAllocatedDisplayIDs.markBit(i);
  109. }
  110. if (mHwc) {
  111. ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
  112. (hwcApiVersion(mHwc) >> 24) & 0xff,
  113. (hwcApiVersion(mHwc) >> 16) & 0xff);
  114. if (mHwc->registerProcs) {
  115. mCBContext->hwc = this;
  116. mCBContext->procs.invalidate = &hook_invalidate;
  117. mCBContext->procs.vsync = &hook_vsync;
  118. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
  119. mCBContext->procs.hotplug = &hook_hotplug;
  120. else
  121. mCBContext->procs.hotplug = NULL;
  122. memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
  123. mHwc->registerProcs(mHwc, &mCBContext->procs);
  124. }
  125. // don't need a vsync thread if we have a hardware composer
  126. needVSyncThread = false;
  127. // always turn vsync off when we start
  128. eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
  129. // the number of displays we actually have depends on the
  130. // hw composer version
  131. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
  132. // 1.3 adds support for virtual displays
  133. mNumDisplays = MAX_HWC_DISPLAYS;
  134. } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  135. // 1.1 adds support for multiple displays
  136. mNumDisplays = NUM_BUILTIN_DISPLAYS;
  137. } else {
  138. mNumDisplays = 1;
  139. }
  140. }
  141. if (mFbDev) {
  142. ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
  143. "should only have fbdev if no hwc or hwc is 1.0");
  144. DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
  145. disp.connected = true;
  146. disp.format = mFbDev->format;
  147. DisplayConfig config = DisplayConfig();
  148. config.width = mFbDev->width;
  149. config.height = mFbDev->height;
  150. config.xdpi = mFbDev->xdpi;
  151. config.ydpi = mFbDev->ydpi;
  152. config.refresh = nsecs_t(1e9 / mFbDev->fps);
  153. disp.configs.push_back(config);
  154. disp.currentConfig = 0;
  155. } else if (mHwc) {
  156. // here we're guaranteed to have at least HWC 1.1
  157. for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
  158. queryDisplayProperties(i);
  159. }
  160. }
  161. if (needVSyncThread) {
  162. // we don't have VSYNC support, we need to fake it
  163. mVSyncThread = new VSyncThread(*this);
  164. }
  165. mDimComp = 0;
  166. if (mHwc) {
  167. mHwc->query(mHwc, HWC_BACKGROUND_LAYER_SUPPORTED, &mDimComp);
  168. }
  169. }
  170. HWComposer::~HWComposer() {
  171. if (mHwc) {
  172. eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
  173. }
  174. if (mVSyncThread != NULL) {
  175. mVSyncThread->requestExitAndWait();
  176. }
  177. if (mHwc) {
  178. hwc_close_1(mHwc);
  179. }
  180. if (mFbDev) {
  181. framebuffer_close(mFbDev);
  182. }
  183. delete mCBContext;
  184. }
  185. // Load and prepare the hardware composer module. Sets mHwc.
  186. void HWComposer::loadHwcModule()
  187. {
  188. hw_module_t const* module;
  189. if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
  190. ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
  191. return;
  192. }
  193. int err = hwc_open_1(module, &mHwc);
  194. if (err) {
  195. ALOGE("%s device failed to initialize (%s)",
  196. HWC_HARDWARE_COMPOSER, strerror(-err));
  197. return;
  198. }
  199. if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
  200. hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
  201. hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
  202. ALOGE("%s device version %#x unsupported, will not be used",
  203. HWC_HARDWARE_COMPOSER, mHwc->common.version);
  204. hwc_close_1(mHwc);
  205. mHwc = NULL;
  206. return;
  207. }
  208. }
  209. // Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
  210. int HWComposer::loadFbHalModule()
  211. {
  212. hw_module_t const* module;
  213. int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
  214. if (err != 0) {
  215. ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
  216. return err;
  217. }
  218. return framebuffer_open(module, &mFbDev);
  219. }
  220. status_t HWComposer::initCheck() const {
  221. return mHwc ? NO_ERROR : NO_INIT;
  222. }
  223. void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
  224. cb_context* ctx = reinterpret_cast<cb_context*>(
  225. const_cast<hwc_procs_t*>(procs));
  226. ctx->hwc->invalidate();
  227. }
  228. void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
  229. int64_t timestamp) {
  230. cb_context* ctx = reinterpret_cast<cb_context*>(
  231. const_cast<hwc_procs_t*>(procs));
  232. ctx->hwc->vsync(disp, timestamp);
  233. }
  234. void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
  235. int connected) {
  236. cb_context* ctx = reinterpret_cast<cb_context*>(
  237. const_cast<hwc_procs_t*>(procs));
  238. ctx->hwc->hotplug(disp, connected);
  239. }
  240. void HWComposer::invalidate() {
  241. mFlinger->repaintEverything();
  242. }
  243. void HWComposer::vsync(int disp, int64_t timestamp) {
  244. if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
  245. {
  246. Mutex::Autolock _l(mLock);
  247. // There have been reports of HWCs that signal several vsync events
  248. // with the same timestamp when turning the display off and on. This
  249. // is a bug in the HWC implementation, but filter the extra events
  250. // out here so they don't cause havoc downstream.
  251. if (timestamp == mLastHwVSync[disp]) {
  252. ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")",
  253. timestamp);
  254. return;
  255. }
  256. mLastHwVSync[disp] = timestamp;
  257. }
  258. char tag[16];
  259. snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp);
  260. ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
  261. mEventHandler.onVSyncReceived(disp, timestamp);
  262. }
  263. }
  264. void HWComposer::hotplug(int disp, int connected) {
  265. if (disp >= VIRTUAL_DISPLAY_ID_BASE) {
  266. ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
  267. disp, connected);
  268. return;
  269. }
  270. queryDisplayProperties(disp);
  271. // Do not teardown or recreate the primary display
  272. if (disp != HWC_DISPLAY_PRIMARY) {
  273. mEventHandler.onHotplugReceived(disp, bool(connected));
  274. }
  275. }
  276. static float getDefaultDensity(uint32_t width, uint32_t height) {
  277. // Default density is based on TVs: 1080p displays get XHIGH density,
  278. // lower-resolution displays get TV density. Maybe eventually we'll need
  279. // to update it for 4K displays, though hopefully those just report
  280. // accurate DPI information to begin with. This is also used for virtual
  281. // displays and even primary displays with older hwcomposers, so be
  282. // careful about orientation.
  283. uint32_t h = width < height ? width : height;
  284. if (h >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
  285. else return ACONFIGURATION_DENSITY_TV;
  286. }
  287. static const uint32_t DISPLAY_ATTRIBUTES[] = {
  288. HWC_DISPLAY_VSYNC_PERIOD,
  289. HWC_DISPLAY_WIDTH,
  290. HWC_DISPLAY_HEIGHT,
  291. HWC_DISPLAY_DPI_X,
  292. HWC_DISPLAY_DPI_Y,
  293. HWC_DISPLAY_COLOR_TRANSFORM,
  294. HWC_DISPLAY_NO_ATTRIBUTE,
  295. };
  296. #define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
  297. static const uint32_t PRE_HWC15_DISPLAY_ATTRIBUTES[] = {
  298. HWC_DISPLAY_VSYNC_PERIOD,
  299. HWC_DISPLAY_WIDTH,
  300. HWC_DISPLAY_HEIGHT,
  301. HWC_DISPLAY_DPI_X,
  302. HWC_DISPLAY_DPI_Y,
  303. HWC_DISPLAY_NO_ATTRIBUTE,
  304. };
  305. status_t HWComposer::queryDisplayProperties(int disp) {
  306. LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
  307. // use zero as default value for unspecified attributes
  308. int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
  309. memset(values, 0, sizeof(values));
  310. const size_t MAX_NUM_CONFIGS = 128;
  311. uint32_t configs[MAX_NUM_CONFIGS] = {0};
  312. size_t numConfigs = MAX_NUM_CONFIGS;
  313. status_t err = mHwc->getDisplayConfigs(mHwc, disp, configs, &numConfigs);
  314. if (err != NO_ERROR) {
  315. // this can happen if an unpluggable display is not connected
  316. mDisplayData[disp].connected = false;
  317. return err;
  318. }
  319. int currentConfig = getActiveConfig(disp);
  320. if (currentConfig < 0 || currentConfig > static_cast<int>((numConfigs-1))) {
  321. ALOGE("%s: Invalid display config! %d", __FUNCTION__, currentConfig);
  322. currentConfig = 0;
  323. }
  324. mDisplayData[disp].currentConfig = currentConfig;
  325. for (size_t c = 0; c < numConfigs; ++c) {
  326. err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
  327. DISPLAY_ATTRIBUTES, values);
  328. // If this is a pre-1.5 HWC, it may not know about color transform, so
  329. // try again with a smaller set of attributes
  330. if (err != NO_ERROR) {
  331. err = mHwc->getDisplayAttributes(mHwc, disp, configs[c],
  332. PRE_HWC15_DISPLAY_ATTRIBUTES, values);
  333. }
  334. if (err != NO_ERROR) {
  335. // we can't get this display's info. turn it off.
  336. mDisplayData[disp].connected = false;
  337. return err;
  338. }
  339. DisplayConfig config = DisplayConfig();
  340. for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
  341. switch (DISPLAY_ATTRIBUTES[i]) {
  342. case HWC_DISPLAY_VSYNC_PERIOD:
  343. config.refresh = nsecs_t(values[i]);
  344. break;
  345. case HWC_DISPLAY_WIDTH:
  346. config.width = values[i];
  347. break;
  348. case HWC_DISPLAY_HEIGHT:
  349. config.height = values[i];
  350. break;
  351. case HWC_DISPLAY_DPI_X:
  352. config.xdpi = values[i] / 1000.0f;
  353. break;
  354. case HWC_DISPLAY_DPI_Y:
  355. config.ydpi = values[i] / 1000.0f;
  356. break;
  357. case HWC_DISPLAY_COLOR_TRANSFORM:
  358. config.colorTransform = values[i];
  359. break;
  360. default:
  361. ALOG_ASSERT(false, "unknown display attribute[%zu] %#x",
  362. i, DISPLAY_ATTRIBUTES[i]);
  363. break;
  364. }
  365. }
  366. if (config.xdpi == 0.0f || config.ydpi == 0.0f) {
  367. float dpi = getDefaultDensity(config.width, config.height);
  368. config.xdpi = dpi;
  369. config.ydpi = dpi;
  370. }
  371. mDisplayData[disp].configs.push_back(config);
  372. }
  373. // FIXME: what should we set the format to?
  374. #ifdef USE_BGRA_8888
  375. mDisplayData[disp].format = HAL_PIXEL_FORMAT_BGRA_8888;
  376. #else
  377. mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
  378. #endif
  379. mDisplayData[disp].connected = true;
  380. return NO_ERROR;
  381. }
  382. status_t HWComposer::setVirtualDisplayProperties(int32_t id,
  383. uint32_t w, uint32_t h, uint32_t format) {
  384. if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
  385. !mAllocatedDisplayIDs.hasBit(id)) {
  386. return BAD_INDEX;
  387. }
  388. size_t configId = mDisplayData[id].currentConfig;
  389. mDisplayData[id].format = format;
  390. DisplayConfig& config = mDisplayData[id].configs.editItemAt(configId);
  391. config.width = w;
  392. config.height = h;
  393. config.xdpi = config.ydpi = getDefaultDensity(w, h);
  394. return NO_ERROR;
  395. }
  396. int32_t HWComposer::allocateDisplayId() {
  397. if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
  398. return NO_MEMORY;
  399. }
  400. int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
  401. mAllocatedDisplayIDs.markBit(id);
  402. mDisplayData[id].connected = true;
  403. mDisplayData[id].configs.resize(1);
  404. mDisplayData[id].currentConfig = 0;
  405. return id;
  406. }
  407. status_t HWComposer::freeDisplayId(int32_t id) {
  408. if (id < NUM_BUILTIN_DISPLAYS) {
  409. // cannot free the reserved IDs
  410. return BAD_VALUE;
  411. }
  412. if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  413. return BAD_INDEX;
  414. }
  415. mAllocatedDisplayIDs.clearBit(id);
  416. mDisplayData[id].connected = false;
  417. return NO_ERROR;
  418. }
  419. nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
  420. // this returns the last refresh timestamp.
  421. // if the last one is not available, we estimate it based on
  422. // the refresh period and whatever closest timestamp we have.
  423. Mutex::Autolock _l(mLock);
  424. nsecs_t now = systemTime(CLOCK_MONOTONIC);
  425. size_t configId = mDisplayData[disp].currentConfig;
  426. return now - ((now - mLastHwVSync[disp]) %
  427. mDisplayData[disp].configs[configId].refresh);
  428. }
  429. sp<Fence> HWComposer::getDisplayFence(int disp) const {
  430. return mDisplayData[disp].lastDisplayFence;
  431. }
  432. uint32_t HWComposer::getFormat(int disp) const {
  433. if (static_cast<uint32_t>(disp) >= MAX_HWC_DISPLAYS || !mAllocatedDisplayIDs.hasBit(disp)) {
  434. #ifdef USE_BGRA_8888
  435. return HAL_PIXEL_FORMAT_BGRA_8888;
  436. #else
  437. return HAL_PIXEL_FORMAT_RGBA_8888;
  438. #endif
  439. } else {
  440. return mDisplayData[disp].format;
  441. }
  442. }
  443. bool HWComposer::isConnected(int disp) const {
  444. return mDisplayData[disp].connected;
  445. }
  446. uint32_t HWComposer::getWidth(int disp) const {
  447. size_t currentConfig = mDisplayData[disp].currentConfig;
  448. return mDisplayData[disp].configs[currentConfig].width;
  449. }
  450. uint32_t HWComposer::getHeight(int disp) const {
  451. size_t currentConfig = mDisplayData[disp].currentConfig;
  452. return mDisplayData[disp].configs[currentConfig].height;
  453. }
  454. float HWComposer::getDpiX(int disp) const {
  455. size_t currentConfig = mDisplayData[disp].currentConfig;
  456. return mDisplayData[disp].configs[currentConfig].xdpi;
  457. }
  458. float HWComposer::getDpiY(int disp) const {
  459. size_t currentConfig = mDisplayData[disp].currentConfig;
  460. return mDisplayData[disp].configs[currentConfig].ydpi;
  461. }
  462. nsecs_t HWComposer::getRefreshPeriod(int disp) const {
  463. size_t currentConfig = mDisplayData[disp].currentConfig;
  464. return mDisplayData[disp].configs[currentConfig].refresh;
  465. }
  466. const Vector<HWComposer::DisplayConfig>& HWComposer::getConfigs(int disp) const {
  467. return mDisplayData[disp].configs;
  468. }
  469. size_t HWComposer::getCurrentConfig(int disp) const {
  470. return mDisplayData[disp].currentConfig;
  471. }
  472. void HWComposer::eventControl(int disp, int event, int enabled) {
  473. if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
  474. ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
  475. event, disp, enabled);
  476. return;
  477. }
  478. if (event != EVENT_VSYNC) {
  479. ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
  480. event, disp, enabled);
  481. return;
  482. }
  483. status_t err = NO_ERROR;
  484. if (mHwc && !mDebugForceFakeVSync) {
  485. // NOTE: we use our own internal lock here because we have to call
  486. // into the HWC with the lock held, and we want to make sure
  487. // that even if HWC blocks (which it shouldn't), it won't
  488. // affect other threads.
  489. Mutex::Autolock _l(mEventControlLock);
  490. const int32_t eventBit = 1UL << event;
  491. const int32_t newValue = enabled ? eventBit : 0;
  492. const int32_t oldValue = mDisplayData[disp].events & eventBit;
  493. if (newValue != oldValue) {
  494. ATRACE_CALL();
  495. err = mHwc->eventControl(mHwc, disp, event, enabled);
  496. if (!err) {
  497. int32_t& events(mDisplayData[disp].events);
  498. events = (events & ~eventBit) | newValue;
  499. char tag[16];
  500. snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", disp);
  501. ATRACE_INT(tag, enabled);
  502. }
  503. }
  504. // error here should not happen -- not sure what we should
  505. // do if it does.
  506. ALOGE_IF(err, "eventControl(%d, %d) failed %s",
  507. event, enabled, strerror(-err));
  508. }
  509. if (err == NO_ERROR && mVSyncThread != NULL) {
  510. mVSyncThread->setEnabled(enabled);
  511. }
  512. }
  513. status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
  514. if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  515. return BAD_INDEX;
  516. }
  517. if (mHwc) {
  518. DisplayData& disp(mDisplayData[id]);
  519. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  520. // we need space for the HWC_FRAMEBUFFER_TARGET
  521. numLayers++;
  522. }
  523. if (disp.capacity < numLayers || disp.list == NULL) {
  524. size_t size = sizeof(hwc_display_contents_1_t)
  525. + numLayers * sizeof(hwc_layer_1_t);
  526. free(disp.list);
  527. disp.list = (hwc_display_contents_1_t*)malloc(size);
  528. disp.capacity = numLayers;
  529. }
  530. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  531. disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
  532. memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
  533. const DisplayConfig& currentConfig =
  534. disp.configs[disp.currentConfig];
  535. const hwc_rect_t r = { 0, 0,
  536. (int) currentConfig.width, (int) currentConfig.height };
  537. disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
  538. disp.framebufferTarget->hints = 0;
  539. disp.framebufferTarget->flags = 0;
  540. disp.framebufferTarget->handle = disp.fbTargetHandle;
  541. disp.framebufferTarget->transform = 0;
  542. disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
  543. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
  544. disp.framebufferTarget->sourceCropf.left = 0;
  545. disp.framebufferTarget->sourceCropf.top = 0;
  546. disp.framebufferTarget->sourceCropf.right =
  547. currentConfig.width;
  548. disp.framebufferTarget->sourceCropf.bottom =
  549. currentConfig.height;
  550. } else {
  551. disp.framebufferTarget->sourceCrop = r;
  552. }
  553. disp.framebufferTarget->displayFrame = r;
  554. disp.framebufferTarget->visibleRegionScreen.numRects = 1;
  555. disp.framebufferTarget->visibleRegionScreen.rects =
  556. &disp.framebufferTarget->displayFrame;
  557. disp.framebufferTarget->acquireFenceFd = -1;
  558. disp.framebufferTarget->releaseFenceFd = -1;
  559. disp.framebufferTarget->planeAlpha = 0xFF;
  560. }
  561. disp.list->retireFenceFd = -1;
  562. disp.list->flags = HWC_GEOMETRY_CHANGED;
  563. disp.list->numHwLayers = numLayers;
  564. }
  565. return NO_ERROR;
  566. }
  567. status_t HWComposer::setFramebufferTarget(int32_t id,
  568. const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
  569. if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  570. return BAD_INDEX;
  571. }
  572. DisplayData& disp(mDisplayData[id]);
  573. if (!disp.framebufferTarget) {
  574. // this should never happen, but apparently eglCreateWindowSurface()
  575. // triggers a Surface::queueBuffer() on some
  576. // devices (!?) -- log and ignore.
  577. ALOGE("HWComposer: framebufferTarget is null");
  578. return NO_ERROR;
  579. }
  580. int acquireFenceFd = -1;
  581. if (acquireFence->isValid()) {
  582. acquireFenceFd = acquireFence->dup();
  583. }
  584. // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
  585. disp.fbTargetHandle = buf->handle;
  586. disp.framebufferTarget->handle = disp.fbTargetHandle;
  587. disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
  588. return NO_ERROR;
  589. }
  590. status_t HWComposer::prepare() {
  591. Mutex::Autolock _l(mDisplayLock);
  592. for (size_t i=0 ; i<mNumDisplays ; i++) {
  593. DisplayData& disp(mDisplayData[i]);
  594. if (disp.framebufferTarget) {
  595. // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
  596. // DO NOT reset the handle field to NULL, because it's possible
  597. // that we have nothing to redraw (eg: eglSwapBuffers() not called)
  598. // in which case, we should continue to use the same buffer.
  599. LOG_FATAL_IF(disp.list == NULL);
  600. disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
  601. }
  602. if (!disp.connected && disp.list != NULL) {
  603. ALOGW("WARNING: disp %zu: connected, non-null list, layers=%zu",
  604. i, disp.list->numHwLayers);
  605. }
  606. mLists[i] = disp.list;
  607. if (mLists[i]) {
  608. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
  609. mLists[i]->outbuf = disp.outbufHandle;
  610. mLists[i]->outbufAcquireFenceFd = -1;
  611. } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  612. // garbage data to catch improper use
  613. mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
  614. mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
  615. } else {
  616. mLists[i]->dpy = EGL_NO_DISPLAY;
  617. mLists[i]->sur = EGL_NO_SURFACE;
  618. }
  619. }
  620. }
  621. int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
  622. ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
  623. if (err == NO_ERROR) {
  624. // here we're just making sure that "skip" layers are set
  625. // to HWC_FRAMEBUFFER and we're also counting how many layers
  626. // we have of each type.
  627. //
  628. // If there are no window layers, we treat the display has having FB
  629. // composition, because SurfaceFlinger will use GLES to draw the
  630. // wormhole region.
  631. for (size_t i=0 ; i<mNumDisplays ; i++) {
  632. DisplayData& disp(mDisplayData[i]);
  633. disp.hasFbComp = false;
  634. disp.hasOvComp = false;
  635. if (disp.list) {
  636. for (size_t j=0 ; j<disp.list->numHwLayers ; j++) {
  637. hwc_layer_1_t& l = disp.list->hwLayers[j];
  638. //ALOGD("prepare: %d, type=%d, handle=%p",
  639. // i, l.compositionType, l.handle);
  640. if ((i == DisplayDevice::DISPLAY_PRIMARY) &&
  641. l.flags & HWC_SKIP_LAYER) {
  642. l.compositionType = HWC_FRAMEBUFFER;
  643. }
  644. if (l.compositionType == HWC_FRAMEBUFFER) {
  645. disp.hasFbComp = true;
  646. }
  647. if (l.compositionType == HWC_OVERLAY) {
  648. disp.hasOvComp = true;
  649. }
  650. if (isCompositionTypeBlit(l.compositionType)) {
  651. disp.hasFbComp = true;
  652. }
  653. if (l.compositionType == HWC_CURSOR_OVERLAY) {
  654. disp.hasOvComp = true;
  655. }
  656. }
  657. if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) {
  658. disp.hasFbComp = true;
  659. }
  660. } else {
  661. disp.hasFbComp = true;
  662. }
  663. }
  664. }
  665. return (status_t)err;
  666. }
  667. bool HWComposer::hasHwcComposition(int32_t id) const {
  668. if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  669. return false;
  670. return mDisplayData[id].hasOvComp;
  671. }
  672. bool HWComposer::hasGlesComposition(int32_t id) const {
  673. if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  674. return true;
  675. return mDisplayData[id].hasFbComp;
  676. }
  677. sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
  678. if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  679. return Fence::NO_FENCE;
  680. int fd = INVALID_OPERATION;
  681. if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  682. const DisplayData& disp(mDisplayData[id]);
  683. if (disp.framebufferTarget) {
  684. fd = disp.framebufferTarget->releaseFenceFd;
  685. disp.framebufferTarget->acquireFenceFd = -1;
  686. disp.framebufferTarget->releaseFenceFd = -1;
  687. }
  688. }
  689. return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
  690. }
  691. status_t HWComposer::commit() {
  692. int err = NO_ERROR;
  693. if (mHwc) {
  694. if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  695. // On version 1.0, the OpenGL ES target surface is communicated
  696. // by the (dpy, sur) fields and we are guaranteed to have only
  697. // a single display.
  698. mLists[0]->dpy = eglGetCurrentDisplay();
  699. mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
  700. }
  701. for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
  702. DisplayData& disp(mDisplayData[i]);
  703. if (disp.outbufHandle) {
  704. mLists[i]->outbuf = disp.outbufHandle;
  705. mLists[i]->outbufAcquireFenceFd =
  706. disp.outbufAcquireFence->dup();
  707. }
  708. }
  709. err = mHwc->set(mHwc, mNumDisplays, mLists);
  710. for (size_t i=0 ; i<mNumDisplays ; i++) {
  711. DisplayData& disp(mDisplayData[i]);
  712. disp.lastDisplayFence = disp.lastRetireFence;
  713. disp.lastRetireFence = Fence::NO_FENCE;
  714. if (disp.list) {
  715. if (disp.list->retireFenceFd != -1) {
  716. disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
  717. disp.list->retireFenceFd = -1;
  718. }
  719. disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
  720. }
  721. }
  722. }
  723. return (status_t)err;
  724. }
  725. status_t HWComposer::setPowerMode(int disp, int mode) {
  726. LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
  727. if (mHwc) {
  728. if (mode == HWC_POWER_MODE_OFF) {
  729. eventControl(disp, HWC_EVENT_VSYNC, 0);
  730. }
  731. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
  732. return (status_t)mHwc->setPowerMode(mHwc, disp, mode);
  733. } else {
  734. return (status_t)mHwc->blank(mHwc, disp,
  735. mode == HWC_POWER_MODE_OFF ? 1 : 0);
  736. }
  737. }
  738. return NO_ERROR;
  739. }
  740. status_t HWComposer::setActiveConfig(int disp, int mode) {
  741. LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
  742. DisplayData& dd(mDisplayData[disp]);
  743. if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
  744. status_t status = static_cast<status_t>(
  745. mHwc->setActiveConfig(mHwc, disp, mode));
  746. if (status == NO_ERROR) {
  747. dd.currentConfig = mode;
  748. } else {
  749. ALOGE("%s Failed to set new config (%d) for display (%d)",
  750. __FUNCTION__, mode, disp);
  751. }
  752. return status;
  753. } else {
  754. LOG_FATAL_IF(mode != 0);
  755. }
  756. return NO_ERROR;
  757. }
  758. int HWComposer::getActiveConfig(int disp) const {
  759. LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
  760. if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
  761. return mHwc->getActiveConfig(mHwc, disp);
  762. } else {
  763. return 0;
  764. }
  765. }
  766. void HWComposer::disconnectDisplay(int disp) {
  767. LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
  768. DisplayData& dd(mDisplayData[disp]);
  769. free(dd.list);
  770. dd.list = NULL;
  771. dd.framebufferTarget = NULL; // points into dd.list
  772. dd.fbTargetHandle = NULL;
  773. dd.outbufHandle = NULL;
  774. dd.lastRetireFence = Fence::NO_FENCE;
  775. dd.lastDisplayFence = Fence::NO_FENCE;
  776. dd.outbufAcquireFence = Fence::NO_FENCE;
  777. // clear all the previous configs and repopulate when a new
  778. // device is added
  779. dd.configs.clear();
  780. }
  781. int HWComposer::getVisualID() const {
  782. if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  783. // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
  784. // is supported by the implementation. we can only be in this case
  785. // if we have HWC 1.1
  786. #ifdef USE_BGRA_8888
  787. return HAL_PIXEL_FORMAT_BGRA_8888;
  788. #else
  789. return HAL_PIXEL_FORMAT_RGBA_8888;
  790. #endif
  791. //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
  792. } else {
  793. return mFbDev->format;
  794. }
  795. }
  796. bool HWComposer::supportsFramebufferTarget() const {
  797. return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
  798. }
  799. int HWComposer::fbPost(int32_t id,
  800. const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
  801. if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  802. return setFramebufferTarget(id, acquireFence, buffer);
  803. } else {
  804. acquireFence->waitForever("HWComposer::fbPost");
  805. return mFbDev->post(mFbDev, buffer->handle);
  806. }
  807. }
  808. int HWComposer::fbCompositionComplete() {
  809. if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
  810. return NO_ERROR;
  811. if (mFbDev->compositionComplete) {
  812. return mFbDev->compositionComplete(mFbDev);
  813. } else {
  814. return INVALID_OPERATION;
  815. }
  816. }
  817. void HWComposer::fbDump(String8& result) {
  818. if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
  819. const size_t SIZE = 4096;
  820. char buffer[SIZE];
  821. mFbDev->dump(mFbDev, buffer, SIZE);
  822. result.append(buffer);
  823. }
  824. }
  825. status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
  826. const sp<GraphicBuffer>& buf) {
  827. if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  828. return BAD_INDEX;
  829. if (id < VIRTUAL_DISPLAY_ID_BASE)
  830. return INVALID_OPERATION;
  831. DisplayData& disp(mDisplayData[id]);
  832. disp.outbufHandle = buf->handle;
  833. disp.outbufAcquireFence = acquireFence;
  834. return NO_ERROR;
  835. }
  836. sp<Fence> HWComposer::getLastRetireFence(int32_t id) const {
  837. if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
  838. return Fence::NO_FENCE;
  839. return mDisplayData[id].lastRetireFence;
  840. }
  841. status_t HWComposer::setCursorPositionAsync(int32_t id, const Rect& pos)
  842. {
  843. if (mHwc->setCursorPositionAsync) {
  844. return (status_t)mHwc->setCursorPositionAsync(mHwc, id, pos.left, pos.top);
  845. }
  846. else {
  847. return NO_ERROR;
  848. }
  849. }
  850. /*
  851. * Helper template to implement a concrete HWCLayer
  852. * This holds the pointer to the concrete hwc layer type
  853. * and implements the "iterable" side of HWCLayer.
  854. */
  855. template<typename CONCRETE, typename HWCTYPE>
  856. class Iterable : public HWComposer::HWCLayer {
  857. protected:
  858. HWCTYPE* const mLayerList;
  859. HWCTYPE* mCurrentLayer;
  860. Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
  861. inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
  862. inline HWCTYPE* getLayer() { return mCurrentLayer; }
  863. virtual ~Iterable() { }
  864. private:
  865. // returns a copy of ourselves
  866. virtual HWComposer::HWCLayer* dup() {
  867. return new CONCRETE( static_cast<const CONCRETE&>(*this) );
  868. }
  869. virtual status_t setLayer(size_t index) {
  870. mCurrentLayer = &mLayerList[index];
  871. return NO_ERROR;
  872. }
  873. };
  874. /*
  875. * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
  876. * This implements the HWCLayer side of HWCIterableLayer.
  877. */
  878. class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
  879. struct hwc_composer_device_1* mHwc;
  880. public:
  881. HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
  882. : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
  883. virtual int32_t getCompositionType() const {
  884. return getLayer()->compositionType;
  885. }
  886. virtual uint32_t getHints() const {
  887. return getLayer()->hints;
  888. }
  889. virtual sp<Fence> getAndResetReleaseFence() {
  890. int fd = getLayer()->releaseFenceFd;
  891. getLayer()->releaseFenceFd = -1;
  892. return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
  893. }
  894. virtual void setAcquireFenceFd(int fenceFd) {
  895. getLayer()->acquireFenceFd = fenceFd;
  896. }
  897. virtual void setPerFrameDefaultState() {
  898. //getLayer()->compositionType = HWC_FRAMEBUFFER;
  899. }
  900. virtual void setPlaneAlpha(uint8_t alpha) {
  901. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
  902. getLayer()->planeAlpha = alpha;
  903. } else {
  904. if (alpha < 0xFF) {
  905. getLayer()->flags |= HWC_SKIP_LAYER;
  906. }
  907. }
  908. }
  909. virtual void setAnimating(bool animating) {
  910. if (animating) {
  911. #ifdef QTI_BSP
  912. getLayer()->flags |= HWC_SCREENSHOT_ANIMATOR_LAYER;
  913. #endif
  914. } else {
  915. #ifdef QTI_BSP
  916. getLayer()->flags &= ~HWC_SCREENSHOT_ANIMATOR_LAYER;
  917. #endif
  918. }
  919. }
  920. virtual void setDefaultState() {
  921. hwc_layer_1_t* const l = getLayer();
  922. l->compositionType = HWC_FRAMEBUFFER;
  923. l->hints = 0;
  924. l->flags = HWC_SKIP_LAYER;
  925. l->handle = 0;
  926. l->transform = 0;
  927. l->blending = HWC_BLENDING_NONE;
  928. l->visibleRegionScreen.numRects = 0;
  929. l->visibleRegionScreen.rects = NULL;
  930. l->acquireFenceFd = -1;
  931. l->releaseFenceFd = -1;
  932. l->planeAlpha = 0xFF;
  933. }
  934. virtual void setSkip(bool skip) {
  935. if (skip) {
  936. getLayer()->flags |= HWC_SKIP_LAYER;
  937. } else {
  938. getLayer()->flags &= ~HWC_SKIP_LAYER;
  939. }
  940. }
  941. virtual void setDim() {
  942. setSkip(false);
  943. getLayer()->flags |= 0x80000000;
  944. }
  945. virtual void setIsCursorLayerHint(bool isCursor) {
  946. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) {
  947. if (isCursor) {
  948. getLayer()->flags |= HWC_IS_CURSOR_LAYER;
  949. }
  950. else {
  951. getLayer()->flags &= ~HWC_IS_CURSOR_LAYER;
  952. }
  953. }
  954. }
  955. virtual void setBlending(uint32_t blending) {
  956. getLayer()->blending = blending;
  957. }
  958. virtual void setTransform(uint32_t transform) {
  959. getLayer()->transform = transform;
  960. }
  961. virtual void setFrame(const Rect& frame) {
  962. getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
  963. }
  964. virtual void setCrop(const FloatRect& crop) {
  965. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
  966. getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
  967. } else {
  968. /*
  969. * Since h/w composer didn't support a flot crop rect before version 1.3,
  970. * using integer coordinates instead produces a different output from the GL code in
  971. * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
  972. * window size ratio is large and a window crop is defined
  973. * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
  974. */
  975. hwc_rect_t& r = getLayer()->sourceCrop;
  976. r.left = int(ceilf(crop.left));
  977. r.top = int(ceilf(crop.top));
  978. r.right = int(floorf(crop.right));
  979. r.bottom= int(floorf(crop.bottom));
  980. }
  981. }
  982. virtual void setVisibleRegionScreen(const Region& reg) {
  983. // Region::getSharedBuffer creates a reference to the underlying
  984. // SharedBuffer of this Region, this reference is freed
  985. // in onDisplayed()
  986. hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
  987. SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
  988. visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
  989. }
  990. virtual void setSurfaceDamage(const Region& reg) {
  991. if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_5)) {
  992. return;
  993. }
  994. hwc_region_t& surfaceDamage = getLayer()->surfaceDamage;
  995. // We encode default full-screen damage as INVALID_RECT upstream, but as
  996. // 0 rects for HWComposer
  997. if (reg.isRect() && reg.getBounds() == Rect::INVALID_RECT) {
  998. surfaceDamage.numRects = 0;
  999. surfaceDamage.rects = NULL;
  1000. return;
  1001. }
  1002. SharedBuffer const* sb = reg.getSharedBuffer(&surfaceDamage.numRects);
  1003. surfaceDamage.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
  1004. }
  1005. virtual void setSidebandStream(const sp<NativeHandle>& stream) {
  1006. ALOG_ASSERT(stream->handle() != NULL);
  1007. getLayer()->compositionType = HWC_SIDEBAND;
  1008. getLayer()->sidebandStream = stream->handle();
  1009. }
  1010. virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
  1011. if (buffer == 0 || buffer->handle == 0) {
  1012. getLayer()->compositionType = HWC_FRAMEBUFFER;
  1013. getLayer()->flags |= HWC_SKIP_LAYER;
  1014. getLayer()->handle = 0;
  1015. } else {
  1016. if (getLayer()->compositionType == HWC_SIDEBAND) {
  1017. // If this was a sideband layer but the stream was removed, reset
  1018. // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare.
  1019. getLayer()->compositionType = HWC_FRAMEBUFFER;
  1020. }
  1021. getLayer()->handle = buffer->handle;
  1022. }
  1023. }
  1024. virtual void onDisplayed() {
  1025. hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
  1026. SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
  1027. if (sb) {
  1028. sb->release();
  1029. // not technically needed but safer
  1030. visibleRegion.numRects = 0;
  1031. visibleRegion.rects = NULL;
  1032. }
  1033. getLayer()->acquireFenceFd = -1;
  1034. if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_5)) {
  1035. return;
  1036. }
  1037. hwc_region_t& surfaceDamage = getLayer()->surfaceDamage;
  1038. sb = SharedBuffer::bufferFromData(surfaceDamage.rects);
  1039. if (sb) {
  1040. sb->release();
  1041. surfaceDamage.numRects = 0;
  1042. surfaceDamage.rects = NULL;
  1043. }
  1044. }
  1045. };
  1046. /*
  1047. * returns an iterator initialized at a given index in the layer list
  1048. */
  1049. HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
  1050. if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
  1051. return LayerListIterator();
  1052. }
  1053. const DisplayData& disp(mDisplayData[id]);
  1054. if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
  1055. return LayerListIterator();
  1056. }
  1057. return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
  1058. }
  1059. /*
  1060. * returns an iterator on the beginning of the layer list
  1061. */
  1062. HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
  1063. return getLayerIterator(id, 0);
  1064. }
  1065. /*
  1066. * returns an iterator on the end of the layer list
  1067. */
  1068. HWComposer::LayerListIterator HWComposer::end(int32_t id) {
  1069. size_t numLayers = 0;
  1070. if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
  1071. const DisplayData& disp(mDisplayData[id]);
  1072. if (mHwc && disp.list) {
  1073. numLayers = disp.list->numHwLayers;
  1074. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
  1075. // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
  1076. // which we ignore when iterating through the layer list.
  1077. ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
  1078. if (numLayers) {
  1079. numLayers--;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. return getLayerIterator(id, numLayers);
  1085. }
  1086. // Converts a PixelFormat to a human-readable string. Max 11 chars.
  1087. // (Could use a table of prefab String8 objects.)
  1088. static String8 getFormatStr(PixelFormat format) {
  1089. switch (format) {
  1090. case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888");
  1091. case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888");
  1092. case PIXEL_FORMAT_RGB_888: return String8("RGB_888");
  1093. case PIXEL_FORMAT_RGB_565: return String8("RGB_565");
  1094. case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888");
  1095. case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
  1096. return String8("ImplDef");
  1097. default:
  1098. String8 result;
  1099. result.appendFormat("? %08x", format);
  1100. return result;
  1101. }
  1102. }
  1103. void HWComposer::dump(String8& result) const {
  1104. Mutex::Autolock _l(mDisplayLock);
  1105. if (mHwc) {
  1106. result.appendFormat("Hardware Composer state (version %08x):\n", hwcApiVersion(mHwc));
  1107. result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
  1108. for (size_t i=0 ; i<mNumDisplays ; i++) {
  1109. const DisplayData& disp(mDisplayData[i]);
  1110. if (!disp.connected)
  1111. continue;
  1112. const Vector< sp<Layer> >& visibleLayersSortedByZ =
  1113. mFlinger->getLayerSortedByZForHwcDisplay(i);
  1114. result.appendFormat(" Display[%zd] configurations (* current):\n", i);
  1115. for (size_t c = 0; c < disp.configs.size(); ++c) {
  1116. const DisplayConfig& config(disp.configs[c]);
  1117. result.appendFormat(" %s%zd: %ux%u, xdpi=%f, ydpi=%f"
  1118. ", refresh=%" PRId64 ", colorTransform=%d\n",
  1119. c == disp.currentConfig ? "* " : "", c,
  1120. config.width, config.height, config.xdpi, config.ydpi,
  1121. config.refresh, config.colorTransform);
  1122. }
  1123. if (disp.list) {
  1124. result.appendFormat(
  1125. " numHwLayers=%zu, flags=%08x\n",
  1126. disp.list->numHwLayers, disp.list->flags);
  1127. result.append(
  1128. " type | handle | hint | flag | tr | blnd | format | source crop (l,t,r,b) | frame | name \n"
  1129. "-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------\n");
  1130. // " _________ | ________ | ____ | ____ | __ | ____ | ___________ |_____._,_____._,_____._,_____._ |_____,_____,_____,_____ | ___...
  1131. for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
  1132. const hwc_layer_1_t&l = disp.list->hwLayers[i];
  1133. int32_t format = -1;
  1134. String8 name("unknown");
  1135. if (i < visibleLayersSortedByZ.size()) {
  1136. const sp<Layer>& layer(visibleLayersSortedByZ[i]);
  1137. const sp<GraphicBuffer>& buffer(
  1138. layer->getActiveBuffer());
  1139. if (buffer != NULL) {
  1140. format = buffer->getPixelFormat();
  1141. }
  1142. name = layer->getName();
  1143. }
  1144. int type = l.compositionType;
  1145. if (type == HWC_FRAMEBUFFER_TARGET) {
  1146. name = "HWC_FRAMEBUFFER_TARGET";
  1147. format = disp.format;
  1148. }
  1149. static char const* compositionTypeName[] = {
  1150. "GLES",
  1151. "HWC",
  1152. "BKGND",
  1153. "FB TARGET",
  1154. "SIDEBAND",
  1155. "HWC_CURSOR",
  1156. "UNKNOWN"};
  1157. if (type >= NELEM(compositionTypeName))
  1158. type = NELEM(compositionTypeName) - 1;
  1159. String8 formatStr = getFormatStr(format);
  1160. if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
  1161. result.appendFormat(
  1162. " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d | %s\n",
  1163. (isCompositionTypeBlit(l.compositionType)) ? "HWC_BLIT" : compositionTypeName[type],
  1164. intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
  1165. l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
  1166. l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
  1167. name.string());
  1168. } else {
  1169. result.appendFormat(
  1170. " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7d,%7d,%7d,%7d |%5d,%5d,%5d,%5d | %s\n",
  1171. (isCompositionTypeBlit(l.compositionType)) ? "HWC_BLIT" : compositionTypeName[type],
  1172. intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(),
  1173. l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
  1174. l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
  1175. name.string());
  1176. }
  1177. }
  1178. }
  1179. }
  1180. }
  1181. if (mHwc && mHwc->dump) {
  1182. const size_t SIZE = 4096;
  1183. char buffer[SIZE];
  1184. mHwc->dump(mHwc, buffer, SIZE);
  1185. result.append(buffer);
  1186. }
  1187. }
  1188. // ---------------------------------------------------------------------------
  1189. HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
  1190. : mHwc(hwc), mEnabled(false),
  1191. mNextFakeVSync(0),
  1192. mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
  1193. {
  1194. }
  1195. void HWComposer::VSyncThread::setEnabled(bool enabled) {
  1196. Mutex::Autolock _l(mLock);
  1197. if (mEnabled != enabled) {
  1198. mEnabled = enabled;
  1199. mCondition.signal();
  1200. }
  1201. }
  1202. void HWComposer::VSyncThread::onFirstRef() {
  1203. run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
  1204. }
  1205. bool HWComposer::VSyncThread::threadLoop() {
  1206. { // scope for lock
  1207. Mutex::Autolock _l(mLock);
  1208. while (!mEnabled) {
  1209. mCondition.wait(mLock);
  1210. }
  1211. }
  1212. const nsecs_t period = mRefreshPeriod;
  1213. const nsecs_t now = systemTime(CLOCK_MONOTONIC);
  1214. nsecs_t next_vsync = mNextFakeVSync;
  1215. nsecs_t sleep = next_vsync - now;
  1216. if (sleep < 0) {
  1217. // we missed, find where the next vsync should be
  1218. sleep = (period - ((now - next_vsync) % period));
  1219. next_vsync = now + sleep;
  1220. }
  1221. mNextFakeVSync = next_vsync + period;
  1222. struct timespec spec;
  1223. spec.tv_sec = next_vsync / 1000000000;
  1224. spec.tv_nsec = next_vsync % 1000000000;
  1225. int err;
  1226. do {
  1227. err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
  1228. } while (err<0 && errno == EINTR);
  1229. if (err == 0) {
  1230. mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
  1231. }
  1232. return true;
  1233. }
  1234. HWComposer::DisplayData::DisplayData()
  1235. : configs(),
  1236. currentConfig(0),
  1237. #ifdef USE_BGRA_8888
  1238. format(HAL_PIXEL_FORMAT_BGRA_8888),
  1239. #else
  1240. format(HAL_PIXEL_FORMAT_RGBA_8888),
  1241. #endif
  1242. connected(false),
  1243. hasFbComp(false), hasOvComp(false),
  1244. capacity(0), list(NULL),
  1245. framebufferTarget(NULL), fbTargetHandle(0),
  1246. lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
  1247. outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
  1248. events(0)
  1249. {}
  1250. HWComposer::DisplayData::~DisplayData() {
  1251. free(list);
  1252. }
  1253. // ---------------------------------------------------------------------------
  1254. }; // namespace android