Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // #define LOG_NDEBUG 0 |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "HWC2" |
| 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
| 23 | #include "HWC2.h" |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 24 | #include "ComposerHal.h" |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 25 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 26 | #include <ui/Fence.h> |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 27 | #include <ui/FloatRect.h> |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 28 | #include <ui/GraphicBuffer.h> |
| 29 | #include <ui/Region.h> |
| 30 | |
| 31 | #include <android/configuration.h> |
| 32 | |
Dan Stoza | 09e7a27 | 2016-04-14 12:31:01 -0700 | [diff] [blame] | 33 | #include <algorithm> |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 34 | #include <inttypes.h> |
| 35 | |
| 36 | extern "C" { |
| 37 | static void hotplug_hook(hwc2_callback_data_t callbackData, |
| 38 | hwc2_display_t displayId, int32_t intConnected) { |
| 39 | auto device = static_cast<HWC2::Device*>(callbackData); |
| 40 | auto display = device->getDisplayById(displayId); |
| 41 | if (display) { |
| 42 | auto connected = static_cast<HWC2::Connection>(intConnected); |
| 43 | device->callHotplug(std::move(display), connected); |
| 44 | } else { |
| 45 | ALOGE("Hotplug callback called with unknown display %" PRIu64, |
| 46 | displayId); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void refresh_hook(hwc2_callback_data_t callbackData, |
| 51 | hwc2_display_t displayId) { |
| 52 | auto device = static_cast<HWC2::Device*>(callbackData); |
| 53 | auto display = device->getDisplayById(displayId); |
| 54 | if (display) { |
| 55 | device->callRefresh(std::move(display)); |
| 56 | } else { |
| 57 | ALOGE("Refresh callback called with unknown display %" PRIu64, |
| 58 | displayId); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static void vsync_hook(hwc2_callback_data_t callbackData, |
| 63 | hwc2_display_t displayId, int64_t timestamp) { |
| 64 | auto device = static_cast<HWC2::Device*>(callbackData); |
| 65 | auto display = device->getDisplayById(displayId); |
| 66 | if (display) { |
| 67 | device->callVsync(std::move(display), timestamp); |
| 68 | } else { |
| 69 | ALOGE("Vsync callback called with unknown display %" PRIu64, |
| 70 | displayId); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | using android::Fence; |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 76 | using android::FloatRect; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 77 | using android::GraphicBuffer; |
Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 78 | using android::HdrCapabilities; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 79 | using android::Rect; |
| 80 | using android::Region; |
| 81 | using android::sp; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 82 | using android::hardware::Return; |
| 83 | using android::hardware::Void; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 84 | |
| 85 | namespace HWC2 { |
| 86 | |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 87 | namespace Hwc2 = android::Hwc2; |
| 88 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 89 | // Device methods |
| 90 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 91 | Device::Device(bool useVrComposer) |
| 92 | : mComposer(std::make_unique<Hwc2::Composer>(useVrComposer)), |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 93 | mCapabilities(), |
| 94 | mDisplays(), |
| 95 | mHotplug(), |
| 96 | mPendingHotplugs(), |
| 97 | mRefresh(), |
| 98 | mPendingRefreshes(), |
| 99 | mVsync(), |
| 100 | mPendingVsyncs() |
| 101 | { |
| 102 | loadCapabilities(); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 103 | registerCallbacks(); |
| 104 | } |
| 105 | |
| 106 | Device::~Device() |
| 107 | { |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 108 | for (auto element : mDisplays) { |
Dan Stoza | 3862898 | 2016-07-13 15:48:58 -0700 | [diff] [blame] | 109 | auto display = element.second.lock(); |
| 110 | if (!display) { |
| 111 | ALOGE("~Device: Found a display (%" PRId64 " that has already been" |
| 112 | " destroyed", element.first); |
| 113 | continue; |
| 114 | } |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 115 | |
| 116 | DisplayType displayType = HWC2::DisplayType::Invalid; |
| 117 | auto error = display->getType(&displayType); |
| 118 | if (error != Error::None) { |
| 119 | ALOGE("~Device: Failed to determine type of display %" PRIu64 |
| 120 | ": %s (%d)", display->getId(), to_string(error).c_str(), |
| 121 | static_cast<int32_t>(error)); |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | if (displayType == HWC2::DisplayType::Physical) { |
| 126 | error = display->setVsyncEnabled(HWC2::Vsync::Disable); |
| 127 | if (error != Error::None) { |
| 128 | ALOGE("~Device: Failed to disable vsync for display %" PRIu64 |
| 129 | ": %s (%d)", display->getId(), to_string(error).c_str(), |
| 130 | static_cast<int32_t>(error)); |
| 131 | } |
| 132 | } |
| 133 | } |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Required by HWC2 device |
| 137 | |
| 138 | std::string Device::dump() const |
| 139 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 140 | return mComposer->dumpDebugInfo(); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | uint32_t Device::getMaxVirtualDisplayCount() const |
| 144 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 145 | return mComposer->getMaxVirtualDisplayCount(); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | Error Device::createVirtualDisplay(uint32_t width, uint32_t height, |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 149 | android_pixel_format_t* format, std::shared_ptr<Display>* outDisplay) |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 150 | { |
| 151 | ALOGI("Creating virtual display"); |
| 152 | |
| 153 | hwc2_display_t displayId = 0; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 154 | auto intFormat = static_cast<Hwc2::PixelFormat>(*format); |
| 155 | auto intError = mComposer->createVirtualDisplay(width, height, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 156 | &intFormat, &displayId); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 157 | auto error = static_cast<Error>(intError); |
| 158 | if (error != Error::None) { |
| 159 | return error; |
| 160 | } |
| 161 | |
| 162 | ALOGI("Created virtual display"); |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 163 | *format = static_cast<android_pixel_format_t>(intFormat); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 164 | *outDisplay = getDisplayById(displayId); |
Dan Stoza | 3862898 | 2016-07-13 15:48:58 -0700 | [diff] [blame] | 165 | if (!*outDisplay) { |
| 166 | ALOGE("Failed to get display by id"); |
| 167 | return Error::BadDisplay; |
| 168 | } |
Chris Forbes | ceb67d1 | 2017-04-11 12:20:00 -0700 | [diff] [blame] | 169 | (*outDisplay)->setConnected(true); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 170 | return Error::None; |
| 171 | } |
| 172 | |
| 173 | void Device::registerHotplugCallback(HotplugCallback hotplug) |
| 174 | { |
| 175 | ALOGV("registerHotplugCallback"); |
| 176 | mHotplug = hotplug; |
| 177 | for (auto& pending : mPendingHotplugs) { |
| 178 | auto& display = pending.first; |
| 179 | auto connected = pending.second; |
| 180 | ALOGV("Sending pending hotplug(%" PRIu64 ", %s)", display->getId(), |
| 181 | to_string(connected).c_str()); |
| 182 | mHotplug(std::move(display), connected); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void Device::registerRefreshCallback(RefreshCallback refresh) |
| 187 | { |
| 188 | mRefresh = refresh; |
| 189 | for (auto& pending : mPendingRefreshes) { |
| 190 | mRefresh(std::move(pending)); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void Device::registerVsyncCallback(VsyncCallback vsync) |
| 195 | { |
| 196 | mVsync = vsync; |
| 197 | for (auto& pending : mPendingVsyncs) { |
| 198 | auto& display = pending.first; |
| 199 | auto timestamp = pending.second; |
| 200 | mVsync(std::move(display), timestamp); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // For use by Device callbacks |
| 205 | |
| 206 | void Device::callHotplug(std::shared_ptr<Display> display, Connection connected) |
| 207 | { |
| 208 | if (connected == Connection::Connected) { |
| 209 | if (!display->isConnected()) { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 210 | mComposer->setClientTargetSlotCount(display->getId()); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 211 | display->loadConfigs(); |
| 212 | display->setConnected(true); |
| 213 | } |
| 214 | } else { |
| 215 | display->setConnected(false); |
| 216 | mDisplays.erase(display->getId()); |
| 217 | } |
| 218 | |
| 219 | if (mHotplug) { |
| 220 | mHotplug(std::move(display), connected); |
| 221 | } else { |
| 222 | ALOGV("callHotplug called, but no valid callback registered, storing"); |
| 223 | mPendingHotplugs.emplace_back(std::move(display), connected); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void Device::callRefresh(std::shared_ptr<Display> display) |
| 228 | { |
| 229 | if (mRefresh) { |
| 230 | mRefresh(std::move(display)); |
| 231 | } else { |
| 232 | ALOGV("callRefresh called, but no valid callback registered, storing"); |
| 233 | mPendingRefreshes.emplace_back(std::move(display)); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void Device::callVsync(std::shared_ptr<Display> display, nsecs_t timestamp) |
| 238 | { |
| 239 | if (mVsync) { |
| 240 | mVsync(std::move(display), timestamp); |
| 241 | } else { |
| 242 | ALOGV("callVsync called, but no valid callback registered, storing"); |
| 243 | mPendingVsyncs.emplace_back(std::move(display), timestamp); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Other Device methods |
| 248 | |
| 249 | std::shared_ptr<Display> Device::getDisplayById(hwc2_display_t id) { |
| 250 | if (mDisplays.count(id) != 0) { |
Dan Stoza | 3862898 | 2016-07-13 15:48:58 -0700 | [diff] [blame] | 251 | auto strongDisplay = mDisplays[id].lock(); |
| 252 | ALOGE_IF(!strongDisplay, "Display %" PRId64 " is in mDisplays but is no" |
| 253 | " longer alive", id); |
| 254 | return strongDisplay; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | auto display = std::make_shared<Display>(*this, id); |
| 258 | mDisplays.emplace(id, display); |
| 259 | return display; |
| 260 | } |
| 261 | |
| 262 | // Device initialization methods |
| 263 | |
| 264 | void Device::loadCapabilities() |
| 265 | { |
| 266 | static_assert(sizeof(Capability) == sizeof(int32_t), |
| 267 | "Capability size has changed"); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 268 | auto capabilities = mComposer->getCapabilities(); |
| 269 | for (auto capability : capabilities) { |
| 270 | mCapabilities.emplace(static_cast<Capability>(capability)); |
| 271 | } |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Dan Stoza | 09e7a27 | 2016-04-14 12:31:01 -0700 | [diff] [blame] | 274 | bool Device::hasCapability(HWC2::Capability capability) const |
| 275 | { |
| 276 | return std::find(mCapabilities.cbegin(), mCapabilities.cend(), |
| 277 | capability) != mCapabilities.cend(); |
| 278 | } |
| 279 | |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 280 | namespace { |
| 281 | class ComposerCallback : public Hwc2::IComposerCallback { |
| 282 | public: |
| 283 | ComposerCallback(Device* device) : mDevice(device) {} |
| 284 | |
| 285 | Return<void> onHotplug(Hwc2::Display display, |
| 286 | Connection connected) override |
| 287 | { |
| 288 | hotplug_hook(mDevice, display, static_cast<int32_t>(connected)); |
| 289 | return Void(); |
| 290 | } |
| 291 | |
| 292 | Return<void> onRefresh(Hwc2::Display display) override |
| 293 | { |
| 294 | refresh_hook(mDevice, display); |
| 295 | return Void(); |
| 296 | } |
| 297 | |
| 298 | Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override |
| 299 | { |
| 300 | vsync_hook(mDevice, display, timestamp); |
| 301 | return Void(); |
| 302 | } |
| 303 | |
| 304 | private: |
| 305 | Device* mDevice; |
| 306 | }; |
| 307 | } // namespace anonymous |
| 308 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 309 | void Device::registerCallbacks() |
| 310 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 311 | sp<ComposerCallback> callback = new ComposerCallback(this); |
| 312 | mComposer->registerCallback(callback); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | // For use by Display |
| 317 | |
| 318 | void Device::destroyVirtualDisplay(hwc2_display_t display) |
| 319 | { |
| 320 | ALOGI("Destroying virtual display"); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 321 | auto intError = mComposer->destroyVirtualDisplay(display); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 322 | auto error = static_cast<Error>(intError); |
| 323 | ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64 ") failed:" |
| 324 | " %s (%d)", display, to_string(error).c_str(), intError); |
Dan Stoza | 3862898 | 2016-07-13 15:48:58 -0700 | [diff] [blame] | 325 | mDisplays.erase(display); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Display methods |
| 329 | |
| 330 | Display::Display(Device& device, hwc2_display_t id) |
| 331 | : mDevice(device), |
| 332 | mId(id), |
| 333 | mIsConnected(false), |
Chris Forbes | 016d73c | 2017-04-11 10:04:31 -0700 | [diff] [blame] | 334 | mType(DisplayType::Invalid) |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 335 | { |
| 336 | ALOGV("Created display %" PRIu64, id); |
Chris Forbes | 016d73c | 2017-04-11 10:04:31 -0700 | [diff] [blame] | 337 | |
Chris Forbes | 016d73c | 2017-04-11 10:04:31 -0700 | [diff] [blame] | 338 | auto intError = mDevice.mComposer->getDisplayType(mId, |
| 339 | reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(&mType)); |
Chris Forbes | 016d73c | 2017-04-11 10:04:31 -0700 | [diff] [blame] | 340 | auto error = static_cast<Error>(intError); |
| 341 | if (error != Error::None) { |
| 342 | ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d)", |
| 343 | id, to_string(error).c_str(), intError); |
| 344 | } |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | Display::~Display() |
| 348 | { |
| 349 | ALOGV("Destroyed display %" PRIu64, mId); |
Chris Forbes | ceb67d1 | 2017-04-11 12:20:00 -0700 | [diff] [blame] | 350 | if (mType == DisplayType::Virtual) { |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 351 | mDevice.destroyVirtualDisplay(mId); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | Display::Config::Config(Display& display, hwc2_config_t id) |
| 356 | : mDisplay(display), |
| 357 | mId(id), |
| 358 | mWidth(-1), |
| 359 | mHeight(-1), |
| 360 | mVsyncPeriod(-1), |
| 361 | mDpiX(-1), |
| 362 | mDpiY(-1) {} |
| 363 | |
| 364 | Display::Config::Builder::Builder(Display& display, hwc2_config_t id) |
| 365 | : mConfig(new Config(display, id)) {} |
| 366 | |
| 367 | float Display::Config::Builder::getDefaultDensity() { |
| 368 | // Default density is based on TVs: 1080p displays get XHIGH density, lower- |
| 369 | // resolution displays get TV density. Maybe eventually we'll need to update |
| 370 | // it for 4k displays, though hopefully those will just report accurate DPI |
| 371 | // information to begin with. This is also used for virtual displays and |
| 372 | // older HWC implementations, so be careful about orientation. |
| 373 | |
| 374 | auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight); |
| 375 | if (longDimension >= 1080) { |
| 376 | return ACONFIGURATION_DENSITY_XHIGH; |
| 377 | } else { |
| 378 | return ACONFIGURATION_DENSITY_TV; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // Required by HWC2 display |
| 383 | |
| 384 | Error Display::acceptChanges() |
| 385 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 386 | auto intError = mDevice.mComposer->acceptDisplayChanges(mId); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 387 | return static_cast<Error>(intError); |
| 388 | } |
| 389 | |
| 390 | Error Display::createLayer(std::shared_ptr<Layer>* outLayer) |
| 391 | { |
| 392 | hwc2_layer_t layerId = 0; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 393 | auto intError = mDevice.mComposer->createLayer(mId, &layerId); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 394 | auto error = static_cast<Error>(intError); |
| 395 | if (error != Error::None) { |
| 396 | return error; |
| 397 | } |
| 398 | |
| 399 | auto layer = std::make_shared<Layer>(shared_from_this(), layerId); |
| 400 | mLayers.emplace(layerId, layer); |
| 401 | *outLayer = std::move(layer); |
| 402 | return Error::None; |
| 403 | } |
| 404 | |
| 405 | Error Display::getActiveConfig( |
| 406 | std::shared_ptr<const Display::Config>* outConfig) const |
| 407 | { |
| 408 | ALOGV("[%" PRIu64 "] getActiveConfig", mId); |
| 409 | hwc2_config_t configId = 0; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 410 | auto intError = mDevice.mComposer->getActiveConfig(mId, &configId); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 411 | auto error = static_cast<Error>(intError); |
| 412 | |
| 413 | if (error != Error::None) { |
Fabien Sanglard | b7432cc | 2016-11-11 09:40:27 -0800 | [diff] [blame] | 414 | ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId); |
| 415 | *outConfig = nullptr; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 416 | return error; |
| 417 | } |
| 418 | |
| 419 | if (mConfigs.count(configId) != 0) { |
| 420 | *outConfig = mConfigs.at(configId); |
| 421 | } else { |
| 422 | ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId, |
| 423 | configId); |
| 424 | // Return no error, but the caller needs to check for a null pointer to |
| 425 | // detect this case |
| 426 | *outConfig = nullptr; |
| 427 | } |
| 428 | |
| 429 | return Error::None; |
| 430 | } |
| 431 | |
| 432 | Error Display::getChangedCompositionTypes( |
| 433 | std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes) |
| 434 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 435 | std::vector<Hwc2::Layer> layerIds; |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 436 | std::vector<Hwc2::IComposerClient::Composition> types; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 437 | auto intError = mDevice.mComposer->getChangedCompositionTypes(mId, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 438 | &layerIds, &types); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 439 | uint32_t numElements = layerIds.size(); |
| 440 | auto error = static_cast<Error>(intError); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 441 | error = static_cast<Error>(intError); |
| 442 | if (error != Error::None) { |
| 443 | return error; |
| 444 | } |
| 445 | |
| 446 | outTypes->clear(); |
| 447 | outTypes->reserve(numElements); |
| 448 | for (uint32_t element = 0; element < numElements; ++element) { |
| 449 | auto layer = getLayerById(layerIds[element]); |
| 450 | if (layer) { |
| 451 | auto type = static_cast<Composition>(types[element]); |
| 452 | ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s", |
| 453 | layer->getId(), to_string(type).c_str()); |
| 454 | outTypes->emplace(layer, type); |
| 455 | } else { |
| 456 | ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found" |
| 457 | " on display %" PRIu64, layerIds[element], mId); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return Error::None; |
| 462 | } |
| 463 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 464 | Error Display::getColorModes(std::vector<android_color_mode_t>* outModes) const |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 465 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 466 | std::vector<Hwc2::ColorMode> modes; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 467 | auto intError = mDevice.mComposer->getColorModes(mId, &modes); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 468 | uint32_t numModes = modes.size(); |
| 469 | auto error = static_cast<Error>(intError); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 470 | if (error != Error::None) { |
| 471 | return error; |
| 472 | } |
| 473 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 474 | outModes->resize(numModes); |
| 475 | for (size_t i = 0; i < numModes; i++) { |
| 476 | (*outModes)[i] = static_cast<android_color_mode_t>(modes[i]); |
| 477 | } |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 478 | return Error::None; |
| 479 | } |
| 480 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 481 | std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const |
| 482 | { |
| 483 | std::vector<std::shared_ptr<const Config>> configs; |
| 484 | for (const auto& element : mConfigs) { |
| 485 | configs.emplace_back(element.second); |
| 486 | } |
| 487 | return configs; |
| 488 | } |
| 489 | |
| 490 | Error Display::getName(std::string* outName) const |
| 491 | { |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 492 | auto intError = mDevice.mComposer->getDisplayName(mId, outName); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 493 | return static_cast<Error>(intError); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests, |
| 497 | std::unordered_map<std::shared_ptr<Layer>, LayerRequest>* |
| 498 | outLayerRequests) |
| 499 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 500 | uint32_t intDisplayRequests; |
| 501 | std::vector<Hwc2::Layer> layerIds; |
| 502 | std::vector<uint32_t> layerRequests; |
| 503 | auto intError = mDevice.mComposer->getDisplayRequests(mId, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 504 | &intDisplayRequests, &layerIds, &layerRequests); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 505 | uint32_t numElements = layerIds.size(); |
| 506 | auto error = static_cast<Error>(intError); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 507 | if (error != Error::None) { |
| 508 | return error; |
| 509 | } |
| 510 | |
| 511 | *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests); |
| 512 | outLayerRequests->clear(); |
| 513 | outLayerRequests->reserve(numElements); |
| 514 | for (uint32_t element = 0; element < numElements; ++element) { |
| 515 | auto layer = getLayerById(layerIds[element]); |
| 516 | if (layer) { |
| 517 | auto layerRequest = |
| 518 | static_cast<LayerRequest>(layerRequests[element]); |
| 519 | outLayerRequests->emplace(layer, layerRequest); |
| 520 | } else { |
| 521 | ALOGE("getRequests: invalid layer %" PRIu64 " found on display %" |
| 522 | PRIu64, layerIds[element], mId); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return Error::None; |
| 527 | } |
| 528 | |
| 529 | Error Display::getType(DisplayType* outType) const |
| 530 | { |
Chris Forbes | 016d73c | 2017-04-11 10:04:31 -0700 | [diff] [blame] | 531 | *outType = mType; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 532 | return Error::None; |
| 533 | } |
| 534 | |
| 535 | Error Display::supportsDoze(bool* outSupport) const |
| 536 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 537 | bool intSupport = false; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 538 | auto intError = mDevice.mComposer->getDozeSupport(mId, &intSupport); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 539 | auto error = static_cast<Error>(intError); |
| 540 | if (error != Error::None) { |
| 541 | return error; |
| 542 | } |
| 543 | *outSupport = static_cast<bool>(intSupport); |
| 544 | return Error::None; |
| 545 | } |
| 546 | |
Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 547 | Error Display::getHdrCapabilities( |
| 548 | std::unique_ptr<HdrCapabilities>* outCapabilities) const |
| 549 | { |
| 550 | uint32_t numTypes = 0; |
| 551 | float maxLuminance = -1.0f; |
| 552 | float maxAverageLuminance = -1.0f; |
| 553 | float minLuminance = -1.0f; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 554 | std::vector<Hwc2::Hdr> intTypes; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 555 | auto intError = mDevice.mComposer->getHdrCapabilities(mId, &intTypes, |
| 556 | &maxLuminance, &maxAverageLuminance, &minLuminance); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 557 | auto error = static_cast<HWC2::Error>(intError); |
| 558 | |
| 559 | std::vector<int32_t> types; |
| 560 | for (auto type : intTypes) { |
| 561 | types.push_back(static_cast<int32_t>(type)); |
| 562 | } |
| 563 | numTypes = types.size(); |
Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 564 | if (error != Error::None) { |
| 565 | return error; |
| 566 | } |
| 567 | |
| 568 | *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types), |
| 569 | maxLuminance, maxAverageLuminance, minLuminance); |
| 570 | return Error::None; |
| 571 | } |
| 572 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 573 | Error Display::getReleaseFences( |
| 574 | std::unordered_map<std::shared_ptr<Layer>, sp<Fence>>* outFences) const |
| 575 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 576 | std::vector<Hwc2::Layer> layerIds; |
| 577 | std::vector<int> fenceFds; |
| 578 | auto intError = mDevice.mComposer->getReleaseFences(mId, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 579 | &layerIds, &fenceFds); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 580 | auto error = static_cast<Error>(intError); |
| 581 | uint32_t numElements = layerIds.size(); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 582 | if (error != Error::None) { |
| 583 | return error; |
| 584 | } |
| 585 | |
| 586 | std::unordered_map<std::shared_ptr<Layer>, sp<Fence>> releaseFences; |
| 587 | releaseFences.reserve(numElements); |
| 588 | for (uint32_t element = 0; element < numElements; ++element) { |
| 589 | auto layer = getLayerById(layerIds[element]); |
| 590 | if (layer) { |
| 591 | sp<Fence> fence(new Fence(fenceFds[element])); |
| 592 | releaseFences.emplace(std::move(layer), fence); |
| 593 | } else { |
| 594 | ALOGE("getReleaseFences: invalid layer %" PRIu64 |
| 595 | " found on display %" PRIu64, layerIds[element], mId); |
Chia-I Wu | 5e74c65 | 2017-05-17 13:43:16 -0700 | [diff] [blame] | 596 | for (; element < numElements; ++element) { |
| 597 | close(fenceFds[element]); |
| 598 | } |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 599 | return Error::BadLayer; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | *outFences = std::move(releaseFences); |
| 604 | return Error::None; |
| 605 | } |
| 606 | |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 607 | Error Display::present(sp<Fence>* outPresentFence) |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 608 | { |
Naseer Ahmed | 847650b | 2016-06-17 11:14:25 -0400 | [diff] [blame] | 609 | int32_t presentFenceFd = -1; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 610 | auto intError = mDevice.mComposer->presentDisplay(mId, &presentFenceFd); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 611 | auto error = static_cast<Error>(intError); |
| 612 | if (error != Error::None) { |
| 613 | return error; |
| 614 | } |
| 615 | |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 616 | *outPresentFence = new Fence(presentFenceFd); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 617 | return Error::None; |
| 618 | } |
| 619 | |
| 620 | Error Display::setActiveConfig(const std::shared_ptr<const Config>& config) |
| 621 | { |
| 622 | if (config->getDisplayId() != mId) { |
| 623 | ALOGE("setActiveConfig received config %u for the wrong display %" |
| 624 | PRIu64 " (expected %" PRIu64 ")", config->getId(), |
| 625 | config->getDisplayId(), mId); |
| 626 | return Error::BadConfig; |
| 627 | } |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 628 | auto intError = mDevice.mComposer->setActiveConfig(mId, config->getId()); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 629 | return static_cast<Error>(intError); |
| 630 | } |
| 631 | |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 632 | Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target, |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 633 | const sp<Fence>& acquireFence, android_dataspace_t dataspace) |
| 634 | { |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 635 | // TODO: Properly encode client target surface damage |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 636 | int32_t fenceFd = acquireFence->dup(); |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 637 | auto intError = mDevice.mComposer->setClientTarget(mId, slot, target, |
| 638 | fenceFd, static_cast<Hwc2::Dataspace>(dataspace), |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 639 | std::vector<Hwc2::IComposerClient::Rect>()); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 640 | return static_cast<Error>(intError); |
| 641 | } |
| 642 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 643 | Error Display::setColorMode(android_color_mode_t mode) |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 644 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 645 | auto intError = mDevice.mComposer->setColorMode(mId, |
| 646 | static_cast<Hwc2::ColorMode>(mode)); |
Dan Stoza | 076ac67 | 2016-03-14 10:47:53 -0700 | [diff] [blame] | 647 | return static_cast<Error>(intError); |
| 648 | } |
| 649 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 650 | Error Display::setColorTransform(const android::mat4& matrix, |
| 651 | android_color_transform_t hint) |
| 652 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 653 | auto intError = mDevice.mComposer->setColorTransform(mId, |
| 654 | matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint)); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 655 | return static_cast<Error>(intError); |
| 656 | } |
| 657 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 658 | Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer, |
| 659 | const sp<Fence>& releaseFence) |
| 660 | { |
| 661 | int32_t fenceFd = releaseFence->dup(); |
| 662 | auto handle = buffer->getNativeBuffer()->handle; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 663 | auto intError = mDevice.mComposer->setOutputBuffer(mId, handle, fenceFd); |
Dan Stoza | 3862898 | 2016-07-13 15:48:58 -0700 | [diff] [blame] | 664 | close(fenceFd); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 665 | return static_cast<Error>(intError); |
| 666 | } |
| 667 | |
| 668 | Error Display::setPowerMode(PowerMode mode) |
| 669 | { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 670 | auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 671 | auto intError = mDevice.mComposer->setPowerMode(mId, intMode); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 672 | return static_cast<Error>(intError); |
| 673 | } |
| 674 | |
| 675 | Error Display::setVsyncEnabled(Vsync enabled) |
| 676 | { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 677 | auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 678 | auto intError = mDevice.mComposer->setVsyncEnabled(mId, intEnabled); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 679 | return static_cast<Error>(intError); |
| 680 | } |
| 681 | |
| 682 | Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests) |
| 683 | { |
| 684 | uint32_t numTypes = 0; |
| 685 | uint32_t numRequests = 0; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 686 | auto intError = mDevice.mComposer->validateDisplay(mId, |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 687 | &numTypes, &numRequests); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 688 | auto error = static_cast<Error>(intError); |
| 689 | if (error != Error::None && error != Error::HasChanges) { |
| 690 | return error; |
| 691 | } |
| 692 | |
| 693 | *outNumTypes = numTypes; |
| 694 | *outNumRequests = numRequests; |
| 695 | return error; |
| 696 | } |
| 697 | |
| 698 | // For use by Device |
| 699 | |
| 700 | int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute) |
| 701 | { |
| 702 | int32_t value = 0; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 703 | auto intError = mDevice.mComposer->getDisplayAttribute(mId, configId, |
| 704 | static_cast<Hwc2::IComposerClient::Attribute>(attribute), |
| 705 | &value); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 706 | auto error = static_cast<Error>(intError); |
| 707 | if (error != Error::None) { |
| 708 | ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId, |
| 709 | configId, to_string(attribute).c_str(), |
| 710 | to_string(error).c_str(), intError); |
| 711 | return -1; |
| 712 | } |
| 713 | return value; |
| 714 | } |
| 715 | |
| 716 | void Display::loadConfig(hwc2_config_t configId) |
| 717 | { |
| 718 | ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId); |
| 719 | |
| 720 | auto config = Config::Builder(*this, configId) |
| 721 | .setWidth(getAttribute(configId, Attribute::Width)) |
| 722 | .setHeight(getAttribute(configId, Attribute::Height)) |
| 723 | .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod)) |
| 724 | .setDpiX(getAttribute(configId, Attribute::DpiX)) |
| 725 | .setDpiY(getAttribute(configId, Attribute::DpiY)) |
| 726 | .build(); |
| 727 | mConfigs.emplace(configId, std::move(config)); |
| 728 | } |
| 729 | |
| 730 | void Display::loadConfigs() |
| 731 | { |
| 732 | ALOGV("[%" PRIu64 "] loadConfigs", mId); |
| 733 | |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 734 | std::vector<Hwc2::Config> configIds; |
Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 735 | auto intError = mDevice.mComposer->getDisplayConfigs(mId, &configIds); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 736 | auto error = static_cast<Error>(intError); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 737 | if (error != Error::None) { |
| 738 | ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId, |
| 739 | to_string(error).c_str(), intError); |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | for (auto configId : configIds) { |
| 744 | loadConfig(configId); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // For use by Layer |
| 749 | |
| 750 | void Display::destroyLayer(hwc2_layer_t layerId) |
| 751 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 752 | auto intError =mDevice.mComposer->destroyLayer(mId, layerId); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 753 | auto error = static_cast<Error>(intError); |
| 754 | ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")" |
| 755 | " failed: %s (%d)", mId, layerId, to_string(error).c_str(), |
| 756 | intError); |
| 757 | mLayers.erase(layerId); |
| 758 | } |
| 759 | |
| 760 | // Other Display methods |
| 761 | |
| 762 | std::shared_ptr<Layer> Display::getLayerById(hwc2_layer_t id) const |
| 763 | { |
| 764 | if (mLayers.count(id) == 0) { |
| 765 | return nullptr; |
| 766 | } |
| 767 | |
| 768 | auto layer = mLayers.at(id).lock(); |
| 769 | return layer; |
| 770 | } |
| 771 | |
| 772 | // Layer methods |
| 773 | |
| 774 | Layer::Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id) |
| 775 | : mDisplay(display), |
| 776 | mDisplayId(display->getId()), |
| 777 | mDevice(display->getDevice()), |
| 778 | mId(id) |
| 779 | { |
| 780 | ALOGV("Created layer %" PRIu64 " on display %" PRIu64, id, |
| 781 | display->getId()); |
| 782 | } |
| 783 | |
| 784 | Layer::~Layer() |
| 785 | { |
| 786 | auto display = mDisplay.lock(); |
| 787 | if (display) { |
| 788 | display->destroyLayer(mId); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | Error Layer::setCursorPosition(int32_t x, int32_t y) |
| 793 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 794 | auto intError = mDevice.mComposer->setCursorPosition(mDisplayId, |
| 795 | mId, x, y); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 796 | return static_cast<Error>(intError); |
| 797 | } |
| 798 | |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 799 | Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer, |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 800 | const sp<Fence>& acquireFence) |
| 801 | { |
| 802 | int32_t fenceFd = acquireFence->dup(); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 803 | auto intError = mDevice.mComposer->setLayerBuffer(mDisplayId, |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 804 | mId, slot, buffer, fenceFd); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 805 | return static_cast<Error>(intError); |
| 806 | } |
| 807 | |
| 808 | Error Layer::setSurfaceDamage(const Region& damage) |
| 809 | { |
| 810 | // We encode default full-screen damage as INVALID_RECT upstream, but as 0 |
| 811 | // rects for HWC |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 812 | Hwc2::Error intError = Hwc2::Error::NONE; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 813 | if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 814 | intError = mDevice.mComposer->setLayerSurfaceDamage(mDisplayId, |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 815 | mId, std::vector<Hwc2::IComposerClient::Rect>()); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 816 | } else { |
| 817 | size_t rectCount = 0; |
| 818 | auto rectArray = damage.getArray(&rectCount); |
| 819 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 820 | std::vector<Hwc2::IComposerClient::Rect> hwcRects; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 821 | for (size_t rect = 0; rect < rectCount; ++rect) { |
| 822 | hwcRects.push_back({rectArray[rect].left, rectArray[rect].top, |
| 823 | rectArray[rect].right, rectArray[rect].bottom}); |
| 824 | } |
| 825 | |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 826 | intError = mDevice.mComposer->setLayerSurfaceDamage(mDisplayId, |
| 827 | mId, hwcRects); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | return static_cast<Error>(intError); |
| 831 | } |
| 832 | |
| 833 | Error Layer::setBlendMode(BlendMode mode) |
| 834 | { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 835 | auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 836 | auto intError = mDevice.mComposer->setLayerBlendMode(mDisplayId, |
| 837 | mId, intMode); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 838 | return static_cast<Error>(intError); |
| 839 | } |
| 840 | |
| 841 | Error Layer::setColor(hwc_color_t color) |
| 842 | { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 843 | Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a}; |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 844 | auto intError = mDevice.mComposer->setLayerColor(mDisplayId, |
| 845 | mId, hwcColor); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 846 | return static_cast<Error>(intError); |
| 847 | } |
| 848 | |
| 849 | Error Layer::setCompositionType(Composition type) |
| 850 | { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 851 | auto intType = static_cast<Hwc2::IComposerClient::Composition>(type); |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 852 | auto intError = mDevice.mComposer->setLayerCompositionType(mDisplayId, |
| 853 | mId, intType); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 854 | return static_cast<Error>(intError); |
| 855 | } |
| 856 | |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 857 | Error Layer::setDataspace(android_dataspace_t dataspace) |
| 858 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 859 | auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace); |
| 860 | auto intError = mDevice.mComposer->setLayerDataspace(mDisplayId, |
| 861 | mId, intDataspace); |
Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 862 | return static_cast<Error>(intError); |
| 863 | } |
| 864 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 865 | Error Layer::setDisplayFrame(const Rect& frame) |
| 866 | { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 867 | Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top, |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 868 | frame.right, frame.bottom}; |
| 869 | auto intError = mDevice.mComposer->setLayerDisplayFrame(mDisplayId, |
| 870 | mId, hwcRect); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 871 | return static_cast<Error>(intError); |
| 872 | } |
| 873 | |
| 874 | Error Layer::setPlaneAlpha(float alpha) |
| 875 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 876 | auto intError = mDevice.mComposer->setLayerPlaneAlpha(mDisplayId, |
| 877 | mId, alpha); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 878 | return static_cast<Error>(intError); |
| 879 | } |
| 880 | |
| 881 | Error Layer::setSidebandStream(const native_handle_t* stream) |
| 882 | { |
Dan Stoza | 09e7a27 | 2016-04-14 12:31:01 -0700 | [diff] [blame] | 883 | if (!mDevice.hasCapability(Capability::SidebandStream)) { |
| 884 | ALOGE("Attempted to call setSidebandStream without checking that the " |
| 885 | "device supports sideband streams"); |
| 886 | return Error::Unsupported; |
| 887 | } |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 888 | auto intError = mDevice.mComposer->setLayerSidebandStream(mDisplayId, |
| 889 | mId, stream); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 890 | return static_cast<Error>(intError); |
| 891 | } |
| 892 | |
| 893 | Error Layer::setSourceCrop(const FloatRect& crop) |
| 894 | { |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 895 | Hwc2::IComposerClient::FRect hwcRect{ |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 896 | crop.left, crop.top, crop.right, crop.bottom}; |
| 897 | auto intError = mDevice.mComposer->setLayerSourceCrop(mDisplayId, |
| 898 | mId, hwcRect); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 899 | return static_cast<Error>(intError); |
| 900 | } |
| 901 | |
| 902 | Error Layer::setTransform(Transform transform) |
| 903 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 904 | auto intTransform = static_cast<Hwc2::Transform>(transform); |
| 905 | auto intError = mDevice.mComposer->setLayerTransform(mDisplayId, |
| 906 | mId, intTransform); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 907 | return static_cast<Error>(intError); |
| 908 | } |
| 909 | |
| 910 | Error Layer::setVisibleRegion(const Region& region) |
| 911 | { |
| 912 | size_t rectCount = 0; |
| 913 | auto rectArray = region.getArray(&rectCount); |
| 914 | |
Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 915 | std::vector<Hwc2::IComposerClient::Rect> hwcRects; |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 916 | for (size_t rect = 0; rect < rectCount; ++rect) { |
| 917 | hwcRects.push_back({rectArray[rect].left, rectArray[rect].top, |
| 918 | rectArray[rect].right, rectArray[rect].bottom}); |
| 919 | } |
| 920 | |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 921 | auto intError = mDevice.mComposer->setLayerVisibleRegion(mDisplayId, |
| 922 | mId, hwcRects); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 923 | return static_cast<Error>(intError); |
| 924 | } |
| 925 | |
| 926 | Error Layer::setZOrder(uint32_t z) |
| 927 | { |
Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 928 | auto intError = mDevice.mComposer->setLayerZOrder(mDisplayId, mId, z); |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 929 | return static_cast<Error>(intError); |
| 930 | } |
| 931 | |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 932 | Error Layer::setInfo(uint32_t type, uint32_t appId) |
| 933 | { |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 934 | auto intError = mDevice.mComposer->setLayerInfo(mDisplayId, mId, type, appId); |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 935 | return static_cast<Error>(intError); |
| 936 | } |
| 937 | |
Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 938 | } // namespace HWC2 |