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