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