Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 17 | #include <android-base/stringprintf.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 18 | #include <compositionengine/CompositionEngine.h> |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 19 | #include <compositionengine/CompositionRefreshArgs.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 20 | #include <compositionengine/DisplayCreationArgs.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 21 | #include <compositionengine/DisplaySurface.h> |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 22 | #include <compositionengine/LayerFE.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 23 | #include <compositionengine/impl/Display.h> |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 24 | #include <compositionengine/impl/DisplayColorProfile.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 25 | #include <compositionengine/impl/DumpHelpers.h> |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 26 | #include <compositionengine/impl/OutputLayer.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 27 | #include <compositionengine/impl/RenderSurface.h> |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 28 | #include <utils/Trace.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 29 | |
| 30 | #include "DisplayHardware/HWComposer.h" |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 31 | #include "DisplayHardware/PowerAdvisor.h" |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android::compositionengine::impl { |
| 34 | |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 35 | std::shared_ptr<Display> createDisplay( |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 36 | const compositionengine::CompositionEngine& compositionEngine, |
| 37 | compositionengine::DisplayCreationArgs&& args) { |
| 38 | return std::make_shared<Display>(compositionEngine, std::move(args)); |
| 39 | } |
| 40 | |
| 41 | Display::Display(const CompositionEngine& compositionEngine, DisplayCreationArgs&& args) |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 42 | : compositionengine::impl::Output(compositionEngine), |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 43 | mIsVirtual(args.isVirtual), |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 44 | mId(args.displayId), |
| 45 | mPowerAdvisor(args.powerAdvisor) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 46 | editState().isSecure = args.isSecure; |
| 47 | } |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 48 | |
| 49 | Display::~Display() = default; |
| 50 | |
| 51 | const std::optional<DisplayId>& Display::getId() const { |
| 52 | return mId; |
| 53 | } |
| 54 | |
| 55 | bool Display::isSecure() const { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 56 | return getState().isSecure; |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool Display::isVirtual() const { |
| 60 | return mIsVirtual; |
| 61 | } |
| 62 | |
| 63 | void Display::disconnect() { |
| 64 | if (!mId) { |
| 65 | return; |
| 66 | } |
| 67 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 68 | auto& hwc = getCompositionEngine().getHwComposer(); |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 69 | hwc.disconnectDisplay(*mId); |
| 70 | mId.reset(); |
| 71 | } |
| 72 | |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 73 | void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) { |
| 74 | Output::setColorTransform(args); |
| 75 | |
| 76 | if (!mId || CC_LIKELY(!args.colorTransformMatrix)) { |
| 77 | return; |
| 78 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 79 | |
| 80 | auto& hwc = getCompositionEngine().getHwComposer(); |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 81 | status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 82 | ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d", |
| 83 | mId ? to_string(*mId).c_str() : "", result); |
| 84 | } |
| 85 | |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 86 | void Display::setColorProfile(const ColorProfile& colorProfile) { |
| 87 | const ui::Dataspace targetDataspace = |
| 88 | getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace, |
| 89 | colorProfile.colorSpaceAgnosticDataspace); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 90 | |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 91 | if (colorProfile.mode == getState().colorMode && |
| 92 | colorProfile.dataspace == getState().dataspace && |
| 93 | colorProfile.renderIntent == getState().renderIntent && |
| 94 | targetDataspace == getState().targetDataspace) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (mIsVirtual) { |
| 99 | ALOGW("%s: Invalid operation on virtual display", __FUNCTION__); |
| 100 | return; |
| 101 | } |
| 102 | |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 103 | Output::setColorProfile(colorProfile); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 104 | |
| 105 | auto& hwc = getCompositionEngine().getHwComposer(); |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 106 | hwc.setActiveColorMode(*mId, colorProfile.mode, colorProfile.renderIntent); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void Display::dump(std::string& out) const { |
| 110 | using android::base::StringAppendF; |
| 111 | |
| 112 | StringAppendF(&out, " Composition Display State: [\"%s\"]", getName().c_str()); |
| 113 | |
| 114 | out.append("\n "); |
| 115 | |
| 116 | dumpVal(out, "isVirtual", mIsVirtual); |
| 117 | if (mId) { |
| 118 | dumpVal(out, "hwcId", to_string(*mId)); |
| 119 | } else { |
| 120 | StringAppendF(&out, "no hwcId, "); |
| 121 | } |
| 122 | |
| 123 | out.append("\n"); |
| 124 | |
| 125 | Output::dumpBase(out); |
| 126 | } |
| 127 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 128 | void Display::createDisplayColorProfile(DisplayColorProfileCreationArgs&& args) { |
| 129 | setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(std::move(args))); |
| 130 | } |
| 131 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 132 | void Display::createRenderSurface(RenderSurfaceCreationArgs&& args) { |
| 133 | setRenderSurface(compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, |
| 134 | std::move(args))); |
| 135 | } |
| 136 | |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 137 | std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer( |
| 138 | const std::shared_ptr<compositionengine::Layer>& layer, |
| 139 | const sp<compositionengine::LayerFE>& layerFE) const { |
| 140 | auto result = Output::createOutputLayer(layer, layerFE); |
| 141 | |
| 142 | if (result && mId) { |
| 143 | auto& hwc = getCompositionEngine().getHwComposer(); |
| 144 | auto displayId = *mId; |
| 145 | // Note: For the moment we ensure it is safe to take a reference to the |
| 146 | // HWComposer implementation by destroying all the OutputLayers (and |
| 147 | // hence the HWC2::Layers they own) before setting a new HWComposer. See |
| 148 | // for example SurfaceFlinger::updateVrFlinger(). |
| 149 | // TODO(b/121291683): Make this safer. |
| 150 | auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId), |
| 151 | [&hwc, displayId](HWC2::Layer* layer) { |
| 152 | hwc.destroyLayer(displayId, layer); |
| 153 | }); |
| 154 | ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s", |
| 155 | getName().c_str()); |
| 156 | result->setHwcLayer(std::move(hwcLayer)); |
| 157 | } |
| 158 | return result; |
| 159 | } |
| 160 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 161 | void Display::chooseCompositionStrategy() { |
| 162 | ATRACE_CALL(); |
| 163 | ALOGV(__FUNCTION__); |
| 164 | |
| 165 | // Default to the base settings -- client composition only. |
| 166 | Output::chooseCompositionStrategy(); |
| 167 | |
| 168 | // If we don't have a HWC display, then we are done |
| 169 | if (!mId) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | // Get any composition changes requested by the HWC device, and apply them. |
| 174 | std::optional<android::HWComposer::DeviceRequestedChanges> changes; |
| 175 | auto& hwc = getCompositionEngine().getHwComposer(); |
| 176 | if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(), |
| 177 | &changes); |
| 178 | result != NO_ERROR) { |
| 179 | ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result, |
| 180 | strerror(-result)); |
| 181 | return; |
| 182 | } |
| 183 | if (changes) { |
| 184 | applyChangedTypesToLayers(changes->changedTypes); |
| 185 | applyDisplayRequests(changes->displayRequests); |
| 186 | applyLayerRequestsToLayers(changes->layerRequests); |
| 187 | } |
| 188 | |
| 189 | // Determine what type of composition we are doing from the final state |
| 190 | auto& state = editState(); |
| 191 | state.usesClientComposition = anyLayersRequireClientComposition(); |
| 192 | state.usesDeviceComposition = !allLayersRequireClientComposition(); |
| 193 | } |
| 194 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 195 | bool Display::getSkipColorTransform() const { |
| 196 | if (!mId) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | auto& hwc = getCompositionEngine().getHwComposer(); |
| 201 | return hwc.hasDisplayCapability(*mId, HWC2::DisplayCapability::SkipClientColorTransform); |
| 202 | } |
| 203 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 204 | bool Display::anyLayersRequireClientComposition() const { |
| 205 | const auto& layers = getOutputLayersOrderedByZ(); |
| 206 | return std::any_of(layers.cbegin(), layers.cend(), |
| 207 | [](const auto& layer) { return layer->requiresClientComposition(); }); |
| 208 | } |
| 209 | |
| 210 | bool Display::allLayersRequireClientComposition() const { |
| 211 | const auto& layers = getOutputLayersOrderedByZ(); |
| 212 | return std::all_of(layers.cbegin(), layers.cend(), |
| 213 | [](const auto& layer) { return layer->requiresClientComposition(); }); |
| 214 | } |
| 215 | |
| 216 | void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) { |
| 217 | if (changedTypes.empty()) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | for (auto& layer : getOutputLayersOrderedByZ()) { |
| 222 | auto hwcLayer = layer->getHwcLayer(); |
| 223 | if (!hwcLayer) { |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) { |
| 228 | layer->applyDeviceCompositionTypeChange( |
| 229 | static_cast<Hwc2::IComposerClient::Composition>(it->second)); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void Display::applyDisplayRequests(const DisplayRequests& displayRequests) { |
| 235 | auto& state = editState(); |
| 236 | state.flipClientTarget = (static_cast<uint32_t>(displayRequests) & |
| 237 | static_cast<uint32_t>(HWC2::DisplayRequest::FlipClientTarget)) != 0; |
| 238 | // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored. |
| 239 | } |
| 240 | |
| 241 | void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) { |
| 242 | for (auto& layer : getOutputLayersOrderedByZ()) { |
| 243 | layer->prepareForDeviceLayerRequests(); |
| 244 | |
| 245 | auto hwcLayer = layer->getHwcLayer(); |
| 246 | if (!hwcLayer) { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) { |
| 251 | layer->applyDeviceLayerRequest( |
| 252 | static_cast<Hwc2::IComposerClient::LayerRequest>(it->second)); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 257 | compositionengine::Output::FrameFences Display::presentAndGetFrameFences() { |
| 258 | auto result = impl::Output::presentAndGetFrameFences(); |
| 259 | |
| 260 | if (!mId) { |
| 261 | return result; |
| 262 | } |
| 263 | |
| 264 | auto& hwc = getCompositionEngine().getHwComposer(); |
| 265 | hwc.presentAndGetReleaseFences(*mId); |
| 266 | |
| 267 | result.presentFence = hwc.getPresentFence(*mId); |
| 268 | |
| 269 | // TODO(b/121291683): Change HWComposer call to return entire map |
| 270 | for (const auto& layer : getOutputLayersOrderedByZ()) { |
| 271 | auto hwcLayer = layer->getHwcLayer(); |
| 272 | if (!hwcLayer) { |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | result.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*mId, hwcLayer)); |
| 277 | } |
| 278 | |
| 279 | hwc.clearReleaseFences(*mId); |
| 280 | |
| 281 | return result; |
| 282 | } |
| 283 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 284 | void Display::setExpensiveRenderingExpected(bool enabled) { |
| 285 | Output::setExpensiveRenderingExpected(enabled); |
| 286 | |
| 287 | if (mPowerAdvisor && mId) { |
| 288 | mPowerAdvisor->setExpensiveRenderingExpected(*mId, enabled); |
| 289 | } |
| 290 | } |
| 291 | |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 292 | void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) { |
| 293 | // We only need to actually compose the display if: |
| 294 | // 1) It is being handled by hardware composer, which may need this to |
| 295 | // keep its virtual display state machine in sync, or |
| 296 | // 2) There is work to be done (the dirty region isn't empty) |
| 297 | if (!mId) { |
| 298 | if (getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) { |
| 299 | ALOGV("Skipping display composition"); |
| 300 | return; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | impl::Output::finishFrame(refreshArgs); |
| 305 | } |
| 306 | |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 307 | } // namespace android::compositionengine::impl |