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