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 | #pragma once |
| 18 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 19 | #include <compositionengine/CompositionEngine.h> |
| 20 | #include <compositionengine/Output.h> |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 21 | #include <compositionengine/impl/ClientCompositionRequestCache.h> |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 22 | #include <compositionengine/impl/OutputCompositionState.h> |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 23 | #include <renderengine/DisplaySettings.h> |
| 24 | #include <renderengine/LayerSettings.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 25 | #include <memory> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 26 | #include <utility> |
| 27 | #include <vector> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 28 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 29 | namespace android::compositionengine::impl { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 30 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 31 | // The implementation class contains the common implementation, but does not |
| 32 | // actually contain the final output state. |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 33 | class Output : public virtual compositionengine::Output { |
| 34 | public: |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 35 | ~Output() override; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 36 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 37 | // compositionengine::Output overrides |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 38 | bool isValid() const override; |
Lloyd Pique | 6c564cf | 2019-05-17 17:31:36 -0700 | [diff] [blame] | 39 | std::optional<DisplayId> getDisplayId() const override; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 40 | void setCompositionEnabled(bool) override; |
Lloyd Pique | 0a45623 | 2020-01-16 17:51:13 -0800 | [diff] [blame] | 41 | void setProjection(const ui::Transform&, uint32_t orientation, const Rect& frame, |
Lloyd Pique | e8fe474 | 2020-01-21 15:26:18 -0800 | [diff] [blame] | 42 | const Rect& viewport, const Rect& sourceClip, const Rect& destinationClip, |
| 43 | bool needsFiltering) override; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 44 | void setBounds(const ui::Size&) override; |
Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 45 | void setLayerStackFilter(uint32_t layerStackId, bool isInternal) override; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 46 | |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 47 | void setColorTransform(const compositionengine::CompositionRefreshArgs&) override; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 48 | void setColorProfile(const ColorProfile&) override; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 49 | |
| 50 | void dump(std::string&) const override; |
| 51 | |
| 52 | const std::string& getName() const override; |
| 53 | void setName(const std::string&) override; |
| 54 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 55 | compositionengine::DisplayColorProfile* getDisplayColorProfile() const override; |
| 56 | void setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile>) override; |
| 57 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 58 | compositionengine::RenderSurface* getRenderSurface() const override; |
| 59 | void setRenderSurface(std::unique_ptr<compositionengine::RenderSurface>) override; |
| 60 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 61 | Region getDirtyRegion(bool repaintEverything) const override; |
Lloyd Pique | c668734 | 2019-03-07 21:34:57 -0800 | [diff] [blame] | 62 | bool belongsInOutput(std::optional<uint32_t>, bool) const override; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 63 | bool belongsInOutput(const sp<LayerFE>&) const override; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 64 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 65 | compositionengine::OutputLayer* getOutputLayerForLayer(const sp<LayerFE>&) const override; |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 66 | |
Lloyd Pique | c7ef21b | 2019-01-29 18:43:00 -0800 | [diff] [blame] | 67 | void setReleasedLayers(ReleasedLayers&&) override; |
Lloyd Pique | c7ef21b | 2019-01-29 18:43:00 -0800 | [diff] [blame] | 68 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 69 | void prepare(const CompositionRefreshArgs&, LayerFESet&) override; |
| 70 | void present(const CompositionRefreshArgs&) override; |
Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 71 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 72 | void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) override; |
| 73 | void collectVisibleLayers(const CompositionRefreshArgs&, |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 74 | compositionengine::Output::CoverageState&) override; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 75 | void ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>&, |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 76 | compositionengine::Output::CoverageState&) override; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 77 | void setReleasedLayers(const compositionengine::CompositionRefreshArgs&) override; |
| 78 | |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 79 | void updateLayerStateFromFE(const CompositionRefreshArgs&) const override; |
| 80 | void updateAndWriteCompositionState(const compositionengine::CompositionRefreshArgs&) override; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 81 | void updateColorProfile(const compositionengine::CompositionRefreshArgs&) override; |
Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 82 | void beginFrame() override; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 83 | void prepareFrame() override; |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 84 | void devOptRepaintFlash(const CompositionRefreshArgs&) override; |
| 85 | void finishFrame(const CompositionRefreshArgs&) override; |
Lucas Dupin | 2dd6f39 | 2020-02-18 17:43:36 -0800 | [diff] [blame] | 86 | std::optional<base::unique_fd> composeSurfaces( |
| 87 | const Region&, const compositionengine::CompositionRefreshArgs& refreshArgs) override; |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 88 | void postFramebuffer() override; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 89 | void cacheClientCompositionRequests(uint32_t) override; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 90 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 91 | // Testing |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 92 | const ReleasedLayers& getReleasedLayersForTest() const; |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 93 | void setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile>); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 94 | void setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface>); |
| 95 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 96 | protected: |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 97 | std::unique_ptr<compositionengine::OutputLayer> createOutputLayer(const sp<LayerFE>&) const; |
| 98 | std::optional<size_t> findCurrentOutputLayerForLayer( |
| 99 | const sp<compositionengine::LayerFE>&) const; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 100 | void chooseCompositionStrategy() override; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 101 | bool getSkipColorTransform() const override; |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 102 | compositionengine::Output::FrameFences presentAndGetFrameFences() override; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 103 | std::vector<LayerFE::LayerSettings> generateClientCompositionRequests( |
Vishnu Nair | 3a7346c | 2019-12-04 08:09:09 -0800 | [diff] [blame] | 104 | bool supportsProtectedContent, Region& clearRegion, |
| 105 | ui::Dataspace outputDataspace) override; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 106 | void appendRegionFlashRequests(const Region&, std::vector<LayerFE::LayerSettings>&) override; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 107 | void setExpensiveRenderingExpected(bool enabled) override; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 108 | void dumpBase(std::string&) const; |
| 109 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 110 | // Implemented by the final implementation for the final state it uses. |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 111 | virtual compositionengine::OutputLayer* ensureOutputLayer(std::optional<size_t>, |
| 112 | const sp<LayerFE>&) = 0; |
| 113 | virtual compositionengine::OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0; |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 114 | virtual void finalizePendingOutputLayers() = 0; |
| 115 | virtual const compositionengine::CompositionEngine& getCompositionEngine() const = 0; |
| 116 | virtual void dumpState(std::string& out) const = 0; |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 117 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 118 | private: |
| 119 | void dirtyEntireOutput(); |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 120 | compositionengine::OutputLayer* findLayerRequestingBackgroundComposition() const; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 121 | ui::Dataspace getBestDataspace(ui::Dataspace*, bool*) const; |
| 122 | compositionengine::Output::ColorProfile pickColorProfile( |
| 123 | const compositionengine::CompositionRefreshArgs&) const; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 124 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 125 | std::string mName; |
| 126 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 127 | std::unique_ptr<compositionengine::DisplayColorProfile> mDisplayColorProfile; |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 128 | std::unique_ptr<compositionengine::RenderSurface> mRenderSurface; |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 129 | |
Lloyd Pique | c7ef21b | 2019-01-29 18:43:00 -0800 | [diff] [blame] | 130 | ReleasedLayers mReleasedLayers; |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 131 | OutputLayer* mLayerRequestingBackgroundBlur = nullptr; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 132 | std::unique_ptr<ClientCompositionRequestCache> mClientCompositionRequestCache; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 135 | // This template factory function standardizes the implementation details of the |
| 136 | // final class using the types actually required by the implementation. This is |
| 137 | // not possible to do in the base class as those types may not even be visible |
| 138 | // to the base code. |
| 139 | template <typename BaseOutput, typename CompositionEngine, typename... Args> |
| 140 | std::shared_ptr<BaseOutput> createOutputTemplated(const CompositionEngine& compositionEngine, |
| 141 | Args... args) { |
| 142 | class Output final : public BaseOutput { |
| 143 | public: |
| 144 | // Clang incorrectly complains that these are unused. |
| 145 | #pragma clang diagnostic push |
| 146 | #pragma clang diagnostic ignored "-Wunused-local-typedef" |
| 147 | |
| 148 | using OutputCompositionState = std::remove_const_t< |
| 149 | std::remove_reference_t<decltype(std::declval<BaseOutput>().getState())>>; |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 150 | using OutputLayer = std::remove_pointer_t<decltype( |
| 151 | std::declval<BaseOutput>().getOutputLayerOrderedByZByIndex(0))>; |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 152 | |
| 153 | #pragma clang diagnostic pop |
| 154 | |
| 155 | explicit Output(const CompositionEngine& compositionEngine, Args... args) |
| 156 | : BaseOutput(std::forward<Args>(args)...), mCompositionEngine(compositionEngine) {} |
| 157 | ~Output() override = default; |
| 158 | |
| 159 | private: |
| 160 | // compositionengine::Output overrides |
| 161 | const OutputCompositionState& getState() const override { return mState; } |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 162 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 163 | OutputCompositionState& editState() override { return mState; } |
| 164 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 165 | size_t getOutputLayerCount() const override { |
| 166 | return mCurrentOutputLayersOrderedByZ.size(); |
| 167 | } |
| 168 | |
| 169 | OutputLayer* getOutputLayerOrderedByZByIndex(size_t index) const override { |
| 170 | if (index >= mCurrentOutputLayersOrderedByZ.size()) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | return mCurrentOutputLayersOrderedByZ[index].get(); |
| 174 | } |
| 175 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 176 | // compositionengine::impl::Output overrides |
| 177 | const CompositionEngine& getCompositionEngine() const override { |
| 178 | return mCompositionEngine; |
| 179 | }; |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 180 | |
| 181 | OutputLayer* ensureOutputLayer(std::optional<size_t> prevIndex, |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 182 | const sp<LayerFE>& layerFE) { |
| 183 | auto outputLayer = (prevIndex && *prevIndex <= mCurrentOutputLayersOrderedByZ.size()) |
| 184 | ? std::move(mCurrentOutputLayersOrderedByZ[*prevIndex]) |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 185 | : BaseOutput::createOutputLayer(layerFE); |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 186 | auto result = outputLayer.get(); |
| 187 | mPendingOutputLayersOrderedByZ.emplace_back(std::move(outputLayer)); |
| 188 | return result; |
| 189 | } |
| 190 | |
| 191 | void finalizePendingOutputLayers() override { |
| 192 | // The pending layers are added in reverse order. Reverse them to |
| 193 | // get the back-to-front ordered list of layers. |
| 194 | std::reverse(mPendingOutputLayersOrderedByZ.begin(), |
| 195 | mPendingOutputLayersOrderedByZ.end()); |
| 196 | |
| 197 | mCurrentOutputLayersOrderedByZ = std::move(mPendingOutputLayersOrderedByZ); |
| 198 | } |
| 199 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 200 | void dumpState(std::string& out) const override { mState.dump(out); } |
| 201 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 202 | OutputLayer* injectOutputLayerForTest(const sp<LayerFE>& layerFE) override { |
| 203 | auto outputLayer = BaseOutput::createOutputLayer(layerFE); |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 204 | auto result = outputLayer.get(); |
| 205 | mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer)); |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | // Note: This is declared as a private virtual non-override so it can be |
| 210 | // an override implementation in the unit tests, but otherwise is not an |
| 211 | // accessible override for the normal implementation. |
| 212 | virtual void injectOutputLayerForTest(std::unique_ptr<OutputLayer> outputLayer) { |
| 213 | mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer)); |
| 214 | } |
| 215 | |
| 216 | void clearOutputLayers() override { |
| 217 | mCurrentOutputLayersOrderedByZ.clear(); |
| 218 | mPendingOutputLayersOrderedByZ.clear(); |
| 219 | } |
| 220 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 221 | const CompositionEngine& mCompositionEngine; |
| 222 | OutputCompositionState mState; |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 223 | std::vector<std::unique_ptr<OutputLayer>> mCurrentOutputLayersOrderedByZ; |
| 224 | std::vector<std::unique_ptr<OutputLayer>> mPendingOutputLayersOrderedByZ; |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | return std::make_shared<Output>(compositionEngine, std::forward<Args>(args)...); |
| 228 | } |
| 229 | |
| 230 | std::shared_ptr<Output> createOutput(const compositionengine::CompositionEngine&); |
| 231 | |
| 232 | } // namespace android::compositionengine::impl |