| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -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 |  | 
|  | 17 | #include <android-base/stringprintf.h> | 
|  | 18 | #include <compositionengine/CompositionEngine.h> | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 19 | #include <compositionengine/DisplayColorProfile.h> | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 20 | #include <compositionengine/LayerFE.h> | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 21 | #include <compositionengine/RenderSurface.h> | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 22 | #include <compositionengine/impl/Output.h> | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 23 | #include <compositionengine/impl/OutputLayer.h> | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 24 | #include <ui/DebugUtils.h> | 
|  | 25 |  | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 26 | namespace android::compositionengine { | 
|  | 27 |  | 
|  | 28 | Output::~Output() = default; | 
|  | 29 |  | 
|  | 30 | namespace impl { | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | Output::Output(const CompositionEngine& compositionEngine) | 
|  | 33 | : mCompositionEngine(compositionEngine) {} | 
|  | 34 |  | 
|  | 35 | Output::~Output() = default; | 
|  | 36 |  | 
|  | 37 | const CompositionEngine& Output::getCompositionEngine() const { | 
|  | 38 | return mCompositionEngine; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | bool Output::isValid() const { | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 42 | return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface && | 
|  | 43 | mRenderSurface->isValid(); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
|  | 46 | const std::string& Output::getName() const { | 
|  | 47 | return mName; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | void Output::setName(const std::string& name) { | 
|  | 51 | mName = name; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | void Output::setCompositionEnabled(bool enabled) { | 
|  | 55 | if (mState.isEnabled == enabled) { | 
|  | 56 | return; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | mState.isEnabled = enabled; | 
|  | 60 | dirtyEntireOutput(); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | void Output::setProjection(const ui::Transform& transform, int32_t orientation, const Rect& frame, | 
|  | 64 | const Rect& viewport, const Rect& scissor, bool needsFiltering) { | 
|  | 65 | mState.transform = transform; | 
|  | 66 | mState.orientation = orientation; | 
|  | 67 | mState.scissor = scissor; | 
|  | 68 | mState.frame = frame; | 
|  | 69 | mState.viewport = viewport; | 
|  | 70 | mState.needsFiltering = needsFiltering; | 
|  | 71 |  | 
|  | 72 | dirtyEntireOutput(); | 
|  | 73 | } | 
|  | 74 |  | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 75 | // TODO(lpique): Rename setSize() once more is moved. | 
|  | 76 | void Output::setBounds(const ui::Size& size) { | 
|  | 77 | mRenderSurface->setDisplaySize(size); | 
|  | 78 | // TODO(lpique): Rename mState.size once more is moved. | 
|  | 79 | mState.bounds = Rect(mRenderSurface->getSize()); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 80 |  | 
|  | 81 | dirtyEntireOutput(); | 
|  | 82 | } | 
|  | 83 |  | 
| Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 84 | void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) { | 
|  | 85 | mState.layerStackId = layerStackId; | 
|  | 86 | mState.layerStackInternal = isInternal; | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | dirtyEntireOutput(); | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | void Output::setColorTransform(const mat4& transform) { | 
|  | 92 | const bool isIdentity = (transform == mat4()); | 
| Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 93 | const auto newColorTransform = | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 94 | isIdentity ? HAL_COLOR_TRANSFORM_IDENTITY : HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX; | 
| Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 95 |  | 
|  | 96 | if (mState.colorTransform == newColorTransform) { | 
|  | 97 | return; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | mState.colorTransform = newColorTransform; | 
|  | 101 |  | 
|  | 102 | dirtyEntireOutput(); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | void Output::setColorMode(ui::ColorMode mode, ui::Dataspace dataspace, | 
|  | 106 | ui::RenderIntent renderIntent) { | 
| Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 107 | if (mState.colorMode == mode && mState.dataspace == dataspace && | 
|  | 108 | mState.renderIntent == renderIntent) { | 
|  | 109 | return; | 
|  | 110 | } | 
|  | 111 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 112 | mState.colorMode = mode; | 
|  | 113 | mState.dataspace = dataspace; | 
|  | 114 | mState.renderIntent = renderIntent; | 
|  | 115 |  | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 116 | mRenderSurface->setBufferDataspace(dataspace); | 
|  | 117 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 118 | ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)", | 
|  | 119 | decodeColorMode(mode).c_str(), mode, decodeRenderIntent(renderIntent).c_str(), | 
|  | 120 | renderIntent); | 
| Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | dirtyEntireOutput(); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
|  | 125 | void Output::dump(std::string& out) const { | 
|  | 126 | using android::base::StringAppendF; | 
|  | 127 |  | 
|  | 128 | StringAppendF(&out, "   Composition Output State: [\"%s\"]", mName.c_str()); | 
|  | 129 |  | 
|  | 130 | out.append("\n   "); | 
|  | 131 |  | 
|  | 132 | dumpBase(out); | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | void Output::dumpBase(std::string& out) const { | 
|  | 136 | mState.dump(out); | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 137 |  | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 138 | if (mDisplayColorProfile) { | 
|  | 139 | mDisplayColorProfile->dump(out); | 
|  | 140 | } else { | 
|  | 141 | out.append("    No display color profile!\n"); | 
|  | 142 | } | 
|  | 143 |  | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 144 | if (mRenderSurface) { | 
|  | 145 | mRenderSurface->dump(out); | 
|  | 146 | } else { | 
|  | 147 | out.append("    No render surface!\n"); | 
|  | 148 | } | 
| Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 149 |  | 
|  | 150 | out.append("\n   %d Layers", mOutputLayersOrderedByZ.size()); | 
|  | 151 | for (const auto& outputLayer : mOutputLayersOrderedByZ) { | 
|  | 152 | if (!outputLayer) { | 
|  | 153 | continue; | 
|  | 154 | } | 
|  | 155 | outputLayer->dump(out); | 
|  | 156 | } | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
| Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 159 | compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const { | 
|  | 160 | return mDisplayColorProfile.get(); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) { | 
|  | 164 | mDisplayColorProfile = std::move(mode); | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | void Output::setDisplayColorProfileForTest( | 
|  | 168 | std::unique_ptr<compositionengine::DisplayColorProfile> mode) { | 
|  | 169 | mDisplayColorProfile = std::move(mode); | 
|  | 170 | } | 
|  | 171 |  | 
| Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 172 | compositionengine::RenderSurface* Output::getRenderSurface() const { | 
|  | 173 | return mRenderSurface.get(); | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) { | 
|  | 177 | mRenderSurface = std::move(surface); | 
|  | 178 | mState.bounds = Rect(mRenderSurface->getSize()); | 
|  | 179 |  | 
|  | 180 | dirtyEntireOutput(); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) { | 
|  | 184 | mRenderSurface = std::move(surface); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
|  | 187 | const OutputCompositionState& Output::getState() const { | 
|  | 188 | return mState; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | OutputCompositionState& Output::editState() { | 
|  | 192 | return mState; | 
|  | 193 | } | 
|  | 194 |  | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 195 | Region Output::getDirtyRegion(bool repaintEverything) const { | 
|  | 196 | Region dirty(mState.viewport); | 
|  | 197 | if (!repaintEverything) { | 
|  | 198 | dirty.andSelf(mState.dirtyRegion); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 199 | } | 
|  | 200 | return dirty; | 
|  | 201 | } | 
|  | 202 |  | 
| Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 203 | bool Output::belongsInOutput(uint32_t layerStackId, bool internalOnly) const { | 
|  | 204 | // The layerStackId's must match, and also the layer must not be internal | 
|  | 205 | // only when not on an internal output. | 
|  | 206 | return (layerStackId == mState.layerStackId) && (!internalOnly || mState.layerStackInternal); | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 209 | compositionengine::OutputLayer* Output::getOutputLayerForLayer( | 
|  | 210 | compositionengine::Layer* layer) const { | 
|  | 211 | for (const auto& outputLayer : mOutputLayersOrderedByZ) { | 
|  | 212 | if (outputLayer && &outputLayer->getLayer() == layer) { | 
|  | 213 | return outputLayer.get(); | 
|  | 214 | } | 
|  | 215 | } | 
|  | 216 | return nullptr; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | std::unique_ptr<compositionengine::OutputLayer> Output::getOrCreateOutputLayer( | 
| Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 220 | std::optional<DisplayId> displayId, std::shared_ptr<compositionengine::Layer> layer, | 
|  | 221 | sp<compositionengine::LayerFE> layerFE) { | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 222 | for (auto& outputLayer : mOutputLayersOrderedByZ) { | 
|  | 223 | if (outputLayer && &outputLayer->getLayer() == layer.get()) { | 
|  | 224 | return std::move(outputLayer); | 
|  | 225 | } | 
|  | 226 | } | 
| Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 227 | return createOutputLayer(mCompositionEngine, displayId, *this, layer, layerFE); | 
| Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
|  | 230 | void Output::setOutputLayersOrderedByZ(OutputLayers&& layers) { | 
|  | 231 | mOutputLayersOrderedByZ = std::move(layers); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | const Output::OutputLayers& Output::getOutputLayersOrderedByZ() const { | 
|  | 235 | return mOutputLayersOrderedByZ; | 
|  | 236 | } | 
|  | 237 |  | 
| Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 238 | void Output::dirtyEntireOutput() { | 
|  | 239 | mState.dirtyRegion.set(mState.bounds); | 
|  | 240 | } | 
|  | 241 |  | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 242 | } // namespace impl | 
|  | 243 | } // namespace android::compositionengine |