Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
| 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "HwcComposer" |
| 23 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 24 | |
| 25 | #include "HidlComposerHal.h" |
| 26 | |
| 27 | #include <android/binder_manager.h> |
| 28 | #include <composer-command-buffer/2.2/ComposerCommandBuffer.h> |
| 29 | #include <hidl/HidlTransportSupport.h> |
| 30 | #include <hidl/HidlTransportUtils.h> |
| 31 | #include <log/log.h> |
| 32 | #include <utils/Trace.h> |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <cinttypes> |
| 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | using hardware::hidl_handle; |
| 40 | using hardware::hidl_vec; |
| 41 | using hardware::Return; |
| 42 | |
| 43 | namespace Hwc2 { |
| 44 | |
| 45 | HidlComposer::~HidlComposer() = default; |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | class BufferHandle { |
| 50 | public: |
| 51 | explicit BufferHandle(const native_handle_t* buffer) { |
| 52 | // nullptr is not a valid handle to HIDL |
| 53 | mHandle = (buffer) ? buffer : native_handle_init(mStorage, 0, 0); |
| 54 | } |
| 55 | |
| 56 | operator const hidl_handle&() const // NOLINT(google-explicit-constructor) |
| 57 | { |
| 58 | return mHandle; |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | NATIVE_HANDLE_DECLARE_STORAGE(mStorage, 0, 0); |
| 63 | hidl_handle mHandle; |
| 64 | }; |
| 65 | |
| 66 | class FenceHandle { |
| 67 | public: |
| 68 | FenceHandle(int fd, bool owned) : mOwned(owned) { |
| 69 | native_handle_t* handle; |
| 70 | if (fd >= 0) { |
| 71 | handle = native_handle_init(mStorage, 1, 0); |
| 72 | handle->data[0] = fd; |
| 73 | } else { |
| 74 | // nullptr is not a valid handle to HIDL |
| 75 | handle = native_handle_init(mStorage, 0, 0); |
| 76 | } |
| 77 | mHandle = handle; |
| 78 | } |
| 79 | |
| 80 | ~FenceHandle() { |
| 81 | if (mOwned) { |
| 82 | native_handle_close(mHandle); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | operator const hidl_handle&() const // NOLINT(google-explicit-constructor) |
| 87 | { |
| 88 | return mHandle; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | bool mOwned; |
| 93 | NATIVE_HANDLE_DECLARE_STORAGE(mStorage, 1, 0); |
| 94 | hidl_handle mHandle; |
| 95 | }; |
| 96 | |
| 97 | // assume NO_RESOURCES when Status::isOk returns false |
| 98 | constexpr Error kDefaultError = Error::NO_RESOURCES; |
| 99 | constexpr V2_4::Error kDefaultError_2_4 = static_cast<V2_4::Error>(kDefaultError); |
| 100 | |
| 101 | template <typename T, typename U> |
| 102 | T unwrapRet(Return<T>& ret, const U& default_val) { |
| 103 | return (ret.isOk()) ? static_cast<T>(ret) : static_cast<T>(default_val); |
| 104 | } |
| 105 | |
| 106 | Error unwrapRet(Return<Error>& ret) { |
| 107 | return unwrapRet(ret, kDefaultError); |
| 108 | } |
| 109 | |
| 110 | } // anonymous namespace |
| 111 | |
| 112 | HidlComposer::HidlComposer(const std::string& serviceName) : mWriter(kWriterInitialSize) { |
| 113 | mComposer = V2_1::IComposer::getService(serviceName); |
| 114 | |
| 115 | if (mComposer == nullptr) { |
| 116 | LOG_ALWAYS_FATAL("failed to get hwcomposer service"); |
| 117 | } |
| 118 | |
| 119 | if (sp<IComposer> composer_2_4 = IComposer::castFrom(mComposer)) { |
| 120 | composer_2_4->createClient_2_4([&](const auto& tmpError, const auto& tmpClient) { |
| 121 | if (tmpError == V2_4::Error::NONE) { |
| 122 | mClient = tmpClient; |
| 123 | mClient_2_2 = tmpClient; |
| 124 | mClient_2_3 = tmpClient; |
| 125 | mClient_2_4 = tmpClient; |
| 126 | } |
| 127 | }); |
| 128 | } else if (sp<V2_3::IComposer> composer_2_3 = V2_3::IComposer::castFrom(mComposer)) { |
| 129 | composer_2_3->createClient_2_3([&](const auto& tmpError, const auto& tmpClient) { |
| 130 | if (tmpError == Error::NONE) { |
| 131 | mClient = tmpClient; |
| 132 | mClient_2_2 = tmpClient; |
| 133 | mClient_2_3 = tmpClient; |
| 134 | } |
| 135 | }); |
| 136 | } else { |
| 137 | mComposer->createClient([&](const auto& tmpError, const auto& tmpClient) { |
| 138 | if (tmpError != Error::NONE) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | mClient = tmpClient; |
| 143 | if (sp<V2_2::IComposer> composer_2_2 = V2_2::IComposer::castFrom(mComposer)) { |
| 144 | mClient_2_2 = V2_2::IComposerClient::castFrom(mClient); |
| 145 | LOG_ALWAYS_FATAL_IF(mClient_2_2 == nullptr, |
| 146 | "IComposer 2.2 did not return IComposerClient 2.2"); |
| 147 | } |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | if (mClient == nullptr) { |
| 152 | LOG_ALWAYS_FATAL("failed to create composer client"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | std::vector<IComposer::Capability> HidlComposer::getCapabilities() { |
| 157 | std::vector<IComposer::Capability> capabilities; |
| 158 | mComposer->getCapabilities( |
| 159 | [&](const auto& tmpCapabilities) { capabilities = tmpCapabilities; }); |
| 160 | return capabilities; |
| 161 | } |
| 162 | |
| 163 | std::string HidlComposer::dumpDebugInfo() { |
| 164 | std::string info; |
| 165 | mComposer->dumpDebugInfo([&](const auto& tmpInfo) { info = tmpInfo.c_str(); }); |
| 166 | |
| 167 | return info; |
| 168 | } |
| 169 | |
| 170 | void HidlComposer::registerCallback(const sp<IComposerCallback>& callback) { |
| 171 | android::hardware::setMinSchedulerPolicy(callback, SCHED_FIFO, 2); |
| 172 | |
| 173 | auto ret = [&]() { |
| 174 | if (mClient_2_4) { |
| 175 | return mClient_2_4->registerCallback_2_4(callback); |
| 176 | } |
| 177 | return mClient->registerCallback(callback); |
| 178 | }(); |
| 179 | if (!ret.isOk()) { |
| 180 | ALOGE("failed to register IComposerCallback"); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void HidlComposer::resetCommands() { |
| 185 | mWriter.reset(); |
| 186 | } |
| 187 | |
| 188 | Error HidlComposer::executeCommands() { |
| 189 | return execute(); |
| 190 | } |
| 191 | |
| 192 | uint32_t HidlComposer::getMaxVirtualDisplayCount() { |
| 193 | auto ret = mClient->getMaxVirtualDisplayCount(); |
| 194 | return unwrapRet(ret, 0); |
| 195 | } |
| 196 | |
| 197 | Error HidlComposer::createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format, |
| 198 | Display* outDisplay) { |
| 199 | const uint32_t bufferSlotCount = 1; |
| 200 | Error error = kDefaultError; |
| 201 | if (mClient_2_2) { |
| 202 | mClient_2_2->createVirtualDisplay_2_2(width, height, |
| 203 | static_cast<types::V1_1::PixelFormat>(*format), |
| 204 | bufferSlotCount, |
| 205 | [&](const auto& tmpError, const auto& tmpDisplay, |
| 206 | const auto& tmpFormat) { |
| 207 | error = tmpError; |
| 208 | if (error != Error::NONE) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | *outDisplay = tmpDisplay; |
| 213 | *format = static_cast<types::V1_2::PixelFormat>( |
| 214 | tmpFormat); |
| 215 | }); |
| 216 | } else { |
| 217 | mClient->createVirtualDisplay(width, height, static_cast<types::V1_0::PixelFormat>(*format), |
| 218 | bufferSlotCount, |
| 219 | [&](const auto& tmpError, const auto& tmpDisplay, |
| 220 | const auto& tmpFormat) { |
| 221 | error = tmpError; |
| 222 | if (error != Error::NONE) { |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | *outDisplay = tmpDisplay; |
| 227 | *format = static_cast<PixelFormat>(tmpFormat); |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | return error; |
| 232 | } |
| 233 | |
| 234 | Error HidlComposer::destroyVirtualDisplay(Display display) { |
| 235 | auto ret = mClient->destroyVirtualDisplay(display); |
| 236 | return unwrapRet(ret); |
| 237 | } |
| 238 | |
| 239 | Error HidlComposer::acceptDisplayChanges(Display display) { |
| 240 | mWriter.selectDisplay(display); |
| 241 | mWriter.acceptDisplayChanges(); |
| 242 | return Error::NONE; |
| 243 | } |
| 244 | |
| 245 | Error HidlComposer::createLayer(Display display, Layer* outLayer) { |
| 246 | Error error = kDefaultError; |
| 247 | mClient->createLayer(display, kMaxLayerBufferCount, |
| 248 | [&](const auto& tmpError, const auto& tmpLayer) { |
| 249 | error = tmpError; |
| 250 | if (error != Error::NONE) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | *outLayer = tmpLayer; |
| 255 | }); |
| 256 | |
| 257 | return error; |
| 258 | } |
| 259 | |
| 260 | Error HidlComposer::destroyLayer(Display display, Layer layer) { |
| 261 | auto ret = mClient->destroyLayer(display, layer); |
| 262 | return unwrapRet(ret); |
| 263 | } |
| 264 | |
| 265 | Error HidlComposer::getActiveConfig(Display display, Config* outConfig) { |
| 266 | Error error = kDefaultError; |
| 267 | mClient->getActiveConfig(display, [&](const auto& tmpError, const auto& tmpConfig) { |
| 268 | error = tmpError; |
| 269 | if (error != Error::NONE) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | *outConfig = tmpConfig; |
| 274 | }); |
| 275 | |
| 276 | return error; |
| 277 | } |
| 278 | |
| 279 | Error HidlComposer::getChangedCompositionTypes( |
| 280 | Display display, std::vector<Layer>* outLayers, |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame^] | 281 | std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) { |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 282 | mReader.takeChangedCompositionTypes(display, outLayers, outTypes); |
| 283 | return Error::NONE; |
| 284 | } |
| 285 | |
| 286 | Error HidlComposer::getColorModes(Display display, std::vector<ColorMode>* outModes) { |
| 287 | Error error = kDefaultError; |
| 288 | |
| 289 | if (mClient_2_3) { |
| 290 | mClient_2_3->getColorModes_2_3(display, [&](const auto& tmpError, const auto& tmpModes) { |
| 291 | error = tmpError; |
| 292 | if (error != Error::NONE) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | *outModes = tmpModes; |
| 297 | }); |
| 298 | } else if (mClient_2_2) { |
| 299 | mClient_2_2->getColorModes_2_2(display, [&](const auto& tmpError, const auto& tmpModes) { |
| 300 | error = tmpError; |
| 301 | if (error != Error::NONE) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | for (types::V1_1::ColorMode colorMode : tmpModes) { |
| 306 | outModes->push_back(static_cast<ColorMode>(colorMode)); |
| 307 | } |
| 308 | }); |
| 309 | } else { |
| 310 | mClient->getColorModes(display, [&](const auto& tmpError, const auto& tmpModes) { |
| 311 | error = tmpError; |
| 312 | if (error != Error::NONE) { |
| 313 | return; |
| 314 | } |
| 315 | for (types::V1_0::ColorMode colorMode : tmpModes) { |
| 316 | outModes->push_back(static_cast<ColorMode>(colorMode)); |
| 317 | } |
| 318 | }); |
| 319 | } |
| 320 | |
| 321 | return error; |
| 322 | } |
| 323 | |
| 324 | Error HidlComposer::getDisplayAttribute(Display display, Config config, |
| 325 | IComposerClient::Attribute attribute, int32_t* outValue) { |
| 326 | Error error = kDefaultError; |
| 327 | if (mClient_2_4) { |
| 328 | mClient_2_4->getDisplayAttribute_2_4(display, config, attribute, |
| 329 | [&](const auto& tmpError, const auto& tmpValue) { |
| 330 | error = static_cast<Error>(tmpError); |
| 331 | if (error != Error::NONE) { |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | *outValue = tmpValue; |
| 336 | }); |
| 337 | } else { |
| 338 | mClient->getDisplayAttribute(display, config, |
| 339 | static_cast<V2_1::IComposerClient::Attribute>(attribute), |
| 340 | [&](const auto& tmpError, const auto& tmpValue) { |
| 341 | error = tmpError; |
| 342 | if (error != Error::NONE) { |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | *outValue = tmpValue; |
| 347 | }); |
| 348 | } |
| 349 | |
| 350 | return error; |
| 351 | } |
| 352 | |
| 353 | Error HidlComposer::getDisplayConfigs(Display display, std::vector<Config>* outConfigs) { |
| 354 | Error error = kDefaultError; |
| 355 | mClient->getDisplayConfigs(display, [&](const auto& tmpError, const auto& tmpConfigs) { |
| 356 | error = tmpError; |
| 357 | if (error != Error::NONE) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | *outConfigs = tmpConfigs; |
| 362 | }); |
| 363 | |
| 364 | return error; |
| 365 | } |
| 366 | |
| 367 | Error HidlComposer::getDisplayName(Display display, std::string* outName) { |
| 368 | Error error = kDefaultError; |
| 369 | mClient->getDisplayName(display, [&](const auto& tmpError, const auto& tmpName) { |
| 370 | error = tmpError; |
| 371 | if (error != Error::NONE) { |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | *outName = tmpName.c_str(); |
| 376 | }); |
| 377 | |
| 378 | return error; |
| 379 | } |
| 380 | |
| 381 | Error HidlComposer::getDisplayRequests(Display display, uint32_t* outDisplayRequestMask, |
| 382 | std::vector<Layer>* outLayers, |
| 383 | std::vector<uint32_t>* outLayerRequestMasks) { |
| 384 | mReader.takeDisplayRequests(display, outDisplayRequestMask, outLayers, outLayerRequestMasks); |
| 385 | return Error::NONE; |
| 386 | } |
| 387 | |
| 388 | Error HidlComposer::getDozeSupport(Display display, bool* outSupport) { |
| 389 | Error error = kDefaultError; |
| 390 | mClient->getDozeSupport(display, [&](const auto& tmpError, const auto& tmpSupport) { |
| 391 | error = tmpError; |
| 392 | if (error != Error::NONE) { |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | *outSupport = tmpSupport; |
| 397 | }); |
| 398 | |
| 399 | return error; |
| 400 | } |
| 401 | |
| 402 | Error HidlComposer::getHdrCapabilities(Display display, std::vector<Hdr>* outTypes, |
| 403 | float* outMaxLuminance, float* outMaxAverageLuminance, |
| 404 | float* outMinLuminance) { |
| 405 | Error error = kDefaultError; |
| 406 | if (mClient_2_3) { |
| 407 | mClient_2_3->getHdrCapabilities_2_3(display, |
| 408 | [&](const auto& tmpError, const auto& tmpTypes, |
| 409 | const auto& tmpMaxLuminance, |
| 410 | const auto& tmpMaxAverageLuminance, |
| 411 | const auto& tmpMinLuminance) { |
| 412 | error = tmpError; |
| 413 | if (error != Error::NONE) { |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | *outTypes = tmpTypes; |
| 418 | *outMaxLuminance = tmpMaxLuminance; |
| 419 | *outMaxAverageLuminance = tmpMaxAverageLuminance; |
| 420 | *outMinLuminance = tmpMinLuminance; |
| 421 | }); |
| 422 | } else { |
| 423 | mClient->getHdrCapabilities(display, |
| 424 | [&](const auto& tmpError, const auto& tmpTypes, |
| 425 | const auto& tmpMaxLuminance, |
| 426 | const auto& tmpMaxAverageLuminance, |
| 427 | const auto& tmpMinLuminance) { |
| 428 | error = tmpError; |
| 429 | if (error != Error::NONE) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | outTypes->clear(); |
| 434 | for (auto type : tmpTypes) { |
| 435 | outTypes->push_back(static_cast<Hdr>(type)); |
| 436 | } |
| 437 | |
| 438 | *outMaxLuminance = tmpMaxLuminance; |
| 439 | *outMaxAverageLuminance = tmpMaxAverageLuminance; |
| 440 | *outMinLuminance = tmpMinLuminance; |
| 441 | }); |
| 442 | } |
| 443 | |
| 444 | return error; |
| 445 | } |
| 446 | |
| 447 | Error HidlComposer::getReleaseFences(Display display, std::vector<Layer>* outLayers, |
| 448 | std::vector<int>* outReleaseFences) { |
| 449 | mReader.takeReleaseFences(display, outLayers, outReleaseFences); |
| 450 | return Error::NONE; |
| 451 | } |
| 452 | |
| 453 | Error HidlComposer::presentDisplay(Display display, int* outPresentFence) { |
| 454 | ATRACE_NAME("HwcPresentDisplay"); |
| 455 | mWriter.selectDisplay(display); |
| 456 | mWriter.presentDisplay(); |
| 457 | |
| 458 | Error error = execute(); |
| 459 | if (error != Error::NONE) { |
| 460 | return error; |
| 461 | } |
| 462 | |
| 463 | mReader.takePresentFence(display, outPresentFence); |
| 464 | |
| 465 | return Error::NONE; |
| 466 | } |
| 467 | |
| 468 | Error HidlComposer::setActiveConfig(Display display, Config config) { |
| 469 | auto ret = mClient->setActiveConfig(display, config); |
| 470 | return unwrapRet(ret); |
| 471 | } |
| 472 | |
| 473 | Error HidlComposer::setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target, |
| 474 | int acquireFence, Dataspace dataspace, |
| 475 | const std::vector<IComposerClient::Rect>& damage) { |
| 476 | mWriter.selectDisplay(display); |
| 477 | |
| 478 | const native_handle_t* handle = nullptr; |
| 479 | if (target.get()) { |
| 480 | handle = target->getNativeBuffer()->handle; |
| 481 | } |
| 482 | |
| 483 | mWriter.setClientTarget(slot, handle, acquireFence, dataspace, damage); |
| 484 | return Error::NONE; |
| 485 | } |
| 486 | |
| 487 | Error HidlComposer::setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) { |
| 488 | hardware::Return<Error> ret(kDefaultError); |
| 489 | if (mClient_2_3) { |
| 490 | ret = mClient_2_3->setColorMode_2_3(display, mode, renderIntent); |
| 491 | } else if (mClient_2_2) { |
| 492 | ret = mClient_2_2->setColorMode_2_2(display, static_cast<types::V1_1::ColorMode>(mode), |
| 493 | renderIntent); |
| 494 | } else { |
| 495 | ret = mClient->setColorMode(display, static_cast<types::V1_0::ColorMode>(mode)); |
| 496 | } |
| 497 | return unwrapRet(ret); |
| 498 | } |
| 499 | |
| 500 | Error HidlComposer::setColorTransform(Display display, const float* matrix, ColorTransform hint) { |
| 501 | mWriter.selectDisplay(display); |
| 502 | mWriter.setColorTransform(matrix, hint); |
| 503 | return Error::NONE; |
| 504 | } |
| 505 | |
| 506 | Error HidlComposer::setOutputBuffer(Display display, const native_handle_t* buffer, |
| 507 | int releaseFence) { |
| 508 | mWriter.selectDisplay(display); |
| 509 | mWriter.setOutputBuffer(0, buffer, dup(releaseFence)); |
| 510 | return Error::NONE; |
| 511 | } |
| 512 | |
| 513 | Error HidlComposer::setPowerMode(Display display, IComposerClient::PowerMode mode) { |
| 514 | Return<Error> ret(Error::UNSUPPORTED); |
| 515 | if (mClient_2_2) { |
| 516 | ret = mClient_2_2->setPowerMode_2_2(display, mode); |
| 517 | } else if (mode != IComposerClient::PowerMode::ON_SUSPEND) { |
| 518 | ret = mClient->setPowerMode(display, static_cast<V2_1::IComposerClient::PowerMode>(mode)); |
| 519 | } |
| 520 | |
| 521 | return unwrapRet(ret); |
| 522 | } |
| 523 | |
| 524 | Error HidlComposer::setVsyncEnabled(Display display, IComposerClient::Vsync enabled) { |
| 525 | auto ret = mClient->setVsyncEnabled(display, enabled); |
| 526 | return unwrapRet(ret); |
| 527 | } |
| 528 | |
| 529 | Error HidlComposer::setClientTargetSlotCount(Display display) { |
| 530 | const uint32_t bufferSlotCount = BufferQueue::NUM_BUFFER_SLOTS; |
| 531 | auto ret = mClient->setClientTargetSlotCount(display, bufferSlotCount); |
| 532 | return unwrapRet(ret); |
| 533 | } |
| 534 | |
| 535 | Error HidlComposer::validateDisplay(Display display, uint32_t* outNumTypes, |
| 536 | uint32_t* outNumRequests) { |
| 537 | ATRACE_NAME("HwcValidateDisplay"); |
| 538 | mWriter.selectDisplay(display); |
| 539 | mWriter.validateDisplay(); |
| 540 | |
| 541 | Error error = execute(); |
| 542 | if (error != Error::NONE) { |
| 543 | return error; |
| 544 | } |
| 545 | |
| 546 | mReader.hasChanges(display, outNumTypes, outNumRequests); |
| 547 | |
| 548 | return Error::NONE; |
| 549 | } |
| 550 | |
| 551 | Error HidlComposer::presentOrValidateDisplay(Display display, uint32_t* outNumTypes, |
| 552 | uint32_t* outNumRequests, int* outPresentFence, |
| 553 | uint32_t* state) { |
| 554 | ATRACE_NAME("HwcPresentOrValidateDisplay"); |
| 555 | mWriter.selectDisplay(display); |
| 556 | mWriter.presentOrvalidateDisplay(); |
| 557 | |
| 558 | Error error = execute(); |
| 559 | if (error != Error::NONE) { |
| 560 | return error; |
| 561 | } |
| 562 | |
| 563 | mReader.takePresentOrValidateStage(display, state); |
| 564 | |
| 565 | if (*state == 1) { // Present succeeded |
| 566 | mReader.takePresentFence(display, outPresentFence); |
| 567 | } |
| 568 | |
| 569 | if (*state == 0) { // Validate succeeded. |
| 570 | mReader.hasChanges(display, outNumTypes, outNumRequests); |
| 571 | } |
| 572 | |
| 573 | return Error::NONE; |
| 574 | } |
| 575 | |
| 576 | Error HidlComposer::setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) { |
| 577 | mWriter.selectDisplay(display); |
| 578 | mWriter.selectLayer(layer); |
| 579 | mWriter.setLayerCursorPosition(x, y); |
| 580 | return Error::NONE; |
| 581 | } |
| 582 | |
| 583 | Error HidlComposer::setLayerBuffer(Display display, Layer layer, uint32_t slot, |
| 584 | const sp<GraphicBuffer>& buffer, int acquireFence) { |
| 585 | mWriter.selectDisplay(display); |
| 586 | mWriter.selectLayer(layer); |
| 587 | |
| 588 | const native_handle_t* handle = nullptr; |
| 589 | if (buffer.get()) { |
| 590 | handle = buffer->getNativeBuffer()->handle; |
| 591 | } |
| 592 | |
| 593 | mWriter.setLayerBuffer(slot, handle, acquireFence); |
| 594 | return Error::NONE; |
| 595 | } |
| 596 | |
| 597 | Error HidlComposer::setLayerSurfaceDamage(Display display, Layer layer, |
| 598 | const std::vector<IComposerClient::Rect>& damage) { |
| 599 | mWriter.selectDisplay(display); |
| 600 | mWriter.selectLayer(layer); |
| 601 | mWriter.setLayerSurfaceDamage(damage); |
| 602 | return Error::NONE; |
| 603 | } |
| 604 | |
| 605 | Error HidlComposer::setLayerBlendMode(Display display, Layer layer, |
| 606 | IComposerClient::BlendMode mode) { |
| 607 | mWriter.selectDisplay(display); |
| 608 | mWriter.selectLayer(layer); |
| 609 | mWriter.setLayerBlendMode(mode); |
| 610 | return Error::NONE; |
| 611 | } |
| 612 | |
| 613 | Error HidlComposer::setLayerColor(Display display, Layer layer, |
| 614 | const IComposerClient::Color& color) { |
| 615 | mWriter.selectDisplay(display); |
| 616 | mWriter.selectLayer(layer); |
| 617 | mWriter.setLayerColor(color); |
| 618 | return Error::NONE; |
| 619 | } |
| 620 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame^] | 621 | static IComposerClient::Composition to_hidl_type( |
| 622 | aidl::android::hardware::graphics::composer3::Composition type) { |
| 623 | return static_cast<IComposerClient::Composition>(type); |
| 624 | } |
| 625 | |
| 626 | Error HidlComposer::setLayerCompositionType( |
| 627 | Display display, Layer layer, |
| 628 | aidl::android::hardware::graphics::composer3::Composition type) { |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 629 | mWriter.selectDisplay(display); |
| 630 | mWriter.selectLayer(layer); |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame^] | 631 | mWriter.setLayerCompositionType(to_hidl_type(type)); |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 632 | return Error::NONE; |
| 633 | } |
| 634 | |
| 635 | Error HidlComposer::setLayerDataspace(Display display, Layer layer, Dataspace dataspace) { |
| 636 | mWriter.selectDisplay(display); |
| 637 | mWriter.selectLayer(layer); |
| 638 | mWriter.setLayerDataspace(dataspace); |
| 639 | return Error::NONE; |
| 640 | } |
| 641 | |
| 642 | Error HidlComposer::setLayerDisplayFrame(Display display, Layer layer, |
| 643 | const IComposerClient::Rect& frame) { |
| 644 | mWriter.selectDisplay(display); |
| 645 | mWriter.selectLayer(layer); |
| 646 | mWriter.setLayerDisplayFrame(frame); |
| 647 | return Error::NONE; |
| 648 | } |
| 649 | |
| 650 | Error HidlComposer::setLayerPlaneAlpha(Display display, Layer layer, float alpha) { |
| 651 | mWriter.selectDisplay(display); |
| 652 | mWriter.selectLayer(layer); |
| 653 | mWriter.setLayerPlaneAlpha(alpha); |
| 654 | return Error::NONE; |
| 655 | } |
| 656 | |
| 657 | Error HidlComposer::setLayerSidebandStream(Display display, Layer layer, |
| 658 | const native_handle_t* stream) { |
| 659 | mWriter.selectDisplay(display); |
| 660 | mWriter.selectLayer(layer); |
| 661 | mWriter.setLayerSidebandStream(stream); |
| 662 | return Error::NONE; |
| 663 | } |
| 664 | |
| 665 | Error HidlComposer::setLayerSourceCrop(Display display, Layer layer, |
| 666 | const IComposerClient::FRect& crop) { |
| 667 | mWriter.selectDisplay(display); |
| 668 | mWriter.selectLayer(layer); |
| 669 | mWriter.setLayerSourceCrop(crop); |
| 670 | return Error::NONE; |
| 671 | } |
| 672 | |
| 673 | Error HidlComposer::setLayerTransform(Display display, Layer layer, Transform transform) { |
| 674 | mWriter.selectDisplay(display); |
| 675 | mWriter.selectLayer(layer); |
| 676 | mWriter.setLayerTransform(transform); |
| 677 | return Error::NONE; |
| 678 | } |
| 679 | |
| 680 | Error HidlComposer::setLayerVisibleRegion(Display display, Layer layer, |
| 681 | const std::vector<IComposerClient::Rect>& visible) { |
| 682 | mWriter.selectDisplay(display); |
| 683 | mWriter.selectLayer(layer); |
| 684 | mWriter.setLayerVisibleRegion(visible); |
| 685 | return Error::NONE; |
| 686 | } |
| 687 | |
| 688 | Error HidlComposer::setLayerZOrder(Display display, Layer layer, uint32_t z) { |
| 689 | mWriter.selectDisplay(display); |
| 690 | mWriter.selectLayer(layer); |
| 691 | mWriter.setLayerZOrder(z); |
| 692 | return Error::NONE; |
| 693 | } |
| 694 | |
| 695 | Error HidlComposer::execute() { |
| 696 | // prepare input command queue |
| 697 | bool queueChanged = false; |
| 698 | uint32_t commandLength = 0; |
| 699 | hidl_vec<hidl_handle> commandHandles; |
| 700 | if (!mWriter.writeQueue(&queueChanged, &commandLength, &commandHandles)) { |
| 701 | mWriter.reset(); |
| 702 | return Error::NO_RESOURCES; |
| 703 | } |
| 704 | |
| 705 | // set up new input command queue if necessary |
| 706 | if (queueChanged) { |
| 707 | auto ret = mClient->setInputCommandQueue(*mWriter.getMQDescriptor()); |
| 708 | auto error = unwrapRet(ret); |
| 709 | if (error != Error::NONE) { |
| 710 | mWriter.reset(); |
| 711 | return error; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (commandLength == 0) { |
| 716 | mWriter.reset(); |
| 717 | return Error::NONE; |
| 718 | } |
| 719 | |
| 720 | Error error = kDefaultError; |
| 721 | hardware::Return<void> ret; |
| 722 | auto hidl_callback = [&](const auto& tmpError, const auto& tmpOutChanged, |
| 723 | const auto& tmpOutLength, const auto& tmpOutHandles) { |
| 724 | error = tmpError; |
| 725 | |
| 726 | // set up new output command queue if necessary |
| 727 | if (error == Error::NONE && tmpOutChanged) { |
| 728 | error = kDefaultError; |
| 729 | mClient->getOutputCommandQueue([&](const auto& tmpError, const auto& tmpDescriptor) { |
| 730 | error = tmpError; |
| 731 | if (error != Error::NONE) { |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | mReader.setMQDescriptor(tmpDescriptor); |
| 736 | }); |
| 737 | } |
| 738 | |
| 739 | if (error != Error::NONE) { |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | if (mReader.readQueue(tmpOutLength, tmpOutHandles)) { |
| 744 | error = mReader.parse(); |
| 745 | mReader.reset(); |
| 746 | } else { |
| 747 | error = Error::NO_RESOURCES; |
| 748 | } |
| 749 | }; |
| 750 | if (mClient_2_2) { |
| 751 | ret = mClient_2_2->executeCommands_2_2(commandLength, commandHandles, hidl_callback); |
| 752 | } else { |
| 753 | ret = mClient->executeCommands(commandLength, commandHandles, hidl_callback); |
| 754 | } |
| 755 | // executeCommands can fail because of out-of-fd and we do not want to |
| 756 | // abort() in that case |
| 757 | if (!ret.isOk()) { |
| 758 | ALOGE("executeCommands failed because of %s", ret.description().c_str()); |
| 759 | } |
| 760 | |
| 761 | if (error == Error::NONE) { |
| 762 | std::vector<CommandReader::CommandError> commandErrors = mReader.takeErrors(); |
| 763 | |
| 764 | for (const auto& cmdErr : commandErrors) { |
| 765 | auto command = |
| 766 | static_cast<IComposerClient::Command>(mWriter.getCommand(cmdErr.location)); |
| 767 | |
| 768 | if (command == IComposerClient::Command::VALIDATE_DISPLAY || |
| 769 | command == IComposerClient::Command::PRESENT_DISPLAY || |
| 770 | command == IComposerClient::Command::PRESENT_OR_VALIDATE_DISPLAY) { |
| 771 | error = cmdErr.error; |
| 772 | } else { |
| 773 | ALOGW("command 0x%x generated error %d", command, cmdErr.error); |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | mWriter.reset(); |
| 779 | |
| 780 | return error; |
| 781 | } |
| 782 | |
| 783 | // Composer HAL 2.2 |
| 784 | |
| 785 | Error HidlComposer::setLayerPerFrameMetadata( |
| 786 | Display display, Layer layer, |
| 787 | const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) { |
| 788 | if (!mClient_2_2) { |
| 789 | return Error::UNSUPPORTED; |
| 790 | } |
| 791 | |
| 792 | mWriter.selectDisplay(display); |
| 793 | mWriter.selectLayer(layer); |
| 794 | mWriter.setLayerPerFrameMetadata(perFrameMetadatas); |
| 795 | return Error::NONE; |
| 796 | } |
| 797 | |
| 798 | std::vector<IComposerClient::PerFrameMetadataKey> HidlComposer::getPerFrameMetadataKeys( |
| 799 | Display display) { |
| 800 | std::vector<IComposerClient::PerFrameMetadataKey> keys; |
| 801 | if (!mClient_2_2) { |
| 802 | return keys; |
| 803 | } |
| 804 | |
| 805 | Error error = kDefaultError; |
| 806 | if (mClient_2_3) { |
| 807 | mClient_2_3->getPerFrameMetadataKeys_2_3(display, |
| 808 | [&](const auto& tmpError, const auto& tmpKeys) { |
| 809 | error = tmpError; |
| 810 | if (error != Error::NONE) { |
| 811 | ALOGW("getPerFrameMetadataKeys failed " |
| 812 | "with %d", |
| 813 | tmpError); |
| 814 | return; |
| 815 | } |
| 816 | keys = tmpKeys; |
| 817 | }); |
| 818 | } else { |
| 819 | mClient_2_2 |
| 820 | ->getPerFrameMetadataKeys(display, [&](const auto& tmpError, const auto& tmpKeys) { |
| 821 | error = tmpError; |
| 822 | if (error != Error::NONE) { |
| 823 | ALOGW("getPerFrameMetadataKeys failed with %d", tmpError); |
| 824 | return; |
| 825 | } |
| 826 | |
| 827 | keys.clear(); |
| 828 | for (auto key : tmpKeys) { |
| 829 | keys.push_back(static_cast<IComposerClient::PerFrameMetadataKey>(key)); |
| 830 | } |
| 831 | }); |
| 832 | } |
| 833 | |
| 834 | return keys; |
| 835 | } |
| 836 | |
| 837 | Error HidlComposer::getRenderIntents(Display display, ColorMode colorMode, |
| 838 | std::vector<RenderIntent>* outRenderIntents) { |
| 839 | if (!mClient_2_2) { |
| 840 | outRenderIntents->push_back(RenderIntent::COLORIMETRIC); |
| 841 | return Error::NONE; |
| 842 | } |
| 843 | |
| 844 | Error error = kDefaultError; |
| 845 | |
| 846 | auto getRenderIntentsLambda = [&](const auto& tmpError, const auto& tmpKeys) { |
| 847 | error = tmpError; |
| 848 | if (error != Error::NONE) { |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | *outRenderIntents = tmpKeys; |
| 853 | }; |
| 854 | |
| 855 | if (mClient_2_3) { |
| 856 | mClient_2_3->getRenderIntents_2_3(display, colorMode, getRenderIntentsLambda); |
| 857 | } else { |
| 858 | mClient_2_2->getRenderIntents(display, static_cast<types::V1_1::ColorMode>(colorMode), |
| 859 | getRenderIntentsLambda); |
| 860 | } |
| 861 | |
| 862 | return error; |
| 863 | } |
| 864 | |
| 865 | Error HidlComposer::getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) { |
| 866 | if (!mClient_2_2) { |
| 867 | *outMatrix = mat4(); |
| 868 | return Error::NONE; |
| 869 | } |
| 870 | |
| 871 | Error error = kDefaultError; |
| 872 | mClient_2_2->getDataspaceSaturationMatrix(static_cast<types::V1_1::Dataspace>(dataspace), |
| 873 | [&](const auto& tmpError, const auto& tmpMatrix) { |
| 874 | error = tmpError; |
| 875 | if (error != Error::NONE) { |
| 876 | return; |
| 877 | } |
| 878 | *outMatrix = mat4(tmpMatrix.data()); |
| 879 | }); |
| 880 | |
| 881 | return error; |
| 882 | } |
| 883 | |
| 884 | // Composer HAL 2.3 |
| 885 | |
| 886 | Error HidlComposer::getDisplayIdentificationData(Display display, uint8_t* outPort, |
| 887 | std::vector<uint8_t>* outData) { |
| 888 | if (!mClient_2_3) { |
| 889 | return Error::UNSUPPORTED; |
| 890 | } |
| 891 | |
| 892 | Error error = kDefaultError; |
| 893 | mClient_2_3->getDisplayIdentificationData(display, |
| 894 | [&](const auto& tmpError, const auto& tmpPort, |
| 895 | const auto& tmpData) { |
| 896 | error = tmpError; |
| 897 | if (error != Error::NONE) { |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | *outPort = tmpPort; |
| 902 | *outData = tmpData; |
| 903 | }); |
| 904 | |
| 905 | return error; |
| 906 | } |
| 907 | |
| 908 | Error HidlComposer::setLayerColorTransform(Display display, Layer layer, const float* matrix) { |
| 909 | if (!mClient_2_3) { |
| 910 | return Error::UNSUPPORTED; |
| 911 | } |
| 912 | |
| 913 | mWriter.selectDisplay(display); |
| 914 | mWriter.selectLayer(layer); |
| 915 | mWriter.setLayerColorTransform(matrix); |
| 916 | return Error::NONE; |
| 917 | } |
| 918 | |
| 919 | Error HidlComposer::getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat, |
| 920 | Dataspace* outDataspace, |
| 921 | uint8_t* outComponentMask) { |
| 922 | if (!outFormat || !outDataspace || !outComponentMask) { |
| 923 | return Error::BAD_PARAMETER; |
| 924 | } |
| 925 | if (!mClient_2_3) { |
| 926 | return Error::UNSUPPORTED; |
| 927 | } |
| 928 | Error error = kDefaultError; |
| 929 | mClient_2_3->getDisplayedContentSamplingAttributes(display, |
| 930 | [&](const auto tmpError, |
| 931 | const auto& tmpFormat, |
| 932 | const auto& tmpDataspace, |
| 933 | const auto& tmpComponentMask) { |
| 934 | error = tmpError; |
| 935 | if (error == Error::NONE) { |
| 936 | *outFormat = tmpFormat; |
| 937 | *outDataspace = tmpDataspace; |
| 938 | *outComponentMask = |
| 939 | static_cast<uint8_t>( |
| 940 | tmpComponentMask); |
| 941 | } |
| 942 | }); |
| 943 | return error; |
| 944 | } |
| 945 | |
| 946 | Error HidlComposer::setDisplayContentSamplingEnabled(Display display, bool enabled, |
| 947 | uint8_t componentMask, uint64_t maxFrames) { |
| 948 | if (!mClient_2_3) { |
| 949 | return Error::UNSUPPORTED; |
| 950 | } |
| 951 | |
| 952 | auto enable = enabled ? V2_3::IComposerClient::DisplayedContentSampling::ENABLE |
| 953 | : V2_3::IComposerClient::DisplayedContentSampling::DISABLE; |
| 954 | return mClient_2_3->setDisplayedContentSamplingEnabled(display, enable, componentMask, |
| 955 | maxFrames); |
| 956 | } |
| 957 | |
| 958 | Error HidlComposer::getDisplayedContentSample(Display display, uint64_t maxFrames, |
| 959 | uint64_t timestamp, DisplayedFrameStats* outStats) { |
| 960 | if (!outStats) { |
| 961 | return Error::BAD_PARAMETER; |
| 962 | } |
| 963 | if (!mClient_2_3) { |
| 964 | return Error::UNSUPPORTED; |
| 965 | } |
| 966 | Error error = kDefaultError; |
| 967 | mClient_2_3->getDisplayedContentSample(display, maxFrames, timestamp, |
| 968 | [&](const auto tmpError, auto tmpNumFrames, |
| 969 | const auto& tmpSamples0, const auto& tmpSamples1, |
| 970 | const auto& tmpSamples2, const auto& tmpSamples3) { |
| 971 | error = tmpError; |
| 972 | if (error == Error::NONE) { |
| 973 | outStats->numFrames = tmpNumFrames; |
| 974 | outStats->component_0_sample = tmpSamples0; |
| 975 | outStats->component_1_sample = tmpSamples1; |
| 976 | outStats->component_2_sample = tmpSamples2; |
| 977 | outStats->component_3_sample = tmpSamples3; |
| 978 | } |
| 979 | }); |
| 980 | return error; |
| 981 | } |
| 982 | |
| 983 | Error HidlComposer::setLayerPerFrameMetadataBlobs( |
| 984 | Display display, Layer layer, |
| 985 | const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) { |
| 986 | if (!mClient_2_3) { |
| 987 | return Error::UNSUPPORTED; |
| 988 | } |
| 989 | |
| 990 | mWriter.selectDisplay(display); |
| 991 | mWriter.selectLayer(layer); |
| 992 | mWriter.setLayerPerFrameMetadataBlobs(metadata); |
| 993 | return Error::NONE; |
| 994 | } |
| 995 | |
| 996 | Error HidlComposer::setDisplayBrightness(Display display, float brightness) { |
| 997 | if (!mClient_2_3) { |
| 998 | return Error::UNSUPPORTED; |
| 999 | } |
| 1000 | return mClient_2_3->setDisplayBrightness(display, brightness); |
| 1001 | } |
| 1002 | |
| 1003 | // Composer HAL 2.4 |
| 1004 | |
| 1005 | Error HidlComposer::getDisplayCapabilities(Display display, |
| 1006 | std::vector<DisplayCapability>* outCapabilities) { |
| 1007 | if (!mClient_2_3) { |
| 1008 | return Error::UNSUPPORTED; |
| 1009 | } |
| 1010 | |
| 1011 | V2_4::Error error = kDefaultError_2_4; |
| 1012 | if (mClient_2_4) { |
| 1013 | mClient_2_4->getDisplayCapabilities_2_4(display, |
| 1014 | [&](const auto& tmpError, const auto& tmpCaps) { |
| 1015 | error = tmpError; |
| 1016 | if (error != V2_4::Error::NONE) { |
| 1017 | return; |
| 1018 | } |
| 1019 | *outCapabilities = tmpCaps; |
| 1020 | }); |
| 1021 | } else { |
| 1022 | mClient_2_3 |
| 1023 | ->getDisplayCapabilities(display, [&](const auto& tmpError, const auto& tmpCaps) { |
| 1024 | error = static_cast<V2_4::Error>(tmpError); |
| 1025 | if (error != V2_4::Error::NONE) { |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | outCapabilities->resize(tmpCaps.size()); |
| 1030 | std::transform(tmpCaps.begin(), tmpCaps.end(), outCapabilities->begin(), |
| 1031 | [](auto cap) { return static_cast<DisplayCapability>(cap); }); |
| 1032 | }); |
| 1033 | } |
| 1034 | |
| 1035 | return static_cast<Error>(error); |
| 1036 | } |
| 1037 | |
| 1038 | V2_4::Error HidlComposer::getDisplayConnectionType( |
| 1039 | Display display, IComposerClient::DisplayConnectionType* outType) { |
| 1040 | using Error = V2_4::Error; |
| 1041 | if (!mClient_2_4) { |
| 1042 | return Error::UNSUPPORTED; |
| 1043 | } |
| 1044 | |
| 1045 | Error error = kDefaultError_2_4; |
| 1046 | mClient_2_4->getDisplayConnectionType(display, [&](const auto& tmpError, const auto& tmpType) { |
| 1047 | error = tmpError; |
| 1048 | if (error != V2_4::Error::NONE) { |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | *outType = tmpType; |
| 1053 | }); |
| 1054 | |
| 1055 | return error; |
| 1056 | } |
| 1057 | |
| 1058 | V2_4::Error HidlComposer::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) { |
| 1059 | using Error = V2_4::Error; |
| 1060 | if (!mClient_2_4) { |
| 1061 | return Error::UNSUPPORTED; |
| 1062 | } |
| 1063 | |
| 1064 | Error error = kDefaultError_2_4; |
| 1065 | mClient_2_4->getDisplayVsyncPeriod(display, |
| 1066 | [&](const auto& tmpError, const auto& tmpVsyncPeriod) { |
| 1067 | error = tmpError; |
| 1068 | if (error != Error::NONE) { |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | *outVsyncPeriod = tmpVsyncPeriod; |
| 1073 | }); |
| 1074 | |
| 1075 | return error; |
| 1076 | } |
| 1077 | |
| 1078 | V2_4::Error HidlComposer::setActiveConfigWithConstraints( |
| 1079 | Display display, Config config, |
| 1080 | const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints, |
| 1081 | VsyncPeriodChangeTimeline* outTimeline) { |
| 1082 | using Error = V2_4::Error; |
| 1083 | if (!mClient_2_4) { |
| 1084 | return Error::UNSUPPORTED; |
| 1085 | } |
| 1086 | |
| 1087 | Error error = kDefaultError_2_4; |
| 1088 | mClient_2_4->setActiveConfigWithConstraints(display, config, vsyncPeriodChangeConstraints, |
| 1089 | [&](const auto& tmpError, const auto& tmpTimeline) { |
| 1090 | error = tmpError; |
| 1091 | if (error != Error::NONE) { |
| 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | *outTimeline = tmpTimeline; |
| 1096 | }); |
| 1097 | |
| 1098 | return error; |
| 1099 | } |
| 1100 | |
| 1101 | V2_4::Error HidlComposer::setAutoLowLatencyMode(Display display, bool on) { |
| 1102 | using Error = V2_4::Error; |
| 1103 | if (!mClient_2_4) { |
| 1104 | return Error::UNSUPPORTED; |
| 1105 | } |
| 1106 | |
| 1107 | return mClient_2_4->setAutoLowLatencyMode(display, on); |
| 1108 | } |
| 1109 | |
| 1110 | V2_4::Error HidlComposer::getSupportedContentTypes( |
| 1111 | Display displayId, std::vector<IComposerClient::ContentType>* outSupportedContentTypes) { |
| 1112 | using Error = V2_4::Error; |
| 1113 | if (!mClient_2_4) { |
| 1114 | return Error::UNSUPPORTED; |
| 1115 | } |
| 1116 | |
| 1117 | Error error = kDefaultError_2_4; |
| 1118 | mClient_2_4->getSupportedContentTypes(displayId, |
| 1119 | [&](const auto& tmpError, |
| 1120 | const auto& tmpSupportedContentTypes) { |
| 1121 | error = tmpError; |
| 1122 | if (error != Error::NONE) { |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | *outSupportedContentTypes = tmpSupportedContentTypes; |
| 1127 | }); |
| 1128 | return error; |
| 1129 | } |
| 1130 | |
| 1131 | V2_4::Error HidlComposer::setContentType(Display display, |
| 1132 | IComposerClient::ContentType contentType) { |
| 1133 | using Error = V2_4::Error; |
| 1134 | if (!mClient_2_4) { |
| 1135 | return Error::UNSUPPORTED; |
| 1136 | } |
| 1137 | |
| 1138 | return mClient_2_4->setContentType(display, contentType); |
| 1139 | } |
| 1140 | |
| 1141 | V2_4::Error HidlComposer::setLayerGenericMetadata(Display display, Layer layer, |
| 1142 | const std::string& key, bool mandatory, |
| 1143 | const std::vector<uint8_t>& value) { |
| 1144 | using Error = V2_4::Error; |
| 1145 | if (!mClient_2_4) { |
| 1146 | return Error::UNSUPPORTED; |
| 1147 | } |
| 1148 | mWriter.selectDisplay(display); |
| 1149 | mWriter.selectLayer(layer); |
| 1150 | mWriter.setLayerGenericMetadata(key, mandatory, value); |
| 1151 | return Error::NONE; |
| 1152 | } |
| 1153 | |
| 1154 | V2_4::Error HidlComposer::getLayerGenericMetadataKeys( |
| 1155 | std::vector<IComposerClient::LayerGenericMetadataKey>* outKeys) { |
| 1156 | using Error = V2_4::Error; |
| 1157 | if (!mClient_2_4) { |
| 1158 | return Error::UNSUPPORTED; |
| 1159 | } |
| 1160 | Error error = kDefaultError_2_4; |
| 1161 | mClient_2_4->getLayerGenericMetadataKeys([&](const auto& tmpError, const auto& tmpKeys) { |
| 1162 | error = tmpError; |
| 1163 | if (error != Error::NONE) { |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | *outKeys = tmpKeys; |
| 1168 | }); |
| 1169 | return error; |
| 1170 | } |
| 1171 | |
| 1172 | Error HidlComposer::getClientTargetProperty( |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 1173 | Display display, IComposerClient::ClientTargetProperty* outClientTargetProperty, |
| 1174 | float* outWhitePointNits) { |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 1175 | mReader.takeClientTargetProperty(display, outClientTargetProperty); |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 1176 | *outWhitePointNits = -1.f; |
| 1177 | return Error::NONE; |
| 1178 | } |
| 1179 | |
| 1180 | Error HidlComposer::setLayerWhitePointNits(Display, Layer, float) { |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 1181 | return Error::NONE; |
| 1182 | } |
| 1183 | |
| 1184 | CommandReader::~CommandReader() { |
| 1185 | resetData(); |
| 1186 | } |
| 1187 | |
| 1188 | Error CommandReader::parse() { |
| 1189 | resetData(); |
| 1190 | |
| 1191 | IComposerClient::Command command; |
| 1192 | uint16_t length = 0; |
| 1193 | |
| 1194 | while (!isEmpty()) { |
| 1195 | if (!beginCommand(&command, &length)) { |
| 1196 | break; |
| 1197 | } |
| 1198 | |
| 1199 | bool parsed = false; |
| 1200 | switch (command) { |
| 1201 | case IComposerClient::Command::SELECT_DISPLAY: |
| 1202 | parsed = parseSelectDisplay(length); |
| 1203 | break; |
| 1204 | case IComposerClient::Command::SET_ERROR: |
| 1205 | parsed = parseSetError(length); |
| 1206 | break; |
| 1207 | case IComposerClient::Command::SET_CHANGED_COMPOSITION_TYPES: |
| 1208 | parsed = parseSetChangedCompositionTypes(length); |
| 1209 | break; |
| 1210 | case IComposerClient::Command::SET_DISPLAY_REQUESTS: |
| 1211 | parsed = parseSetDisplayRequests(length); |
| 1212 | break; |
| 1213 | case IComposerClient::Command::SET_PRESENT_FENCE: |
| 1214 | parsed = parseSetPresentFence(length); |
| 1215 | break; |
| 1216 | case IComposerClient::Command::SET_RELEASE_FENCES: |
| 1217 | parsed = parseSetReleaseFences(length); |
| 1218 | break; |
| 1219 | case IComposerClient::Command ::SET_PRESENT_OR_VALIDATE_DISPLAY_RESULT: |
| 1220 | parsed = parseSetPresentOrValidateDisplayResult(length); |
| 1221 | break; |
| 1222 | case IComposerClient::Command::SET_CLIENT_TARGET_PROPERTY: |
| 1223 | parsed = parseSetClientTargetProperty(length); |
| 1224 | break; |
| 1225 | default: |
| 1226 | parsed = false; |
| 1227 | break; |
| 1228 | } |
| 1229 | |
| 1230 | endCommand(); |
| 1231 | |
| 1232 | if (!parsed) { |
| 1233 | ALOGE("failed to parse command 0x%x length %" PRIu16, command, length); |
| 1234 | break; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return isEmpty() ? Error::NONE : Error::NO_RESOURCES; |
| 1239 | } |
| 1240 | |
| 1241 | bool CommandReader::parseSelectDisplay(uint16_t length) { |
| 1242 | if (length != CommandWriterBase::kSelectDisplayLength) { |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
| 1246 | mCurrentReturnData = &mReturnData[read64()]; |
| 1247 | |
| 1248 | return true; |
| 1249 | } |
| 1250 | |
| 1251 | bool CommandReader::parseSetError(uint16_t length) { |
| 1252 | if (length != CommandWriterBase::kSetErrorLength) { |
| 1253 | return false; |
| 1254 | } |
| 1255 | |
| 1256 | auto location = read(); |
| 1257 | auto error = static_cast<Error>(readSigned()); |
| 1258 | |
| 1259 | mErrors.emplace_back(CommandError{location, error}); |
| 1260 | |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
| 1264 | bool CommandReader::parseSetChangedCompositionTypes(uint16_t length) { |
| 1265 | // (layer id, composition type) pairs |
| 1266 | if (length % 3 != 0 || !mCurrentReturnData) { |
| 1267 | return false; |
| 1268 | } |
| 1269 | |
| 1270 | uint32_t count = length / 3; |
| 1271 | mCurrentReturnData->changedLayers.reserve(count); |
| 1272 | mCurrentReturnData->compositionTypes.reserve(count); |
| 1273 | while (count > 0) { |
| 1274 | auto layer = read64(); |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame^] | 1275 | auto type = static_cast<aidl::android::hardware::graphics::composer3::Composition>( |
| 1276 | readSigned()); |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 1277 | |
| 1278 | mCurrentReturnData->changedLayers.push_back(layer); |
| 1279 | mCurrentReturnData->compositionTypes.push_back(type); |
| 1280 | |
| 1281 | count--; |
| 1282 | } |
| 1283 | |
| 1284 | return true; |
| 1285 | } |
| 1286 | |
| 1287 | bool CommandReader::parseSetDisplayRequests(uint16_t length) { |
| 1288 | // display requests followed by (layer id, layer requests) pairs |
| 1289 | if (length % 3 != 1 || !mCurrentReturnData) { |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
| 1293 | mCurrentReturnData->displayRequests = read(); |
| 1294 | |
| 1295 | uint32_t count = (length - 1) / 3; |
| 1296 | mCurrentReturnData->requestedLayers.reserve(count); |
| 1297 | mCurrentReturnData->requestMasks.reserve(count); |
| 1298 | while (count > 0) { |
| 1299 | auto layer = read64(); |
| 1300 | auto layerRequestMask = read(); |
| 1301 | |
| 1302 | mCurrentReturnData->requestedLayers.push_back(layer); |
| 1303 | mCurrentReturnData->requestMasks.push_back(layerRequestMask); |
| 1304 | |
| 1305 | count--; |
| 1306 | } |
| 1307 | |
| 1308 | return true; |
| 1309 | } |
| 1310 | |
| 1311 | bool CommandReader::parseSetPresentFence(uint16_t length) { |
| 1312 | if (length != CommandWriterBase::kSetPresentFenceLength || !mCurrentReturnData) { |
| 1313 | return false; |
| 1314 | } |
| 1315 | |
| 1316 | if (mCurrentReturnData->presentFence >= 0) { |
| 1317 | close(mCurrentReturnData->presentFence); |
| 1318 | } |
| 1319 | mCurrentReturnData->presentFence = readFence(); |
| 1320 | |
| 1321 | return true; |
| 1322 | } |
| 1323 | |
| 1324 | bool CommandReader::parseSetReleaseFences(uint16_t length) { |
| 1325 | // (layer id, release fence index) pairs |
| 1326 | if (length % 3 != 0 || !mCurrentReturnData) { |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
| 1330 | uint32_t count = length / 3; |
| 1331 | mCurrentReturnData->releasedLayers.reserve(count); |
| 1332 | mCurrentReturnData->releaseFences.reserve(count); |
| 1333 | while (count > 0) { |
| 1334 | auto layer = read64(); |
| 1335 | auto fence = readFence(); |
| 1336 | |
| 1337 | mCurrentReturnData->releasedLayers.push_back(layer); |
| 1338 | mCurrentReturnData->releaseFences.push_back(fence); |
| 1339 | |
| 1340 | count--; |
| 1341 | } |
| 1342 | |
| 1343 | return true; |
| 1344 | } |
| 1345 | |
| 1346 | bool CommandReader::parseSetPresentOrValidateDisplayResult(uint16_t length) { |
| 1347 | if (length != CommandWriterBase::kPresentOrValidateDisplayResultLength || !mCurrentReturnData) { |
| 1348 | return false; |
| 1349 | } |
| 1350 | mCurrentReturnData->presentOrValidateState = read(); |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
| 1354 | bool CommandReader::parseSetClientTargetProperty(uint16_t length) { |
| 1355 | if (length != CommandWriterBase::kSetClientTargetPropertyLength || !mCurrentReturnData) { |
| 1356 | return false; |
| 1357 | } |
| 1358 | mCurrentReturnData->clientTargetProperty.pixelFormat = static_cast<PixelFormat>(readSigned()); |
| 1359 | mCurrentReturnData->clientTargetProperty.dataspace = static_cast<Dataspace>(readSigned()); |
| 1360 | return true; |
| 1361 | } |
| 1362 | |
| 1363 | void CommandReader::resetData() { |
| 1364 | mErrors.clear(); |
| 1365 | |
| 1366 | for (auto& data : mReturnData) { |
| 1367 | if (data.second.presentFence >= 0) { |
| 1368 | close(data.second.presentFence); |
| 1369 | } |
| 1370 | for (auto fence : data.second.releaseFences) { |
| 1371 | if (fence >= 0) { |
| 1372 | close(fence); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | mReturnData.clear(); |
| 1378 | mCurrentReturnData = nullptr; |
| 1379 | } |
| 1380 | |
| 1381 | std::vector<CommandReader::CommandError> CommandReader::takeErrors() { |
| 1382 | return std::move(mErrors); |
| 1383 | } |
| 1384 | |
| 1385 | bool CommandReader::hasChanges(Display display, uint32_t* outNumChangedCompositionTypes, |
| 1386 | uint32_t* outNumLayerRequestMasks) const { |
| 1387 | auto found = mReturnData.find(display); |
| 1388 | if (found == mReturnData.end()) { |
| 1389 | *outNumChangedCompositionTypes = 0; |
| 1390 | *outNumLayerRequestMasks = 0; |
| 1391 | return false; |
| 1392 | } |
| 1393 | |
| 1394 | const ReturnData& data = found->second; |
| 1395 | |
| 1396 | *outNumChangedCompositionTypes = data.compositionTypes.size(); |
| 1397 | *outNumLayerRequestMasks = data.requestMasks.size(); |
| 1398 | |
| 1399 | return !(data.compositionTypes.empty() && data.requestMasks.empty()); |
| 1400 | } |
| 1401 | |
| 1402 | void CommandReader::takeChangedCompositionTypes( |
| 1403 | Display display, std::vector<Layer>* outLayers, |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame^] | 1404 | std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) { |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 1405 | auto found = mReturnData.find(display); |
| 1406 | if (found == mReturnData.end()) { |
| 1407 | outLayers->clear(); |
| 1408 | outTypes->clear(); |
| 1409 | return; |
| 1410 | } |
| 1411 | |
| 1412 | ReturnData& data = found->second; |
| 1413 | |
| 1414 | *outLayers = std::move(data.changedLayers); |
| 1415 | *outTypes = std::move(data.compositionTypes); |
| 1416 | } |
| 1417 | |
| 1418 | void CommandReader::takeDisplayRequests(Display display, uint32_t* outDisplayRequestMask, |
| 1419 | std::vector<Layer>* outLayers, |
| 1420 | std::vector<uint32_t>* outLayerRequestMasks) { |
| 1421 | auto found = mReturnData.find(display); |
| 1422 | if (found == mReturnData.end()) { |
| 1423 | *outDisplayRequestMask = 0; |
| 1424 | outLayers->clear(); |
| 1425 | outLayerRequestMasks->clear(); |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | ReturnData& data = found->second; |
| 1430 | |
| 1431 | *outDisplayRequestMask = data.displayRequests; |
| 1432 | *outLayers = std::move(data.requestedLayers); |
| 1433 | *outLayerRequestMasks = std::move(data.requestMasks); |
| 1434 | } |
| 1435 | |
| 1436 | void CommandReader::takeReleaseFences(Display display, std::vector<Layer>* outLayers, |
| 1437 | std::vector<int>* outReleaseFences) { |
| 1438 | auto found = mReturnData.find(display); |
| 1439 | if (found == mReturnData.end()) { |
| 1440 | outLayers->clear(); |
| 1441 | outReleaseFences->clear(); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | ReturnData& data = found->second; |
| 1446 | |
| 1447 | *outLayers = std::move(data.releasedLayers); |
| 1448 | *outReleaseFences = std::move(data.releaseFences); |
| 1449 | } |
| 1450 | |
| 1451 | void CommandReader::takePresentFence(Display display, int* outPresentFence) { |
| 1452 | auto found = mReturnData.find(display); |
| 1453 | if (found == mReturnData.end()) { |
| 1454 | *outPresentFence = -1; |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | ReturnData& data = found->second; |
| 1459 | |
| 1460 | *outPresentFence = data.presentFence; |
| 1461 | data.presentFence = -1; |
| 1462 | } |
| 1463 | |
| 1464 | void CommandReader::takePresentOrValidateStage(Display display, uint32_t* state) { |
| 1465 | auto found = mReturnData.find(display); |
| 1466 | if (found == mReturnData.end()) { |
| 1467 | *state = -1; |
| 1468 | return; |
| 1469 | } |
| 1470 | ReturnData& data = found->second; |
| 1471 | *state = data.presentOrValidateState; |
| 1472 | } |
| 1473 | |
| 1474 | void CommandReader::takeClientTargetProperty( |
| 1475 | Display display, IComposerClient::ClientTargetProperty* outClientTargetProperty) { |
| 1476 | auto found = mReturnData.find(display); |
| 1477 | |
| 1478 | // If not found, return the default values. |
| 1479 | if (found == mReturnData.end()) { |
| 1480 | outClientTargetProperty->pixelFormat = PixelFormat::RGBA_8888; |
| 1481 | outClientTargetProperty->dataspace = Dataspace::UNKNOWN; |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | ReturnData& data = found->second; |
| 1486 | *outClientTargetProperty = data.clientTargetProperty; |
| 1487 | } |
| 1488 | |
| 1489 | } // namespace Hwc2 |
| 1490 | } // namespace android |
| 1491 | |
| 1492 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 1493 | #pragma clang diagnostic pop // ignored "-Wconversion" |