HWComposer.cpp revision 87670ffe26079eb50b62f698eb0f7bfa8ee51deb
1b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov/* 211a89b445f3bde56bf07e6a0d04f0b0256dcb215Andrey Somov * Copyright (C) 2010 The Android Open Source Project 3b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 4b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Licensed under the Apache License, Version 2.0 (the "License"); 5b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * you may not use this file except in compliance with the License. 6b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * You may obtain a copy of the License at 7b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 8b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * http://www.apache.org/licenses/LICENSE-2.0 9b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 10b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Unless required by applicable law or agreed to in writing, software 11b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * distributed under the License is distributed on an "AS IS" BASIS, 12b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * See the License for the specific language governing permissions and 14b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * limitations under the License. 15b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */ 16b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 17b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov// #define LOG_NDEBUG 0 18b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 19b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#undef LOG_TAG 20b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#define LOG_TAG "HWComposer" 21b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#define ATRACE_TAG ATRACE_TAG_GRAPHICS 22b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 23b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <inttypes.h> 24b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <math.h> 25b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <stdint.h> 26b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <stdio.h> 27b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <stdlib.h> 28b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <string.h> 29b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <sys/types.h> 30b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 31b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <utils/Errors.h> 32b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <utils/misc.h> 33b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <utils/NativeHandle.h> 34b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#include <utils/String8.h> 35b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov#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(const sp<SurfaceFlinger>& flinger, bool useVrComposer) 64 : mFlinger(flinger), 65 mAdapter(), 66 mHwcDevice(), 67 mDisplayData(2), 68 mFreeDisplaySlots(), 69 mHwcDisplaySlots(), 70 mCBContext(), 71 mEventHandler(nullptr), 72 mVSyncCounts(), 73 mRemainingHwcVirtualDisplays(0) 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 mFlinger->repaintEverything(); 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(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 uint32_t numTypes = 0; 494 uint32_t numRequests = 0; 495 auto error = hwcDisplay->validate(&numTypes, &numRequests); 496 if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) { 497 ALOGE("prepare: validate failed for display %d: %s (%d)", displayId, 498 to_string(error).c_str(), static_cast<int32_t>(error)); 499 return BAD_INDEX; 500 } 501 502 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::Composition> 503 changedTypes; 504 changedTypes.reserve(numTypes); 505 error = hwcDisplay->getChangedCompositionTypes(&changedTypes); 506 if (error != HWC2::Error::None) { 507 ALOGE("prepare: getChangedCompositionTypes failed on display %d: " 508 "%s (%d)", displayId, to_string(error).c_str(), 509 static_cast<int32_t>(error)); 510 return BAD_INDEX; 511 } 512 513 514 displayData.displayRequests = static_cast<HWC2::DisplayRequest>(0); 515 std::unordered_map<std::shared_ptr<HWC2::Layer>, HWC2::LayerRequest> 516 layerRequests; 517 layerRequests.reserve(numRequests); 518 error = hwcDisplay->getRequests(&displayData.displayRequests, 519 &layerRequests); 520 if (error != HWC2::Error::None) { 521 ALOGE("prepare: getRequests failed on display %d: %s (%d)", displayId, 522 to_string(error).c_str(), static_cast<int32_t>(error)); 523 return BAD_INDEX; 524 } 525 526 displayData.hasClientComposition = false; 527 displayData.hasDeviceComposition = false; 528 for (auto& layer : displayDevice.getVisibleLayersSortedByZ()) { 529 auto hwcLayer = layer->getHwcLayer(displayId); 530 531 if (changedTypes.count(hwcLayer) != 0) { 532 // We pass false so we only update our state and don't call back 533 // into the HWC device 534 validateChange(layer->getCompositionType(displayId), 535 changedTypes[hwcLayer]); 536 layer->setCompositionType(displayId, changedTypes[hwcLayer], false); 537 } 538 539 switch (layer->getCompositionType(displayId)) { 540 case HWC2::Composition::Client: 541 displayData.hasClientComposition = true; 542 break; 543 case HWC2::Composition::Device: 544 case HWC2::Composition::SolidColor: 545 case HWC2::Composition::Cursor: 546 case HWC2::Composition::Sideband: 547 displayData.hasDeviceComposition = true; 548 break; 549 default: 550 break; 551 } 552 553 if (layerRequests.count(hwcLayer) != 0 && 554 layerRequests[hwcLayer] == 555 HWC2::LayerRequest::ClearClientTarget) { 556 layer->setClearClientTarget(displayId, true); 557 } else { 558 if (layerRequests.count(hwcLayer) != 0) { 559 ALOGE("prepare: Unknown layer request: %s", 560 to_string(layerRequests[hwcLayer]).c_str()); 561 } 562 layer->setClearClientTarget(displayId, false); 563 } 564 } 565 566 error = hwcDisplay->acceptChanges(); 567 if (error != HWC2::Error::None) { 568 ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str()); 569 return BAD_INDEX; 570 } 571 572 return NO_ERROR; 573} 574 575bool HWComposer::hasDeviceComposition(int32_t displayId) const { 576 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 577 // Displays without a corresponding HWC display are never composed by 578 // the device 579 return false; 580 } 581 if (!isValidDisplay(displayId)) { 582 ALOGE("hasDeviceComposition: Invalid display %d", displayId); 583 return false; 584 } 585 return mDisplayData[displayId].hasDeviceComposition; 586} 587 588bool HWComposer::hasClientComposition(int32_t displayId) const { 589 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) { 590 // Displays without a corresponding HWC display are always composed by 591 // the client 592 return true; 593 } 594 if (!isValidDisplay(displayId)) { 595 ALOGE("hasClientComposition: Invalid display %d", displayId); 596 return true; 597 } 598 return mDisplayData[displayId].hasClientComposition; 599} 600 601sp<Fence> HWComposer::getPresentFence(int32_t displayId) const { 602 if (!isValidDisplay(displayId)) { 603 ALOGE("getPresentFence failed for invalid display %d", displayId); 604 return Fence::NO_FENCE; 605 } 606 return mDisplayData[displayId].lastPresentFence; 607} 608 609bool HWComposer::presentFenceRepresentsStartOfScanout() const { 610 return mAdapter ? false : true; 611} 612 613sp<Fence> HWComposer::getLayerReleaseFence(int32_t displayId, 614 const std::shared_ptr<HWC2::Layer>& layer) const { 615 if (!isValidDisplay(displayId)) { 616 ALOGE("getLayerReleaseFence: Invalid display"); 617 return Fence::NO_FENCE; 618 } 619 auto displayFences = mDisplayData[displayId].releaseFences; 620 if (displayFences.count(layer) == 0) { 621 ALOGV("getLayerReleaseFence: Release fence not found"); 622 return Fence::NO_FENCE; 623 } 624 return displayFences[layer]; 625} 626 627status_t HWComposer::presentAndGetReleaseFences(int32_t displayId) { 628 ATRACE_CALL(); 629 630 if (!isValidDisplay(displayId)) { 631 return BAD_INDEX; 632 } 633 634 auto& displayData = mDisplayData[displayId]; 635 auto& hwcDisplay = displayData.hwcDisplay; 636 auto error = hwcDisplay->present(&displayData.lastPresentFence); 637 if (error != HWC2::Error::None) { 638 ALOGE("presentAndGetReleaseFences: failed for display %d: %s (%d)", 639 displayId, to_string(error).c_str(), static_cast<int32_t>(error)); 640 return UNKNOWN_ERROR; 641 } 642 643 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> releaseFences; 644 error = hwcDisplay->getReleaseFences(&releaseFences); 645 if (error != HWC2::Error::None) { 646 ALOGE("presentAndGetReleaseFences: Failed to get release fences " 647 "for display %d: %s (%d)", 648 displayId, to_string(error).c_str(), 649 static_cast<int32_t>(error)); 650 return UNKNOWN_ERROR; 651 } 652 653 displayData.releaseFences = std::move(releaseFences); 654 655 return NO_ERROR; 656} 657 658status_t HWComposer::setPowerMode(int32_t displayId, int32_t intMode) { 659 ALOGV("setPowerMode(%d, %d)", displayId, intMode); 660 if (!isValidDisplay(displayId)) { 661 ALOGE("setPowerMode: Bad display"); 662 return BAD_INDEX; 663 } 664 if (displayId >= VIRTUAL_DISPLAY_ID_BASE) { 665 ALOGE("setPowerMode: Virtual display %d passed in, returning", 666 displayId); 667 return BAD_INDEX; 668 } 669 670 auto mode = static_cast<HWC2::PowerMode>(intMode); 671 if (mode == HWC2::PowerMode::Off) { 672 setVsyncEnabled(displayId, HWC2::Vsync::Disable); 673 } 674 675 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 676 switch (mode) { 677 case HWC2::PowerMode::Off: 678 case HWC2::PowerMode::On: 679 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); 680 { 681 auto error = hwcDisplay->setPowerMode(mode); 682 if (error != HWC2::Error::None) { 683 ALOGE("setPowerMode: Unable to set power mode %s for " 684 "display %d: %s (%d)", to_string(mode).c_str(), 685 displayId, to_string(error).c_str(), 686 static_cast<int32_t>(error)); 687 } 688 } 689 break; 690 case HWC2::PowerMode::Doze: 691 case HWC2::PowerMode::DozeSuspend: 692 ALOGV("setPowerMode: Calling HWC %s", to_string(mode).c_str()); 693 { 694 bool supportsDoze = false; 695 auto error = hwcDisplay->supportsDoze(&supportsDoze); 696 if (error != HWC2::Error::None) { 697 ALOGE("setPowerMode: Unable to query doze support for " 698 "display %d: %s (%d)", displayId, 699 to_string(error).c_str(), 700 static_cast<int32_t>(error)); 701 } 702 if (!supportsDoze) { 703 mode = HWC2::PowerMode::On; 704 } 705 706 error = hwcDisplay->setPowerMode(mode); 707 if (error != HWC2::Error::None) { 708 ALOGE("setPowerMode: Unable to set power mode %s for " 709 "display %d: %s (%d)", to_string(mode).c_str(), 710 displayId, to_string(error).c_str(), 711 static_cast<int32_t>(error)); 712 } 713 } 714 break; 715 default: 716 ALOGV("setPowerMode: Not calling HWC"); 717 break; 718 } 719 720 return NO_ERROR; 721} 722 723status_t HWComposer::setActiveConfig(int32_t displayId, size_t configId) { 724 if (!isValidDisplay(displayId)) { 725 ALOGE("setActiveConfig: Display %d is not valid", displayId); 726 return BAD_INDEX; 727 } 728 729 auto& displayData = mDisplayData[displayId]; 730 if (displayData.configMap.count(configId) == 0) { 731 ALOGE("setActiveConfig: Invalid config %zd", configId); 732 return BAD_INDEX; 733 } 734 735 auto error = displayData.hwcDisplay->setActiveConfig( 736 displayData.configMap[configId]); 737 if (error != HWC2::Error::None) { 738 ALOGE("setActiveConfig: Failed to set config %zu on display %d: " 739 "%s (%d)", configId, displayId, to_string(error).c_str(), 740 static_cast<int32_t>(error)); 741 return UNKNOWN_ERROR; 742 } 743 744 return NO_ERROR; 745} 746 747status_t HWComposer::setColorTransform(int32_t displayId, 748 const mat4& transform) { 749 if (!isValidDisplay(displayId)) { 750 ALOGE("setColorTransform: Display %d is not valid", displayId); 751 return BAD_INDEX; 752 } 753 754 auto& displayData = mDisplayData[displayId]; 755 bool isIdentity = transform == mat4(); 756 auto error = displayData.hwcDisplay->setColorTransform(transform, 757 isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : 758 HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX); 759 if (error != HWC2::Error::None) { 760 ALOGE("setColorTransform: Failed to set transform on display %d: " 761 "%s (%d)", displayId, to_string(error).c_str(), 762 static_cast<int32_t>(error)); 763 return UNKNOWN_ERROR; 764 } 765 766 return NO_ERROR; 767} 768 769void HWComposer::disconnectDisplay(int displayId) { 770 LOG_ALWAYS_FATAL_IF(displayId < 0); 771 auto& displayData = mDisplayData[displayId]; 772 773 auto displayType = HWC2::DisplayType::Invalid; 774 auto error = displayData.hwcDisplay->getType(&displayType); 775 if (error != HWC2::Error::None) { 776 ALOGE("disconnectDisplay: Failed to determine type of display %d", 777 displayId); 778 return; 779 } 780 781 // If this was a virtual display, add its slot back for reuse by future 782 // virtual displays 783 if (displayType == HWC2::DisplayType::Virtual) { 784 mFreeDisplaySlots.insert(displayId); 785 ++mRemainingHwcVirtualDisplays; 786 } 787 788 auto hwcId = displayData.hwcDisplay->getId(); 789 mHwcDisplaySlots.erase(hwcId); 790 displayData.reset(); 791} 792 793status_t HWComposer::setOutputBuffer(int32_t displayId, 794 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) { 795 if (!isValidDisplay(displayId)) { 796 ALOGE("setOutputBuffer: Display %d is not valid", displayId); 797 return BAD_INDEX; 798 } 799 800 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 801 auto displayType = HWC2::DisplayType::Invalid; 802 auto error = hwcDisplay->getType(&displayType); 803 if (error != HWC2::Error::None) { 804 ALOGE("setOutputBuffer: Failed to determine type of display %d", 805 displayId); 806 return NAME_NOT_FOUND; 807 } 808 809 if (displayType != HWC2::DisplayType::Virtual) { 810 ALOGE("setOutputBuffer: Display %d is not virtual", displayId); 811 return INVALID_OPERATION; 812 } 813 814 error = hwcDisplay->setOutputBuffer(buffer, acquireFence); 815 if (error != HWC2::Error::None) { 816 ALOGE("setOutputBuffer: Failed to set buffer on display %d: %s (%d)", 817 displayId, to_string(error).c_str(), 818 static_cast<int32_t>(error)); 819 return UNKNOWN_ERROR; 820 } 821 822 return NO_ERROR; 823} 824 825void HWComposer::clearReleaseFences(int32_t displayId) { 826 if (!isValidDisplay(displayId)) { 827 ALOGE("clearReleaseFences: Display %d is not valid", displayId); 828 return; 829 } 830 mDisplayData[displayId].releaseFences.clear(); 831} 832 833std::unique_ptr<HdrCapabilities> HWComposer::getHdrCapabilities( 834 int32_t displayId) { 835 if (!isValidDisplay(displayId)) { 836 ALOGE("getHdrCapabilities: Display %d is not valid", displayId); 837 return nullptr; 838 } 839 840 auto& hwcDisplay = mDisplayData[displayId].hwcDisplay; 841 std::unique_ptr<HdrCapabilities> capabilities; 842 auto error = hwcDisplay->getHdrCapabilities(&capabilities); 843 if (error != HWC2::Error::None) { 844 ALOGE("getOutputCapabilities: Failed to get capabilities on display %d:" 845 " %s (%d)", displayId, to_string(error).c_str(), 846 static_cast<int32_t>(error)); 847 return nullptr; 848 } 849 850 return capabilities; 851} 852 853// Converts a PixelFormat to a human-readable string. Max 11 chars. 854// (Could use a table of prefab String8 objects.) 855/* 856static String8 getFormatStr(PixelFormat format) { 857 switch (format) { 858 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888"); 859 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888"); 860 case PIXEL_FORMAT_RGB_888: return String8("RGB_888"); 861 case PIXEL_FORMAT_RGB_565: return String8("RGB_565"); 862 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888"); 863 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 864 return String8("ImplDef"); 865 default: 866 String8 result; 867 result.appendFormat("? %08x", format); 868 return result; 869 } 870} 871*/ 872 873bool HWComposer::isUsingVrComposer() const { 874#ifdef BYPASS_IHWC 875 return false; 876#else 877 return getComposer()->isUsingVrComposer(); 878#endif 879} 880 881void HWComposer::dump(String8& result) const { 882 // TODO: In order to provide a dump equivalent to HWC1, we need to shadow 883 // all the state going into the layers. This is probably better done in 884 // Layer itself, but it's going to take a bit of work to get there. 885 result.append(mHwcDevice->dump().c_str()); 886} 887 888// --------------------------------------------------------------------------- 889 890HWComposer::DisplayData::DisplayData() 891 : hasClientComposition(false), 892 hasDeviceComposition(false), 893 hwcDisplay(), 894 lastPresentFence(Fence::NO_FENCE), 895 outbufHandle(nullptr), 896 outbufAcquireFence(Fence::NO_FENCE), 897 vsyncEnabled(HWC2::Vsync::Disable) { 898 ALOGV("Created new DisplayData"); 899} 900 901HWComposer::DisplayData::~DisplayData() { 902} 903 904void HWComposer::DisplayData::reset() { 905 ALOGV("DisplayData reset"); 906 *this = DisplayData(); 907} 908 909void HWComposerBufferCache::clear() 910{ 911 mBuffers.clear(); 912} 913 914void HWComposerBufferCache::getHwcBuffer(int slot, 915 const sp<GraphicBuffer>& buffer, 916 uint32_t* outSlot, sp<GraphicBuffer>* outBuffer) 917{ 918 if (slot == BufferQueue::INVALID_BUFFER_SLOT || slot < 0) { 919 // default to slot 0 920 slot = 0; 921 } 922 923 if (static_cast<size_t>(slot) >= mBuffers.size()) { 924 mBuffers.resize(slot + 1); 925 } 926 927 *outSlot = slot; 928 929 if (mBuffers[slot] == buffer) { 930 // already cached in HWC, skip sending the buffer 931 *outBuffer = nullptr; 932 } else { 933 *outBuffer = buffer; 934 935 // update cache 936 mBuffers[slot] = buffer; 937 } 938} 939 940// --------------------------------------------------------------------------- 941}; // namespace android 942