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