HWComposer.cpp revision 5576a555a14edd8c76addce2cee37b9b9ced2c3f
1d38e174ddbde8e7419ba01deaded4ff0ead1caaaRay Essick/* 23938dc6363949ef126bb6188d26b5bef6db8be4dRay Essick * Copyright (C) 2010 The Android Open Source Project 33938dc6363949ef126bb6188d26b5bef6db8be4dRay Essick * 4a61dff07b71b6999f30c2a15268d8a4d87d4ce48Ray Essick * Licensed under the Apache License, Version 2.0 (the "License"); 53938dc6363949ef126bb6188d26b5bef6db8be4dRay Essick * you may not use this file except in compliance with the License. 63938dc6363949ef126bb6188d26b5bef6db8be4dRay Essick * 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 17// #define LOG_NDEBUG 0 18 19#undef LOG_TAG 20#define LOG_TAG "HWComposer" 21#define ATRACE_TAG ATRACE_TAG_GRAPHICS 22 23#include <inttypes.h> 24#include <math.h> 25#include <stdint.h> 26#include <stdio.h> 27#include <stdlib.h> 28#include <string.h> 29#include <sys/types.h> 30 31#include <utils/Errors.h> 32#include <utils/misc.h> 33#include <utils/NativeHandle.h> 34#include <utils/String8.h> 35#include <utils/Thread.h> 36#include <utils/Trace.h> 37#include <utils/Vector.h> 38 39#include <ui/GraphicBuffer.h> 40 41#include <hardware/hardware.h> 42#include <hardware/hwcomposer.h> 43 44#include <android/configuration.h> 45 46#include <cutils/properties.h> 47#include <log/log.h> 48 49#include "HWComposer.h" 50#include "hwc2on1adapter/HWC2On1Adapter.h" 51#include "HWC2.h" 52#include "ComposerHal.h" 53 54#include "../Layer.h" // needed only for debugging 55#include "../SurfaceFlinger.h" 56 57namespace android { 58 59#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION 60 61// --------------------------------------------------------------------------- 62 63HWComposer::HWComposer(bool useVrComposer) 64 : mAdapter(), 65 mHwcDevice(), 66 mDisplayData(2), 67 mFreeDisplaySlots(), 68 mHwcDisplaySlots(), 69 mCBContext(), 70 mEventHandler(nullptr), 71 mVSyncCounts(), 72 mRemainingHwcVirtualDisplays(0), 73 mDumpMayLockUp(false) 74{ 75 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) { 76 mLastHwVSync[i] = 0; 77 mVSyncCounts[i] = 0; 78 } 79 80 loadHwcModule(useVrComposer); 81} 82 83HWComposer::~HWComposer() {} 84 85void HWComposer::setEventHandler(EventHandler* handler) 86{ 87 if (handler == nullptr) { 88 ALOGE("setEventHandler: Rejected attempt to clear handler"); 89 return; 90 } 91 92 bool wasNull = (mEventHandler == nullptr); 93 mEventHandler = handler; 94 95 if (wasNull) { 96 auto hotplugHook = std::bind(&HWComposer::hotplug, this, 97 std::placeholders::_1, std::placeholders::_2); 98 mHwcDevice->registerHotplugCallback(hotplugHook); 99 auto invalidateHook = std::bind(&HWComposer::invalidate, this, 100 std::placeholders::_1); 101 mHwcDevice->registerRefreshCallback(invalidateHook); 102 auto vsyncHook = std::bind(&HWComposer::vsync, this, 103 std::placeholders::_1, std::placeholders::_2); 104 mHwcDevice->registerVsyncCallback(vsyncHook); 105 } 106} 107 108// Load and prepare the hardware composer module. Sets mHwc. 109void HWComposer::loadHwcModule(bool useVrComposer) 110{ 111 ALOGV("loadHwcModule"); 112 113#ifdef BYPASS_IHWC 114 (void)useVrComposer; // Silence unused parameter warning. 115 116 hw_module_t const* module; 117 118 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) { 119 ALOGE("%s module not found, aborting", HWC_HARDWARE_MODULE_ID); 120 abort(); 121 } 122 123 hw_device_t* device = nullptr; 124 int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device); 125 if (error != 0) { 126 ALOGE("Failed to open HWC device (%s), aborting", strerror(-error)); 127 abort(); 128 } 129 130 uint32_t majorVersion = (device->version >> 24) & 0xF; 131 if (majorVersion == 2) { 132 mHwcDevice = std::make_unique<HWC2::Device>( 133 reinterpret_cast<hwc2_device_t*>(device)); 134 } else { 135 mAdapter = std::make_unique<HWC2On1Adapter>( 136 reinterpret_cast<hwc_composer_device_1_t*>(device)); 137 uint8_t minorVersion = mAdapter->getHwc1MinorVersion(); 138 if (minorVersion < 1) { 139 ALOGE("Cannot adapt to HWC version %d.%d", 140 static_cast<int32_t>((minorVersion >> 8) & 0xF), 141 static_cast<int32_t>(minorVersion & 0xF)); 142 abort(); 143 } 144 mHwcDevice = std::make_unique<HWC2::Device>( 145 static_cast<hwc2_device_t*>(mAdapter.get())); 146 } 147#else 148 mHwcDevice = std::make_unique<HWC2::Device>(useVrComposer); 149#endif 150 151 mRemainingHwcVirtualDisplays = mHwcDevice->getMaxVirtualDisplayCount(); 152} 153 154bool HWComposer::hasCapability(HWC2::Capability capability) const 155{ 156 return mHwcDevice->getCapabilities().count(capability) > 0; 157} 158 159bool HWComposer::isValidDisplay(int32_t displayId) const { 160 return static_cast<size_t>(displayId) < mDisplayData.size() && 161 mDisplayData[displayId].hwcDisplay; 162} 163 164void HWComposer::validateChange(HWC2::Composition from, HWC2::Composition to) { 165 bool valid = true; 166 switch (from) { 167 case HWC2::Composition::Client: 168 valid = false; 169 break; 170 case HWC2::Composition::Device: 171 case HWC2::Composition::SolidColor: 172 valid = (to == HWC2::Composition::Client); 173 break; 174 case HWC2::Composition::Cursor: 175 case HWC2::Composition::Sideband: 176 valid = (to == HWC2::Composition::Client || 177 to == HWC2::Composition::Device); 178 break; 179 default: 180 break; 181 } 182 183 if (!valid) { 184 ALOGE("Invalid layer type change: %s --> %s", to_string(from).c_str(), 185 to_string(to).c_str()); 186 } 187} 188 189void HWComposer::hotplug(const std::shared_ptr<HWC2::Display>& display, 190 HWC2::Connection connected) { 191 ALOGV("hotplug: %" PRIu64 ", %s", display->getId(), 192 to_string(connected).c_str()); 193 int32_t disp = 0; 194 if (!mDisplayData[0].hwcDisplay) { 195 ALOGE_IF(connected != HWC2::Connection::Connected, "Assumed primary" 196 " display would be connected"); 197 mDisplayData[0].hwcDisplay = display; 198 mHwcDisplaySlots[display->getId()] = 0; 199 disp = DisplayDevice::DISPLAY_PRIMARY; 200 } else { 201 // Disconnect is handled through HWComposer::disconnectDisplay via 202 // SurfaceFlinger's onHotplugReceived callback handling 203 if (connected == HWC2::Connection::Connected) { 204 mDisplayData[1].hwcDisplay = display; 205 mHwcDisplaySlots[display->getId()] = 1; 206 } 207 disp = DisplayDevice::DISPLAY_EXTERNAL; 208 } 209 mEventHandler->onHotplugReceived(this, disp, 210 connected == HWC2::Connection::Connected); 211} 212 213void HWComposer::invalidate(const std::shared_ptr<HWC2::Display>& /*display*/) { 214 mEventHandler->onInvalidateReceived(this); 215} 216 217void HWComposer::vsync(const std::shared_ptr<HWC2::Display>& display, 218 int64_t timestamp) { 219 auto displayType = HWC2::DisplayType::Invalid; 220 auto error = display->getType(&displayType); 221 if (error != HWC2::Error::None) { 222 ALOGE("vsync: Failed to determine type of display %" PRIu64, 223 display->getId()); 224 return; 225 } 226 227 if (displayType == HWC2::DisplayType::Virtual) { 228 ALOGE("Virtual display %" PRIu64 " passed to vsync callback", 229 display->getId()); 230 return; 231 } 232 233 if (mHwcDisplaySlots.count(display->getId()) == 0) { 234 ALOGE("Unknown physical display %" PRIu64 " passed to vsync callback", 235 display->getId()); 236 return; 237 } 238 239 int32_t disp = mHwcDisplaySlots[display->getId()]; 240 { 241 Mutex::Autolock _l(mLock); 242 243 // There have been reports of HWCs that signal several vsync events 244 // with the same timestamp when turning the display off and on. This 245 // is a bug in the HWC implementation, but filter the extra events 246 // out here so they don't cause havoc downstream. 247 if (timestamp == mLastHwVSync[disp]) { 248 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%" PRId64 ")", 249 timestamp); 250 return; 251 } 252 253 mLastHwVSync[disp] = timestamp; 254 } 255 256 char tag[16]; 257 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp); 258 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1); 259 260 mEventHandler->onVSyncReceived(this, disp, timestamp); 261} 262 263status_t HWComposer::allocateVirtualDisplay(uint32_t width, uint32_t height, 264 android_pixel_format_t* format, int32_t *outId) { 265 if (mRemainingHwcVirtualDisplays == 0) { 266 ALOGE("allocateVirtualDisplay: No remaining virtual displays"); 267 return NO_MEMORY; 268 } 269 270 if (SurfaceFlinger::maxVirtualDisplaySize != 0 && 271 (width > SurfaceFlinger::maxVirtualDisplaySize || 272 height > SurfaceFlinger::maxVirtualDisplaySize)) { 273 ALOGE("createVirtualDisplay: Can't create a virtual display with" 274 " a dimension > %" PRIu64 " (tried %u x %u)", 275 SurfaceFlinger::maxVirtualDisplaySize, width, height); 276 return INVALID_OPERATION; 277 } 278 279 std::shared_ptr<HWC2::Display> display; 280 auto error = mHwcDevice->createVirtualDisplay(width, height, format, 281 &display); 282 if (error != HWC2::Error::None) { 283 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display"); 284 return NO_MEMORY; 285 } 286 287 size_t displaySlot = 0; 288 if (!mFreeDisplaySlots.empty()) { 289 displaySlot = *mFreeDisplaySlots.begin(); 290 mFreeDisplaySlots.erase(displaySlot); 291 } else if (mDisplayData.size() < INT32_MAX) { 292 // Don't bother allocating a slot larger than we can return 293 displaySlot = mDisplayData.size(); 294 mDisplayData.resize(displaySlot + 1); 295 } else { 296 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot"); 297 return NO_MEMORY; 298 } 299 300 mDisplayData[displaySlot].hwcDisplay = display; 301 302 --mRemainingHwcVirtualDisplays; 303 *outId = static_cast<int32_t>(displaySlot); 304 305 return NO_ERROR; 306} 307 308std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) { 309 if (!isValidDisplay(displayId)) { 310 ALOGE("Failed to create layer on invalid display %d", displayId); 311 return nullptr; 312 } 313 auto display = mDisplayData[displayId].hwcDisplay; 314 std::shared_ptr<HWC2::Layer> layer; 315 auto error = display->createLayer(&layer); 316 if (error != HWC2::Error::None) { 317 ALOGE("Failed to create layer on display %d: %s (%d)", displayId, 318 to_string(error).c_str(), static_cast<int32_t>(error)); 319 return nullptr; 320 } 321 return layer; 322} 323 324nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const { 325 // this returns the last refresh timestamp. 326 // if the last one is not available, we estimate it based on 327 // the refresh period and whatever closest timestamp we have. 328 Mutex::Autolock _l(mLock); 329 nsecs_t now = systemTime(CLOCK_MONOTONIC); 330 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod(); 331 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod); 332} 333 334bool HWComposer::isConnected(int32_t displayId) const { 335 if (!isValidDisplay(displayId)) { 336 ALOGE("isConnected: Attempted to access invalid display %d", displayId); 337 return false; 338 } 339 return mDisplayData[displayId].hwcDisplay->isConnected(); 340} 341 342std::vector<std::shared_ptr<const HWC2::Display::Config>> 343 HWComposer::getConfigs(int32_t displayId) const { 344 if (!isValidDisplay(displayId)) { 345 ALOGE("getConfigs: Attempted to access invalid display %d", displayId); 346 return {}; 347 } 348 auto& displayData = mDisplayData[displayId]; 349 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs(); 350 if (displayData.configMap.empty()) { 351 for (size_t i = 0; i < configs.size(); ++i) { 352 displayData.configMap[i] = configs[i]; 353 } 354 } 355 return configs; 356} 357 358std::shared_ptr<const HWC2::Display::Config> 359 HWComposer::getActiveConfig(int32_t displayId) const { 360 if (!isValidDisplay(displayId)) { 361 ALOGV("getActiveConfigs: Attempted to access invalid display %d", 362 displayId); 363 return nullptr; 364 } 365 std::shared_ptr<const HWC2::Display::Config> config; 366 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config); 367 if (error == HWC2::Error::BadConfig) { 368 ALOGE("getActiveConfig: No config active, returning null"); 369 return nullptr; 370 } else if (error != HWC2::Error::None) { 371 ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId, 372 to_string(error).c_str(), static_cast<int32_t>(error)); 373 return nullptr; 374 } else if (!config) { 375 ALOGE("getActiveConfig returned an unknown config for display %d", 376 displayId); 377 return nullptr; 378 } 379 380 return config; 381} 382 383std::vector<android_color_mode_t> HWComposer::getColorModes(int32_t displayId) const { 384 std::vector<android_color_mode_t> modes; 385 386 if (!isValidDisplay(displayId)) { 387 ALOGE("getColorModes: Attempted to access invalid display %d", 388 displayId); 389 return modes; 390 } 391 const std::shared_ptr<HWC2::Display>& hwcDisplay = 392 mDisplayData[displayId].hwcDisplay; 393 394 auto error = hwcDisplay->getColorModes(&modes); 395 if (error != HWC2::Error::None) { 396 ALOGE("getColorModes failed for display %d: %s (%d)", displayId, 397 to_string(error).c_str(), static_cast<int32_t>(error)); 398 return std::vector<android_color_mode_t>(); 399 } 400 401 return modes; 402} 403 404status_t HWComposer::setActiveColorMode(int32_t displayId, android_color_mode_t mode) { 405 if (!isValidDisplay(displayId)) { 406 ALOGE("setActiveColorMode: Display %d is not valid", displayId); 407 return BAD_INDEX; 408 } 409 410 auto& displayData = mDisplayData[displayId]; 411 auto error = displayData.hwcDisplay->setColorMode(mode); 412 if (error != HWC2::Error::None) { 413 ALOGE("setActiveConfig: Failed to set color mode %d on display %d: " 414 "%s (%d)", mode, displayId, to_string(error).c_str(), 415 static_cast<int32_t>(error)); 416 return UNKNOWN_ERROR; 417 } 418 419 return NO_ERROR; 420} 421 422 423void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) { 424 if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) { 425 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId); 426 return; 427 } 428 429 if (!isValidDisplay(displayId)) { 430 ALOGE("setVsyncEnabled: Attempted to access invalid display %d", 431 displayId); 432 return; 433 } 434 435 // NOTE: we use our own internal lock here because we have to call 436 // into the HWC with the lock held, and we want to make sure 437 // that even if HWC blocks (which it shouldn't), it won't 438 // affect other threads. 439 Mutex::Autolock _l(mVsyncLock); 440 auto& displayData = mDisplayData[displayId]; 441 if (enabled != displayData.vsyncEnabled) { 442 ATRACE_CALL(); 443 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled); 444 if (error == HWC2::Error::None) { 445 displayData.vsyncEnabled = enabled; 446 447 char tag[16]; 448 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId); 449 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0); 450 } else { 451 ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64 452 ": %s (%d)", to_string(enabled).c_str(), displayId, 453 mDisplayData[displayId].hwcDisplay->getId(), 454 to_string(error).c_str(), static_cast<int32_t>(error)); 455 } 456 } 457} 458 459status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot, 460 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target, 461 android_dataspace_t dataspace) { 462 if (!isValidDisplay(displayId)) { 463 return BAD_INDEX; 464 } 465 466 ALOGV("setClientTarget for display %d", displayId); 467 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 468 buffer_handle_t handle = nullptr; 469 if ((target != nullptr) && target->getNativeBuffer()) { 470 handle = target->getNativeBuffer()->handle; 471 } 472 auto error = hwcDisplay->setClientTarget(slot, handle, 473 acquireFence, dataspace); 474 if (error != HWC2::Error::None) { 475 ALOGE("Failed to set client target for display %d: %s (%d)", displayId, 476 to_string(error).c_str(), static_cast<int32_t>(error)); 477 return BAD_VALUE; 478 } 479 480 return NO_ERROR; 481} 482 483status_t HWComposer::prepare(DisplayDevice& displayDevice) { 484 ATRACE_CALL(); 485 486 Mutex::Autolock _l(mDisplayLock); 487 auto displayId = displayDevice.getHwcDisplayId(); 488 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 489 ALOGV("Skipping HWComposer prepare for non-HWC display"); 490 return NO_ERROR; 491 } 492 if (!isValidDisplay(displayId)) { 493 return BAD_INDEX; 494 } 495 496 auto& displayData = mDisplayData[displayId]; 497 auto& hwcDisplay = displayData.hwcDisplay; 498 if (!hwcDisplay->isConnected()) { 499 return NO_ERROR; 500 } 501 502 mDumpMayLockUp = true; 503 504 uint32_t numTypes = 0; 505 uint32_t numRequests = 0; 506 auto error = hwcDisplay->validate(&numTypes, &numRequests); 507 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) { 508 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId, 509 to_string(error).c_str(), static_cast<int32_t>(error)); 510 return BAD_INDEX; 511 } 512 513 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition> 514 changedTypes; 515 changedTypes.reserve(numTypes); 516 error = hwcDisplay->getChangedCompositionTypes(&changedTypes); 517 if (error != HWC2::Error::None) { 518 ALOGE("prepare: getChangedCompositionTypes failed on display %d: " 519 "%s (%d)", displayId, to_string(error).c_str(), 520 static_cast<int32_t>(error)); 521 return BAD_INDEX; 522 } 523 524 525 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0); 526 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest> 527 layerRequests; 528 layerRequests.reserve(numRequests); 529 error = hwcDisplay->getRequests(&displayData.displayRequests, 530 &layerRequests); 531 if (error != HWC2::Error::None) { 532 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId, 533 to_string(error).c_str(), static_cast<int32_t>(error)); 534 return BAD_INDEX; 535 } 536 537 displayData.hasClientComposition = false; 538 displayData.hasDeviceComposition = false; 539 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) { 540 auto hwcLayer = layer->getHwcLayer(displayId); 541 542 if (changedTypes.count(hwcLayer) != 0) { 543 // We pass false so we only update our state and don't call back 544 // into the HWC device 545 validateChange(layer->getCompositionType(displayId), 546 changedTypes[hwcLayer]); 547 layer->setCompositionType(displayId, changedTypes[hwcLayer], false); 548 } 549 550 switch (layer->getCompositionType(displayId)) { 551 case HWC2::Composition::Client: 552 displayData.hasClientComposition = true; 553 break; 554 case HWC2::Composition::Device: 555 case HWC2::Composition::SolidColor: 556 case HWC2::Composition::Cursor: 557 case HWC2::Composition::Sideband: 558 displayData.hasDeviceComposition = true; 559 break; 560 default: 561 break; 562 } 563 564 if (layerRequests.count(hwcLayer) != 0 && 565 layerRequests[hwcLayer] == 566 HWC2::LayerRequest::ClearClientTarget) { 567 layer->setClearClientTarget(displayId, true); 568 } else { 569 if (layerRequests.count(hwcLayer) != 0) { 570 ALOGE("prepare: Unknown layer request: %s", 571 to_string(layerRequests[hwcLayer]).c_str()); 572 } 573 layer->setClearClientTarget(displayId, false); 574 } 575 } 576 577 error = hwcDisplay->acceptChanges(); 578 if (error != HWC2::Error::None) { 579 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str()); 580 return BAD_INDEX; 581 } 582 583 return NO_ERROR; 584} 585 586bool HWComposer::hasDeviceComposition(int32_t displayId) const { 587 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 588 // Displays without a corresponding HWC display are never composed by 589 // the device 590 return false; 591 } 592 if (!isValidDisplay(displayId)) { 593 ALOGE("hasDeviceComposition: Invalid display %d", displayId); 594 return false; 595 } 596 return mDisplayData[displayId].hasDeviceComposition; 597} 598 599bool HWComposer::hasClientComposition(int32_t displayId) const { 600 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 601 // Displays without a corresponding HWC display are always composed by 602 // the client 603 return true; 604 } 605 if (!isValidDisplay(displayId)) { 606 ALOGE("hasClientComposition: Invalid display %d", displayId); 607 return true; 608 } 609 return mDisplayData[displayId].hasClientComposition; 610} 611 612sp<Fence> HWComposer::getPresentFence(int32_t displayId) const { 613 if (!isValidDisplay(displayId)) { 614 ALOGE("getPresentFence failed for invalid display %d", displayId); 615 return Fence::NO_FENCE; 616 } 617 return mDisplayData[displayId].lastPresentFence; 618} 619 620sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId, 621 const std::shared_ptr<HWC2::Layer>& layer) const { 622 if (!isValidDisplay(displayId)) { 623 ALOGE("getLayerReleaseFence: Invalid display"); 624 return Fence::NO_FENCE; 625 } 626 auto displayFences = mDisplayData[displayId].releaseFences; 627 if (displayFences.count(layer) == 0) { 628 ALOGV("getLayerReleaseFence: Release fence not found"); 629 return Fence::NO_FENCE; 630 } 631 return displayFences[layer]; 632} 633 634status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) { 635 ATRACE_CALL(); 636 637 if (!isValidDisplay(displayId)) { 638 return BAD_INDEX; 639 } 640 641 auto& displayData = mDisplayData[displayId]; 642 auto& hwcDisplay = displayData.hwcDisplay; 643 auto error = hwcDisplay->present(&displayData.lastPresentFence); 644 645 mDumpMayLockUp = false; 646 647 if (error != HWC2::Error::None) { 648 ALOGE("presentAndGetReleaseFences: failed for display %d: %s (%d)", 649 displayId, to_string(error).c_str(), static_cast<int32_t>(error)); 650 return UNKNOWN_ERROR; 651 } 652 653 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences; 654 error = hwcDisplay->getReleaseFences(&releaseFences); 655 if (error != HWC2::Error::None) { 656 ALOGE("presentAndGetReleaseFences: Failed to get release fences " 657 "for display %d: %s (%d)", 658 displayId, to_string(error).c_str(), 659 static_cast<int32_t>(error)); 660 return UNKNOWN_ERROR; 661 } 662 663 displayData.releaseFences = std::move(releaseFences); 664 665 return NO_ERROR; 666} 667 668status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) { 669 ALOGV("setPowerMode(%d, %d)", displayId, intMode); 670 if (!isValidDisplay(displayId)) { 671 ALOGE("setPowerMode: Bad display"); 672 return BAD_INDEX; 673 } 674 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) { 675 ALOGE("setPowerMode: Virtual display %d passed in, returning", 676 displayId); 677 return BAD_INDEX; 678 } 679 680 auto mode = static_cast<HWC2::PowerMode>(intMode); 681 if (mode == HWC2::PowerMode::Off) { 682 setVsyncEnabled(displayId, HWC2::Vsync::Disable); 683 } 684 685 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 686 switch (mode) { 687 case HWC2::PowerMode::Off: 688 case HWC2::PowerMode::On: 689 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); 690 { 691 auto error = hwcDisplay->setPowerMode(mode); 692 if (error != HWC2::Error::None) { 693 ALOGE("setPowerMode: Unable to set power mode %s for " 694 "display %d: %s (%d)", to_string(mode).c_str(), 695 displayId, to_string(error).c_str(), 696 static_cast<int32_t>(error)); 697 } 698 } 699 break; 700 case HWC2::PowerMode::Doze: 701 case HWC2::PowerMode::DozeSuspend: 702 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); 703 { 704 bool supportsDoze = false; 705 auto error = hwcDisplay->supportsDoze(&supportsDoze); 706 if (error != HWC2::Error::None) { 707 ALOGE("setPowerMode: Unable to query doze support for " 708 "display %d: %s (%d)", displayId, 709 to_string(error).c_str(), 710 static_cast<int32_t>(error)); 711 } 712 if (!supportsDoze) { 713 mode = HWC2::PowerMode::On; 714 } 715 716 error = hwcDisplay->setPowerMode(mode); 717 if (error != HWC2::Error::None) { 718 ALOGE("setPowerMode: Unable to set power mode %s for " 719 "display %d: %s (%d)", to_string(mode).c_str(), 720 displayId, to_string(error).c_str(), 721 static_cast<int32_t>(error)); 722 } 723 } 724 break; 725 default: 726 ALOGV("setPowerMode: Not calling HWC"); 727 break; 728 } 729 730 return NO_ERROR; 731} 732 733status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) { 734 if (!isValidDisplay(displayId)) { 735 ALOGE("setActiveConfig: Display %d is not valid", displayId); 736 return BAD_INDEX; 737 } 738 739 auto& displayData = mDisplayData[displayId]; 740 if (displayData.configMap.count(configId) == 0) { 741 ALOGE("setActiveConfig: Invalid config %zd", configId); 742 return BAD_INDEX; 743 } 744 745 auto error = displayData.hwcDisplay->setActiveConfig( 746 displayData.configMap[configId]); 747 if (error != HWC2::Error::None) { 748 ALOGE("setActiveConfig: Failed to set config %zu on display %d: " 749 "%s (%d)", configId, displayId, to_string(error).c_str(), 750 static_cast<int32_t>(error)); 751 return UNKNOWN_ERROR; 752 } 753 754 return NO_ERROR; 755} 756 757status_t HWComposer::setColorTransform(int32_t displayId, 758 const mat4& transform) { 759 if (!isValidDisplay(displayId)) { 760 ALOGE("setColorTransform: Display %d is not valid", displayId); 761 return BAD_INDEX; 762 } 763 764 auto& displayData = mDisplayData[displayId]; 765 bool isIdentity = transform == mat4(); 766 auto error = displayData.hwcDisplay->setColorTransform(transform, 767 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : 768 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX); 769 if (error != HWC2::Error::None) { 770 ALOGE("setColorTransform: Failed to set transform on display %d: " 771 "%s (%d)", displayId, to_string(error).c_str(), 772 static_cast<int32_t>(error)); 773 return UNKNOWN_ERROR; 774 } 775 776 return NO_ERROR; 777} 778 779void HWComposer::disconnectDisplay(int displayId) { 780 LOG_ALWAYS_FATAL_IF(displayId < 0); 781 auto& displayData = mDisplayData[displayId]; 782 783 auto displayType = HWC2::DisplayType::Invalid; 784 auto error = displayData.hwcDisplay->getType(&displayType); 785 if (error != HWC2::Error::None) { 786 ALOGE("disconnectDisplay: Failed to determine type of display %d", 787 displayId); 788 return; 789 } 790 791 // If this was a virtual display, add its slot back for reuse by future 792 // virtual displays 793 if (displayType == HWC2::DisplayType::Virtual) { 794 mFreeDisplaySlots.insert(displayId); 795 ++mRemainingHwcVirtualDisplays; 796 } 797 798 auto hwcId = displayData.hwcDisplay->getId(); 799 mHwcDisplaySlots.erase(hwcId); 800 displayData.reset(); 801} 802 803status_t HWComposer::setOutputBuffer(int32_t displayId, 804 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) { 805 if (!isValidDisplay(displayId)) { 806 ALOGE("setOutputBuffer: Display %d is not valid", displayId); 807 return BAD_INDEX; 808 } 809 810 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 811 auto displayType = HWC2::DisplayType::Invalid; 812 auto error = hwcDisplay->getType(&displayType); 813 if (error != HWC2::Error::None) { 814 ALOGE("setOutputBuffer: Failed to determine type of display %d", 815 displayId); 816 return NAME_NOT_FOUND; 817 } 818 819 if (displayType != HWC2::DisplayType::Virtual) { 820 ALOGE("setOutputBuffer: Display %d is not virtual", displayId); 821 return INVALID_OPERATION; 822 } 823 824 error = hwcDisplay->setOutputBuffer(buffer, acquireFence); 825 if (error != HWC2::Error::None) { 826 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)", 827 displayId, to_string(error).c_str(), 828 static_cast<int32_t>(error)); 829 return UNKNOWN_ERROR; 830 } 831 832 return NO_ERROR; 833} 834 835void HWComposer::clearReleaseFences(int32_t displayId) { 836 if (!isValidDisplay(displayId)) { 837 ALOGE("clearReleaseFences: Display %d is not valid", displayId); 838 return; 839 } 840 mDisplayData[displayId].releaseFences.clear(); 841} 842 843std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities( 844 int32_t displayId) { 845 if (!isValidDisplay(displayId)) { 846 ALOGE("getHdrCapabilities: Display %d is not valid", displayId); 847 return nullptr; 848 } 849 850 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 851 std::unique_ptr<HdrCapabilities> capabilities; 852 auto error = hwcDisplay->getHdrCapabilities(&capabilities); 853 if (error != HWC2::Error::None) { 854 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:" 855 " %s (%d)", displayId, to_string(error).c_str(), 856 static_cast<int32_t>(error)); 857 return nullptr; 858 } 859 860 return capabilities; 861} 862 863// Converts a PixelFormat to a human-readable string. Max 11 chars. 864// (Could use a table of prefab String8 objects.) 865/* 866static String8 getFormatStr(PixelFormat format) { 867 switch (format) { 868 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888"); 869 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888"); 870 case PIXEL_FORMAT_RGB_888: return String8("RGB_888"); 871 case PIXEL_FORMAT_RGB_565: return String8("RGB_565"); 872 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888"); 873 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 874 return String8("ImplDef"); 875 default: 876 String8 result; 877 result.appendFormat("? %08x", format); 878 return result; 879 } 880} 881*/ 882 883bool HWComposer::isUsingVrComposer() const { 884#ifdef BYPASS_IHWC 885 return false; 886#else 887 return getComposer()->isUsingVrComposer(); 888#endif 889} 890 891void HWComposer::dump(String8& result) const { 892 if (mDumpMayLockUp) { 893 result.append("HWComposer dump skipped because present in progress"); 894 return; 895 } 896 897 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow 898 // all the state going into the layers. This is probably better done in 899 // Layer itself, but it's going to take a bit of work to get there. 900 result.append(mHwcDevice->dump().c_str()); 901} 902 903// --------------------------------------------------------------------------- 904 905HWComposer::DisplayData::DisplayData() 906 : hasClientComposition(false), 907 hasDeviceComposition(false), 908 hwcDisplay(), 909 lastPresentFence(Fence::NO_FENCE), 910 outbufHandle(nullptr), 911 outbufAcquireFence(Fence::NO_FENCE), 912 vsyncEnabled(HWC2::Vsync::Disable) { 913 ALOGV("Created new DisplayData"); 914} 915 916HWComposer::DisplayData::~DisplayData() { 917} 918 919void HWComposer::DisplayData::reset() { 920 ALOGV("DisplayData reset"); 921 *this = DisplayData(); 922} 923 924// --------------------------------------------------------------------------- 925}; // namespace android 926