HWC2On1Adapter.cpp revision 5df2a86063c6a83813fc1aa3d8938a82f7ff8f14
1/* 2 * Copyright 2015 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 "HWC2On1Adapter" 21#define ATRACE_TAG ATRACE_TAG_GRAPHICS 22 23#include "HWC2On1Adapter.h" 24 25#include <hardware/hwcomposer.h> 26#include <log/log.h> 27#include <utils/Trace.h> 28 29#include <cstdlib> 30#include <chrono> 31#include <inttypes.h> 32#include <sstream> 33 34using namespace std::chrono_literals; 35 36static bool operator==(const hwc_color_t& lhs, const hwc_color_t& rhs) { 37 return lhs.r == rhs.r && 38 lhs.g == rhs.g && 39 lhs.b == rhs.b && 40 lhs.a == rhs.a; 41} 42 43static bool operator==(const hwc_rect_t& lhs, const hwc_rect_t& rhs) { 44 return lhs.left == rhs.left && 45 lhs.top == rhs.top && 46 lhs.right == rhs.right && 47 lhs.bottom == rhs.bottom; 48} 49 50static bool operator==(const hwc_frect_t& lhs, const hwc_frect_t& rhs) { 51 return lhs.left == rhs.left && 52 lhs.top == rhs.top && 53 lhs.right == rhs.right && 54 lhs.bottom == rhs.bottom; 55} 56 57template <typename T> 58static inline bool operator!=(const T& lhs, const T& rhs) 59{ 60 return !(lhs == rhs); 61} 62 63static uint8_t getMinorVersion(struct hwc_composer_device_1* device) 64{ 65 auto version = device->common.version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK; 66 return (version >> 16) & 0xF; 67} 68 69template <typename PFN, typename T> 70static hwc2_function_pointer_t asFP(T function) 71{ 72 static_assert(std::is_same<PFN, T>::value, "Incompatible function pointer"); 73 return reinterpret_cast<hwc2_function_pointer_t>(function); 74} 75 76using namespace HWC2; 77 78namespace android { 79 80void HWC2On1Adapter::DisplayContentsDeleter::operator()( 81 hwc_display_contents_1_t* contents) 82{ 83 if (contents != nullptr) { 84 for (size_t l = 0; l < contents->numHwLayers; ++l) { 85 auto& layer = contents->hwLayers[l]; 86 std::free(const_cast<hwc_rect_t*>(layer.visibleRegionScreen.rects)); 87 } 88 } 89 std::free(contents); 90} 91 92class HWC2On1Adapter::Callbacks : public hwc_procs_t { 93 public: 94 Callbacks(HWC2On1Adapter& adapter) : mAdapter(adapter) { 95 invalidate = &invalidateHook; 96 vsync = &vsyncHook; 97 hotplug = &hotplugHook; 98 } 99 100 static void invalidateHook(const hwc_procs_t* procs) { 101 auto callbacks = static_cast<const Callbacks*>(procs); 102 callbacks->mAdapter.hwc1Invalidate(); 103 } 104 105 static void vsyncHook(const hwc_procs_t* procs, int display, 106 int64_t timestamp) { 107 auto callbacks = static_cast<const Callbacks*>(procs); 108 callbacks->mAdapter.hwc1Vsync(display, timestamp); 109 } 110 111 static void hotplugHook(const hwc_procs_t* procs, int display, 112 int connected) { 113 auto callbacks = static_cast<const Callbacks*>(procs); 114 callbacks->mAdapter.hwc1Hotplug(display, connected); 115 } 116 117 private: 118 HWC2On1Adapter& mAdapter; 119}; 120 121static int closeHook(hw_device_t* /*device*/) 122{ 123 // Do nothing, since the real work is done in the class destructor, but we 124 // need to provide a valid function pointer for hwc2_close to call 125 return 0; 126} 127 128HWC2On1Adapter::HWC2On1Adapter(hwc_composer_device_1_t* hwc1Device) 129 : mDumpString(), 130 mHwc1Device(hwc1Device), 131 mHwc1MinorVersion(getMinorVersion(hwc1Device)), 132 mHwc1SupportsVirtualDisplays(false), 133 mHwc1Callbacks(std::make_unique<Callbacks>(*this)), 134 mCapabilities(), 135 mLayers(), 136 mHwc1VirtualDisplay(), 137 mStateMutex(), 138 mCallbacks(), 139 mHasPendingInvalidate(false), 140 mPendingVsyncs(), 141 mPendingHotplugs(), 142 mDisplays(), 143 mHwc1DisplayMap() 144{ 145 common.close = closeHook; 146 getCapabilities = getCapabilitiesHook; 147 getFunction = getFunctionHook; 148 populateCapabilities(); 149 populatePrimary(); 150 mHwc1Device->registerProcs(mHwc1Device, 151 static_cast<const hwc_procs_t*>(mHwc1Callbacks.get())); 152} 153 154HWC2On1Adapter::~HWC2On1Adapter() { 155 hwc_close_1(mHwc1Device); 156} 157 158void HWC2On1Adapter::doGetCapabilities(uint32_t* outCount, 159 int32_t* outCapabilities) 160{ 161 if (outCapabilities == nullptr) { 162 *outCount = mCapabilities.size(); 163 return; 164 } 165 166 auto capabilityIter = mCapabilities.cbegin(); 167 for (size_t written = 0; written < *outCount; ++written) { 168 if (capabilityIter == mCapabilities.cend()) { 169 return; 170 } 171 outCapabilities[written] = static_cast<int32_t>(*capabilityIter); 172 ++capabilityIter; 173 } 174} 175 176hwc2_function_pointer_t HWC2On1Adapter::doGetFunction( 177 FunctionDescriptor descriptor) 178{ 179 switch (descriptor) { 180 // Device functions 181 case FunctionDescriptor::CreateVirtualDisplay: 182 return asFP<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>( 183 createVirtualDisplayHook); 184 case FunctionDescriptor::DestroyVirtualDisplay: 185 return asFP<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>( 186 destroyVirtualDisplayHook); 187 case FunctionDescriptor::Dump: 188 return asFP<HWC2_PFN_DUMP>(dumpHook); 189 case FunctionDescriptor::GetMaxVirtualDisplayCount: 190 return asFP<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>( 191 getMaxVirtualDisplayCountHook); 192 case FunctionDescriptor::RegisterCallback: 193 return asFP<HWC2_PFN_REGISTER_CALLBACK>(registerCallbackHook); 194 195 // Display functions 196 case FunctionDescriptor::AcceptDisplayChanges: 197 return asFP<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>( 198 displayHook<decltype(&Display::acceptChanges), 199 &Display::acceptChanges>); 200 case FunctionDescriptor::CreateLayer: 201 return asFP<HWC2_PFN_CREATE_LAYER>( 202 displayHook<decltype(&Display::createLayer), 203 &Display::createLayer, hwc2_layer_t*>); 204 case FunctionDescriptor::DestroyLayer: 205 return asFP<HWC2_PFN_DESTROY_LAYER>( 206 displayHook<decltype(&Display::destroyLayer), 207 &Display::destroyLayer, hwc2_layer_t>); 208 case FunctionDescriptor::GetActiveConfig: 209 return asFP<HWC2_PFN_GET_ACTIVE_CONFIG>( 210 displayHook<decltype(&Display::getActiveConfig), 211 &Display::getActiveConfig, hwc2_config_t*>); 212 case FunctionDescriptor::GetChangedCompositionTypes: 213 return asFP<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>( 214 displayHook<decltype(&Display::getChangedCompositionTypes), 215 &Display::getChangedCompositionTypes, uint32_t*, 216 hwc2_layer_t*, int32_t*>); 217 case FunctionDescriptor::GetDisplayAttribute: 218 return asFP<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>( 219 getDisplayAttributeHook); 220 case FunctionDescriptor::GetDisplayConfigs: 221 return asFP<HWC2_PFN_GET_DISPLAY_CONFIGS>( 222 displayHook<decltype(&Display::getConfigs), 223 &Display::getConfigs, uint32_t*, hwc2_config_t*>); 224 case FunctionDescriptor::GetDisplayName: 225 return asFP<HWC2_PFN_GET_DISPLAY_NAME>( 226 displayHook<decltype(&Display::getName), 227 &Display::getName, uint32_t*, char*>); 228 case FunctionDescriptor::GetDisplayRequests: 229 return asFP<HWC2_PFN_GET_DISPLAY_REQUESTS>( 230 displayHook<decltype(&Display::getRequests), 231 &Display::getRequests, int32_t*, uint32_t*, hwc2_layer_t*, 232 int32_t*>); 233 case FunctionDescriptor::GetDisplayType: 234 return asFP<HWC2_PFN_GET_DISPLAY_TYPE>( 235 displayHook<decltype(&Display::getType), 236 &Display::getType, int32_t*>); 237 case FunctionDescriptor::GetDozeSupport: 238 return asFP<HWC2_PFN_GET_DOZE_SUPPORT>( 239 displayHook<decltype(&Display::getDozeSupport), 240 &Display::getDozeSupport, int32_t*>); 241 case FunctionDescriptor::GetHdrCapabilities: 242 return asFP<HWC2_PFN_GET_HDR_CAPABILITIES>( 243 displayHook<decltype(&Display::getHdrCapabilities), 244 &Display::getHdrCapabilities, uint32_t*, int32_t*, float*, 245 float*, float*>); 246 case FunctionDescriptor::GetReleaseFences: 247 return asFP<HWC2_PFN_GET_RELEASE_FENCES>( 248 displayHook<decltype(&Display::getReleaseFences), 249 &Display::getReleaseFences, uint32_t*, hwc2_layer_t*, 250 int32_t*>); 251 case FunctionDescriptor::PresentDisplay: 252 return asFP<HWC2_PFN_PRESENT_DISPLAY>( 253 displayHook<decltype(&Display::present), 254 &Display::present, int32_t*>); 255 case FunctionDescriptor::SetActiveConfig: 256 return asFP<HWC2_PFN_SET_ACTIVE_CONFIG>( 257 displayHook<decltype(&Display::setActiveConfig), 258 &Display::setActiveConfig, hwc2_config_t>); 259 case FunctionDescriptor::SetClientTarget: 260 return asFP<HWC2_PFN_SET_CLIENT_TARGET>( 261 displayHook<decltype(&Display::setClientTarget), 262 &Display::setClientTarget, buffer_handle_t, int32_t, 263 int32_t>); 264 case FunctionDescriptor::SetColorTransform: 265 return asFP<HWC2_PFN_SET_COLOR_TRANSFORM>(setColorTransformHook); 266 case FunctionDescriptor::SetOutputBuffer: 267 return asFP<HWC2_PFN_SET_OUTPUT_BUFFER>( 268 displayHook<decltype(&Display::setOutputBuffer), 269 &Display::setOutputBuffer, buffer_handle_t, int32_t>); 270 case FunctionDescriptor::SetPowerMode: 271 return asFP<HWC2_PFN_SET_POWER_MODE>(setPowerModeHook); 272 case FunctionDescriptor::SetVsyncEnabled: 273 return asFP<HWC2_PFN_SET_VSYNC_ENABLED>(setVsyncEnabledHook); 274 case FunctionDescriptor::ValidateDisplay: 275 return asFP<HWC2_PFN_VALIDATE_DISPLAY>( 276 displayHook<decltype(&Display::validate), 277 &Display::validate, uint32_t*, uint32_t*>); 278 279 // Layer functions 280 case FunctionDescriptor::SetCursorPosition: 281 return asFP<HWC2_PFN_SET_CURSOR_POSITION>( 282 layerHook<decltype(&Layer::setCursorPosition), 283 &Layer::setCursorPosition, int32_t, int32_t>); 284 case FunctionDescriptor::SetLayerBuffer: 285 return asFP<HWC2_PFN_SET_LAYER_BUFFER>( 286 layerHook<decltype(&Layer::setBuffer), &Layer::setBuffer, 287 buffer_handle_t, int32_t>); 288 case FunctionDescriptor::SetLayerSurfaceDamage: 289 return asFP<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>( 290 layerHook<decltype(&Layer::setSurfaceDamage), 291 &Layer::setSurfaceDamage, hwc_region_t>); 292 293 // Layer state functions 294 case FunctionDescriptor::SetLayerBlendMode: 295 return asFP<HWC2_PFN_SET_LAYER_BLEND_MODE>( 296 setLayerBlendModeHook); 297 case FunctionDescriptor::SetLayerColor: 298 return asFP<HWC2_PFN_SET_LAYER_COLOR>( 299 layerHook<decltype(&Layer::setColor), &Layer::setColor, 300 hwc_color_t>); 301 case FunctionDescriptor::SetLayerCompositionType: 302 return asFP<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>( 303 setLayerCompositionTypeHook); 304 case FunctionDescriptor::SetLayerDataspace: 305 return asFP<HWC2_PFN_SET_LAYER_DATASPACE>(setLayerDataspaceHook); 306 case FunctionDescriptor::SetLayerDisplayFrame: 307 return asFP<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>( 308 layerHook<decltype(&Layer::setDisplayFrame), 309 &Layer::setDisplayFrame, hwc_rect_t>); 310 case FunctionDescriptor::SetLayerPlaneAlpha: 311 return asFP<HWC2_PFN_SET_LAYER_PLANE_ALPHA>( 312 layerHook<decltype(&Layer::setPlaneAlpha), 313 &Layer::setPlaneAlpha, float>); 314 case FunctionDescriptor::SetLayerSidebandStream: 315 return asFP<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>( 316 layerHook<decltype(&Layer::setSidebandStream), 317 &Layer::setSidebandStream, const native_handle_t*>); 318 case FunctionDescriptor::SetLayerSourceCrop: 319 return asFP<HWC2_PFN_SET_LAYER_SOURCE_CROP>( 320 layerHook<decltype(&Layer::setSourceCrop), 321 &Layer::setSourceCrop, hwc_frect_t>); 322 case FunctionDescriptor::SetLayerTransform: 323 return asFP<HWC2_PFN_SET_LAYER_TRANSFORM>(setLayerTransformHook); 324 case FunctionDescriptor::SetLayerVisibleRegion: 325 return asFP<HWC2_PFN_SET_LAYER_VISIBLE_REGION>( 326 layerHook<decltype(&Layer::setVisibleRegion), 327 &Layer::setVisibleRegion, hwc_region_t>); 328 case FunctionDescriptor::SetLayerZOrder: 329 return asFP<HWC2_PFN_SET_LAYER_Z_ORDER>(setLayerZOrderHook); 330 331 default: 332 ALOGE("doGetFunction: Unknown function descriptor: %d (%s)", 333 static_cast<int32_t>(descriptor), 334 to_string(descriptor).c_str()); 335 return nullptr; 336 } 337} 338 339// Device functions 340 341Error HWC2On1Adapter::createVirtualDisplay(uint32_t width, 342 uint32_t height, hwc2_display_t* outDisplay) 343{ 344 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 345 346 if (mHwc1VirtualDisplay) { 347 // We have already allocated our only HWC1 virtual display 348 ALOGE("createVirtualDisplay: HWC1 virtual display already allocated"); 349 return Error::NoResources; 350 } 351 352 if (MAX_VIRTUAL_DISPLAY_DIMENSION != 0 && 353 (width > MAX_VIRTUAL_DISPLAY_DIMENSION || 354 height > MAX_VIRTUAL_DISPLAY_DIMENSION)) { 355 ALOGE("createVirtualDisplay: Can't create a virtual display with" 356 " a dimension > %u (tried %u x %u)", 357 MAX_VIRTUAL_DISPLAY_DIMENSION, width, height); 358 return Error::NoResources; 359 } 360 361 mHwc1VirtualDisplay = std::make_shared<HWC2On1Adapter::Display>(*this, 362 HWC2::DisplayType::Virtual); 363 mHwc1VirtualDisplay->populateConfigs(width, height); 364 const auto displayId = mHwc1VirtualDisplay->getId(); 365 mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL] = displayId; 366 mHwc1VirtualDisplay->setHwc1Id(HWC_DISPLAY_VIRTUAL); 367 mDisplays.emplace(displayId, mHwc1VirtualDisplay); 368 *outDisplay = displayId; 369 370 return Error::None; 371} 372 373Error HWC2On1Adapter::destroyVirtualDisplay(hwc2_display_t displayId) 374{ 375 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 376 377 if (!mHwc1VirtualDisplay || (mHwc1VirtualDisplay->getId() != displayId)) { 378 return Error::BadDisplay; 379 } 380 381 mHwc1VirtualDisplay.reset(); 382 mHwc1DisplayMap.erase(HWC_DISPLAY_VIRTUAL); 383 mDisplays.erase(displayId); 384 385 return Error::None; 386} 387 388void HWC2On1Adapter::dump(uint32_t* outSize, char* outBuffer) 389{ 390 if (outBuffer != nullptr) { 391 auto copiedBytes = mDumpString.copy(outBuffer, *outSize); 392 *outSize = static_cast<uint32_t>(copiedBytes); 393 return; 394 } 395 396 std::stringstream output; 397 398 output << "-- HWC2On1Adapter --\n"; 399 400 output << "Adapting to a HWC 1." << static_cast<int>(mHwc1MinorVersion) << 401 " device\n"; 402 403 // Attempt to acquire the lock for 1 second, but proceed without the lock 404 // after that, so we can still get some information if we're deadlocked 405 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex, 406 std::defer_lock); 407 lock.try_lock_for(1s); 408 409 if (mCapabilities.empty()) { 410 output << "Capabilities: None\n"; 411 } else { 412 output << "Capabilities:\n"; 413 for (auto capability : mCapabilities) { 414 output << " " << to_string(capability) << '\n'; 415 } 416 } 417 418 output << "Displays:\n"; 419 for (const auto& element : mDisplays) { 420 const auto& display = element.second; 421 output << display->dump(); 422 } 423 output << '\n'; 424 425 // Release the lock before calling into HWC1, and since we no longer require 426 // mutual exclusion to access mCapabilities or mDisplays 427 lock.unlock(); 428 429 if (mHwc1Device->dump) { 430 output << "HWC1 dump:\n"; 431 std::vector<char> hwc1Dump(4096); 432 // Call with size - 1 to preserve a null character at the end 433 mHwc1Device->dump(mHwc1Device, hwc1Dump.data(), 434 static_cast<int>(hwc1Dump.size() - 1)); 435 output << hwc1Dump.data(); 436 } 437 438 mDumpString = output.str(); 439 *outSize = static_cast<uint32_t>(mDumpString.size()); 440} 441 442uint32_t HWC2On1Adapter::getMaxVirtualDisplayCount() 443{ 444 return mHwc1SupportsVirtualDisplays ? 1 : 0; 445} 446 447static bool isValid(Callback descriptor) { 448 switch (descriptor) { 449 case Callback::Hotplug: // Fall-through 450 case Callback::Refresh: // Fall-through 451 case Callback::Vsync: return true; 452 default: return false; 453 } 454} 455 456Error HWC2On1Adapter::registerCallback(Callback descriptor, 457 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer) 458{ 459 if (!isValid(descriptor)) { 460 return Error::BadParameter; 461 } 462 463 ALOGV("registerCallback(%s, %p, %p)", to_string(descriptor).c_str(), 464 callbackData, pointer); 465 466 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 467 468 mCallbacks[descriptor] = {callbackData, pointer}; 469 470 bool hasPendingInvalidate = false; 471 std::vector<hwc2_display_t> displayIds; 472 std::vector<std::pair<hwc2_display_t, int64_t>> pendingVsyncs; 473 std::vector<std::pair<hwc2_display_t, int>> pendingHotplugs; 474 475 if (descriptor == Callback::Refresh) { 476 hasPendingInvalidate = mHasPendingInvalidate; 477 if (hasPendingInvalidate) { 478 for (auto& displayPair : mDisplays) { 479 displayIds.emplace_back(displayPair.first); 480 } 481 } 482 mHasPendingInvalidate = false; 483 } else if (descriptor == Callback::Vsync) { 484 for (auto pending : mPendingVsyncs) { 485 auto hwc1DisplayId = pending.first; 486 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { 487 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", 488 hwc1DisplayId); 489 continue; 490 } 491 auto displayId = mHwc1DisplayMap[hwc1DisplayId]; 492 auto timestamp = pending.second; 493 pendingVsyncs.emplace_back(displayId, timestamp); 494 } 495 mPendingVsyncs.clear(); 496 } else if (descriptor == Callback::Hotplug) { 497 // Hotplug the primary display 498 pendingHotplugs.emplace_back(mHwc1DisplayMap[HWC_DISPLAY_PRIMARY], 499 static_cast<int32_t>(Connection::Connected)); 500 501 for (auto pending : mPendingHotplugs) { 502 auto hwc1DisplayId = pending.first; 503 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { 504 ALOGE("hwc1Hotplug: Couldn't find display for HWC1 id %d", 505 hwc1DisplayId); 506 continue; 507 } 508 auto displayId = mHwc1DisplayMap[hwc1DisplayId]; 509 auto connected = pending.second; 510 pendingHotplugs.emplace_back(displayId, connected); 511 } 512 } 513 514 // Call pending callbacks without the state lock held 515 lock.unlock(); 516 517 if (hasPendingInvalidate) { 518 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(pointer); 519 for (auto displayId : displayIds) { 520 refresh(callbackData, displayId); 521 } 522 } 523 if (!pendingVsyncs.empty()) { 524 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(pointer); 525 for (auto& pendingVsync : pendingVsyncs) { 526 vsync(callbackData, pendingVsync.first, pendingVsync.second); 527 } 528 } 529 if (!pendingHotplugs.empty()) { 530 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(pointer); 531 for (auto& pendingHotplug : pendingHotplugs) { 532 hotplug(callbackData, pendingHotplug.first, pendingHotplug.second); 533 } 534 } 535 return Error::None; 536} 537 538// Display functions 539 540std::atomic<hwc2_display_t> HWC2On1Adapter::Display::sNextId(1); 541 542HWC2On1Adapter::Display::Display(HWC2On1Adapter& device, HWC2::DisplayType type) 543 : mId(sNextId++), 544 mDevice(device), 545 mDirtyCount(0), 546 mStateMutex(), 547 mZIsDirty(false), 548 mHwc1RequestedContents(nullptr), 549 mHwc1ReceivedContents(nullptr), 550 mRetireFence(), 551 mChanges(), 552 mHwc1Id(-1), 553 mConfigs(), 554 mActiveConfig(nullptr), 555 mName(), 556 mType(type), 557 mPowerMode(PowerMode::Off), 558 mVsyncEnabled(Vsync::Invalid), 559 mClientTarget(), 560 mOutputBuffer(), 561 mHasColorTransform(false), 562 mLayers(), 563 mHwc1LayerMap() {} 564 565Error HWC2On1Adapter::Display::acceptChanges() 566{ 567 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 568 569 if (!mChanges) { 570 ALOGV("[%" PRIu64 "] acceptChanges failed, not validated", mId); 571 return Error::NotValidated; 572 } 573 574 ALOGV("[%" PRIu64 "] acceptChanges", mId); 575 576 for (auto& change : mChanges->getTypeChanges()) { 577 auto layerId = change.first; 578 auto type = change.second; 579 auto layer = mDevice.mLayers[layerId]; 580 layer->setCompositionType(type); 581 } 582 583 mChanges->clearTypeChanges(); 584 585 mHwc1RequestedContents = std::move(mHwc1ReceivedContents); 586 587 return Error::None; 588} 589 590Error HWC2On1Adapter::Display::createLayer(hwc2_layer_t* outLayerId) 591{ 592 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 593 594 auto layer = *mLayers.emplace(std::make_shared<Layer>(*this)); 595 mDevice.mLayers.emplace(std::make_pair(layer->getId(), layer)); 596 *outLayerId = layer->getId(); 597 ALOGV("[%" PRIu64 "] created layer %" PRIu64, mId, *outLayerId); 598 return Error::None; 599} 600 601Error HWC2On1Adapter::Display::destroyLayer(hwc2_layer_t layerId) 602{ 603 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 604 605 const auto mapLayer = mDevice.mLayers.find(layerId); 606 if (mapLayer == mDevice.mLayers.end()) { 607 ALOGV("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer", 608 mId, layerId); 609 return Error::BadLayer; 610 } 611 const auto layer = mapLayer->second; 612 mDevice.mLayers.erase(mapLayer); 613 const auto zRange = mLayers.equal_range(layer); 614 for (auto current = zRange.first; current != zRange.second; ++current) { 615 if (**current == *layer) { 616 current = mLayers.erase(current); 617 break; 618 } 619 } 620 ALOGV("[%" PRIu64 "] destroyed layer %" PRIu64, mId, layerId); 621 return Error::None; 622} 623 624Error HWC2On1Adapter::Display::getActiveConfig(hwc2_config_t* outConfig) 625{ 626 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 627 628 if (!mActiveConfig) { 629 ALOGV("[%" PRIu64 "] getActiveConfig --> %s", mId, 630 to_string(Error::BadConfig).c_str()); 631 return Error::BadConfig; 632 } 633 auto configId = mActiveConfig->getId(); 634 ALOGV("[%" PRIu64 "] getActiveConfig --> %u", mId, configId); 635 *outConfig = configId; 636 return Error::None; 637} 638 639Error HWC2On1Adapter::Display::getAttribute(hwc2_config_t configId, 640 Attribute attribute, int32_t* outValue) 641{ 642 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 643 644 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) { 645 ALOGV("[%" PRIu64 "] getAttribute failed: bad config (%u)", mId, 646 configId); 647 return Error::BadConfig; 648 } 649 *outValue = mConfigs[configId]->getAttribute(attribute); 650 ALOGV("[%" PRIu64 "] getAttribute(%u, %s) --> %d", mId, configId, 651 to_string(attribute).c_str(), *outValue); 652 return Error::None; 653} 654 655Error HWC2On1Adapter::Display::getChangedCompositionTypes( 656 uint32_t* outNumElements, hwc2_layer_t* outLayers, int32_t* outTypes) 657{ 658 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 659 660 if (!mChanges) { 661 ALOGE("[%" PRIu64 "] getChangedCompositionTypes failed: not validated", 662 mId); 663 return Error::NotValidated; 664 } 665 666 if ((outLayers == nullptr) || (outTypes == nullptr)) { 667 *outNumElements = mChanges->getTypeChanges().size(); 668 return Error::None; 669 } 670 671 uint32_t numWritten = 0; 672 for (const auto& element : mChanges->getTypeChanges()) { 673 if (numWritten == *outNumElements) { 674 break; 675 } 676 auto layerId = element.first; 677 auto intType = static_cast<int32_t>(element.second); 678 ALOGV("Adding %" PRIu64 " %s", layerId, 679 to_string(element.second).c_str()); 680 outLayers[numWritten] = layerId; 681 outTypes[numWritten] = intType; 682 ++numWritten; 683 } 684 *outNumElements = numWritten; 685 686 return Error::None; 687} 688 689Error HWC2On1Adapter::Display::getConfigs(uint32_t* outNumConfigs, 690 hwc2_config_t* outConfigs) 691{ 692 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 693 694 if (!outConfigs) { 695 *outNumConfigs = mConfigs.size(); 696 return Error::None; 697 } 698 uint32_t numWritten = 0; 699 for (const auto& config : mConfigs) { 700 if (numWritten == *outNumConfigs) { 701 break; 702 } 703 outConfigs[numWritten] = config->getId(); 704 ++numWritten; 705 } 706 *outNumConfigs = numWritten; 707 return Error::None; 708} 709 710Error HWC2On1Adapter::Display::getDozeSupport(int32_t* outSupport) 711{ 712 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 713 714 if (mDevice.mHwc1MinorVersion < 4 || mHwc1Id != 0) { 715 *outSupport = 0; 716 } else { 717 *outSupport = 1; 718 } 719 return Error::None; 720} 721 722Error HWC2On1Adapter::Display::getHdrCapabilities(uint32_t* outNumTypes, 723 int32_t* /*outTypes*/, float* /*outMaxLuminance*/, 724 float* /*outMaxAverageLuminance*/, float* /*outMinLuminance*/) 725{ 726 // This isn't supported on HWC1, so per the HWC2 header, return numTypes = 0 727 *outNumTypes = 0; 728 return Error::None; 729} 730 731Error HWC2On1Adapter::Display::getName(uint32_t* outSize, char* outName) 732{ 733 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 734 735 if (!outName) { 736 *outSize = mName.size(); 737 return Error::None; 738 } 739 auto numCopied = mName.copy(outName, *outSize); 740 *outSize = numCopied; 741 return Error::None; 742} 743 744Error HWC2On1Adapter::Display::getReleaseFences(uint32_t* outNumElements, 745 hwc2_layer_t* outLayers, int32_t* outFences) 746{ 747 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 748 749 uint32_t numWritten = 0; 750 bool outputsNonNull = (outLayers != nullptr) && (outFences != nullptr); 751 for (const auto& layer : mLayers) { 752 if (outputsNonNull && (numWritten == *outNumElements)) { 753 break; 754 } 755 756 auto releaseFence = layer->getReleaseFence(); 757 if (releaseFence != Fence::NO_FENCE) { 758 if (outputsNonNull) { 759 outLayers[numWritten] = layer->getId(); 760 outFences[numWritten] = releaseFence->dup(); 761 } 762 ++numWritten; 763 } 764 } 765 *outNumElements = numWritten; 766 767 return Error::None; 768} 769 770Error HWC2On1Adapter::Display::getRequests(int32_t* outDisplayRequests, 771 uint32_t* outNumElements, hwc2_layer_t* outLayers, 772 int32_t* outLayerRequests) 773{ 774 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 775 776 if (!mChanges) { 777 return Error::NotValidated; 778 } 779 780 if (outLayers == nullptr || outLayerRequests == nullptr) { 781 *outNumElements = mChanges->getNumLayerRequests(); 782 return Error::None; 783 } 784 785 *outDisplayRequests = mChanges->getDisplayRequests(); 786 uint32_t numWritten = 0; 787 for (const auto& request : mChanges->getLayerRequests()) { 788 if (numWritten == *outNumElements) { 789 break; 790 } 791 outLayers[numWritten] = request.first; 792 outLayerRequests[numWritten] = static_cast<int32_t>(request.second); 793 ++numWritten; 794 } 795 796 return Error::None; 797} 798 799Error HWC2On1Adapter::Display::getType(int32_t* outType) 800{ 801 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 802 803 *outType = static_cast<int32_t>(mType); 804 return Error::None; 805} 806 807Error HWC2On1Adapter::Display::present(int32_t* outRetireFence) 808{ 809 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 810 811 if (mChanges) { 812 Error error = mDevice.setAllDisplays(); 813 if (error != Error::None) { 814 ALOGE("[%" PRIu64 "] present: setAllDisplaysFailed (%s)", mId, 815 to_string(error).c_str()); 816 return error; 817 } 818 } 819 820 *outRetireFence = mRetireFence.get()->dup(); 821 ALOGV("[%" PRIu64 "] present returning retire fence %d", mId, 822 *outRetireFence); 823 824 return Error::None; 825} 826 827Error HWC2On1Adapter::Display::setActiveConfig(hwc2_config_t configId) 828{ 829 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 830 831 auto config = getConfig(configId); 832 if (!config) { 833 return Error::BadConfig; 834 } 835 mActiveConfig = config; 836 if (mDevice.mHwc1MinorVersion >= 4) { 837 int error = mDevice.mHwc1Device->setActiveConfig(mDevice.mHwc1Device, 838 mHwc1Id, static_cast<int>(configId)); 839 ALOGE_IF(error != 0, 840 "setActiveConfig: Failed to set active config on HWC1 (%d)", 841 error); 842 } 843 return Error::None; 844} 845 846Error HWC2On1Adapter::Display::setClientTarget(buffer_handle_t target, 847 int32_t acquireFence, int32_t /*dataspace*/) 848{ 849 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 850 851 ALOGV("[%" PRIu64 "] setClientTarget(%p, %d)", mId, target, acquireFence); 852 mClientTarget.setBuffer(target); 853 mClientTarget.setFence(acquireFence); 854 // dataspace can't be used by HWC1, so ignore it 855 return Error::None; 856} 857 858Error HWC2On1Adapter::Display::setColorTransform(android_color_transform_t hint) 859{ 860 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 861 862 ALOGV("%" PRIu64 "] setColorTransform(%d)", mId, 863 static_cast<int32_t>(hint)); 864 mHasColorTransform = (hint != HAL_COLOR_TRANSFORM_IDENTITY); 865 return Error::None; 866} 867 868Error HWC2On1Adapter::Display::setOutputBuffer(buffer_handle_t buffer, 869 int32_t releaseFence) 870{ 871 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 872 873 ALOGV("[%" PRIu64 "] setOutputBuffer(%p, %d)", mId, buffer, releaseFence); 874 mOutputBuffer.setBuffer(buffer); 875 mOutputBuffer.setFence(releaseFence); 876 return Error::None; 877} 878 879static bool isValid(PowerMode mode) 880{ 881 switch (mode) { 882 case PowerMode::Off: // Fall-through 883 case PowerMode::DozeSuspend: // Fall-through 884 case PowerMode::Doze: // Fall-through 885 case PowerMode::On: return true; 886 default: return false; 887 } 888} 889 890static int getHwc1PowerMode(PowerMode mode) 891{ 892 switch (mode) { 893 case PowerMode::Off: return HWC_POWER_MODE_OFF; 894 case PowerMode::DozeSuspend: return HWC_POWER_MODE_DOZE_SUSPEND; 895 case PowerMode::Doze: return HWC_POWER_MODE_DOZE; 896 case PowerMode::On: return HWC_POWER_MODE_NORMAL; 897 default: return HWC_POWER_MODE_OFF; 898 } 899} 900 901Error HWC2On1Adapter::Display::setPowerMode(PowerMode mode) 902{ 903 if (!isValid(mode)) { 904 return Error::BadParameter; 905 } 906 if (mode == mPowerMode) { 907 return Error::None; 908 } 909 910 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 911 912 int error = 0; 913 if (mDevice.mHwc1MinorVersion < 4) { 914 error = mDevice.mHwc1Device->blank(mDevice.mHwc1Device, mHwc1Id, 915 mode == PowerMode::Off); 916 } else { 917 error = mDevice.mHwc1Device->setPowerMode(mDevice.mHwc1Device, 918 mHwc1Id, getHwc1PowerMode(mode)); 919 } 920 ALOGE_IF(error != 0, "setPowerMode: Failed to set power mode on HWC1 (%d)", 921 error); 922 923 ALOGV("[%" PRIu64 "] setPowerMode(%s)", mId, to_string(mode).c_str()); 924 mPowerMode = mode; 925 return Error::None; 926} 927 928static bool isValid(Vsync enable) { 929 switch (enable) { 930 case Vsync::Enable: // Fall-through 931 case Vsync::Disable: return true; 932 default: return false; 933 } 934} 935 936Error HWC2On1Adapter::Display::setVsyncEnabled(Vsync enable) 937{ 938 if (!isValid(enable)) { 939 return Error::BadParameter; 940 } 941 if (enable == mVsyncEnabled) { 942 return Error::None; 943 } 944 945 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 946 947 int error = mDevice.mHwc1Device->eventControl(mDevice.mHwc1Device, 948 mHwc1Id, HWC_EVENT_VSYNC, enable == Vsync::Enable); 949 ALOGE_IF(error != 0, "setVsyncEnabled: Failed to set vsync on HWC1 (%d)", 950 error); 951 952 mVsyncEnabled = enable; 953 return Error::None; 954} 955 956Error HWC2On1Adapter::Display::validate(uint32_t* outNumTypes, 957 uint32_t* outNumRequests) 958{ 959 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 960 961 ALOGV("[%" PRIu64 "] Entering validate", mId); 962 963 if (!mChanges) { 964 if (!mDevice.prepareAllDisplays()) { 965 return Error::BadDisplay; 966 } 967 } 968 969 *outNumTypes = mChanges->getNumTypes(); 970 *outNumRequests = mChanges->getNumLayerRequests(); 971 ALOGV("[%" PRIu64 "] validate --> %u types, %u requests", mId, *outNumTypes, 972 *outNumRequests); 973 for (auto request : mChanges->getTypeChanges()) { 974 ALOGV("Layer %" PRIu64 " --> %s", request.first, 975 to_string(request.second).c_str()); 976 } 977 return *outNumTypes > 0 ? Error::HasChanges : Error::None; 978} 979 980// Display helpers 981 982Error HWC2On1Adapter::Display::updateLayerZ(hwc2_layer_t layerId, uint32_t z) 983{ 984 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 985 986 const auto mapLayer = mDevice.mLayers.find(layerId); 987 if (mapLayer == mDevice.mLayers.end()) { 988 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer", mId); 989 return Error::BadLayer; 990 } 991 992 const auto layer = mapLayer->second; 993 const auto zRange = mLayers.equal_range(layer); 994 bool layerOnDisplay = false; 995 for (auto current = zRange.first; current != zRange.second; ++current) { 996 if (**current == *layer) { 997 if ((*current)->getZ() == z) { 998 // Don't change anything if the Z hasn't changed 999 return Error::None; 1000 } 1001 current = mLayers.erase(current); 1002 layerOnDisplay = true; 1003 break; 1004 } 1005 } 1006 1007 if (!layerOnDisplay) { 1008 ALOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display", 1009 mId); 1010 return Error::BadLayer; 1011 } 1012 1013 layer->setZ(z); 1014 mLayers.emplace(std::move(layer)); 1015 mZIsDirty = true; 1016 1017 return Error::None; 1018} 1019 1020static constexpr uint32_t ATTRIBUTES[] = { 1021 HWC_DISPLAY_VSYNC_PERIOD, 1022 HWC_DISPLAY_WIDTH, 1023 HWC_DISPLAY_HEIGHT, 1024 HWC_DISPLAY_DPI_X, 1025 HWC_DISPLAY_DPI_Y, 1026 HWC_DISPLAY_NO_ATTRIBUTE, 1027}; 1028static constexpr size_t NUM_ATTRIBUTES = sizeof(ATTRIBUTES) / sizeof(uint32_t); 1029 1030static constexpr uint32_t ATTRIBUTE_MAP[] = { 1031 5, // HWC_DISPLAY_NO_ATTRIBUTE = 0 1032 0, // HWC_DISPLAY_VSYNC_PERIOD = 1, 1033 1, // HWC_DISPLAY_WIDTH = 2, 1034 2, // HWC_DISPLAY_HEIGHT = 3, 1035 3, // HWC_DISPLAY_DPI_X = 4, 1036 4, // HWC_DISPLAY_DPI_Y = 5, 1037}; 1038 1039template <uint32_t attribute> 1040static constexpr bool attributesMatch() 1041{ 1042 return ATTRIBUTES[ATTRIBUTE_MAP[attribute]] == attribute; 1043} 1044static_assert(attributesMatch<HWC_DISPLAY_VSYNC_PERIOD>(), 1045 "Tables out of sync"); 1046static_assert(attributesMatch<HWC_DISPLAY_WIDTH>(), "Tables out of sync"); 1047static_assert(attributesMatch<HWC_DISPLAY_HEIGHT>(), "Tables out of sync"); 1048static_assert(attributesMatch<HWC_DISPLAY_DPI_X>(), "Tables out of sync"); 1049static_assert(attributesMatch<HWC_DISPLAY_DPI_Y>(), "Tables out of sync"); 1050 1051void HWC2On1Adapter::Display::populateConfigs() 1052{ 1053 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1054 1055 ALOGV("[%" PRIu64 "] populateConfigs", mId); 1056 1057 if (mHwc1Id == -1) { 1058 ALOGE("populateConfigs: HWC1 ID not set"); 1059 return; 1060 } 1061 1062 const size_t MAX_NUM_CONFIGS = 128; 1063 uint32_t configs[MAX_NUM_CONFIGS] = {}; 1064 size_t numConfigs = MAX_NUM_CONFIGS; 1065 mDevice.mHwc1Device->getDisplayConfigs(mDevice.mHwc1Device, mHwc1Id, 1066 configs, &numConfigs); 1067 1068 for (size_t c = 0; c < numConfigs; ++c) { 1069 uint32_t hwc1ConfigId = configs[c]; 1070 hwc2_config_t id = static_cast<hwc2_config_t>(mConfigs.size()); 1071 mConfigs.emplace_back( 1072 std::make_shared<Config>(*this, id, hwc1ConfigId)); 1073 auto& config = mConfigs[id]; 1074 1075 int32_t values[NUM_ATTRIBUTES] = {}; 1076 mDevice.mHwc1Device->getDisplayAttributes(mDevice.mHwc1Device, mHwc1Id, 1077 hwc1ConfigId, ATTRIBUTES, values); 1078 1079 config->setAttribute(Attribute::VsyncPeriod, 1080 values[ATTRIBUTE_MAP[HWC_DISPLAY_VSYNC_PERIOD]]); 1081 config->setAttribute(Attribute::Width, 1082 values[ATTRIBUTE_MAP[HWC_DISPLAY_WIDTH]]); 1083 config->setAttribute(Attribute::Height, 1084 values[ATTRIBUTE_MAP[HWC_DISPLAY_HEIGHT]]); 1085 config->setAttribute(Attribute::DpiX, 1086 values[ATTRIBUTE_MAP[HWC_DISPLAY_DPI_X]]); 1087 config->setAttribute(Attribute::DpiY, 1088 values[ATTRIBUTE_MAP[HWC_DISPLAY_DPI_Y]]); 1089 1090 ALOGV("Found config: %s", config->toString().c_str()); 1091 } 1092 1093 ALOGV("Getting active config"); 1094 if (mDevice.mHwc1Device->getActiveConfig != nullptr) { 1095 auto activeConfig = mDevice.mHwc1Device->getActiveConfig( 1096 mDevice.mHwc1Device, mHwc1Id); 1097 if (activeConfig >= 0) { 1098 ALOGV("Setting active config to %d", activeConfig); 1099 mActiveConfig = mConfigs[activeConfig]; 1100 } 1101 } else { 1102 ALOGV("getActiveConfig is null, choosing config 0"); 1103 mActiveConfig = mConfigs[0]; 1104 } 1105} 1106 1107void HWC2On1Adapter::Display::populateConfigs(uint32_t width, uint32_t height) 1108{ 1109 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1110 1111 mConfigs.emplace_back(std::make_shared<Config>(*this, 0, 0)); 1112 auto& config = mConfigs[0]; 1113 1114 config->setAttribute(Attribute::Width, static_cast<int32_t>(width)); 1115 config->setAttribute(Attribute::Height, static_cast<int32_t>(height)); 1116 mActiveConfig = config; 1117} 1118 1119bool HWC2On1Adapter::Display::prepare() 1120{ 1121 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1122 1123 // Only prepare display contents for displays HWC1 knows about 1124 if (mHwc1Id == -1) { 1125 return true; 1126 } 1127 1128 // It doesn't make sense to prepare a display for which there is no active 1129 // config, so return early 1130 if (!mActiveConfig) { 1131 ALOGE("[%" PRIu64 "] Attempted to prepare, but no config active", mId); 1132 return false; 1133 } 1134 1135 ALOGV("[%" PRIu64 "] Entering prepare", mId); 1136 1137 auto currentCount = mHwc1RequestedContents ? 1138 mHwc1RequestedContents->numHwLayers : 0; 1139 auto requiredCount = mLayers.size() + 1; 1140 ALOGV("[%" PRIu64 "] Requires %zd layers, %zd allocated in %p", mId, 1141 requiredCount, currentCount, mHwc1RequestedContents.get()); 1142 1143 bool layerCountChanged = (currentCount != requiredCount); 1144 if (layerCountChanged) { 1145 reallocateHwc1Contents(); 1146 } 1147 1148 bool applyAllState = false; 1149 if (layerCountChanged || mZIsDirty) { 1150 assignHwc1LayerIds(); 1151 mZIsDirty = false; 1152 applyAllState = true; 1153 } 1154 1155 mHwc1RequestedContents->retireFenceFd = -1; 1156 mHwc1RequestedContents->flags = 0; 1157 if (isDirty() || applyAllState) { 1158 mHwc1RequestedContents->flags |= HWC_GEOMETRY_CHANGED; 1159 } 1160 1161 for (auto& layer : mLayers) { 1162 auto& hwc1Layer = mHwc1RequestedContents->hwLayers[layer->getHwc1Id()]; 1163 hwc1Layer.releaseFenceFd = -1; 1164 layer->applyState(hwc1Layer, applyAllState); 1165 } 1166 1167 mHwc1RequestedContents->outbuf = mOutputBuffer.getBuffer(); 1168 mHwc1RequestedContents->outbufAcquireFenceFd = mOutputBuffer.getFence(); 1169 1170 prepareFramebufferTarget(); 1171 1172 return true; 1173} 1174 1175static void cloneHWCRegion(hwc_region_t& region) 1176{ 1177 auto size = sizeof(hwc_rect_t) * region.numRects; 1178 auto newRects = static_cast<hwc_rect_t*>(std::malloc(size)); 1179 std::copy_n(region.rects, region.numRects, newRects); 1180 region.rects = newRects; 1181} 1182 1183HWC2On1Adapter::Display::HWC1Contents 1184 HWC2On1Adapter::Display::cloneRequestedContents() const 1185{ 1186 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1187 1188 size_t size = sizeof(hwc_display_contents_1_t) + 1189 sizeof(hwc_layer_1_t) * (mHwc1RequestedContents->numHwLayers); 1190 auto contents = static_cast<hwc_display_contents_1_t*>(std::malloc(size)); 1191 std::memcpy(contents, mHwc1RequestedContents.get(), size); 1192 for (size_t layerId = 0; layerId < contents->numHwLayers; ++layerId) { 1193 auto& layer = contents->hwLayers[layerId]; 1194 // Deep copy the regions to avoid double-frees 1195 cloneHWCRegion(layer.visibleRegionScreen); 1196 cloneHWCRegion(layer.surfaceDamage); 1197 } 1198 return HWC1Contents(contents); 1199} 1200 1201void HWC2On1Adapter::Display::setReceivedContents(HWC1Contents contents) 1202{ 1203 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1204 1205 mHwc1ReceivedContents = std::move(contents); 1206 1207 mChanges.reset(new Changes); 1208 1209 size_t numLayers = mHwc1ReceivedContents->numHwLayers; 1210 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) { 1211 const auto& receivedLayer = mHwc1ReceivedContents->hwLayers[hwc1Id]; 1212 if (mHwc1LayerMap.count(hwc1Id) == 0) { 1213 ALOGE_IF(receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET, 1214 "setReceivedContents: HWC1 layer %zd doesn't have a" 1215 " matching HWC2 layer, and isn't the framebuffer target", 1216 hwc1Id); 1217 continue; 1218 } 1219 1220 Layer& layer = *mHwc1LayerMap[hwc1Id]; 1221 updateTypeChanges(receivedLayer, layer); 1222 updateLayerRequests(receivedLayer, layer); 1223 } 1224} 1225 1226bool HWC2On1Adapter::Display::hasChanges() const 1227{ 1228 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1229 return mChanges != nullptr; 1230} 1231 1232Error HWC2On1Adapter::Display::set(hwc_display_contents_1& hwcContents) 1233{ 1234 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1235 1236 if (!mChanges || (mChanges->getNumTypes() > 0)) { 1237 ALOGE("[%" PRIu64 "] set failed: not validated", mId); 1238 return Error::NotValidated; 1239 } 1240 1241 // Set up the client/framebuffer target 1242 auto numLayers = hwcContents.numHwLayers; 1243 1244 // Close acquire fences on FRAMEBUFFER layers, since they will not be used 1245 // by HWC 1246 for (size_t l = 0; l < numLayers - 1; ++l) { 1247 auto& layer = hwcContents.hwLayers[l]; 1248 if (layer.compositionType == HWC_FRAMEBUFFER) { 1249 ALOGV("Closing fence %d for layer %zd", layer.acquireFenceFd, l); 1250 close(layer.acquireFenceFd); 1251 layer.acquireFenceFd = -1; 1252 } 1253 } 1254 1255 auto& clientTargetLayer = hwcContents.hwLayers[numLayers - 1]; 1256 if (clientTargetLayer.compositionType == HWC_FRAMEBUFFER_TARGET) { 1257 clientTargetLayer.handle = mClientTarget.getBuffer(); 1258 clientTargetLayer.acquireFenceFd = mClientTarget.getFence(); 1259 } else { 1260 ALOGE("[%" PRIu64 "] set: last HWC layer wasn't FRAMEBUFFER_TARGET", 1261 mId); 1262 } 1263 1264 mChanges.reset(); 1265 1266 return Error::None; 1267} 1268 1269void HWC2On1Adapter::Display::addRetireFence(int fenceFd) 1270{ 1271 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1272 mRetireFence.add(fenceFd); 1273} 1274 1275void HWC2On1Adapter::Display::addReleaseFences( 1276 const hwc_display_contents_1_t& hwcContents) 1277{ 1278 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1279 1280 size_t numLayers = hwcContents.numHwLayers; 1281 for (size_t hwc1Id = 0; hwc1Id < numLayers; ++hwc1Id) { 1282 const auto& receivedLayer = hwcContents.hwLayers[hwc1Id]; 1283 if (mHwc1LayerMap.count(hwc1Id) == 0) { 1284 if (receivedLayer.compositionType != HWC_FRAMEBUFFER_TARGET) { 1285 ALOGE("addReleaseFences: HWC1 layer %zd doesn't have a" 1286 " matching HWC2 layer, and isn't the framebuffer" 1287 " target", hwc1Id); 1288 } 1289 // Close the framebuffer target release fence since we will use the 1290 // display retire fence instead 1291 if (receivedLayer.releaseFenceFd != -1) { 1292 close(receivedLayer.releaseFenceFd); 1293 } 1294 continue; 1295 } 1296 1297 Layer& layer = *mHwc1LayerMap[hwc1Id]; 1298 ALOGV("Adding release fence %d to layer %" PRIu64, 1299 receivedLayer.releaseFenceFd, layer.getId()); 1300 layer.addReleaseFence(receivedLayer.releaseFenceFd); 1301 } 1302} 1303 1304bool HWC2On1Adapter::Display::hasColorTransform() const 1305{ 1306 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1307 return mHasColorTransform; 1308} 1309 1310static std::string hwc1CompositionString(int32_t type) 1311{ 1312 switch (type) { 1313 case HWC_FRAMEBUFFER: return "Framebuffer"; 1314 case HWC_OVERLAY: return "Overlay"; 1315 case HWC_BACKGROUND: return "Background"; 1316 case HWC_FRAMEBUFFER_TARGET: return "FramebufferTarget"; 1317 case HWC_SIDEBAND: return "Sideband"; 1318 case HWC_CURSOR_OVERLAY: return "CursorOverlay"; 1319 default: 1320 return std::string("Unknown (") + std::to_string(type) + ")"; 1321 } 1322} 1323 1324static std::string hwc1TransformString(int32_t transform) 1325{ 1326 switch (transform) { 1327 case 0: return "None"; 1328 case HWC_TRANSFORM_FLIP_H: return "FlipH"; 1329 case HWC_TRANSFORM_FLIP_V: return "FlipV"; 1330 case HWC_TRANSFORM_ROT_90: return "Rotate90"; 1331 case HWC_TRANSFORM_ROT_180: return "Rotate180"; 1332 case HWC_TRANSFORM_ROT_270: return "Rotate270"; 1333 case HWC_TRANSFORM_FLIP_H_ROT_90: return "FlipHRotate90"; 1334 case HWC_TRANSFORM_FLIP_V_ROT_90: return "FlipVRotate90"; 1335 default: 1336 return std::string("Unknown (") + std::to_string(transform) + ")"; 1337 } 1338} 1339 1340static std::string hwc1BlendModeString(int32_t mode) 1341{ 1342 switch (mode) { 1343 case HWC_BLENDING_NONE: return "None"; 1344 case HWC_BLENDING_PREMULT: return "Premultiplied"; 1345 case HWC_BLENDING_COVERAGE: return "Coverage"; 1346 default: 1347 return std::string("Unknown (") + std::to_string(mode) + ")"; 1348 } 1349} 1350 1351static std::string rectString(hwc_rect_t rect) 1352{ 1353 std::stringstream output; 1354 output << "[" << rect.left << ", " << rect.top << ", "; 1355 output << rect.right << ", " << rect.bottom << "]"; 1356 return output.str(); 1357} 1358 1359static std::string approximateFloatString(float f) 1360{ 1361 if (static_cast<int32_t>(f) == f) { 1362 return std::to_string(static_cast<int32_t>(f)); 1363 } 1364 int32_t truncated = static_cast<int32_t>(f * 10); 1365 bool approximate = (static_cast<float>(truncated) != f * 10); 1366 const size_t BUFFER_SIZE = 32; 1367 char buffer[BUFFER_SIZE] = {}; 1368 auto bytesWritten = snprintf(buffer, BUFFER_SIZE, 1369 "%s%.1f", approximate ? "~" : "", f); 1370 return std::string(buffer, bytesWritten); 1371} 1372 1373static std::string frectString(hwc_frect_t frect) 1374{ 1375 std::stringstream output; 1376 output << "[" << approximateFloatString(frect.left) << ", "; 1377 output << approximateFloatString(frect.top) << ", "; 1378 output << approximateFloatString(frect.right) << ", "; 1379 output << approximateFloatString(frect.bottom) << "]"; 1380 return output.str(); 1381} 1382 1383static std::string colorString(hwc_color_t color) 1384{ 1385 std::stringstream output; 1386 output << "RGBA ["; 1387 output << static_cast<int32_t>(color.r) << ", "; 1388 output << static_cast<int32_t>(color.g) << ", "; 1389 output << static_cast<int32_t>(color.b) << ", "; 1390 output << static_cast<int32_t>(color.a) << "]"; 1391 return output.str(); 1392} 1393 1394static std::string alphaString(float f) 1395{ 1396 const size_t BUFFER_SIZE = 8; 1397 char buffer[BUFFER_SIZE] = {}; 1398 auto bytesWritten = snprintf(buffer, BUFFER_SIZE, "%.3f", f); 1399 return std::string(buffer, bytesWritten); 1400} 1401 1402static std::string to_string(const hwc_layer_1_t& hwcLayer, 1403 int32_t hwc1MinorVersion) 1404{ 1405 const char* fill = " "; 1406 1407 std::stringstream output; 1408 1409 output << " Composition: " << 1410 hwc1CompositionString(hwcLayer.compositionType); 1411 1412 if (hwcLayer.compositionType == HWC_BACKGROUND) { 1413 output << " Color: " << colorString(hwcLayer.backgroundColor) << '\n'; 1414 } else if (hwcLayer.compositionType == HWC_SIDEBAND) { 1415 output << " Stream: " << hwcLayer.sidebandStream << '\n'; 1416 } else { 1417 output << " Buffer: " << hwcLayer.handle << "/" << 1418 hwcLayer.acquireFenceFd << '\n'; 1419 } 1420 1421 output << fill << "Display frame: " << rectString(hwcLayer.displayFrame) << 1422 '\n'; 1423 1424 output << fill << "Source crop: "; 1425 if (hwc1MinorVersion >= 3) { 1426 output << frectString(hwcLayer.sourceCropf) << '\n'; 1427 } else { 1428 output << rectString(hwcLayer.sourceCropi) << '\n'; 1429 } 1430 1431 output << fill << "Transform: " << hwc1TransformString(hwcLayer.transform); 1432 output << " Blend mode: " << hwc1BlendModeString(hwcLayer.blending); 1433 if (hwcLayer.planeAlpha != 0xFF) { 1434 output << " Alpha: " << alphaString(hwcLayer.planeAlpha / 255.0f); 1435 } 1436 output << '\n'; 1437 1438 if (hwcLayer.hints != 0) { 1439 output << fill << "Hints:"; 1440 if ((hwcLayer.hints & HWC_HINT_TRIPLE_BUFFER) != 0) { 1441 output << " TripleBuffer"; 1442 } 1443 if ((hwcLayer.hints & HWC_HINT_CLEAR_FB) != 0) { 1444 output << " ClearFB"; 1445 } 1446 output << '\n'; 1447 } 1448 1449 if (hwcLayer.flags != 0) { 1450 output << fill << "Flags:"; 1451 if ((hwcLayer.flags & HWC_SKIP_LAYER) != 0) { 1452 output << " SkipLayer"; 1453 } 1454 if ((hwcLayer.flags & HWC_IS_CURSOR_LAYER) != 0) { 1455 output << " IsCursorLayer"; 1456 } 1457 output << '\n'; 1458 } 1459 1460 return output.str(); 1461} 1462 1463static std::string to_string(const hwc_display_contents_1_t& hwcContents, 1464 int32_t hwc1MinorVersion) 1465{ 1466 const char* fill = " "; 1467 1468 std::stringstream output; 1469 output << fill << "Geometry changed: " << 1470 ((hwcContents.flags & HWC_GEOMETRY_CHANGED) != 0 ? "Y\n" : "N\n"); 1471 1472 output << fill << hwcContents.numHwLayers << " Layer" << 1473 ((hwcContents.numHwLayers == 1) ? "\n" : "s\n"); 1474 for (size_t layer = 0; layer < hwcContents.numHwLayers; ++layer) { 1475 output << fill << " Layer " << layer; 1476 output << to_string(hwcContents.hwLayers[layer], hwc1MinorVersion); 1477 } 1478 1479 if (hwcContents.outbuf != nullptr) { 1480 output << fill << "Output buffer: " << hwcContents.outbuf << "/" << 1481 hwcContents.outbufAcquireFenceFd << '\n'; 1482 } 1483 1484 return output.str(); 1485} 1486 1487std::string HWC2On1Adapter::Display::dump() const 1488{ 1489 std::unique_lock<std::recursive_mutex> lock(mStateMutex); 1490 1491 std::stringstream output; 1492 1493 output << " Display " << mId << ": "; 1494 output << to_string(mType) << " "; 1495 output << "HWC1 ID: " << mHwc1Id << " "; 1496 output << "Power mode: " << to_string(mPowerMode) << " "; 1497 output << "Vsync: " << to_string(mVsyncEnabled) << '\n'; 1498 1499 output << " " << mConfigs.size() << " Config" << 1500 (mConfigs.size() == 1 ? "" : "s") << " (* Active)\n"; 1501 for (const auto& config : mConfigs) { 1502 if (config == mActiveConfig) { 1503 output << " * " << config->toString(); 1504 } else { 1505 output << " " << config->toString(); 1506 } 1507 } 1508 output << '\n'; 1509 1510 output << " " << mLayers.size() << " Layer" << 1511 (mLayers.size() == 1 ? "" : "s") << '\n'; 1512 for (const auto& layer : mLayers) { 1513 output << layer->dump(); 1514 } 1515 1516 output << " Client target: " << mClientTarget.getBuffer() << '\n'; 1517 1518 if (mOutputBuffer.getBuffer() != nullptr) { 1519 output << " Output buffer: " << mOutputBuffer.getBuffer() << '\n'; 1520 } 1521 1522 if (mHwc1ReceivedContents) { 1523 output << " Last received HWC1 state\n"; 1524 output << to_string(*mHwc1ReceivedContents, mDevice.mHwc1MinorVersion); 1525 } else if (mHwc1RequestedContents) { 1526 output << " Last requested HWC1 state\n"; 1527 output << to_string(*mHwc1RequestedContents, mDevice.mHwc1MinorVersion); 1528 } 1529 1530 return output.str(); 1531} 1532 1533void HWC2On1Adapter::Display::Config::setAttribute(HWC2::Attribute attribute, 1534 int32_t value) 1535{ 1536 mAttributes[attribute] = value; 1537} 1538 1539int32_t HWC2On1Adapter::Display::Config::getAttribute(Attribute attribute) const 1540{ 1541 if (mAttributes.count(attribute) == 0) { 1542 return -1; 1543 } 1544 return mAttributes.at(attribute); 1545} 1546 1547std::string HWC2On1Adapter::Display::Config::toString() const 1548{ 1549 std::string output; 1550 1551 const size_t BUFFER_SIZE = 100; 1552 char buffer[BUFFER_SIZE] = {}; 1553 auto writtenBytes = snprintf(buffer, BUFFER_SIZE, 1554 "[%u] %u x %u", mHwcId, 1555 mAttributes.at(HWC2::Attribute::Width), 1556 mAttributes.at(HWC2::Attribute::Height)); 1557 output.append(buffer, writtenBytes); 1558 1559 if (mAttributes.count(HWC2::Attribute::VsyncPeriod) != 0) { 1560 std::memset(buffer, 0, BUFFER_SIZE); 1561 writtenBytes = snprintf(buffer, BUFFER_SIZE, " @ %.1f Hz", 1562 1e9 / mAttributes.at(HWC2::Attribute::VsyncPeriod)); 1563 output.append(buffer, writtenBytes); 1564 } 1565 1566 if (mAttributes.count(HWC2::Attribute::DpiX) != 0 && 1567 mAttributes.at(HWC2::Attribute::DpiX) != -1) { 1568 std::memset(buffer, 0, BUFFER_SIZE); 1569 writtenBytes = snprintf(buffer, BUFFER_SIZE, 1570 ", DPI: %.1f x %.1f", 1571 mAttributes.at(HWC2::Attribute::DpiX) / 1000.0f, 1572 mAttributes.at(HWC2::Attribute::DpiY) / 1000.0f); 1573 output.append(buffer, writtenBytes); 1574 } 1575 1576 return output; 1577} 1578 1579std::shared_ptr<const HWC2On1Adapter::Display::Config> 1580 HWC2On1Adapter::Display::getConfig(hwc2_config_t configId) const 1581{ 1582 if (configId > mConfigs.size() || !mConfigs[configId]->isOnDisplay(*this)) { 1583 return nullptr; 1584 } 1585 return mConfigs[configId]; 1586} 1587 1588void HWC2On1Adapter::Display::reallocateHwc1Contents() 1589{ 1590 // Allocate an additional layer for the framebuffer target 1591 auto numLayers = mLayers.size() + 1; 1592 size_t size = sizeof(hwc_display_contents_1_t) + 1593 sizeof(hwc_layer_1_t) * numLayers; 1594 ALOGV("[%" PRIu64 "] reallocateHwc1Contents creating %zd layer%s", mId, 1595 numLayers, numLayers != 1 ? "s" : ""); 1596 auto contents = 1597 static_cast<hwc_display_contents_1_t*>(std::calloc(size, 1)); 1598 contents->numHwLayers = numLayers; 1599 mHwc1RequestedContents.reset(contents); 1600} 1601 1602void HWC2On1Adapter::Display::assignHwc1LayerIds() 1603{ 1604 mHwc1LayerMap.clear(); 1605 size_t nextHwc1Id = 0; 1606 for (auto& layer : mLayers) { 1607 mHwc1LayerMap[nextHwc1Id] = layer; 1608 layer->setHwc1Id(nextHwc1Id++); 1609 } 1610} 1611 1612void HWC2On1Adapter::Display::updateTypeChanges(const hwc_layer_1_t& hwc1Layer, 1613 const Layer& layer) 1614{ 1615 auto layerId = layer.getId(); 1616 switch (hwc1Layer.compositionType) { 1617 case HWC_FRAMEBUFFER: 1618 if (layer.getCompositionType() != Composition::Client) { 1619 mChanges->addTypeChange(layerId, Composition::Client); 1620 } 1621 break; 1622 case HWC_OVERLAY: 1623 if (layer.getCompositionType() != Composition::Device) { 1624 mChanges->addTypeChange(layerId, Composition::Device); 1625 } 1626 break; 1627 case HWC_BACKGROUND: 1628 ALOGE_IF(layer.getCompositionType() != Composition::SolidColor, 1629 "updateTypeChanges: HWC1 requested BACKGROUND, but HWC2" 1630 " wasn't expecting SolidColor"); 1631 break; 1632 case HWC_FRAMEBUFFER_TARGET: 1633 // Do nothing, since it shouldn't be modified by HWC1 1634 break; 1635 case HWC_SIDEBAND: 1636 ALOGE_IF(layer.getCompositionType() != Composition::Sideband, 1637 "updateTypeChanges: HWC1 requested SIDEBAND, but HWC2" 1638 " wasn't expecting Sideband"); 1639 break; 1640 case HWC_CURSOR_OVERLAY: 1641 ALOGE_IF(layer.getCompositionType() != Composition::Cursor, 1642 "updateTypeChanges: HWC1 requested CURSOR_OVERLAY, but" 1643 " HWC2 wasn't expecting Cursor"); 1644 break; 1645 } 1646} 1647 1648void HWC2On1Adapter::Display::updateLayerRequests( 1649 const hwc_layer_1_t& hwc1Layer, const Layer& layer) 1650{ 1651 if ((hwc1Layer.hints & HWC_HINT_CLEAR_FB) != 0) { 1652 mChanges->addLayerRequest(layer.getId(), 1653 LayerRequest::ClearClientTarget); 1654 } 1655} 1656 1657void HWC2On1Adapter::Display::prepareFramebufferTarget() 1658{ 1659 // We check that mActiveConfig is valid in Display::prepare 1660 int32_t width = mActiveConfig->getAttribute(Attribute::Width); 1661 int32_t height = mActiveConfig->getAttribute(Attribute::Height); 1662 1663 auto& hwc1Target = mHwc1RequestedContents->hwLayers[mLayers.size()]; 1664 hwc1Target.compositionType = HWC_FRAMEBUFFER_TARGET; 1665 hwc1Target.releaseFenceFd = -1; 1666 hwc1Target.hints = 0; 1667 hwc1Target.flags = 0; 1668 hwc1Target.transform = 0; 1669 hwc1Target.blending = HWC_BLENDING_PREMULT; 1670 if (mDevice.getHwc1MinorVersion() < 3) { 1671 hwc1Target.sourceCropi = {0, 0, width, height}; 1672 } else { 1673 hwc1Target.sourceCropf = {0.0f, 0.0f, static_cast<float>(width), 1674 static_cast<float>(height)}; 1675 } 1676 hwc1Target.displayFrame = {0, 0, width, height}; 1677 hwc1Target.planeAlpha = 255; 1678 hwc1Target.visibleRegionScreen.numRects = 1; 1679 auto rects = static_cast<hwc_rect_t*>(std::malloc(sizeof(hwc_rect_t))); 1680 rects[0].left = 0; 1681 rects[0].top = 0; 1682 rects[0].right = width; 1683 rects[0].bottom = height; 1684 hwc1Target.visibleRegionScreen.rects = rects; 1685 1686 // We will set this to the correct value in set 1687 hwc1Target.acquireFenceFd = -1; 1688} 1689 1690// Layer functions 1691 1692std::atomic<hwc2_layer_t> HWC2On1Adapter::Layer::sNextId(1); 1693 1694HWC2On1Adapter::Layer::Layer(Display& display) 1695 : mId(sNextId++), 1696 mDisplay(display), 1697 mDirtyCount(0), 1698 mBuffer(), 1699 mSurfaceDamage(), 1700 mBlendMode(*this, BlendMode::None), 1701 mColor(*this, {0, 0, 0, 0}), 1702 mCompositionType(*this, Composition::Invalid), 1703 mDisplayFrame(*this, {0, 0, -1, -1}), 1704 mPlaneAlpha(*this, 0.0f), 1705 mSidebandStream(*this, nullptr), 1706 mSourceCrop(*this, {0.0f, 0.0f, -1.0f, -1.0f}), 1707 mTransform(*this, Transform::None), 1708 mVisibleRegion(*this, std::vector<hwc_rect_t>()), 1709 mZ(0), 1710 mReleaseFence(), 1711 mHwc1Id(0), 1712 mHasUnsupportedDataspace(false), 1713 mHasUnsupportedPlaneAlpha(false) {} 1714 1715bool HWC2On1Adapter::SortLayersByZ::operator()( 1716 const std::shared_ptr<Layer>& lhs, const std::shared_ptr<Layer>& rhs) 1717{ 1718 return lhs->getZ() < rhs->getZ(); 1719} 1720 1721Error HWC2On1Adapter::Layer::setBuffer(buffer_handle_t buffer, 1722 int32_t acquireFence) 1723{ 1724 ALOGV("Setting acquireFence to %d for layer %" PRIu64, acquireFence, mId); 1725 mBuffer.setBuffer(buffer); 1726 mBuffer.setFence(acquireFence); 1727 return Error::None; 1728} 1729 1730Error HWC2On1Adapter::Layer::setCursorPosition(int32_t x, int32_t y) 1731{ 1732 if (mCompositionType.getValue() != Composition::Cursor) { 1733 return Error::BadLayer; 1734 } 1735 1736 if (mDisplay.hasChanges()) { 1737 return Error::NotValidated; 1738 } 1739 1740 auto displayId = mDisplay.getHwc1Id(); 1741 auto hwc1Device = mDisplay.getDevice().getHwc1Device(); 1742 hwc1Device->setCursorPositionAsync(hwc1Device, displayId, x, y); 1743 return Error::None; 1744} 1745 1746Error HWC2On1Adapter::Layer::setSurfaceDamage(hwc_region_t damage) 1747{ 1748 mSurfaceDamage.resize(damage.numRects); 1749 std::copy_n(damage.rects, damage.numRects, mSurfaceDamage.begin()); 1750 return Error::None; 1751} 1752 1753// Layer state functions 1754 1755Error HWC2On1Adapter::Layer::setBlendMode(BlendMode mode) 1756{ 1757 mBlendMode.setPending(mode); 1758 return Error::None; 1759} 1760 1761Error HWC2On1Adapter::Layer::setColor(hwc_color_t color) 1762{ 1763 mColor.setPending(color); 1764 return Error::None; 1765} 1766 1767Error HWC2On1Adapter::Layer::setCompositionType(Composition type) 1768{ 1769 mCompositionType.setPending(type); 1770 return Error::None; 1771} 1772 1773Error HWC2On1Adapter::Layer::setDataspace(android_dataspace_t dataspace) 1774{ 1775 mHasUnsupportedDataspace = (dataspace != HAL_DATASPACE_UNKNOWN); 1776 return Error::None; 1777} 1778 1779Error HWC2On1Adapter::Layer::setDisplayFrame(hwc_rect_t frame) 1780{ 1781 mDisplayFrame.setPending(frame); 1782 return Error::None; 1783} 1784 1785Error HWC2On1Adapter::Layer::setPlaneAlpha(float alpha) 1786{ 1787 mPlaneAlpha.setPending(alpha); 1788 return Error::None; 1789} 1790 1791Error HWC2On1Adapter::Layer::setSidebandStream(const native_handle_t* stream) 1792{ 1793 mSidebandStream.setPending(stream); 1794 return Error::None; 1795} 1796 1797Error HWC2On1Adapter::Layer::setSourceCrop(hwc_frect_t crop) 1798{ 1799 mSourceCrop.setPending(crop); 1800 return Error::None; 1801} 1802 1803Error HWC2On1Adapter::Layer::setTransform(Transform transform) 1804{ 1805 mTransform.setPending(transform); 1806 return Error::None; 1807} 1808 1809Error HWC2On1Adapter::Layer::setVisibleRegion(hwc_region_t rawVisible) 1810{ 1811 std::vector<hwc_rect_t> visible(rawVisible.rects, 1812 rawVisible.rects + rawVisible.numRects); 1813 mVisibleRegion.setPending(std::move(visible)); 1814 return Error::None; 1815} 1816 1817Error HWC2On1Adapter::Layer::setZ(uint32_t z) 1818{ 1819 mZ = z; 1820 return Error::None; 1821} 1822 1823void HWC2On1Adapter::Layer::addReleaseFence(int fenceFd) 1824{ 1825 ALOGV("addReleaseFence %d to layer %" PRIu64, fenceFd, mId); 1826 mReleaseFence.add(fenceFd); 1827} 1828 1829const sp<Fence>& HWC2On1Adapter::Layer::getReleaseFence() const 1830{ 1831 return mReleaseFence.get(); 1832} 1833 1834void HWC2On1Adapter::Layer::applyState(hwc_layer_1_t& hwc1Layer, 1835 bool applyAllState) 1836{ 1837 applyCommonState(hwc1Layer, applyAllState); 1838 auto compositionType = mCompositionType.getPendingValue(); 1839 if (compositionType == Composition::SolidColor) { 1840 applySolidColorState(hwc1Layer, applyAllState); 1841 } else if (compositionType == Composition::Sideband) { 1842 applySidebandState(hwc1Layer, applyAllState); 1843 } else { 1844 applyBufferState(hwc1Layer); 1845 } 1846 applyCompositionType(hwc1Layer, applyAllState); 1847} 1848 1849// Layer dump helpers 1850 1851static std::string regionStrings(const std::vector<hwc_rect_t>& visibleRegion, 1852 const std::vector<hwc_rect_t>& surfaceDamage) 1853{ 1854 std::string regions; 1855 regions += " Visible Region"; 1856 regions.resize(40, ' '); 1857 regions += "Surface Damage\n"; 1858 1859 size_t numPrinted = 0; 1860 size_t maxSize = std::max(visibleRegion.size(), surfaceDamage.size()); 1861 while (numPrinted < maxSize) { 1862 std::string line(" "); 1863 if (visibleRegion.empty() && numPrinted == 0) { 1864 line += "None"; 1865 } else if (numPrinted < visibleRegion.size()) { 1866 line += rectString(visibleRegion[numPrinted]); 1867 } 1868 line.resize(40, ' '); 1869 if (surfaceDamage.empty() && numPrinted == 0) { 1870 line += "None"; 1871 } else if (numPrinted < surfaceDamage.size()) { 1872 line += rectString(surfaceDamage[numPrinted]); 1873 } 1874 line += '\n'; 1875 regions += line; 1876 ++numPrinted; 1877 } 1878 return regions; 1879} 1880 1881std::string HWC2On1Adapter::Layer::dump() const 1882{ 1883 std::stringstream output; 1884 const char* fill = " "; 1885 1886 output << fill << to_string(mCompositionType.getPendingValue()); 1887 output << " Layer HWC2/1: " << mId << "/" << mHwc1Id << " "; 1888 output << "Z: " << mZ; 1889 if (mCompositionType.getValue() == HWC2::Composition::SolidColor) { 1890 output << " " << colorString(mColor.getValue()); 1891 } else if (mCompositionType.getValue() == HWC2::Composition::Sideband) { 1892 output << " Handle: " << mSidebandStream.getValue() << '\n'; 1893 } else { 1894 output << " Buffer: " << mBuffer.getBuffer() << "/" << 1895 mBuffer.getFence() << '\n'; 1896 output << fill << " Display frame [LTRB]: " << 1897 rectString(mDisplayFrame.getValue()) << '\n'; 1898 output << fill << " Source crop: " << 1899 frectString(mSourceCrop.getValue()) << '\n'; 1900 output << fill << " Transform: " << to_string(mTransform.getValue()); 1901 output << " Blend mode: " << to_string(mBlendMode.getValue()); 1902 if (mPlaneAlpha.getValue() != 1.0f) { 1903 output << " Alpha: " << 1904 alphaString(mPlaneAlpha.getValue()) << '\n'; 1905 } else { 1906 output << '\n'; 1907 } 1908 output << regionStrings(mVisibleRegion.getValue(), mSurfaceDamage); 1909 } 1910 return output.str(); 1911} 1912 1913static int getHwc1Blending(HWC2::BlendMode blendMode) 1914{ 1915 switch (blendMode) { 1916 case BlendMode::Coverage: return HWC_BLENDING_COVERAGE; 1917 case BlendMode::Premultiplied: return HWC_BLENDING_PREMULT; 1918 default: return HWC_BLENDING_NONE; 1919 } 1920} 1921 1922void HWC2On1Adapter::Layer::applyCommonState(hwc_layer_1_t& hwc1Layer, 1923 bool applyAllState) 1924{ 1925 auto minorVersion = mDisplay.getDevice().getHwc1MinorVersion(); 1926 if (applyAllState || mBlendMode.isDirty()) { 1927 hwc1Layer.blending = getHwc1Blending(mBlendMode.getPendingValue()); 1928 mBlendMode.latch(); 1929 } 1930 if (applyAllState || mDisplayFrame.isDirty()) { 1931 hwc1Layer.displayFrame = mDisplayFrame.getPendingValue(); 1932 mDisplayFrame.latch(); 1933 } 1934 if (applyAllState || mPlaneAlpha.isDirty()) { 1935 auto pendingAlpha = mPlaneAlpha.getPendingValue(); 1936 if (minorVersion < 2) { 1937 mHasUnsupportedPlaneAlpha = pendingAlpha < 1.0f; 1938 } else { 1939 hwc1Layer.planeAlpha = 1940 static_cast<uint8_t>(255.0f * pendingAlpha + 0.5f); 1941 } 1942 mPlaneAlpha.latch(); 1943 } 1944 if (applyAllState || mSourceCrop.isDirty()) { 1945 if (minorVersion < 3) { 1946 auto pending = mSourceCrop.getPendingValue(); 1947 hwc1Layer.sourceCropi.left = 1948 static_cast<int32_t>(std::ceil(pending.left)); 1949 hwc1Layer.sourceCropi.top = 1950 static_cast<int32_t>(std::ceil(pending.top)); 1951 hwc1Layer.sourceCropi.right = 1952 static_cast<int32_t>(std::floor(pending.right)); 1953 hwc1Layer.sourceCropi.bottom = 1954 static_cast<int32_t>(std::floor(pending.bottom)); 1955 } else { 1956 hwc1Layer.sourceCropf = mSourceCrop.getPendingValue(); 1957 } 1958 mSourceCrop.latch(); 1959 } 1960 if (applyAllState || mTransform.isDirty()) { 1961 hwc1Layer.transform = 1962 static_cast<uint32_t>(mTransform.getPendingValue()); 1963 mTransform.latch(); 1964 } 1965 if (applyAllState || mVisibleRegion.isDirty()) { 1966 auto& hwc1VisibleRegion = hwc1Layer.visibleRegionScreen; 1967 1968 std::free(const_cast<hwc_rect_t*>(hwc1VisibleRegion.rects)); 1969 1970 auto pending = mVisibleRegion.getPendingValue(); 1971 hwc_rect_t* newRects = static_cast<hwc_rect_t*>( 1972 std::malloc(sizeof(hwc_rect_t) * pending.size())); 1973 std::copy(pending.begin(), pending.end(), newRects); 1974 hwc1VisibleRegion.rects = const_cast<const hwc_rect_t*>(newRects); 1975 hwc1VisibleRegion.numRects = pending.size(); 1976 mVisibleRegion.latch(); 1977 } 1978} 1979 1980void HWC2On1Adapter::Layer::applySolidColorState(hwc_layer_1_t& hwc1Layer, 1981 bool applyAllState) 1982{ 1983 if (applyAllState || mColor.isDirty()) { 1984 hwc1Layer.backgroundColor = mColor.getPendingValue(); 1985 mColor.latch(); 1986 } 1987} 1988 1989void HWC2On1Adapter::Layer::applySidebandState(hwc_layer_1_t& hwc1Layer, 1990 bool applyAllState) 1991{ 1992 if (applyAllState || mSidebandStream.isDirty()) { 1993 hwc1Layer.sidebandStream = mSidebandStream.getPendingValue(); 1994 mSidebandStream.latch(); 1995 } 1996} 1997 1998void HWC2On1Adapter::Layer::applyBufferState(hwc_layer_1_t& hwc1Layer) 1999{ 2000 hwc1Layer.handle = mBuffer.getBuffer(); 2001 hwc1Layer.acquireFenceFd = mBuffer.getFence(); 2002} 2003 2004void HWC2On1Adapter::Layer::applyCompositionType(hwc_layer_1_t& hwc1Layer, 2005 bool applyAllState) 2006{ 2007 // HWC1 never supports color transforms or dataspaces and only sometimes 2008 // supports plane alpha (depending on the version). These require us to drop 2009 // some or all layers to client composition. 2010 if (mHasUnsupportedDataspace || mHasUnsupportedPlaneAlpha || 2011 mDisplay.hasColorTransform()) { 2012 hwc1Layer.compositionType = HWC_FRAMEBUFFER; 2013 hwc1Layer.flags = HWC_SKIP_LAYER; 2014 return; 2015 } 2016 2017 if (applyAllState || mCompositionType.isDirty()) { 2018 hwc1Layer.flags = 0; 2019 switch (mCompositionType.getPendingValue()) { 2020 case Composition::Client: 2021 hwc1Layer.compositionType = HWC_FRAMEBUFFER; 2022 hwc1Layer.flags |= HWC_SKIP_LAYER; 2023 break; 2024 case Composition::Device: 2025 hwc1Layer.compositionType = HWC_FRAMEBUFFER; 2026 break; 2027 case Composition::SolidColor: 2028 hwc1Layer.compositionType = HWC_BACKGROUND; 2029 break; 2030 case Composition::Cursor: 2031 hwc1Layer.compositionType = HWC_FRAMEBUFFER; 2032 if (mDisplay.getDevice().getHwc1MinorVersion() >= 4) { 2033 hwc1Layer.hints |= HWC_IS_CURSOR_LAYER; 2034 } 2035 break; 2036 case Composition::Sideband: 2037 if (mDisplay.getDevice().getHwc1MinorVersion() < 4) { 2038 hwc1Layer.compositionType = HWC_SIDEBAND; 2039 } else { 2040 hwc1Layer.compositionType = HWC_FRAMEBUFFER; 2041 hwc1Layer.flags |= HWC_SKIP_LAYER; 2042 } 2043 break; 2044 default: 2045 hwc1Layer.compositionType = HWC_FRAMEBUFFER; 2046 hwc1Layer.flags |= HWC_SKIP_LAYER; 2047 break; 2048 } 2049 ALOGV("Layer %" PRIu64 " %s set to %d", mId, 2050 to_string(mCompositionType.getPendingValue()).c_str(), 2051 hwc1Layer.compositionType); 2052 ALOGV_IF(hwc1Layer.flags & HWC_SKIP_LAYER, " and skipping"); 2053 mCompositionType.latch(); 2054 } 2055} 2056 2057// Adapter helpers 2058 2059void HWC2On1Adapter::populateCapabilities() 2060{ 2061 ALOGV("populateCapabilities"); 2062 if (mHwc1MinorVersion >= 3U) { 2063 int supportedTypes = 0; 2064 auto result = mHwc1Device->query(mHwc1Device, 2065 HWC_DISPLAY_TYPES_SUPPORTED, &supportedTypes); 2066 if ((result == 0) && ((supportedTypes & HWC_DISPLAY_VIRTUAL) != 0)) { 2067 ALOGI("Found support for HWC virtual displays"); 2068 mHwc1SupportsVirtualDisplays = true; 2069 } 2070 } 2071 if (mHwc1MinorVersion >= 4U) { 2072 mCapabilities.insert(Capability::SidebandStream); 2073 } 2074} 2075 2076HWC2On1Adapter::Display* HWC2On1Adapter::getDisplay(hwc2_display_t id) 2077{ 2078 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 2079 2080 auto display = mDisplays.find(id); 2081 if (display == mDisplays.end()) { 2082 return nullptr; 2083 } 2084 2085 return display->second.get(); 2086} 2087 2088std::tuple<HWC2On1Adapter::Layer*, Error> HWC2On1Adapter::getLayer( 2089 hwc2_display_t displayId, hwc2_layer_t layerId) 2090{ 2091 auto display = getDisplay(displayId); 2092 if (!display) { 2093 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadDisplay); 2094 } 2095 2096 auto layerEntry = mLayers.find(layerId); 2097 if (layerEntry == mLayers.end()) { 2098 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer); 2099 } 2100 2101 auto layer = layerEntry->second; 2102 if (layer->getDisplay().getId() != displayId) { 2103 return std::make_tuple(static_cast<Layer*>(nullptr), Error::BadLayer); 2104 } 2105 return std::make_tuple(layer.get(), Error::None); 2106} 2107 2108void HWC2On1Adapter::populatePrimary() 2109{ 2110 ALOGV("populatePrimary"); 2111 2112 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 2113 2114 auto display = 2115 std::make_shared<Display>(*this, HWC2::DisplayType::Physical); 2116 mHwc1DisplayMap[HWC_DISPLAY_PRIMARY] = display->getId(); 2117 display->setHwc1Id(HWC_DISPLAY_PRIMARY); 2118 display->populateConfigs(); 2119 mDisplays.emplace(display->getId(), std::move(display)); 2120} 2121 2122bool HWC2On1Adapter::prepareAllDisplays() 2123{ 2124 ATRACE_CALL(); 2125 2126 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 2127 2128 for (const auto& displayPair : mDisplays) { 2129 auto& display = displayPair.second; 2130 if (!display->prepare()) { 2131 return false; 2132 } 2133 } 2134 2135 if (mHwc1DisplayMap.count(0) == 0) { 2136 ALOGE("prepareAllDisplays: Unable to find primary HWC1 display"); 2137 return false; 2138 } 2139 2140 // Always push the primary display 2141 std::vector<HWC2On1Adapter::Display::HWC1Contents> requestedContents; 2142 auto primaryDisplayId = mHwc1DisplayMap[HWC_DISPLAY_PRIMARY]; 2143 auto& primaryDisplay = mDisplays[primaryDisplayId]; 2144 auto primaryDisplayContents = primaryDisplay->cloneRequestedContents(); 2145 requestedContents.push_back(std::move(primaryDisplayContents)); 2146 2147 // Push the external display, if present 2148 if (mHwc1DisplayMap.count(HWC_DISPLAY_EXTERNAL) != 0) { 2149 auto externalDisplayId = mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL]; 2150 auto& externalDisplay = mDisplays[externalDisplayId]; 2151 auto externalDisplayContents = 2152 externalDisplay->cloneRequestedContents(); 2153 requestedContents.push_back(std::move(externalDisplayContents)); 2154 } else { 2155 // Even if an external display isn't present, we still need to send 2156 // at least two displays down to HWC1 2157 requestedContents.push_back(nullptr); 2158 } 2159 2160 // Push the hardware virtual display, if supported and present 2161 if (mHwc1MinorVersion >= 3) { 2162 if (mHwc1DisplayMap.count(HWC_DISPLAY_VIRTUAL) != 0) { 2163 auto virtualDisplayId = mHwc1DisplayMap[HWC_DISPLAY_VIRTUAL]; 2164 auto& virtualDisplay = mDisplays[virtualDisplayId]; 2165 auto virtualDisplayContents = 2166 virtualDisplay->cloneRequestedContents(); 2167 requestedContents.push_back(std::move(virtualDisplayContents)); 2168 } else { 2169 requestedContents.push_back(nullptr); 2170 } 2171 } 2172 2173 mHwc1Contents.clear(); 2174 for (auto& displayContents : requestedContents) { 2175 mHwc1Contents.push_back(displayContents.get()); 2176 if (!displayContents) { 2177 continue; 2178 } 2179 2180 ALOGV("Display %zd layers:", mHwc1Contents.size() - 1); 2181 for (size_t l = 0; l < displayContents->numHwLayers; ++l) { 2182 auto& layer = displayContents->hwLayers[l]; 2183 ALOGV(" %zd: %d", l, layer.compositionType); 2184 } 2185 } 2186 2187 ALOGV("Calling HWC1 prepare"); 2188 { 2189 ATRACE_NAME("HWC1 prepare"); 2190 mHwc1Device->prepare(mHwc1Device, mHwc1Contents.size(), 2191 mHwc1Contents.data()); 2192 } 2193 2194 for (size_t c = 0; c < mHwc1Contents.size(); ++c) { 2195 auto& contents = mHwc1Contents[c]; 2196 if (!contents) { 2197 continue; 2198 } 2199 ALOGV("Display %zd layers:", c); 2200 for (size_t l = 0; l < contents->numHwLayers; ++l) { 2201 ALOGV(" %zd: %d", l, contents->hwLayers[l].compositionType); 2202 } 2203 } 2204 2205 // Return the received contents to their respective displays 2206 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) { 2207 if (mHwc1Contents[hwc1Id] == nullptr) { 2208 continue; 2209 } 2210 2211 auto displayId = mHwc1DisplayMap[hwc1Id]; 2212 auto& display = mDisplays[displayId]; 2213 display->setReceivedContents(std::move(requestedContents[hwc1Id])); 2214 } 2215 2216 return true; 2217} 2218 2219Error HWC2On1Adapter::setAllDisplays() 2220{ 2221 ATRACE_CALL(); 2222 2223 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 2224 2225 // Make sure we're ready to validate 2226 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) { 2227 if (mHwc1Contents[hwc1Id] == nullptr) { 2228 continue; 2229 } 2230 2231 auto displayId = mHwc1DisplayMap[hwc1Id]; 2232 auto& display = mDisplays[displayId]; 2233 Error error = display->set(*mHwc1Contents[hwc1Id]); 2234 if (error != Error::None) { 2235 ALOGE("setAllDisplays: Failed to set display %zd: %s", hwc1Id, 2236 to_string(error).c_str()); 2237 return error; 2238 } 2239 } 2240 2241 ALOGV("Calling HWC1 set"); 2242 { 2243 ATRACE_NAME("HWC1 set"); 2244 mHwc1Device->set(mHwc1Device, mHwc1Contents.size(), 2245 mHwc1Contents.data()); 2246 } 2247 2248 // Add retire and release fences 2249 for (size_t hwc1Id = 0; hwc1Id < mHwc1Contents.size(); ++hwc1Id) { 2250 if (mHwc1Contents[hwc1Id] == nullptr) { 2251 continue; 2252 } 2253 2254 auto displayId = mHwc1DisplayMap[hwc1Id]; 2255 auto& display = mDisplays[displayId]; 2256 auto retireFenceFd = mHwc1Contents[hwc1Id]->retireFenceFd; 2257 ALOGV("setAllDisplays: Adding retire fence %d to display %zd", 2258 retireFenceFd, hwc1Id); 2259 display->addRetireFence(mHwc1Contents[hwc1Id]->retireFenceFd); 2260 display->addReleaseFences(*mHwc1Contents[hwc1Id]); 2261 } 2262 2263 return Error::None; 2264} 2265 2266void HWC2On1Adapter::hwc1Invalidate() 2267{ 2268 ALOGV("Received hwc1Invalidate"); 2269 2270 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 2271 2272 // If the HWC2-side callback hasn't been registered yet, buffer this until 2273 // it is registered 2274 if (mCallbacks.count(Callback::Refresh) == 0) { 2275 mHasPendingInvalidate = true; 2276 return; 2277 } 2278 2279 const auto& callbackInfo = mCallbacks[Callback::Refresh]; 2280 std::vector<hwc2_display_t> displays; 2281 for (const auto& displayPair : mDisplays) { 2282 displays.emplace_back(displayPair.first); 2283 } 2284 2285 // Call back without the state lock held 2286 lock.unlock(); 2287 2288 auto refresh = reinterpret_cast<HWC2_PFN_REFRESH>(callbackInfo.pointer); 2289 for (auto display : displays) { 2290 refresh(callbackInfo.data, display); 2291 } 2292} 2293 2294void HWC2On1Adapter::hwc1Vsync(int hwc1DisplayId, int64_t timestamp) 2295{ 2296 ALOGV("Received hwc1Vsync(%d, %" PRId64 ")", hwc1DisplayId, timestamp); 2297 2298 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 2299 2300 // If the HWC2-side callback hasn't been registered yet, buffer this until 2301 // it is registered 2302 if (mCallbacks.count(Callback::Vsync) == 0) { 2303 mPendingVsyncs.emplace_back(hwc1DisplayId, timestamp); 2304 return; 2305 } 2306 2307 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { 2308 ALOGE("hwc1Vsync: Couldn't find display for HWC1 id %d", hwc1DisplayId); 2309 return; 2310 } 2311 2312 const auto& callbackInfo = mCallbacks[Callback::Vsync]; 2313 auto displayId = mHwc1DisplayMap[hwc1DisplayId]; 2314 2315 // Call back without the state lock held 2316 lock.unlock(); 2317 2318 auto vsync = reinterpret_cast<HWC2_PFN_VSYNC>(callbackInfo.pointer); 2319 vsync(callbackInfo.data, displayId, timestamp); 2320} 2321 2322void HWC2On1Adapter::hwc1Hotplug(int hwc1DisplayId, int connected) 2323{ 2324 ALOGV("Received hwc1Hotplug(%d, %d)", hwc1DisplayId, connected); 2325 2326 if (hwc1DisplayId != HWC_DISPLAY_EXTERNAL) { 2327 ALOGE("hwc1Hotplug: Received hotplug for non-external display"); 2328 return; 2329 } 2330 2331 std::unique_lock<std::recursive_timed_mutex> lock(mStateMutex); 2332 2333 // If the HWC2-side callback hasn't been registered yet, buffer this until 2334 // it is registered 2335 if (mCallbacks.count(Callback::Hotplug) == 0) { 2336 mPendingHotplugs.emplace_back(hwc1DisplayId, connected); 2337 return; 2338 } 2339 2340 hwc2_display_t displayId = UINT64_MAX; 2341 if (mHwc1DisplayMap.count(hwc1DisplayId) == 0) { 2342 if (connected == 0) { 2343 ALOGW("hwc1Hotplug: Received disconnect for unconnected display"); 2344 return; 2345 } 2346 2347 // Create a new display on connect 2348 auto display = std::make_shared<HWC2On1Adapter::Display>(*this, 2349 HWC2::DisplayType::Physical); 2350 display->setHwc1Id(HWC_DISPLAY_EXTERNAL); 2351 display->populateConfigs(); 2352 displayId = display->getId(); 2353 mHwc1DisplayMap[HWC_DISPLAY_EXTERNAL] = displayId; 2354 mDisplays.emplace(displayId, std::move(display)); 2355 } else { 2356 if (connected != 0) { 2357 ALOGW("hwc1Hotplug: Received connect for previously connected " 2358 "display"); 2359 return; 2360 } 2361 2362 // Disconnect an existing display 2363 displayId = mHwc1DisplayMap[hwc1DisplayId]; 2364 mHwc1DisplayMap.erase(HWC_DISPLAY_EXTERNAL); 2365 mDisplays.erase(displayId); 2366 } 2367 2368 const auto& callbackInfo = mCallbacks[Callback::Hotplug]; 2369 2370 // Call back without the state lock held 2371 lock.unlock(); 2372 2373 auto hotplug = reinterpret_cast<HWC2_PFN_HOTPLUG>(callbackInfo.pointer); 2374 auto hwc2Connected = (connected == 0) ? 2375 HWC2::Connection::Disconnected : HWC2::Connection::Connected; 2376 hotplug(callbackInfo.data, displayId, static_cast<int32_t>(hwc2Connected)); 2377} 2378 2379} // namespace android 2380