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 // Constants not yet backed by a sysprop 91 // CachedSets that contain no more than this many layers may be considered inactive on the basis 92 // of FPS. 93 static constexpr int kNumLayersFpsConsideration = 1; 94 // Frames/Second threshold below which these CachedSets may be considered inactive. 95 static constexpr float kFpsActiveThreshold = 1.f; 96 97 Flattener(renderengine::RenderEngine& renderEngine, const Tunables& tunables); 98 setDisplaySize(ui::Size size)99 void setDisplaySize(ui::Size size) { 100 mDisplaySize = size; 101 mTexturePool.setDisplaySize(size); 102 } 103 104 NonBufferHash flattenLayers(const std::vector<const LayerState*>& layers, NonBufferHash, 105 std::chrono::steady_clock::time_point now); 106 107 // Renders the newest cached sets with the supplied output composition state 108 void renderCachedSets(const OutputCompositionState& outputState, 109 std::optional<std::chrono::steady_clock::time_point> renderDeadline, 110 bool deviceHandlesColorTransform); 111 setTexturePoolEnabled(bool enabled)112 void setTexturePoolEnabled(bool enabled) { mTexturePool.setEnabled(enabled); } 113 114 void dump(std::string& result) const; 115 void dumpLayers(std::string& result) const; 116 getNewCachedSetForTesting()117 const std::optional<CachedSet>& getNewCachedSetForTesting() const { return mNewCachedSet; } 118 119 private: 120 size_t calculateDisplayCost(const std::vector<const LayerState*>& layers) const; 121 122 void resetActivities(NonBufferHash, std::chrono::steady_clock::time_point now); 123 124 NonBufferHash computeLayersHash() const; 125 126 bool mergeWithCachedSets(const std::vector<const LayerState*>& layers, 127 std::chrono::steady_clock::time_point now); 128 129 // A Run is a sequence of CachedSets, which is a candidate for flattening into a single 130 // CachedSet. Because it is wasteful to flatten 1 CachedSet, a run must contain more than 131 // 1 CachedSet or be used for a hole punch. 132 class Run { 133 public: 134 // A builder for a Run, to aid in construction 135 class Builder { 136 private: 137 std::vector<CachedSet>::const_iterator mStart; 138 int32_t mNumSets = 0; 139 const CachedSet* mHolePunchCandidate = nullptr; 140 const CachedSet* mBlurringLayer = nullptr; 141 bool mBuilt = false; 142 143 public: 144 // Initializes a Builder a CachedSet to start from. 145 // This start iterator must be an iterator for mLayers init(const std::vector<CachedSet>::const_iterator & start)146 void init(const std::vector<CachedSet>::const_iterator& start) { 147 mStart = start; 148 mNumSets = 1; 149 } 150 151 // Appends a new CachedSet to the end of the run 152 // The provided length must be the size of the next sequential CachedSet in layers increment()153 void increment() { mNumSets++; } 154 155 // Sets the hole punch candidate for the Run. setHolePunchCandidate(const CachedSet * holePunchCandidate)156 void setHolePunchCandidate(const CachedSet* holePunchCandidate) { 157 mHolePunchCandidate = holePunchCandidate; 158 } 159 setBlurringLayer(const CachedSet * blurringLayer)160 void setBlurringLayer(const CachedSet* blurringLayer) { 161 mBlurringLayer = blurringLayer; 162 } 163 164 // Builds a Run instance, if a valid Run may be built. validateAndBuild()165 std::optional<Run> validateAndBuild() { 166 const bool built = mBuilt; 167 mBuilt = true; 168 if (mNumSets <= 0 || built) { 169 return std::nullopt; 170 } 171 172 const bool requiresHolePunch = 173 mHolePunchCandidate && mHolePunchCandidate->requiresHolePunch(); 174 175 if (!requiresHolePunch) { 176 // If we don't require a hole punch, then treat solid color layers at the front 177 // to be "cheap", so remove them from the candidate cached set. 178 while (mNumSets > 1 && mStart->getLayerCount() == 1 && 179 mStart->getFirstLayer().getBuffer() == nullptr) { 180 mStart++; 181 mNumSets--; 182 } 183 184 // Only allow for single cached sets if a hole punch is required. If we're here, 185 // then we don't require a hole punch, so don't build a run. 186 if (mNumSets <= 1) { 187 return std::nullopt; 188 } 189 } 190 191 return Run(mStart, 192 std::reduce(mStart, mStart + mNumSets, 0u, 193 [](size_t length, const CachedSet& set) { 194 return length + set.getLayerCount(); 195 }), 196 mHolePunchCandidate, mBlurringLayer); 197 } 198 reset()199 void reset() { *this = {}; } 200 }; 201 202 // Gets the starting CachedSet of this run. 203 // This is an iterator into mLayers getStart()204 const std::vector<CachedSet>::const_iterator& getStart() const { return mStart; } 205 // Gets the total number of layers encompassing this Run. getLayerLength()206 size_t getLayerLength() const { return mLength; } 207 // Gets the hole punch candidate for this Run. getHolePunchCandidate()208 const CachedSet* getHolePunchCandidate() const { return mHolePunchCandidate; } getBlurringLayer()209 const CachedSet* getBlurringLayer() const { return mBlurringLayer; } 210 211 private: Run(std::vector<CachedSet>::const_iterator start,size_t length,const CachedSet * holePunchCandidate,const CachedSet * blurringLayer)212 Run(std::vector<CachedSet>::const_iterator start, size_t length, 213 const CachedSet* holePunchCandidate, const CachedSet* blurringLayer) 214 : mStart(start), 215 mLength(length), 216 mHolePunchCandidate(holePunchCandidate), 217 mBlurringLayer(blurringLayer) {} 218 const std::vector<CachedSet>::const_iterator mStart; 219 const size_t mLength; 220 const CachedSet* const mHolePunchCandidate; 221 const CachedSet* const mBlurringLayer; 222 223 friend class Builder; 224 }; 225 226 std::vector<Run> findCandidateRuns(std::chrono::steady_clock::time_point now) const; 227 228 std::optional<Run> findBestRun(std::vector<Run>& runs) const; 229 230 void buildCachedSets(std::chrono::steady_clock::time_point now); 231 232 renderengine::RenderEngine& mRenderEngine; 233 const Tunables mTunables; 234 235 TexturePool mTexturePool; 236 237 protected: 238 // mNewCachedSet must be destroyed before mTexturePool is. 239 std::optional<CachedSet> mNewCachedSet; 240 241 private: 242 ui::Size mDisplaySize; 243 244 NonBufferHash mCurrentGeometry; 245 std::chrono::steady_clock::time_point mLastGeometryUpdate; 246 247 std::vector<CachedSet> mLayers; 248 249 // Statistics 250 size_t mUnflattenedDisplayCost = 0; 251 size_t mFlattenedDisplayCost = 0; 252 std::unordered_map<size_t, size_t> mInitialLayerCounts; 253 std::unordered_map<size_t, size_t> mFinalLayerCounts; 254 size_t mCachedSetCreationCount = 0; 255 size_t mCachedSetCreationCost = 0; 256 std::unordered_map<size_t, size_t> mInvalidatedCachedSetAges; 257 }; 258 259 } // namespace compositionengine::impl::planner 260 } // namespace android 261