| 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 |  | 
| Fabien Sanglard | 249c0ae | 2017-06-19 19:22:36 -0700 | [diff] [blame] | 698 | Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests, | 
|  | 699 | sp<android::Fence>* outPresentFence, uint32_t* state) { | 
|  | 700 |  | 
|  | 701 | uint32_t numTypes = 0; | 
|  | 702 | uint32_t numRequests = 0; | 
|  | 703 | int32_t presentFenceFd = -1; | 
|  | 704 | auto intError = mDevice.mComposer->presentOrValidateDisplay(mId, &numTypes, &numRequests, &presentFenceFd, state); | 
|  | 705 | auto error = static_cast<Error>(intError); | 
|  | 706 | if (error != Error::None && error != Error::HasChanges) { | 
|  | 707 | return error; | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 | if (*state == 1) { | 
|  | 711 | *outPresentFence = new Fence(presentFenceFd); | 
|  | 712 | } | 
|  | 713 |  | 
|  | 714 | if (*state == 0) { | 
|  | 715 | *outNumTypes = numTypes; | 
|  | 716 | *outNumRequests = numRequests; | 
|  | 717 | } | 
|  | 718 | return error; | 
|  | 719 | } | 
| Chia-I Wu | 0c6ce46 | 2017-06-22 10:48:28 -0700 | [diff] [blame] | 720 |  | 
|  | 721 | void Display::discardCommands() | 
|  | 722 | { | 
|  | 723 | mDevice.mComposer->resetCommands(); | 
|  | 724 | } | 
|  | 725 |  | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 726 | // For use by Device | 
|  | 727 |  | 
|  | 728 | int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute) | 
|  | 729 | { | 
|  | 730 | int32_t value = 0; | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 731 | auto intError = mDevice.mComposer->getDisplayAttribute(mId, configId, | 
|  | 732 | static_cast<Hwc2::IComposerClient::Attribute>(attribute), | 
|  | 733 | &value); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 734 | auto error = static_cast<Error>(intError); | 
|  | 735 | if (error != Error::None) { | 
|  | 736 | ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId, | 
|  | 737 | configId, to_string(attribute).c_str(), | 
|  | 738 | to_string(error).c_str(), intError); | 
|  | 739 | return -1; | 
|  | 740 | } | 
|  | 741 | return value; | 
|  | 742 | } | 
|  | 743 |  | 
|  | 744 | void Display::loadConfig(hwc2_config_t configId) | 
|  | 745 | { | 
|  | 746 | ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId); | 
|  | 747 |  | 
|  | 748 | auto config = Config::Builder(*this, configId) | 
|  | 749 | .setWidth(getAttribute(configId, Attribute::Width)) | 
|  | 750 | .setHeight(getAttribute(configId, Attribute::Height)) | 
|  | 751 | .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod)) | 
|  | 752 | .setDpiX(getAttribute(configId, Attribute::DpiX)) | 
|  | 753 | .setDpiY(getAttribute(configId, Attribute::DpiY)) | 
|  | 754 | .build(); | 
|  | 755 | mConfigs.emplace(configId, std::move(config)); | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | void Display::loadConfigs() | 
|  | 759 | { | 
|  | 760 | ALOGV("[%" PRIu64 "] loadConfigs", mId); | 
|  | 761 |  | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 762 | std::vector<Hwc2::Config> configIds; | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 763 | auto intError = mDevice.mComposer->getDisplayConfigs(mId, &configIds); | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 764 | auto error = static_cast<Error>(intError); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 765 | if (error != Error::None) { | 
|  | 766 | ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId, | 
|  | 767 | to_string(error).c_str(), intError); | 
|  | 768 | return; | 
|  | 769 | } | 
|  | 770 |  | 
|  | 771 | for (auto configId : configIds) { | 
|  | 772 | loadConfig(configId); | 
|  | 773 | } | 
|  | 774 | } | 
|  | 775 |  | 
|  | 776 | // For use by Layer | 
|  | 777 |  | 
|  | 778 | void Display::destroyLayer(hwc2_layer_t layerId) | 
|  | 779 | { | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 780 | auto intError =mDevice.mComposer->destroyLayer(mId, layerId); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 781 | auto error = static_cast<Error>(intError); | 
|  | 782 | ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")" | 
|  | 783 | " failed: %s (%d)", mId, layerId, to_string(error).c_str(), | 
|  | 784 | intError); | 
|  | 785 | mLayers.erase(layerId); | 
|  | 786 | } | 
|  | 787 |  | 
|  | 788 | // Other Display methods | 
|  | 789 |  | 
|  | 790 | std::shared_ptr<Layer> Display::getLayerById(hwc2_layer_t id) const | 
|  | 791 | { | 
|  | 792 | if (mLayers.count(id) == 0) { | 
|  | 793 | return nullptr; | 
|  | 794 | } | 
|  | 795 |  | 
|  | 796 | auto layer = mLayers.at(id).lock(); | 
|  | 797 | return layer; | 
|  | 798 | } | 
|  | 799 |  | 
|  | 800 | // Layer methods | 
|  | 801 |  | 
|  | 802 | Layer::Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id) | 
|  | 803 | : mDisplay(display), | 
|  | 804 | mDisplayId(display->getId()), | 
|  | 805 | mDevice(display->getDevice()), | 
|  | 806 | mId(id) | 
|  | 807 | { | 
|  | 808 | ALOGV("Created layer %" PRIu64 " on display %" PRIu64, id, | 
|  | 809 | display->getId()); | 
|  | 810 | } | 
|  | 811 |  | 
|  | 812 | Layer::~Layer() | 
|  | 813 | { | 
|  | 814 | auto display = mDisplay.lock(); | 
|  | 815 | if (display) { | 
|  | 816 | display->destroyLayer(mId); | 
|  | 817 | } | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 | Error Layer::setCursorPosition(int32_t x, int32_t y) | 
|  | 821 | { | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 822 | auto intError = mDevice.mComposer->setCursorPosition(mDisplayId, | 
|  | 823 | mId, x, y); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 824 | return static_cast<Error>(intError); | 
|  | 825 | } | 
|  | 826 |  | 
| Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 827 | Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer, | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 828 | const sp<Fence>& acquireFence) | 
|  | 829 | { | 
|  | 830 | int32_t fenceFd = acquireFence->dup(); | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 831 | auto intError = mDevice.mComposer->setLayerBuffer(mDisplayId, | 
| Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 832 | mId, slot, buffer, fenceFd); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 833 | return static_cast<Error>(intError); | 
|  | 834 | } | 
|  | 835 |  | 
|  | 836 | Error Layer::setSurfaceDamage(const Region& damage) | 
|  | 837 | { | 
|  | 838 | // We encode default full-screen damage as INVALID_RECT upstream, but as 0 | 
|  | 839 | // rects for HWC | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 840 | Hwc2::Error intError = Hwc2::Error::NONE; | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 841 | if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) { | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 842 | intError = mDevice.mComposer->setLayerSurfaceDamage(mDisplayId, | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 843 | mId, std::vector<Hwc2::IComposerClient::Rect>()); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 844 | } else { | 
|  | 845 | size_t rectCount = 0; | 
|  | 846 | auto rectArray = damage.getArray(&rectCount); | 
|  | 847 |  | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 848 | std::vector<Hwc2::IComposerClient::Rect> hwcRects; | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 849 | for (size_t rect = 0; rect < rectCount; ++rect) { | 
|  | 850 | hwcRects.push_back({rectArray[rect].left, rectArray[rect].top, | 
|  | 851 | rectArray[rect].right, rectArray[rect].bottom}); | 
|  | 852 | } | 
|  | 853 |  | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 854 | intError = mDevice.mComposer->setLayerSurfaceDamage(mDisplayId, | 
|  | 855 | mId, hwcRects); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 856 | } | 
|  | 857 |  | 
|  | 858 | return static_cast<Error>(intError); | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | Error Layer::setBlendMode(BlendMode mode) | 
|  | 862 | { | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 863 | auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode); | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 864 | auto intError = mDevice.mComposer->setLayerBlendMode(mDisplayId, | 
|  | 865 | mId, intMode); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 866 | return static_cast<Error>(intError); | 
|  | 867 | } | 
|  | 868 |  | 
|  | 869 | Error Layer::setColor(hwc_color_t color) | 
|  | 870 | { | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 871 | 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] | 872 | auto intError = mDevice.mComposer->setLayerColor(mDisplayId, | 
|  | 873 | mId, hwcColor); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 874 | return static_cast<Error>(intError); | 
|  | 875 | } | 
|  | 876 |  | 
|  | 877 | Error Layer::setCompositionType(Composition type) | 
|  | 878 | { | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 879 | auto intType = static_cast<Hwc2::IComposerClient::Composition>(type); | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 880 | auto intError = mDevice.mComposer->setLayerCompositionType(mDisplayId, | 
|  | 881 | mId, intType); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 882 | return static_cast<Error>(intError); | 
|  | 883 | } | 
|  | 884 |  | 
| Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 885 | Error Layer::setDataspace(android_dataspace_t dataspace) | 
|  | 886 | { | 
| Courtney Goeltzenleuchter | c988ee4 | 2017-05-31 17:56:46 -0600 | [diff] [blame] | 887 | if (dataspace == mDataSpace) { | 
|  | 888 | return Error::None; | 
|  | 889 | } | 
|  | 890 | mDataSpace = dataspace; | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 891 | auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace); | 
|  | 892 | auto intError = mDevice.mComposer->setLayerDataspace(mDisplayId, | 
|  | 893 | mId, intDataspace); | 
| Dan Stoza | 5df2a86 | 2016-03-24 16:19:37 -0700 | [diff] [blame] | 894 | return static_cast<Error>(intError); | 
|  | 895 | } | 
|  | 896 |  | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 897 | Error Layer::setDisplayFrame(const Rect& frame) | 
|  | 898 | { | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 899 | Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top, | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 900 | frame.right, frame.bottom}; | 
|  | 901 | auto intError = mDevice.mComposer->setLayerDisplayFrame(mDisplayId, | 
|  | 902 | mId, hwcRect); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 903 | return static_cast<Error>(intError); | 
|  | 904 | } | 
|  | 905 |  | 
|  | 906 | Error Layer::setPlaneAlpha(float alpha) | 
|  | 907 | { | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 908 | auto intError = mDevice.mComposer->setLayerPlaneAlpha(mDisplayId, | 
|  | 909 | mId, alpha); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 910 | return static_cast<Error>(intError); | 
|  | 911 | } | 
|  | 912 |  | 
|  | 913 | Error Layer::setSidebandStream(const native_handle_t* stream) | 
|  | 914 | { | 
| Dan Stoza | 09e7a27 | 2016-04-14 12:31:01 -0700 | [diff] [blame] | 915 | if (!mDevice.hasCapability(Capability::SidebandStream)) { | 
|  | 916 | ALOGE("Attempted to call setSidebandStream without checking that the " | 
|  | 917 | "device supports sideband streams"); | 
|  | 918 | return Error::Unsupported; | 
|  | 919 | } | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 920 | auto intError = mDevice.mComposer->setLayerSidebandStream(mDisplayId, | 
|  | 921 | mId, stream); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 922 | return static_cast<Error>(intError); | 
|  | 923 | } | 
|  | 924 |  | 
|  | 925 | Error Layer::setSourceCrop(const FloatRect& crop) | 
|  | 926 | { | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 927 | Hwc2::IComposerClient::FRect hwcRect{ | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 928 | crop.left, crop.top, crop.right, crop.bottom}; | 
|  | 929 | auto intError = mDevice.mComposer->setLayerSourceCrop(mDisplayId, | 
|  | 930 | mId, hwcRect); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 931 | return static_cast<Error>(intError); | 
|  | 932 | } | 
|  | 933 |  | 
|  | 934 | Error Layer::setTransform(Transform transform) | 
|  | 935 | { | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 936 | auto intTransform = static_cast<Hwc2::Transform>(transform); | 
|  | 937 | auto intError = mDevice.mComposer->setLayerTransform(mDisplayId, | 
|  | 938 | mId, intTransform); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 939 | return static_cast<Error>(intError); | 
|  | 940 | } | 
|  | 941 |  | 
|  | 942 | Error Layer::setVisibleRegion(const Region& region) | 
|  | 943 | { | 
|  | 944 | size_t rectCount = 0; | 
|  | 945 | auto rectArray = region.getArray(&rectCount); | 
|  | 946 |  | 
| Chia-I Wu | cd8d7f0 | 2016-11-16 11:02:31 +0800 | [diff] [blame] | 947 | std::vector<Hwc2::IComposerClient::Rect> hwcRects; | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 948 | for (size_t rect = 0; rect < rectCount; ++rect) { | 
|  | 949 | hwcRects.push_back({rectArray[rect].left, rectArray[rect].top, | 
|  | 950 | rectArray[rect].right, rectArray[rect].bottom}); | 
|  | 951 | } | 
|  | 952 |  | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 953 | auto intError = mDevice.mComposer->setLayerVisibleRegion(mDisplayId, | 
|  | 954 | mId, hwcRects); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 955 | return static_cast<Error>(intError); | 
|  | 956 | } | 
|  | 957 |  | 
|  | 958 | Error Layer::setZOrder(uint32_t z) | 
|  | 959 | { | 
| Chia-I Wu | aab99f5 | 2016-10-05 12:59:58 +0800 | [diff] [blame] | 960 | auto intError = mDevice.mComposer->setLayerZOrder(mDisplayId, mId, z); | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 961 | return static_cast<Error>(intError); | 
|  | 962 | } | 
|  | 963 |  | 
| Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 964 | Error Layer::setInfo(uint32_t type, uint32_t appId) | 
|  | 965 | { | 
| Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 966 | auto intError = mDevice.mComposer->setLayerInfo(mDisplayId, mId, type, appId); | 
| Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 967 | return static_cast<Error>(intError); | 
|  | 968 | } | 
|  | 969 |  | 
| Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 970 | } // namespace HWC2 |