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