HWC2.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * Copyright 2015 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 LOG_NDEBUG 0
  17. #undef LOG_TAG
  18. #define LOG_TAG "HWC2"
  19. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  20. #include "HWC2.h"
  21. #include "ComposerHal.h"
  22. #include <ui/Fence.h>
  23. #include <ui/FloatRect.h>
  24. #include <ui/GraphicBuffer.h>
  25. #include <android/configuration.h>
  26. #include <inttypes.h>
  27. #include <algorithm>
  28. #include <iterator>
  29. #include <set>
  30. using android::Fence;
  31. using android::FloatRect;
  32. using android::GraphicBuffer;
  33. using android::HdrCapabilities;
  34. using android::HdrMetadata;
  35. using android::Rect;
  36. using android::Region;
  37. using android::sp;
  38. using android::hardware::Return;
  39. using android::hardware::Void;
  40. namespace HWC2 {
  41. namespace Hwc2 = android::Hwc2;
  42. using android::ui::ColorMode;
  43. using android::ui::Dataspace;
  44. using android::ui::PixelFormat;
  45. using android::ui::RenderIntent;
  46. namespace {
  47. inline bool hasMetadataKey(const std::set<Hwc2::PerFrameMetadataKey>& keys,
  48. const Hwc2::PerFrameMetadataKey& key) {
  49. return keys.find(key) != keys.end();
  50. }
  51. class ComposerCallbackBridge : public Hwc2::IComposerCallback {
  52. public:
  53. ComposerCallbackBridge(ComposerCallback* callback, int32_t sequenceId)
  54. : mCallback(callback), mSequenceId(sequenceId) {}
  55. Return<void> onHotplug(Hwc2::Display display,
  56. IComposerCallback::Connection conn) override
  57. {
  58. HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
  59. mCallback->onHotplugReceived(mSequenceId, display, connection);
  60. return Void();
  61. }
  62. Return<void> onRefresh(Hwc2::Display display) override
  63. {
  64. mCallback->onRefreshReceived(mSequenceId, display);
  65. return Void();
  66. }
  67. Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
  68. {
  69. mCallback->onVsyncReceived(mSequenceId, display, timestamp);
  70. return Void();
  71. }
  72. private:
  73. ComposerCallback* mCallback;
  74. int32_t mSequenceId;
  75. };
  76. } // namespace anonymous
  77. // Device methods
  78. Device::Device(std::unique_ptr<android::Hwc2::Composer> composer) : mComposer(std::move(composer)) {
  79. loadCapabilities();
  80. }
  81. void Device::registerCallback(ComposerCallback* callback, int32_t sequenceId) {
  82. if (mRegisteredCallback) {
  83. ALOGW("Callback already registered. Ignored extra registration "
  84. "attempt.");
  85. return;
  86. }
  87. mRegisteredCallback = true;
  88. sp<ComposerCallbackBridge> callbackBridge(
  89. new ComposerCallbackBridge(callback, sequenceId));
  90. mComposer->registerCallback(callbackBridge);
  91. }
  92. // Required by HWC2 device
  93. std::string Device::dump() const
  94. {
  95. return mComposer->dumpDebugInfo();
  96. }
  97. uint32_t Device::getMaxVirtualDisplayCount() const
  98. {
  99. return mComposer->getMaxVirtualDisplayCount();
  100. }
  101. Error Device::getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
  102. std::vector<uint8_t>* outData) const {
  103. auto intError = mComposer->getDisplayIdentificationData(hwcDisplayId, outPort, outData);
  104. return static_cast<Error>(intError);
  105. }
  106. Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
  107. PixelFormat* format, Display** outDisplay)
  108. {
  109. ALOGI("Creating virtual display");
  110. hwc2_display_t displayId = 0;
  111. auto intError = mComposer->createVirtualDisplay(width, height,
  112. format, &displayId);
  113. auto error = static_cast<Error>(intError);
  114. if (error != Error::None) {
  115. return error;
  116. }
  117. auto display = std::make_unique<impl::Display>(*mComposer.get(), mCapabilities, displayId,
  118. DisplayType::Virtual);
  119. display->setConnected(true);
  120. *outDisplay = display.get();
  121. mDisplays.emplace(displayId, std::move(display));
  122. ALOGI("Created virtual display");
  123. return Error::None;
  124. }
  125. void Device::destroyDisplay(hwc2_display_t displayId)
  126. {
  127. ALOGI("Destroying display %" PRIu64, displayId);
  128. mDisplays.erase(displayId);
  129. }
  130. void Device::onHotplug(hwc2_display_t displayId, Connection connection) {
  131. if (connection == Connection::Connected) {
  132. // If we get a hotplug connected event for a display we already have,
  133. // destroy the display and recreate it. This will force us to requery
  134. // the display params and recreate all layers on that display.
  135. auto oldDisplay = getDisplayById(displayId);
  136. if (oldDisplay != nullptr && oldDisplay->isConnected()) {
  137. ALOGI("Hotplug connecting an already connected display."
  138. " Clearing old display state.");
  139. }
  140. mDisplays.erase(displayId);
  141. DisplayType displayType;
  142. auto intError = mComposer->getDisplayType(displayId,
  143. reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(
  144. &displayType));
  145. auto error = static_cast<Error>(intError);
  146. if (error != Error::None) {
  147. ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d). "
  148. "Aborting hotplug attempt.",
  149. displayId, to_string(error).c_str(), intError);
  150. return;
  151. }
  152. auto newDisplay = std::make_unique<impl::Display>(*mComposer.get(), mCapabilities,
  153. displayId, displayType);
  154. newDisplay->setConnected(true);
  155. mDisplays.emplace(displayId, std::move(newDisplay));
  156. } else if (connection == Connection::Disconnected) {
  157. // The display will later be destroyed by a call to
  158. // destroyDisplay(). For now we just mark it disconnected.
  159. auto display = getDisplayById(displayId);
  160. if (display) {
  161. display->setConnected(false);
  162. } else {
  163. ALOGW("Attempted to disconnect unknown display %" PRIu64,
  164. displayId);
  165. }
  166. }
  167. }
  168. // Other Device methods
  169. Display* Device::getDisplayById(hwc2_display_t id) {
  170. auto iter = mDisplays.find(id);
  171. return iter == mDisplays.end() ? nullptr : iter->second.get();
  172. }
  173. // Device initialization methods
  174. void Device::loadCapabilities()
  175. {
  176. static_assert(sizeof(Capability) == sizeof(int32_t),
  177. "Capability size has changed");
  178. auto capabilities = mComposer->getCapabilities();
  179. for (auto capability : capabilities) {
  180. mCapabilities.emplace(static_cast<Capability>(capability));
  181. }
  182. }
  183. Error Device::flushCommands()
  184. {
  185. return static_cast<Error>(mComposer->executeCommands());
  186. }
  187. // Display methods
  188. Display::~Display() = default;
  189. Display::Config::Config(Display& display, hwc2_config_t id)
  190. : mDisplay(display),
  191. mId(id),
  192. mWidth(-1),
  193. mHeight(-1),
  194. mVsyncPeriod(-1),
  195. mDpiX(-1),
  196. mDpiY(-1) {}
  197. Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
  198. : mConfig(new Config(display, id)) {}
  199. float Display::Config::Builder::getDefaultDensity() {
  200. // Default density is based on TVs: 1080p displays get XHIGH density, lower-
  201. // resolution displays get TV density. Maybe eventually we'll need to update
  202. // it for 4k displays, though hopefully those will just report accurate DPI
  203. // information to begin with. This is also used for virtual displays and
  204. // older HWC implementations, so be careful about orientation.
  205. auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
  206. if (longDimension >= 1080) {
  207. return ACONFIGURATION_DENSITY_XHIGH;
  208. } else {
  209. return ACONFIGURATION_DENSITY_TV;
  210. }
  211. }
  212. namespace impl {
  213. Display::Display(android::Hwc2::Composer& composer,
  214. const std::unordered_set<Capability>& capabilities, hwc2_display_t id,
  215. DisplayType type)
  216. : mComposer(composer),
  217. mCapabilities(capabilities),
  218. mId(id),
  219. mIsConnected(false),
  220. mType(type) {
  221. ALOGV("Created display %" PRIu64, id);
  222. }
  223. Display::~Display() {
  224. mLayers.clear();
  225. if (mType == DisplayType::Virtual) {
  226. ALOGV("Destroying virtual display");
  227. auto intError = mComposer.destroyVirtualDisplay(mId);
  228. auto error = static_cast<Error>(intError);
  229. ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
  230. ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
  231. } else if (mType == DisplayType::Physical) {
  232. auto error = setVsyncEnabled(HWC2::Vsync::Disable);
  233. if (error != Error::None) {
  234. ALOGE("~Display: Failed to disable vsync for display %" PRIu64
  235. ": %s (%d)", mId, to_string(error).c_str(),
  236. static_cast<int32_t>(error));
  237. }
  238. }
  239. }
  240. // Required by HWC2 display
  241. Error Display::acceptChanges()
  242. {
  243. auto intError = mComposer.acceptDisplayChanges(mId);
  244. return static_cast<Error>(intError);
  245. }
  246. Error Display::createLayer(HWC2::Layer** outLayer) {
  247. if (!outLayer) {
  248. return Error::BadParameter;
  249. }
  250. hwc2_layer_t layerId = 0;
  251. auto intError = mComposer.createLayer(mId, &layerId);
  252. auto error = static_cast<Error>(intError);
  253. if (error != Error::None) {
  254. return error;
  255. }
  256. auto layer = std::make_unique<impl::Layer>(mComposer, mCapabilities, mId, layerId);
  257. *outLayer = layer.get();
  258. mLayers.emplace(layerId, std::move(layer));
  259. return Error::None;
  260. }
  261. Error Display::destroyLayer(HWC2::Layer* layer) {
  262. if (!layer) {
  263. return Error::BadParameter;
  264. }
  265. mLayers.erase(layer->getId());
  266. return Error::None;
  267. }
  268. Error Display::getActiveConfig(
  269. std::shared_ptr<const Display::Config>* outConfig) const
  270. {
  271. ALOGV("[%" PRIu64 "] getActiveConfig", mId);
  272. hwc2_config_t configId = 0;
  273. auto intError = mComposer.getActiveConfig(mId, &configId);
  274. auto error = static_cast<Error>(intError);
  275. if (error != Error::None) {
  276. ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
  277. *outConfig = nullptr;
  278. return error;
  279. }
  280. if (mConfigs.count(configId) != 0) {
  281. *outConfig = mConfigs.at(configId);
  282. } else {
  283. ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
  284. configId);
  285. // Return no error, but the caller needs to check for a null pointer to
  286. // detect this case
  287. *outConfig = nullptr;
  288. }
  289. return Error::None;
  290. }
  291. Error Display::getActiveConfigIndex(int* outIndex) const {
  292. ALOGV("[%" PRIu64 "] getActiveConfigIndex", mId);
  293. hwc2_config_t configId = 0;
  294. auto intError = mComposer.getActiveConfig(mId, &configId);
  295. auto error = static_cast<Error>(intError);
  296. if (error != Error::None) {
  297. ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
  298. *outIndex = -1;
  299. return error;
  300. }
  301. auto pos = mConfigs.find(configId);
  302. if (pos != mConfigs.end()) {
  303. *outIndex = std::distance(mConfigs.begin(), pos);
  304. } else {
  305. ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId, configId);
  306. // Return no error, but the caller needs to check for a negative index
  307. // to detect this case
  308. *outIndex = -1;
  309. }
  310. return Error::None;
  311. }
  312. Error Display::getChangedCompositionTypes(std::unordered_map<HWC2::Layer*, Composition>* outTypes) {
  313. std::vector<Hwc2::Layer> layerIds;
  314. std::vector<Hwc2::IComposerClient::Composition> types;
  315. auto intError = mComposer.getChangedCompositionTypes(
  316. mId, &layerIds, &types);
  317. uint32_t numElements = layerIds.size();
  318. auto error = static_cast<Error>(intError);
  319. error = static_cast<Error>(intError);
  320. if (error != Error::None) {
  321. return error;
  322. }
  323. outTypes->clear();
  324. outTypes->reserve(numElements);
  325. for (uint32_t element = 0; element < numElements; ++element) {
  326. auto layer = getLayerById(layerIds[element]);
  327. if (layer) {
  328. auto type = static_cast<Composition>(types[element]);
  329. ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
  330. layer->getId(), to_string(type).c_str());
  331. outTypes->emplace(layer, type);
  332. } else {
  333. ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
  334. " on display %" PRIu64, layerIds[element], mId);
  335. }
  336. }
  337. return Error::None;
  338. }
  339. Error Display::getColorModes(std::vector<ColorMode>* outModes) const
  340. {
  341. auto intError = mComposer.getColorModes(mId, outModes);
  342. return static_cast<Error>(intError);
  343. }
  344. int32_t Display::getSupportedPerFrameMetadata() const
  345. {
  346. int32_t supportedPerFrameMetadata = 0;
  347. std::vector<Hwc2::PerFrameMetadataKey> tmpKeys = mComposer.getPerFrameMetadataKeys(mId);
  348. std::set<Hwc2::PerFrameMetadataKey> keys(tmpKeys.begin(), tmpKeys.end());
  349. // Check whether a specific metadata type is supported. A metadata type is considered
  350. // supported if and only if all required fields are supported.
  351. // SMPTE2086
  352. if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X) &&
  353. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y) &&
  354. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X) &&
  355. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y) &&
  356. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X) &&
  357. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y) &&
  358. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::WHITE_POINT_X) &&
  359. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::WHITE_POINT_Y) &&
  360. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_LUMINANCE) &&
  361. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MIN_LUMINANCE)) {
  362. supportedPerFrameMetadata |= HdrMetadata::Type::SMPTE2086;
  363. }
  364. // CTA861_3
  365. if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL) &&
  366. hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL)) {
  367. supportedPerFrameMetadata |= HdrMetadata::Type::CTA861_3;
  368. }
  369. // HDR10PLUS
  370. if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::HDR10_PLUS_SEI)) {
  371. supportedPerFrameMetadata |= HdrMetadata::Type::HDR10PLUS;
  372. }
  373. return supportedPerFrameMetadata;
  374. }
  375. Error Display::getRenderIntents(ColorMode colorMode,
  376. std::vector<RenderIntent>* outRenderIntents) const
  377. {
  378. auto intError = mComposer.getRenderIntents(mId, colorMode, outRenderIntents);
  379. return static_cast<Error>(intError);
  380. }
  381. Error Display::getDataspaceSaturationMatrix(Dataspace dataspace, android::mat4* outMatrix)
  382. {
  383. auto intError = mComposer.getDataspaceSaturationMatrix(dataspace, outMatrix);
  384. return static_cast<Error>(intError);
  385. }
  386. std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
  387. {
  388. std::vector<std::shared_ptr<const Config>> configs;
  389. for (const auto& element : mConfigs) {
  390. configs.emplace_back(element.second);
  391. }
  392. return configs;
  393. }
  394. Error Display::getName(std::string* outName) const
  395. {
  396. auto intError = mComposer.getDisplayName(mId, outName);
  397. return static_cast<Error>(intError);
  398. }
  399. Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
  400. std::unordered_map<HWC2::Layer*, LayerRequest>* outLayerRequests) {
  401. uint32_t intDisplayRequests;
  402. std::vector<Hwc2::Layer> layerIds;
  403. std::vector<uint32_t> layerRequests;
  404. auto intError = mComposer.getDisplayRequests(
  405. mId, &intDisplayRequests, &layerIds, &layerRequests);
  406. uint32_t numElements = layerIds.size();
  407. auto error = static_cast<Error>(intError);
  408. if (error != Error::None) {
  409. return error;
  410. }
  411. *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
  412. outLayerRequests->clear();
  413. outLayerRequests->reserve(numElements);
  414. for (uint32_t element = 0; element < numElements; ++element) {
  415. auto layer = getLayerById(layerIds[element]);
  416. if (layer) {
  417. auto layerRequest =
  418. static_cast<LayerRequest>(layerRequests[element]);
  419. outLayerRequests->emplace(layer, layerRequest);
  420. } else {
  421. ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
  422. PRIu64, layerIds[element], mId);
  423. }
  424. }
  425. return Error::None;
  426. }
  427. Error Display::getType(DisplayType* outType) const
  428. {
  429. *outType = mType;
  430. return Error::None;
  431. }
  432. Error Display::supportsDoze(bool* outSupport) const {
  433. *outSupport = mDisplayCapabilities.count(DisplayCapability::Doze) > 0;
  434. return Error::None;
  435. }
  436. Error Display::getHdrCapabilities(HdrCapabilities* outCapabilities) const
  437. {
  438. float maxLuminance = -1.0f;
  439. float maxAverageLuminance = -1.0f;
  440. float minLuminance = -1.0f;
  441. std::vector<Hwc2::Hdr> types;
  442. auto intError = mComposer.getHdrCapabilities(mId, &types,
  443. &maxLuminance, &maxAverageLuminance, &minLuminance);
  444. auto error = static_cast<HWC2::Error>(intError);
  445. if (error != Error::None) {
  446. return error;
  447. }
  448. *outCapabilities = HdrCapabilities(std::move(types),
  449. maxLuminance, maxAverageLuminance, minLuminance);
  450. return Error::None;
  451. }
  452. Error Display::getDisplayedContentSamplingAttributes(PixelFormat* outFormat,
  453. Dataspace* outDataspace,
  454. uint8_t* outComponentMask) const {
  455. auto intError = mComposer.getDisplayedContentSamplingAttributes(mId, outFormat, outDataspace,
  456. outComponentMask);
  457. return static_cast<Error>(intError);
  458. }
  459. Error Display::setDisplayContentSamplingEnabled(bool enabled, uint8_t componentMask,
  460. uint64_t maxFrames) const {
  461. auto intError =
  462. mComposer.setDisplayContentSamplingEnabled(mId, enabled, componentMask, maxFrames);
  463. return static_cast<Error>(intError);
  464. }
  465. Error Display::getDisplayedContentSample(uint64_t maxFrames, uint64_t timestamp,
  466. android::DisplayedFrameStats* outStats) const {
  467. auto intError = mComposer.getDisplayedContentSample(mId, maxFrames, timestamp, outStats);
  468. return static_cast<Error>(intError);
  469. }
  470. Error Display::getReleaseFences(std::unordered_map<HWC2::Layer*, sp<Fence>>* outFences) const {
  471. std::vector<Hwc2::Layer> layerIds;
  472. std::vector<int> fenceFds;
  473. auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
  474. auto error = static_cast<Error>(intError);
  475. uint32_t numElements = layerIds.size();
  476. if (error != Error::None) {
  477. return error;
  478. }
  479. std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
  480. releaseFences.reserve(numElements);
  481. for (uint32_t element = 0; element < numElements; ++element) {
  482. auto layer = getLayerById(layerIds[element]);
  483. if (layer) {
  484. sp<Fence> fence(new Fence(fenceFds[element]));
  485. releaseFences.emplace(layer, fence);
  486. } else {
  487. ALOGE("getReleaseFences: invalid layer %" PRIu64
  488. " found on display %" PRIu64, layerIds[element], mId);
  489. for (; element < numElements; ++element) {
  490. close(fenceFds[element]);
  491. }
  492. return Error::BadLayer;
  493. }
  494. }
  495. *outFences = std::move(releaseFences);
  496. return Error::None;
  497. }
  498. Error Display::present(sp<Fence>* outPresentFence)
  499. {
  500. int32_t presentFenceFd = -1;
  501. auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
  502. auto error = static_cast<Error>(intError);
  503. if (error != Error::None) {
  504. return error;
  505. }
  506. *outPresentFence = new Fence(presentFenceFd);
  507. return Error::None;
  508. }
  509. Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
  510. {
  511. if (config->getDisplayId() != mId) {
  512. ALOGE("setActiveConfig received config %u for the wrong display %"
  513. PRIu64 " (expected %" PRIu64 ")", config->getId(),
  514. config->getDisplayId(), mId);
  515. return Error::BadConfig;
  516. }
  517. auto intError = mComposer.setActiveConfig(mId, config->getId());
  518. return static_cast<Error>(intError);
  519. }
  520. Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
  521. const sp<Fence>& acquireFence, Dataspace dataspace)
  522. {
  523. // TODO: Properly encode client target surface damage
  524. int32_t fenceFd = acquireFence->dup();
  525. auto intError = mComposer.setClientTarget(mId, slot, target,
  526. fenceFd, dataspace, std::vector<Hwc2::IComposerClient::Rect>());
  527. return static_cast<Error>(intError);
  528. }
  529. Error Display::setColorMode(ColorMode mode, RenderIntent renderIntent)
  530. {
  531. auto intError = mComposer.setColorMode(mId, mode, renderIntent);
  532. return static_cast<Error>(intError);
  533. }
  534. Error Display::setColorTransform(const android::mat4& matrix,
  535. android_color_transform_t hint)
  536. {
  537. auto intError = mComposer.setColorTransform(mId,
  538. matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
  539. return static_cast<Error>(intError);
  540. }
  541. Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
  542. const sp<Fence>& releaseFence)
  543. {
  544. int32_t fenceFd = releaseFence->dup();
  545. auto handle = buffer->getNativeBuffer()->handle;
  546. auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
  547. close(fenceFd);
  548. return static_cast<Error>(intError);
  549. }
  550. Error Display::setPowerMode(PowerMode mode)
  551. {
  552. auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
  553. auto intError = mComposer.setPowerMode(mId, intMode);
  554. if (mode == PowerMode::On) {
  555. std::call_once(mDisplayCapabilityQueryFlag, [this]() {
  556. std::vector<Hwc2::DisplayCapability> tmpCapabilities;
  557. auto error =
  558. static_cast<Error>(mComposer.getDisplayCapabilities(mId, &tmpCapabilities));
  559. if (error == Error::None) {
  560. for (auto capability : tmpCapabilities) {
  561. mDisplayCapabilities.emplace(static_cast<DisplayCapability>(capability));
  562. }
  563. } else if (error == Error::Unsupported) {
  564. if (mCapabilities.count(Capability::SkipClientColorTransform)) {
  565. mDisplayCapabilities.emplace(DisplayCapability::SkipClientColorTransform);
  566. }
  567. bool dozeSupport = false;
  568. error = static_cast<Error>(mComposer.getDozeSupport(mId, &dozeSupport));
  569. if (error == Error::None && dozeSupport) {
  570. mDisplayCapabilities.emplace(DisplayCapability::Doze);
  571. }
  572. }
  573. });
  574. }
  575. return static_cast<Error>(intError);
  576. }
  577. Error Display::setVsyncEnabled(Vsync enabled)
  578. {
  579. auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
  580. auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
  581. return static_cast<Error>(intError);
  582. }
  583. Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
  584. {
  585. uint32_t numTypes = 0;
  586. uint32_t numRequests = 0;
  587. auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
  588. auto error = static_cast<Error>(intError);
  589. if (error != Error::None && error != Error::HasChanges) {
  590. return error;
  591. }
  592. *outNumTypes = numTypes;
  593. *outNumRequests = numRequests;
  594. return error;
  595. }
  596. Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
  597. sp<android::Fence>* outPresentFence, uint32_t* state) {
  598. uint32_t numTypes = 0;
  599. uint32_t numRequests = 0;
  600. int32_t presentFenceFd = -1;
  601. auto intError = mComposer.presentOrValidateDisplay(
  602. mId, &numTypes, &numRequests, &presentFenceFd, state);
  603. auto error = static_cast<Error>(intError);
  604. if (error != Error::None && error != Error::HasChanges) {
  605. return error;
  606. }
  607. if (*state == 1) {
  608. *outPresentFence = new Fence(presentFenceFd);
  609. }
  610. if (*state == 0) {
  611. *outNumTypes = numTypes;
  612. *outNumRequests = numRequests;
  613. }
  614. return error;
  615. }
  616. Error Display::setDisplayBrightness(float brightness) const {
  617. auto intError = mComposer.setDisplayBrightness(mId, brightness);
  618. return static_cast<Error>(intError);
  619. }
  620. // For use by Device
  621. void Display::setConnected(bool connected) {
  622. if (!mIsConnected && connected) {
  623. mComposer.setClientTargetSlotCount(mId);
  624. if (mType == DisplayType::Physical) {
  625. loadConfigs();
  626. }
  627. }
  628. mIsConnected = connected;
  629. }
  630. int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
  631. {
  632. int32_t value = 0;
  633. auto intError = mComposer.getDisplayAttribute(mId, configId,
  634. static_cast<Hwc2::IComposerClient::Attribute>(attribute),
  635. &value);
  636. auto error = static_cast<Error>(intError);
  637. if (error != Error::None) {
  638. ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
  639. configId, to_string(attribute).c_str(),
  640. to_string(error).c_str(), intError);
  641. return -1;
  642. }
  643. return value;
  644. }
  645. void Display::loadConfig(hwc2_config_t configId)
  646. {
  647. ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
  648. auto config = Config::Builder(*this, configId)
  649. .setWidth(getAttribute(configId, Attribute::Width))
  650. .setHeight(getAttribute(configId, Attribute::Height))
  651. .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
  652. .setDpiX(getAttribute(configId, Attribute::DpiX))
  653. .setDpiY(getAttribute(configId, Attribute::DpiY))
  654. .build();
  655. mConfigs.emplace(configId, std::move(config));
  656. }
  657. void Display::loadConfigs()
  658. {
  659. ALOGV("[%" PRIu64 "] loadConfigs", mId);
  660. std::vector<Hwc2::Config> configIds;
  661. auto intError = mComposer.getDisplayConfigs(mId, &configIds);
  662. auto error = static_cast<Error>(intError);
  663. if (error != Error::None) {
  664. ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
  665. to_string(error).c_str(), intError);
  666. return;
  667. }
  668. for (auto configId : configIds) {
  669. loadConfig(configId);
  670. }
  671. }
  672. // Other Display methods
  673. HWC2::Layer* Display::getLayerById(hwc2_layer_t id) const {
  674. if (mLayers.count(id) == 0) {
  675. return nullptr;
  676. }
  677. return mLayers.at(id).get();
  678. }
  679. } // namespace impl
  680. // Layer methods
  681. Layer::~Layer() = default;
  682. namespace impl {
  683. Layer::Layer(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities,
  684. hwc2_display_t displayId, hwc2_layer_t layerId)
  685. : mComposer(composer),
  686. mCapabilities(capabilities),
  687. mDisplayId(displayId),
  688. mId(layerId),
  689. mColorMatrix(android::mat4())
  690. {
  691. ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
  692. }
  693. Layer::~Layer()
  694. {
  695. auto intError = mComposer.destroyLayer(mDisplayId, mId);
  696. auto error = static_cast<Error>(intError);
  697. ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
  698. " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
  699. intError);
  700. }
  701. Error Layer::setCursorPosition(int32_t x, int32_t y)
  702. {
  703. auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
  704. return static_cast<Error>(intError);
  705. }
  706. Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
  707. const sp<Fence>& acquireFence)
  708. {
  709. if (buffer == nullptr && mBufferSlot == slot) {
  710. return Error::None;
  711. }
  712. mBufferSlot = slot;
  713. int32_t fenceFd = acquireFence->dup();
  714. auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
  715. fenceFd);
  716. return static_cast<Error>(intError);
  717. }
  718. Error Layer::setSurfaceDamage(const Region& damage)
  719. {
  720. if (damage.isRect() && mDamageRegion.isRect() &&
  721. (damage.getBounds() == mDamageRegion.getBounds())) {
  722. return Error::None;
  723. }
  724. mDamageRegion = damage;
  725. // We encode default full-screen damage as INVALID_RECT upstream, but as 0
  726. // rects for HWC
  727. Hwc2::Error intError = Hwc2::Error::NONE;
  728. if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
  729. intError = mComposer.setLayerSurfaceDamage(mDisplayId,
  730. mId, std::vector<Hwc2::IComposerClient::Rect>());
  731. } else {
  732. size_t rectCount = 0;
  733. auto rectArray = damage.getArray(&rectCount);
  734. std::vector<Hwc2::IComposerClient::Rect> hwcRects;
  735. for (size_t rect = 0; rect < rectCount; ++rect) {
  736. hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
  737. rectArray[rect].right, rectArray[rect].bottom});
  738. }
  739. intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
  740. }
  741. return static_cast<Error>(intError);
  742. }
  743. Error Layer::setBlendMode(BlendMode mode)
  744. {
  745. auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
  746. auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
  747. return static_cast<Error>(intError);
  748. }
  749. Error Layer::setColor(hwc_color_t color)
  750. {
  751. Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
  752. auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
  753. return static_cast<Error>(intError);
  754. }
  755. Error Layer::setCompositionType(Composition type)
  756. {
  757. auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
  758. auto intError = mComposer.setLayerCompositionType(
  759. mDisplayId, mId, intType);
  760. return static_cast<Error>(intError);
  761. }
  762. Error Layer::setDataspace(Dataspace dataspace)
  763. {
  764. if (dataspace == mDataSpace) {
  765. return Error::None;
  766. }
  767. mDataSpace = dataspace;
  768. auto intError = mComposer.setLayerDataspace(mDisplayId, mId, mDataSpace);
  769. return static_cast<Error>(intError);
  770. }
  771. Error Layer::setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
  772. const android::HdrMetadata& metadata)
  773. {
  774. if (metadata == mHdrMetadata) {
  775. return Error::None;
  776. }
  777. mHdrMetadata = metadata;
  778. int validTypes = mHdrMetadata.validTypes & supportedPerFrameMetadata;
  779. std::vector<Hwc2::PerFrameMetadata> perFrameMetadatas;
  780. if (validTypes & HdrMetadata::SMPTE2086) {
  781. perFrameMetadatas.insert(perFrameMetadatas.end(),
  782. {{Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
  783. mHdrMetadata.smpte2086.displayPrimaryRed.x},
  784. {Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
  785. mHdrMetadata.smpte2086.displayPrimaryRed.y},
  786. {Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
  787. mHdrMetadata.smpte2086.displayPrimaryGreen.x},
  788. {Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
  789. mHdrMetadata.smpte2086.displayPrimaryGreen.y},
  790. {Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
  791. mHdrMetadata.smpte2086.displayPrimaryBlue.x},
  792. {Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
  793. mHdrMetadata.smpte2086.displayPrimaryBlue.y},
  794. {Hwc2::PerFrameMetadataKey::WHITE_POINT_X,
  795. mHdrMetadata.smpte2086.whitePoint.x},
  796. {Hwc2::PerFrameMetadataKey::WHITE_POINT_Y,
  797. mHdrMetadata.smpte2086.whitePoint.y},
  798. {Hwc2::PerFrameMetadataKey::MAX_LUMINANCE,
  799. mHdrMetadata.smpte2086.maxLuminance},
  800. {Hwc2::PerFrameMetadataKey::MIN_LUMINANCE,
  801. mHdrMetadata.smpte2086.minLuminance}});
  802. }
  803. if (validTypes & HdrMetadata::CTA861_3) {
  804. perFrameMetadatas.insert(perFrameMetadatas.end(),
  805. {{Hwc2::PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
  806. mHdrMetadata.cta8613.maxContentLightLevel},
  807. {Hwc2::PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
  808. mHdrMetadata.cta8613.maxFrameAverageLightLevel}});
  809. }
  810. Error error = static_cast<Error>(
  811. mComposer.setLayerPerFrameMetadata(mDisplayId, mId, perFrameMetadatas));
  812. if (validTypes & HdrMetadata::HDR10PLUS) {
  813. std::vector<Hwc2::PerFrameMetadataBlob> perFrameMetadataBlobs;
  814. perFrameMetadataBlobs.push_back(
  815. {Hwc2::PerFrameMetadataKey::HDR10_PLUS_SEI, mHdrMetadata.hdr10plus});
  816. Error setMetadataBlobsError = static_cast<Error>(
  817. mComposer.setLayerPerFrameMetadataBlobs(mDisplayId, mId, perFrameMetadataBlobs));
  818. if (error == Error::None) {
  819. return setMetadataBlobsError;
  820. }
  821. }
  822. return error;
  823. }
  824. Error Layer::setDisplayFrame(const Rect& frame)
  825. {
  826. Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
  827. frame.right, frame.bottom};
  828. auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
  829. return static_cast<Error>(intError);
  830. }
  831. Error Layer::setPlaneAlpha(float alpha)
  832. {
  833. auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
  834. return static_cast<Error>(intError);
  835. }
  836. Error Layer::setSidebandStream(const native_handle_t* stream)
  837. {
  838. if (mCapabilities.count(Capability::SidebandStream) == 0) {
  839. ALOGE("Attempted to call setSidebandStream without checking that the "
  840. "device supports sideband streams");
  841. return Error::Unsupported;
  842. }
  843. auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
  844. return static_cast<Error>(intError);
  845. }
  846. Error Layer::setSourceCrop(const FloatRect& crop)
  847. {
  848. Hwc2::IComposerClient::FRect hwcRect{
  849. crop.left, crop.top, crop.right, crop.bottom};
  850. auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
  851. return static_cast<Error>(intError);
  852. }
  853. Error Layer::setTransform(Transform transform)
  854. {
  855. auto intTransform = static_cast<Hwc2::Transform>(transform);
  856. auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
  857. return static_cast<Error>(intError);
  858. }
  859. Error Layer::setVisibleRegion(const Region& region)
  860. {
  861. if (region.isRect() && mVisibleRegion.isRect() &&
  862. (region.getBounds() == mVisibleRegion.getBounds())) {
  863. return Error::None;
  864. }
  865. mVisibleRegion = region;
  866. size_t rectCount = 0;
  867. auto rectArray = region.getArray(&rectCount);
  868. std::vector<Hwc2::IComposerClient::Rect> hwcRects;
  869. for (size_t rect = 0; rect < rectCount; ++rect) {
  870. hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
  871. rectArray[rect].right, rectArray[rect].bottom});
  872. }
  873. auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
  874. return static_cast<Error>(intError);
  875. }
  876. Error Layer::setZOrder(uint32_t z)
  877. {
  878. auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
  879. return static_cast<Error>(intError);
  880. }
  881. Error Layer::setInfo(uint32_t type, uint32_t appId)
  882. {
  883. auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
  884. return static_cast<Error>(intError);
  885. }
  886. // Composer HAL 2.3
  887. Error Layer::setColorTransform(const android::mat4& matrix) {
  888. if (matrix == mColorMatrix) {
  889. return Error::None;
  890. }
  891. auto intError = mComposer.setLayerColorTransform(mDisplayId, mId, matrix.asArray());
  892. Error error = static_cast<Error>(intError);
  893. if (error != Error::None) {
  894. return error;
  895. }
  896. mColorMatrix = matrix;
  897. return error;
  898. }
  899. } // namespace impl
  900. } // namespace HWC2