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