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