HWComposer.cpp revision 843460d1d8ced54ed224cff2b3fad703b6ea98c4
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 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.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(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 std::shared_ptr<HWC2::Display> display; 271 auto error = mHwcDevice->createVirtualDisplay(width, height, format, 272 &display); 273 if (error != HWC2::Error::None) { 274 ALOGE("allocateVirtualDisplay: Failed to create HWC virtual display"); 275 return NO_MEMORY; 276 } 277 278 size_t displaySlot = 0; 279 if (!mFreeDisplaySlots.empty()) { 280 displaySlot = *mFreeDisplaySlots.begin(); 281 mFreeDisplaySlots.erase(displaySlot); 282 } else if (mDisplayData.size() < INT32_MAX) { 283 // Don't bother allocating a slot larger than we can return 284 displaySlot = mDisplayData.size(); 285 mDisplayData.resize(displaySlot + 1); 286 } else { 287 ALOGE("allocateVirtualDisplay: Unable to allocate a display slot"); 288 return NO_MEMORY; 289 } 290 291 mDisplayData[displaySlot].hwcDisplay = display; 292 293 --mRemainingHwcVirtualDisplays; 294 *outId = static_cast<int32_t>(displaySlot); 295 296 return NO_ERROR; 297} 298 299std::shared_ptr<HWC2::Layer> HWComposer::createLayer(int32_t displayId) { 300 if (!isValidDisplay(displayId)) { 301 ALOGE("Failed to create layer on invalid display %d", displayId); 302 return nullptr; 303 } 304 auto display = mDisplayData[displayId].hwcDisplay; 305 std::shared_ptr<HWC2::Layer> layer; 306 auto error = display->createLayer(&layer); 307 if (error != HWC2::Error::None) { 308 ALOGE("Failed to create layer on display %d: %s (%d)", displayId, 309 to_string(error).c_str(), static_cast<int32_t>(error)); 310 return nullptr; 311 } 312 return layer; 313} 314 315nsecs_t HWComposer::getRefreshTimestamp(int32_t displayId) const { 316 // this returns the last refresh timestamp. 317 // if the last one is not available, we estimate it based on 318 // the refresh period and whatever closest timestamp we have. 319 Mutex::Autolock _l(mLock); 320 nsecs_t now = systemTime(CLOCK_MONOTONIC); 321 auto vsyncPeriod = getActiveConfig(displayId)->getVsyncPeriod(); 322 return now - ((now - mLastHwVSync[displayId]) % vsyncPeriod); 323} 324 325bool HWComposer::isConnected(int32_t displayId) const { 326 if (!isValidDisplay(displayId)) { 327 ALOGE("isConnected: Attempted to access invalid display %d", displayId); 328 return false; 329 } 330 return mDisplayData[displayId].hwcDisplay->isConnected(); 331} 332 333std::vector<std::shared_ptr<const HWC2::Display::Config>> 334 HWComposer::getConfigs(int32_t displayId) const { 335 if (!isValidDisplay(displayId)) { 336 ALOGE("getConfigs: Attempted to access invalid display %d", displayId); 337 return {}; 338 } 339 auto& displayData = mDisplayData[displayId]; 340 auto configs = mDisplayData[displayId].hwcDisplay->getConfigs(); 341 if (displayData.configMap.empty()) { 342 for (size_t i = 0; i < configs.size(); ++i) { 343 displayData.configMap[i] = configs[i]; 344 } 345 } 346 return configs; 347} 348 349std::shared_ptr<const HWC2::Display::Config> 350 HWComposer::getActiveConfig(int32_t displayId) const { 351 if (!isValidDisplay(displayId)) { 352 ALOGV("getActiveConfigs: Attempted to access invalid display %d", 353 displayId); 354 return nullptr; 355 } 356 std::shared_ptr<const HWC2::Display::Config> config; 357 auto error = mDisplayData[displayId].hwcDisplay->getActiveConfig(&config); 358 if (error == HWC2::Error::BadConfig) { 359 ALOGE("getActiveConfig: No config active, returning null"); 360 return nullptr; 361 } else if (error != HWC2::Error::None) { 362 ALOGE("getActiveConfig failed for display %d: %s (%d)", displayId, 363 to_string(error).c_str(), static_cast<int32_t>(error)); 364 return nullptr; 365 } else if (!config) { 366 ALOGE("getActiveConfig returned an unknown config for display %d", 367 displayId); 368 return nullptr; 369 } 370 371 return config; 372} 373 374std::vector<android_color_mode_t> HWComposer::getColorModes(int32_t displayId) const { 375 std::vector<android_color_mode_t> modes; 376 377 if (!isValidDisplay(displayId)) { 378 ALOGE("getColorModes: Attempted to access invalid display %d", 379 displayId); 380 return modes; 381 } 382 const std::shared_ptr<HWC2::Display>& hwcDisplay = 383 mDisplayData[displayId].hwcDisplay; 384 385 auto error = hwcDisplay->getColorModes(&modes); 386 if (error != HWC2::Error::None) { 387 ALOGE("getColorModes failed for display %d: %s (%d)", displayId, 388 to_string(error).c_str(), static_cast<int32_t>(error)); 389 return std::vector<android_color_mode_t>(); 390 } 391 392 return modes; 393} 394 395status_t HWComposer::setActiveColorMode(int32_t displayId, android_color_mode_t mode) { 396 if (!isValidDisplay(displayId)) { 397 ALOGE("setActiveColorMode: Display %d is not valid", displayId); 398 return BAD_INDEX; 399 } 400 401 auto& displayData = mDisplayData[displayId]; 402 auto error = displayData.hwcDisplay->setColorMode(mode); 403 if (error != HWC2::Error::None) { 404 ALOGE("setActiveConfig: Failed to set color mode %d on display %d: " 405 "%s (%d)", mode, displayId, to_string(error).c_str(), 406 static_cast<int32_t>(error)); 407 return UNKNOWN_ERROR; 408 } 409 410 return NO_ERROR; 411} 412 413 414void HWComposer::setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled) { 415 if (displayId < 0 || displayId >= HWC_DISPLAY_VIRTUAL) { 416 ALOGD("setVsyncEnabled: Ignoring for virtual display %d", displayId); 417 return; 418 } 419 420 if (!isValidDisplay(displayId)) { 421 ALOGE("setVsyncEnabled: Attempted to access invalid display %d", 422 displayId); 423 return; 424 } 425 426 // NOTE: we use our own internal lock here because we have to call 427 // into the HWC with the lock held, and we want to make sure 428 // that even if HWC blocks (which it shouldn't), it won't 429 // affect other threads. 430 Mutex::Autolock _l(mVsyncLock); 431 auto& displayData = mDisplayData[displayId]; 432 if (enabled != displayData.vsyncEnabled) { 433 ATRACE_CALL(); 434 auto error = displayData.hwcDisplay->setVsyncEnabled(enabled); 435 if (error == HWC2::Error::None) { 436 displayData.vsyncEnabled = enabled; 437 438 char tag[16]; 439 snprintf(tag, sizeof(tag), "HW_VSYNC_ON_%1u", displayId); 440 ATRACE_INT(tag, enabled == HWC2::Vsync::Enable ? 1 : 0); 441 } else { 442 ALOGE("setVsyncEnabled: Failed to set vsync to %s on %d/%" PRIu64 443 ": %s (%d)", to_string(enabled).c_str(), displayId, 444 mDisplayData[displayId].hwcDisplay->getId(), 445 to_string(error).c_str(), static_cast<int32_t>(error)); 446 } 447 } 448} 449 450status_t HWComposer::setClientTarget(int32_t displayId, uint32_t slot, 451 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target, 452 android_dataspace_t dataspace) { 453 if (!isValidDisplay(displayId)) { 454 return BAD_INDEX; 455 } 456 457 ALOGV("setClientTarget for display %d", displayId); 458 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 459 buffer_handle_t handle = nullptr; 460 if ((target != nullptr) && target->getNativeBuffer()) { 461 handle = target->getNativeBuffer()->handle; 462 } 463 auto error = hwcDisplay->setClientTarget(slot, handle, 464 acquireFence, dataspace); 465 if (error != HWC2::Error::None) { 466 ALOGE("Failed to set client target for display %d: %s (%d)", displayId, 467 to_string(error).c_str(), static_cast<int32_t>(error)); 468 return BAD_VALUE; 469 } 470 471 return NO_ERROR; 472} 473 474status_t HWComposer::prepare(DisplayDevice& displayDevice) { 475 ATRACE_CALL(); 476 477 Mutex::Autolock _l(mDisplayLock); 478 auto displayId = displayDevice.getHwcDisplayId(); 479 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 480 ALOGV("Skipping HWComposer prepare for non-HWC display"); 481 return NO_ERROR; 482 } 483 if (!isValidDisplay(displayId)) { 484 return BAD_INDEX; 485 } 486 487 auto& displayData = mDisplayData[displayId]; 488 auto& hwcDisplay = displayData.hwcDisplay; 489 if (!hwcDisplay->isConnected()) { 490 return NO_ERROR; 491 } 492 493 mDumpMayLockUp = true; 494 495 uint32_t numTypes = 0; 496 uint32_t numRequests = 0; 497 auto error = hwcDisplay->validate(&numTypes, &numRequests); 498 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) { 499 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId, 500 to_string(error).c_str(), static_cast<int32_t>(error)); 501 return BAD_INDEX; 502 } 503 504 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition> 505 changedTypes; 506 changedTypes.reserve(numTypes); 507 error = hwcDisplay->getChangedCompositionTypes(&changedTypes); 508 if (error != HWC2::Error::None) { 509 ALOGE("prepare: getChangedCompositionTypes failed on display %d: " 510 "%s (%d)", displayId, to_string(error).c_str(), 511 static_cast<int32_t>(error)); 512 return BAD_INDEX; 513 } 514 515 516 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0); 517 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest> 518 layerRequests; 519 layerRequests.reserve(numRequests); 520 error = hwcDisplay->getRequests(&displayData.displayRequests, 521 &layerRequests); 522 if (error != HWC2::Error::None) { 523 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId, 524 to_string(error).c_str(), static_cast<int32_t>(error)); 525 return BAD_INDEX; 526 } 527 528 displayData.hasClientComposition = false; 529 displayData.hasDeviceComposition = false; 530 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) { 531 auto hwcLayer = layer->getHwcLayer(displayId); 532 533 if (changedTypes.count(hwcLayer) != 0) { 534 // We pass false so we only update our state and don't call back 535 // into the HWC device 536 validateChange(layer->getCompositionType(displayId), 537 changedTypes[hwcLayer]); 538 layer->setCompositionType(displayId, changedTypes[hwcLayer], false); 539 } 540 541 switch (layer->getCompositionType(displayId)) { 542 case HWC2::Composition::Client: 543 displayData.hasClientComposition = true; 544 break; 545 case HWC2::Composition::Device: 546 case HWC2::Composition::SolidColor: 547 case HWC2::Composition::Cursor: 548 case HWC2::Composition::Sideband: 549 displayData.hasDeviceComposition = true; 550 break; 551 default: 552 break; 553 } 554 555 if (layerRequests.count(hwcLayer) != 0 && 556 layerRequests[hwcLayer] == 557 HWC2::LayerRequest::ClearClientTarget) { 558 layer->setClearClientTarget(displayId, true); 559 } else { 560 if (layerRequests.count(hwcLayer) != 0) { 561 ALOGE("prepare: Unknown layer request: %s", 562 to_string(layerRequests[hwcLayer]).c_str()); 563 } 564 layer->setClearClientTarget(displayId, false); 565 } 566 } 567 568 error = hwcDisplay->acceptChanges(); 569 if (error != HWC2::Error::None) { 570 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str()); 571 return BAD_INDEX; 572 } 573 574 return NO_ERROR; 575} 576 577bool HWComposer::hasDeviceComposition(int32_t displayId) const { 578 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 579 // Displays without a corresponding HWC display are never composed by 580 // the device 581 return false; 582 } 583 if (!isValidDisplay(displayId)) { 584 ALOGE("hasDeviceComposition: Invalid display %d", displayId); 585 return false; 586 } 587 return mDisplayData[displayId].hasDeviceComposition; 588} 589 590bool HWComposer::hasClientComposition(int32_t displayId) const { 591 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 592 // Displays without a corresponding HWC display are always composed by 593 // the client 594 return true; 595 } 596 if (!isValidDisplay(displayId)) { 597 ALOGE("hasClientComposition: Invalid display %d", displayId); 598 return true; 599 } 600 return mDisplayData[displayId].hasClientComposition; 601} 602 603sp<Fence> HWComposer::getPresentFence(int32_t displayId) const { 604 if (!isValidDisplay(displayId)) { 605 ALOGE("getPresentFence failed for invalid display %d", displayId); 606 return Fence::NO_FENCE; 607 } 608 return mDisplayData[displayId].lastPresentFence; 609} 610 611bool HWComposer::presentFenceRepresentsStartOfScanout() const { 612 return mAdapter ? false : true; 613} 614 615sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId, 616 const std::shared_ptr<HWC2::Layer>& layer) const { 617 if (!isValidDisplay(displayId)) { 618 ALOGE("getLayerReleaseFence: Invalid display"); 619 return Fence::NO_FENCE; 620 } 621 auto displayFences = mDisplayData[displayId].releaseFences; 622 if (displayFences.count(layer) == 0) { 623 ALOGV("getLayerReleaseFence: Release fence not found"); 624 return Fence::NO_FENCE; 625 } 626 return displayFences[layer]; 627} 628 629status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) { 630 ATRACE_CALL(); 631 632 if (!isValidDisplay(displayId)) { 633 return BAD_INDEX; 634 } 635 636 auto& displayData = mDisplayData[displayId]; 637 auto& hwcDisplay = displayData.hwcDisplay; 638 auto error = hwcDisplay->present(&displayData.lastPresentFence); 639 640 mDumpMayLockUp = false; 641 642 if (error != HWC2::Error::None) { 643 ALOGE("presentAndGetReleaseFences: failed for display %d: %s (%d)", 644 displayId, to_string(error).c_str(), static_cast<int32_t>(error)); 645 return UNKNOWN_ERROR; 646 } 647 648 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences; 649 error = hwcDisplay->getReleaseFences(&releaseFences); 650 if (error != HWC2::Error::None) { 651 ALOGE("presentAndGetReleaseFences: Failed to get release fences " 652 "for display %d: %s (%d)", 653 displayId, to_string(error).c_str(), 654 static_cast<int32_t>(error)); 655 return UNKNOWN_ERROR; 656 } 657 658 displayData.releaseFences = std::move(releaseFences); 659 660 return NO_ERROR; 661} 662 663status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) { 664 ALOGV("setPowerMode(%d, %d)", displayId, intMode); 665 if (!isValidDisplay(displayId)) { 666 ALOGE("setPowerMode: Bad display"); 667 return BAD_INDEX; 668 } 669 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) { 670 ALOGE("setPowerMode: Virtual display %d passed in, returning", 671 displayId); 672 return BAD_INDEX; 673 } 674 675 auto mode = static_cast<HWC2::PowerMode>(intMode); 676 if (mode == HWC2::PowerMode::Off) { 677 setVsyncEnabled(displayId, HWC2::Vsync::Disable); 678 } 679 680 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 681 switch (mode) { 682 case HWC2::PowerMode::Off: 683 case HWC2::PowerMode::On: 684 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); 685 { 686 auto error = hwcDisplay->setPowerMode(mode); 687 if (error != HWC2::Error::None) { 688 ALOGE("setPowerMode: Unable to set power mode %s for " 689 "display %d: %s (%d)", to_string(mode).c_str(), 690 displayId, to_string(error).c_str(), 691 static_cast<int32_t>(error)); 692 } 693 } 694 break; 695 case HWC2::PowerMode::Doze: 696 case HWC2::PowerMode::DozeSuspend: 697 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); 698 { 699 bool supportsDoze = false; 700 auto error = hwcDisplay->supportsDoze(&supportsDoze); 701 if (error != HWC2::Error::None) { 702 ALOGE("setPowerMode: Unable to query doze support for " 703 "display %d: %s (%d)", displayId, 704 to_string(error).c_str(), 705 static_cast<int32_t>(error)); 706 } 707 if (!supportsDoze) { 708 mode = HWC2::PowerMode::On; 709 } 710 711 error = hwcDisplay->setPowerMode(mode); 712 if (error != HWC2::Error::None) { 713 ALOGE("setPowerMode: Unable to set power mode %s for " 714 "display %d: %s (%d)", to_string(mode).c_str(), 715 displayId, to_string(error).c_str(), 716 static_cast<int32_t>(error)); 717 } 718 } 719 break; 720 default: 721 ALOGV("setPowerMode: Not calling HWC"); 722 break; 723 } 724 725 return NO_ERROR; 726} 727 728status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) { 729 if (!isValidDisplay(displayId)) { 730 ALOGE("setActiveConfig: Display %d is not valid", displayId); 731 return BAD_INDEX; 732 } 733 734 auto& displayData = mDisplayData[displayId]; 735 if (displayData.configMap.count(configId) == 0) { 736 ALOGE("setActiveConfig: Invalid config %zd", configId); 737 return BAD_INDEX; 738 } 739 740 auto error = displayData.hwcDisplay->setActiveConfig( 741 displayData.configMap[configId]); 742 if (error != HWC2::Error::None) { 743 ALOGE("setActiveConfig: Failed to set config %zu on display %d: " 744 "%s (%d)", configId, displayId, to_string(error).c_str(), 745 static_cast<int32_t>(error)); 746 return UNKNOWN_ERROR; 747 } 748 749 return NO_ERROR; 750} 751 752status_t HWComposer::setColorTransform(int32_t displayId, 753 const mat4& transform) { 754 if (!isValidDisplay(displayId)) { 755 ALOGE("setColorTransform: Display %d is not valid", displayId); 756 return BAD_INDEX; 757 } 758 759 auto& displayData = mDisplayData[displayId]; 760 bool isIdentity = transform == mat4(); 761 auto error = displayData.hwcDisplay->setColorTransform(transform, 762 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : 763 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX); 764 if (error != HWC2::Error::None) { 765 ALOGE("setColorTransform: Failed to set transform on display %d: " 766 "%s (%d)", displayId, to_string(error).c_str(), 767 static_cast<int32_t>(error)); 768 return UNKNOWN_ERROR; 769 } 770 771 return NO_ERROR; 772} 773 774void HWComposer::disconnectDisplay(int displayId) { 775 LOG_ALWAYS_FATAL_IF(displayId < 0); 776 auto& displayData = mDisplayData[displayId]; 777 778 auto displayType = HWC2::DisplayType::Invalid; 779 auto error = displayData.hwcDisplay->getType(&displayType); 780 if (error != HWC2::Error::None) { 781 ALOGE("disconnectDisplay: Failed to determine type of display %d", 782 displayId); 783 return; 784 } 785 786 // If this was a virtual display, add its slot back for reuse by future 787 // virtual displays 788 if (displayType == HWC2::DisplayType::Virtual) { 789 mFreeDisplaySlots.insert(displayId); 790 ++mRemainingHwcVirtualDisplays; 791 } 792 793 auto hwcId = displayData.hwcDisplay->getId(); 794 mHwcDisplaySlots.erase(hwcId); 795 displayData.reset(); 796} 797 798status_t HWComposer::setOutputBuffer(int32_t displayId, 799 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) { 800 if (!isValidDisplay(displayId)) { 801 ALOGE("setOutputBuffer: Display %d is not valid", displayId); 802 return BAD_INDEX; 803 } 804 805 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 806 auto displayType = HWC2::DisplayType::Invalid; 807 auto error = hwcDisplay->getType(&displayType); 808 if (error != HWC2::Error::None) { 809 ALOGE("setOutputBuffer: Failed to determine type of display %d", 810 displayId); 811 return NAME_NOT_FOUND; 812 } 813 814 if (displayType != HWC2::DisplayType::Virtual) { 815 ALOGE("setOutputBuffer: Display %d is not virtual", displayId); 816 return INVALID_OPERATION; 817 } 818 819 error = hwcDisplay->setOutputBuffer(buffer, acquireFence); 820 if (error != HWC2::Error::None) { 821 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)", 822 displayId, to_string(error).c_str(), 823 static_cast<int32_t>(error)); 824 return UNKNOWN_ERROR; 825 } 826 827 return NO_ERROR; 828} 829 830void HWComposer::clearReleaseFences(int32_t displayId) { 831 if (!isValidDisplay(displayId)) { 832 ALOGE("clearReleaseFences: Display %d is not valid", displayId); 833 return; 834 } 835 mDisplayData[displayId].releaseFences.clear(); 836} 837 838std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities( 839 int32_t displayId) { 840 if (!isValidDisplay(displayId)) { 841 ALOGE("getHdrCapabilities: Display %d is not valid", displayId); 842 return nullptr; 843 } 844 845 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 846 std::unique_ptr<HdrCapabilities> capabilities; 847 auto error = hwcDisplay->getHdrCapabilities(&capabilities); 848 if (error != HWC2::Error::None) { 849 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:" 850 " %s (%d)", displayId, to_string(error).c_str(), 851 static_cast<int32_t>(error)); 852 return nullptr; 853 } 854 855 return capabilities; 856} 857 858// Converts a PixelFormat to a human-readable string. Max 11 chars. 859// (Could use a table of prefab String8 objects.) 860/* 861static String8 getFormatStr(PixelFormat format) { 862 switch (format) { 863 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888"); 864 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888"); 865 case PIXEL_FORMAT_RGB_888: return String8("RGB_888"); 866 case PIXEL_FORMAT_RGB_565: return String8("RGB_565"); 867 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888"); 868 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 869 return String8("ImplDef"); 870 default: 871 String8 result; 872 result.appendFormat("? %08x", format); 873 return result; 874 } 875} 876*/ 877 878bool HWComposer::isUsingVrComposer() const { 879#ifdef BYPASS_IHWC 880 return false; 881#else 882 return getComposer()->isUsingVrComposer(); 883#endif 884} 885 886void HWComposer::dump(String8& result) const { 887 if (mDumpMayLockUp) { 888 result.append("HWComposer dump skipped because present in progress"); 889 return; 890 } 891 892 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow 893 // all the state going into the layers. This is probably better done in 894 // Layer itself, but it's going to take a bit of work to get there. 895 result.append(mHwcDevice->dump().c_str()); 896} 897 898// --------------------------------------------------------------------------- 899 900HWComposer::DisplayData::DisplayData() 901 : hasClientComposition(false), 902 hasDeviceComposition(false), 903 hwcDisplay(), 904 lastPresentFence(Fence::NO_FENCE), 905 outbufHandle(nullptr), 906 outbufAcquireFence(Fence::NO_FENCE), 907 vsyncEnabled(HWC2::Vsync::Disable) { 908 ALOGV("Created new DisplayData"); 909} 910 911HWComposer::DisplayData::~DisplayData() { 912} 913 914void HWComposer::DisplayData::reset() { 915 ALOGV("DisplayData reset"); 916 *this = DisplayData(); 917} 918 919void HWComposerBufferCache::clear() 920{ 921 mBuffers.clear(); 922} 923 924void HWComposerBufferCache::getHwcBuffer(int slot, 925 const sp<GraphicBuffer>& buffer, 926 uint32_t* outSlot, sp<GraphicBuffer>* outBuffer) 927{ 928#ifdef BYPASS_IHWC 929 *outSlot = slot; 930 *outBuffer = buffer; 931#else 932 if (slot == BufferQueue::INVALID_BUFFER_SLOT || slot < 0) { 933 // default to slot 0 934 slot = 0; 935 } 936 937 if (static_cast<size_t>(slot) >= mBuffers.size()) { 938 mBuffers.resize(slot + 1); 939 } 940 941 *outSlot = slot; 942 943 if (mBuffers[slot] == buffer) { 944 // already cached in HWC, skip sending the buffer 945 *outBuffer = nullptr; 946 } else { 947 *outBuffer = buffer; 948 949 // update cache 950 mBuffers[slot] = buffer; 951 } 952#endif 953} 954 955// --------------------------------------------------------------------------- 956}; // namespace android 957