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