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