1 /* 2 * Copyright 2021 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 19 #include <compositionengine/Output.h> 20 #include <compositionengine/impl/planner/CachedSet.h> 21 #include <compositionengine/impl/planner/LayerState.h> 22 23 #include <chrono> 24 #include <numeric> 25 #include <vector> 26 27 namespace android { 28 29 namespace renderengine { 30 class RenderEngine; 31 } // namespace renderengine 32 33 namespace compositionengine::impl::planner { 34 using namespace std::chrono_literals; 35 36 class LayerState; 37 class Predictor; 38 39 class Flattener { 40 public: 41 // Collection of tunables which are backed by sysprops 42 struct Tunables { 43 // Tunables that are specific to scheduling when a cached set should be rendered 44 struct RenderScheduling { 45 // This default assumes that rendering a cached set takes about 3ms. That time is then 46 // cut in half - the next frame using the cached set would have the same workload, 47 // meaning that composition cost is the same. This is best illustrated with the 48 // following example: 49 // 50 // Suppose we're at a 120hz cadence so SurfaceFlinger is budgeted 8.3ms per-frame. If 51 // renderCachedSets costs 3ms, then two consecutive frames have timings: 52 // 53 // First frame: Start at 0ms, end at 6.8ms. 54 // renderCachedSets: Start at 6.8ms, end at 9.8ms. 55 // Second frame: Start at 9.8ms, end at 16.6ms. 56 // 57 // Now the second frame won't render a cached set afterwards, but the first frame didn't 58 // really steal time from the second frame. 59 static const constexpr std::chrono::nanoseconds kDefaultCachedSetRenderDuration = 60 1500us; 61 62 static const constexpr size_t kDefaultMaxDeferRenderAttempts = 240; 63 64 // Duration allocated for rendering a cached set. If we don't have enough time for 65 // rendering a cached set, then rendering is deferred to another frame. 66 const std::chrono::nanoseconds cachedSetRenderDuration; 67 // Maximum of times that we defer rendering a cached set. If we defer rendering a cached 68 // set too many times, then render it anyways so that future frames would benefit from 69 // the flattened cached set. 70 const size_t maxDeferRenderAttempts; 71 }; 72 73 static const constexpr std::chrono::milliseconds kDefaultActiveLayerTimeout = 150ms; 74 75 static const constexpr bool kDefaultEnableHolePunch = true; 76 77 // Threshold for determing whether a layer is active. A layer whose properties, including 78 // the buffer, have not changed in at least this time is considered inactive and is 79 // therefore a candidate for flattening. 80 const std::chrono::milliseconds mActiveLayerTimeout; 81 82 // Toggles for scheduling when it's safe to render a cached set. 83 // See: RenderScheduling 84 const std::optional<RenderScheduling> mRenderScheduling; 85 86 // True if the hole punching feature should be enabled. 87 const bool mEnableHolePunch; 88 }; 89 90 Flattener(renderengine::RenderEngine& renderEngine, const Tunables& tunables); 91 setDisplaySize(ui::Size size)92 void setDisplaySize(ui::Size size) { 93 mDisplaySize = size; 94 mTexturePool.setDisplaySize(size); 95 } 96 97 NonBufferHash flattenLayers(const std::vector<const LayerState*>& layers, NonBufferHash, 98 std::chrono::steady_clock::time_point now); 99 100 // Renders the newest cached sets with the supplied output composition state 101 void renderCachedSets(const OutputCompositionState& outputState, 102 std::optional<std::chrono::steady_clock::time_point> renderDeadline); 103 setTexturePoolEnabled(bool enabled)104 void setTexturePoolEnabled(bool enabled) { mTexturePool.setEnabled(enabled); } 105 106 void dump(std::string& result) const; 107 void dumpLayers(std::string& result) const; 108 getNewCachedSetForTesting()109 const std::optional<CachedSet>& getNewCachedSetForTesting() const { return mNewCachedSet; } 110 111 private: 112 size_t calculateDisplayCost(const std::vector<const LayerState*>& layers) const; 113 114 void resetActivities(NonBufferHash, std::chrono::steady_clock::time_point now); 115 116 NonBufferHash computeLayersHash() const; 117 118 bool mergeWithCachedSets(const std::vector<const LayerState*>& layers, 119 std::chrono::steady_clock::time_point now); 120 121 // A Run is a sequence of CachedSets, which is a candidate for flattening into a single 122 // CachedSet. Because it is wasteful to flatten 1 CachedSet, a Run must contain more than 1 123 // CachedSet 124 class Run { 125 public: 126 // A builder for a Run, to aid in construction 127 class Builder { 128 private: 129 std::vector<CachedSet>::const_iterator mStart; 130 std::vector<size_t> mLengths; 131 const CachedSet* mHolePunchCandidate = nullptr; 132 const CachedSet* mBlurringLayer = nullptr; 133 134 public: 135 // Initializes a Builder a CachedSet to start from. 136 // This start iterator must be an iterator for mLayers init(const std::vector<CachedSet>::const_iterator & start)137 void init(const std::vector<CachedSet>::const_iterator& start) { 138 mStart = start; 139 mLengths.push_back(start->getLayerCount()); 140 } 141 142 // Appends a new CachedSet to the end of the run 143 // The provided length must be the size of the next sequential CachedSet in layers append(size_t length)144 void append(size_t length) { mLengths.push_back(length); } 145 146 // Sets the hole punch candidate for the Run. setHolePunchCandidate(const CachedSet * holePunchCandidate)147 void setHolePunchCandidate(const CachedSet* holePunchCandidate) { 148 mHolePunchCandidate = holePunchCandidate; 149 } 150 setBlurringLayer(const CachedSet * blurringLayer)151 void setBlurringLayer(const CachedSet* blurringLayer) { 152 mBlurringLayer = blurringLayer; 153 } 154 155 // Builds a Run instance, if a valid Run may be built. validateAndBuild()156 std::optional<Run> validateAndBuild() { 157 if (mLengths.size() <= 1) { 158 return std::nullopt; 159 } 160 161 return Run(mStart, 162 std::reduce(mLengths.cbegin(), mLengths.cend(), 0u, 163 [](size_t left, size_t right) { return left + right; }), 164 mHolePunchCandidate, mBlurringLayer); 165 } 166 reset()167 void reset() { *this = {}; } 168 }; 169 170 // Gets the starting CachedSet of this run. 171 // This is an iterator into mLayers getStart()172 const std::vector<CachedSet>::const_iterator& getStart() const { return mStart; } 173 // Gets the total number of layers encompassing this Run. getLayerLength()174 size_t getLayerLength() const { return mLength; } 175 // Gets the hole punch candidate for this Run. getHolePunchCandidate()176 const CachedSet* getHolePunchCandidate() const { return mHolePunchCandidate; } getBlurringLayer()177 const CachedSet* getBlurringLayer() const { return mBlurringLayer; } 178 179 private: Run(std::vector<CachedSet>::const_iterator start,size_t length,const CachedSet * holePunchCandidate,const CachedSet * blurringLayer)180 Run(std::vector<CachedSet>::const_iterator start, size_t length, 181 const CachedSet* holePunchCandidate, const CachedSet* blurringLayer) 182 : mStart(start), 183 mLength(length), 184 mHolePunchCandidate(holePunchCandidate), 185 mBlurringLayer(blurringLayer) {} 186 const std::vector<CachedSet>::const_iterator mStart; 187 const size_t mLength; 188 const CachedSet* const mHolePunchCandidate; 189 const CachedSet* const mBlurringLayer; 190 191 friend class Builder; 192 }; 193 194 std::vector<Run> findCandidateRuns(std::chrono::steady_clock::time_point now) const; 195 196 std::optional<Run> findBestRun(std::vector<Run>& runs) const; 197 198 void buildCachedSets(std::chrono::steady_clock::time_point now); 199 200 renderengine::RenderEngine& mRenderEngine; 201 const Tunables mTunables; 202 203 TexturePool mTexturePool; 204 205 protected: 206 // mNewCachedSet must be destroyed before mTexturePool is. 207 std::optional<CachedSet> mNewCachedSet; 208 209 private: 210 ui::Size mDisplaySize; 211 212 NonBufferHash mCurrentGeometry; 213 std::chrono::steady_clock::time_point mLastGeometryUpdate; 214 215 std::vector<CachedSet> mLayers; 216 217 // Statistics 218 size_t mUnflattenedDisplayCost = 0; 219 size_t mFlattenedDisplayCost = 0; 220 std::unordered_map<size_t, size_t> mInitialLayerCounts; 221 std::unordered_map<size_t, size_t> mFinalLayerCounts; 222 size_t mCachedSetCreationCount = 0; 223 size_t mCachedSetCreationCost = 0; 224 std::unordered_map<size_t, size_t> mInvalidatedCachedSetAges; 225 }; 226 227 } // namespace compositionengine::impl::planner 228 } // namespace android 229