| 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 | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 28 |  | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 29 | #include <utils/Trace.h> | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 30 |  | 
| Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 31 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 32 | #pragma clang diagnostic push | 
|  | 33 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 34 |  | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 35 | #include "DisplayHardware/HWComposer.h" | 
| Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 38 | #pragma clang diagnostic pop // ignored "-Wconversion" | 
|  | 39 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 40 | #include "DisplayHardware/PowerAdvisor.h" | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | namespace android::compositionengine::impl { | 
|  | 43 |  | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 44 | std::shared_ptr<Display> createDisplay( | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 45 | const compositionengine::CompositionEngine& compositionEngine, | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 46 | const compositionengine::DisplayCreationArgs& args) { | 
|  | 47 | return createDisplayTemplated<Display>(compositionEngine, args); | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 50 | Display::~Display() = default; | 
|  | 51 |  | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 52 | void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) { | 
|  | 53 | mIsVirtual = !args.physical; | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 54 | mPowerAdvisor = args.powerAdvisor; | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 55 | editState().isSecure = args.isSecure; | 
| Marin Shalamanov | b15d227 | 2020-09-17 21:41:52 +0200 | [diff] [blame] | 56 | editState().displaySpace.bounds = Rect(args.pixels); | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 57 | setLayerStackFilter(args.layerStackId, | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 58 | args.physical && args.physical->type == DisplayConnectionType::Internal); | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 59 | setName(args.name); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 60 | mGpuVirtualDisplayIdGenerator = args.gpuVirtualDisplayIdGenerator; | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 61 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 62 | if (args.physical) { | 
|  | 63 | mId = args.physical->id; | 
|  | 64 | } else { | 
|  | 65 | std::optional<DisplayId> id; | 
|  | 66 | if (args.useHwcVirtualDisplays) { | 
|  | 67 | id = maybeAllocateDisplayIdForVirtualDisplay(args.pixels, args.pixelFormat); | 
|  | 68 | } | 
|  | 69 | if (!id) { | 
|  | 70 | id = mGpuVirtualDisplayIdGenerator->nextId(); | 
|  | 71 | } | 
|  | 72 | LOG_ALWAYS_FATAL_IF(!id, "Failed to generate display ID"); | 
|  | 73 | mId = *id; | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | std::optional<DisplayId> Display::maybeAllocateDisplayIdForVirtualDisplay( | 
|  | 78 | ui::Size pixels, ui::PixelFormat pixelFormat) const { | 
|  | 79 | auto& hwc = getCompositionEngine().getHwComposer(); | 
|  | 80 | return hwc.allocateVirtualDisplay(static_cast<uint32_t>(pixels.width), | 
|  | 81 | static_cast<uint32_t>(pixels.height), &pixelFormat); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | bool Display::isValid() const { | 
|  | 85 | return Output::isValid() && mPowerAdvisor; | 
|  | 86 | } | 
|  | 87 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 88 | DisplayId Display::getId() const { | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 89 | return mId; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | bool Display::isSecure() const { | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 93 | return getState().isSecure; | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
|  | 96 | bool Display::isVirtual() const { | 
|  | 97 | return mIsVirtual; | 
|  | 98 | } | 
|  | 99 |  | 
| Lloyd Pique | 6c564cf | 2019-05-17 17:31:36 -0700 | [diff] [blame] | 100 | std::optional<DisplayId> Display::getDisplayId() const { | 
|  | 101 | return mId; | 
|  | 102 | } | 
|  | 103 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 104 | void Display::setDisplayIdForTesting(DisplayId displayId) { | 
| Lloyd Pique | aad4ebf | 2019-10-03 17:58:30 -0700 | [diff] [blame] | 105 | mId = displayId; | 
|  | 106 | } | 
|  | 107 |  | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 108 | void Display::disconnect() { | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 109 | if (mIsDisconnected) { | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 110 | return; | 
|  | 111 | } | 
|  | 112 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 113 | mIsDisconnected = true; | 
|  | 114 | if (const auto id = GpuVirtualDisplayId::tryCast(mId)) { | 
|  | 115 | mGpuVirtualDisplayIdGenerator->markUnused(*id); | 
|  | 116 | return; | 
|  | 117 | } | 
|  | 118 | const auto halDisplayId = HalDisplayId::tryCast(mId); | 
|  | 119 | LOG_FATAL_IF(!halDisplayId); | 
|  | 120 | getCompositionEngine().getHwComposer().disconnectDisplay(*halDisplayId); | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 123 | void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) { | 
|  | 124 | Output::setColorTransform(args); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 125 | const auto halDisplayId = HalDisplayId::tryCast(mId); | 
|  | 126 | if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) { | 
| Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 127 | return; | 
|  | 128 | } | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 129 |  | 
|  | 130 | auto& hwc = getCompositionEngine().getHwComposer(); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 131 | status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 132 | ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d", | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 133 | to_string(mId).c_str(), result); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 136 | void Display::setColorProfile(const ColorProfile& colorProfile) { | 
|  | 137 | const ui::Dataspace targetDataspace = | 
|  | 138 | getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace, | 
|  | 139 | colorProfile.colorSpaceAgnosticDataspace); | 
| Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 140 |  | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 141 | if (colorProfile.mode == getState().colorMode && | 
|  | 142 | colorProfile.dataspace == getState().dataspace && | 
|  | 143 | colorProfile.renderIntent == getState().renderIntent && | 
|  | 144 | targetDataspace == getState().targetDataspace) { | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 145 | return; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | if (mIsVirtual) { | 
|  | 149 | ALOGW("%s: Invalid operation on virtual display", __FUNCTION__); | 
|  | 150 | return; | 
|  | 151 | } | 
|  | 152 |  | 
| Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 153 | Output::setColorProfile(colorProfile); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 154 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 155 | const auto physicalId = PhysicalDisplayId::tryCast(mId); | 
|  | 156 | LOG_FATAL_IF(!physicalId); | 
|  | 157 | getCompositionEngine().getHwComposer().setActiveColorMode(*physicalId, colorProfile.mode, | 
|  | 158 | colorProfile.renderIntent); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
|  | 161 | void Display::dump(std::string& out) const { | 
|  | 162 | using android::base::StringAppendF; | 
|  | 163 |  | 
|  | 164 | StringAppendF(&out, "   Composition Display State: [\"%s\"]", getName().c_str()); | 
|  | 165 |  | 
|  | 166 | out.append("\n   "); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 167 | dumpVal(out, "isVirtual", mIsVirtual); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 168 | dumpVal(out, "DisplayId", to_string(mId)); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 169 | out.append("\n"); | 
|  | 170 |  | 
|  | 171 | Output::dumpBase(out); | 
|  | 172 | } | 
|  | 173 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 174 | void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) { | 
|  | 175 | setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args)); | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 178 | void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) { | 
|  | 179 | setRenderSurface( | 
|  | 180 | compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args)); | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 183 | void Display::createClientCompositionCache(uint32_t cacheSize) { | 
|  | 184 | cacheClientCompositionRequests(cacheSize); | 
|  | 185 | } | 
|  | 186 |  | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 187 | std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer( | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 188 | const sp<compositionengine::LayerFE>& layerFE) const { | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 189 | auto outputLayer = impl::createOutputLayer(*this, layerFE); | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 190 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 191 | if (const auto halDisplayId = HalDisplayId::tryCast(mId); | 
|  | 192 | outputLayer && !mIsDisconnected && halDisplayId) { | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 193 | auto& hwc = getCompositionEngine().getHwComposer(); | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 194 | // Note: For the moment we ensure it is safe to take a reference to the | 
|  | 195 | // HWComposer implementation by destroying all the OutputLayers (and | 
|  | 196 | // hence the HWC2::Layers they own) before setting a new HWComposer. See | 
|  | 197 | // for example SurfaceFlinger::updateVrFlinger(). | 
|  | 198 | // TODO(b/121291683): Make this safer. | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 199 | auto hwcLayer = | 
|  | 200 | std::shared_ptr<HWC2::Layer>(hwc.createLayer(*halDisplayId), | 
|  | 201 | [&hwc, id = *halDisplayId](HWC2::Layer* layer) { | 
|  | 202 | hwc.destroyLayer(id, layer); | 
|  | 203 | }); | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 204 | ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s", | 
|  | 205 | getName().c_str()); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 206 | outputLayer->setHwcLayer(std::move(hwcLayer)); | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 207 | } | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 208 | return outputLayer; | 
| Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 209 | } | 
|  | 210 |  | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 211 | void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
|  | 212 | Output::setReleasedLayers(refreshArgs); | 
|  | 213 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 214 | if (mIsDisconnected || GpuVirtualDisplayId::tryCast(mId) || | 
|  | 215 | refreshArgs.layersWithQueuedFrames.empty()) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 216 | return; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | // For layers that are being removed from a HWC display, and that have | 
|  | 220 | // queued frames, add them to a a list of released layers so we can properly | 
|  | 221 | // set a fence. | 
|  | 222 | compositionengine::Output::ReleasedLayers releasedLayers; | 
|  | 223 |  | 
|  | 224 | // Any non-null entries in the current list of layers are layers that are no | 
|  | 225 | // longer going to be visible | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 226 | for (auto* outputLayer : getOutputLayersOrderedByZ()) { | 
|  | 227 | if (!outputLayer) { | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 228 | continue; | 
|  | 229 | } | 
|  | 230 |  | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 231 | compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE(); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 232 | const bool hasQueuedFrames = | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 233 | std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(), | 
|  | 234 | refreshArgs.layersWithQueuedFrames.cend(), | 
|  | 235 | [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) { | 
|  | 236 | return layerFE == layerWithQueuedFrames.get(); | 
|  | 237 | }); | 
| Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 238 |  | 
|  | 239 | if (hasQueuedFrames) { | 
|  | 240 | releasedLayers.emplace_back(layerFE); | 
|  | 241 | } | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | setReleasedLayers(std::move(releasedLayers)); | 
|  | 245 | } | 
|  | 246 |  | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 247 | void Display::chooseCompositionStrategy() { | 
|  | 248 | ATRACE_CALL(); | 
|  | 249 | ALOGV(__FUNCTION__); | 
|  | 250 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 251 | if (mIsDisconnected) { | 
|  | 252 | return; | 
|  | 253 | } | 
|  | 254 |  | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 255 | // Default to the base settings -- client composition only. | 
|  | 256 | Output::chooseCompositionStrategy(); | 
|  | 257 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 258 | // If we don't have a HWC display, then we are done. | 
|  | 259 | const auto halDisplayId = HalDisplayId::tryCast(mId); | 
|  | 260 | if (!halDisplayId) { | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 261 | return; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | // Get any composition changes requested by the HWC device, and apply them. | 
|  | 265 | std::optional<android::HWComposer::DeviceRequestedChanges> changes; | 
|  | 266 | auto& hwc = getCompositionEngine().getHwComposer(); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 267 | if (status_t result = | 
|  | 268 | hwc.getDeviceCompositionChanges(*halDisplayId, anyLayersRequireClientComposition(), | 
|  | 269 | &changes); | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 270 | result != NO_ERROR) { | 
|  | 271 | ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result, | 
|  | 272 | strerror(-result)); | 
|  | 273 | return; | 
|  | 274 | } | 
|  | 275 | if (changes) { | 
|  | 276 | applyChangedTypesToLayers(changes->changedTypes); | 
|  | 277 | applyDisplayRequests(changes->displayRequests); | 
|  | 278 | applyLayerRequestsToLayers(changes->layerRequests); | 
| Peiyong Lin | dfc3f7c | 2020-05-07 20:15:50 -0700 | [diff] [blame] | 279 | applyClientTargetRequests(changes->clientTargetProperty); | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
|  | 282 | // Determine what type of composition we are doing from the final state | 
|  | 283 | auto& state = editState(); | 
|  | 284 | state.usesClientComposition = anyLayersRequireClientComposition(); | 
|  | 285 | state.usesDeviceComposition = !allLayersRequireClientComposition(); | 
|  | 286 | } | 
|  | 287 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 288 | bool Display::getSkipColorTransform() const { | 
| Dominik Laskowski | 1162e47 | 2020-04-02 19:02:47 -0700 | [diff] [blame] | 289 | const auto& hwc = getCompositionEngine().getHwComposer(); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 290 | if (const auto halDisplayId = HalDisplayId::tryCast(mId)) { | 
|  | 291 | return hwc.hasDisplayCapability(*halDisplayId, | 
|  | 292 | hal::DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM); | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | return hwc.hasCapability(hal::Capability::SKIP_CLIENT_COLOR_TRANSFORM); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 298 | bool Display::anyLayersRequireClientComposition() const { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 299 | const auto layers = getOutputLayersOrderedByZ(); | 
|  | 300 | return std::any_of(layers.begin(), layers.end(), | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 301 | [](const auto& layer) { return layer->requiresClientComposition(); }); | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | bool Display::allLayersRequireClientComposition() const { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 305 | const auto layers = getOutputLayersOrderedByZ(); | 
|  | 306 | return std::all_of(layers.begin(), layers.end(), | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 307 | [](const auto& layer) { return layer->requiresClientComposition(); }); | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) { | 
|  | 311 | if (changedTypes.empty()) { | 
|  | 312 | return; | 
|  | 313 | } | 
|  | 314 |  | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 315 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 316 | auto hwcLayer = layer->getHwcLayer(); | 
|  | 317 | if (!hwcLayer) { | 
|  | 318 | continue; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) { | 
|  | 322 | layer->applyDeviceCompositionTypeChange( | 
|  | 323 | static_cast<Hwc2::IComposerClient::Composition>(it->second)); | 
|  | 324 | } | 
|  | 325 | } | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | void Display::applyDisplayRequests(const DisplayRequests& displayRequests) { | 
|  | 329 | auto& state = editState(); | 
|  | 330 | state.flipClientTarget = (static_cast<uint32_t>(displayRequests) & | 
| Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 331 | static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0; | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 332 | // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored. | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) { | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 336 | for (auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 337 | layer->prepareForDeviceLayerRequests(); | 
|  | 338 |  | 
|  | 339 | auto hwcLayer = layer->getHwcLayer(); | 
|  | 340 | if (!hwcLayer) { | 
|  | 341 | continue; | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) { | 
|  | 345 | layer->applyDeviceLayerRequest( | 
|  | 346 | static_cast<Hwc2::IComposerClient::LayerRequest>(it->second)); | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
| Peiyong Lin | dfc3f7c | 2020-05-07 20:15:50 -0700 | [diff] [blame] | 351 | void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) { | 
|  | 352 | if (clientTargetProperty.dataspace == ui::Dataspace::UNKNOWN) { | 
|  | 353 | return; | 
|  | 354 | } | 
|  | 355 | auto outputState = editState(); | 
|  | 356 | outputState.dataspace = clientTargetProperty.dataspace; | 
|  | 357 | getRenderSurface()->setBufferDataspace(clientTargetProperty.dataspace); | 
|  | 358 | getRenderSurface()->setBufferPixelFormat(clientTargetProperty.pixelFormat); | 
|  | 359 | } | 
|  | 360 |  | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 361 | compositionengine::Output::FrameFences Display::presentAndGetFrameFences() { | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 362 | auto fences = impl::Output::presentAndGetFrameFences(); | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 363 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 364 | const auto halDisplayIdOpt = HalDisplayId::tryCast(mId); | 
|  | 365 | if (mIsDisconnected || !halDisplayIdOpt) { | 
|  | 366 | return fences; | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
|  | 369 | auto& hwc = getCompositionEngine().getHwComposer(); | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 370 | hwc.presentAndGetReleaseFences(*halDisplayIdOpt); | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 371 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 372 | fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt); | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 373 |  | 
|  | 374 | // TODO(b/121291683): Change HWComposer call to return entire map | 
| Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 375 | for (const auto* layer : getOutputLayersOrderedByZ()) { | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 376 | auto hwcLayer = layer->getHwcLayer(); | 
|  | 377 | if (!hwcLayer) { | 
|  | 378 | continue; | 
|  | 379 | } | 
|  | 380 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 381 | fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer)); | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 382 | } | 
|  | 383 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 384 | hwc.clearReleaseFences(*halDisplayIdOpt); | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 385 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 386 | return fences; | 
| Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 387 | } | 
|  | 388 |  | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 389 | void Display::setExpensiveRenderingExpected(bool enabled) { | 
|  | 390 | Output::setExpensiveRenderingExpected(enabled); | 
|  | 391 |  | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 392 | if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) { | 
|  | 393 | mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled); | 
| Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 394 | } | 
|  | 395 | } | 
|  | 396 |  | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 397 | void Display::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) { | 
|  | 398 | // We only need to actually compose the display if: | 
|  | 399 | // 1) It is being handled by hardware composer, which may need this to | 
|  | 400 | //    keep its virtual display state machine in sync, or | 
|  | 401 | // 2) There is work to be done (the dirty region isn't empty) | 
| Marin Shalamanov | 0f10d0d | 2020-08-06 20:04:06 +0200 | [diff] [blame] | 402 | if (GpuVirtualDisplayId::tryCast(mId) && | 
|  | 403 | getDirtyRegion(refreshArgs.repaintEverything).isEmpty()) { | 
|  | 404 | ALOGV("Skipping display composition"); | 
|  | 405 | return; | 
| Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 406 | } | 
|  | 407 |  | 
|  | 408 | impl::Output::finishFrame(refreshArgs); | 
|  | 409 | } | 
|  | 410 |  | 
| Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 411 | } // namespace android::compositionengine::impl |