Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 <deque> |
| 20 | #include <mutex> |
| 21 | |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 22 | #include <gui/ISurfaceComposer.h> |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 23 | #include <ui/FenceTime.h> |
| 24 | #include <utils/RefBase.h> |
| 25 | #include <utils/Timers.h> |
| 26 | |
| 27 | namespace android::frametimeline { |
| 28 | |
| 29 | class FrameTimelineTest; |
| 30 | |
| 31 | /* |
| 32 | * Collection of timestamps that can be used for both predictions and actual times. |
| 33 | */ |
| 34 | struct TimelineItem { |
| 35 | TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0, |
| 36 | const nsecs_t presentTime = 0) |
| 37 | : startTime(startTime), endTime(endTime), presentTime(presentTime) {} |
| 38 | |
| 39 | nsecs_t startTime; |
| 40 | nsecs_t endTime; |
| 41 | nsecs_t presentTime; |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It |
| 46 | * saves these predictions for a short period of time and returns the predictions for a given token, |
| 47 | * if it hasn't expired. |
| 48 | */ |
| 49 | class TokenManager { |
| 50 | public: |
| 51 | virtual ~TokenManager() = default; |
| 52 | |
| 53 | // Generates a token for the given set of predictions. Stores the predictions for 120ms and |
| 54 | // destroys it later. |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 55 | virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | enum class PredictionState { |
| 59 | Valid, // Predictions obtained successfully from the TokenManager |
| 60 | Expired, // TokenManager no longer has the predictions |
| 61 | None, // Predictions are either not present or didn't come from TokenManager |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * Stores a set of predictions and the corresponding actual timestamps pertaining to a single frame |
| 66 | * from the app |
| 67 | */ |
| 68 | class SurfaceFrame { |
| 69 | public: |
| 70 | enum class PresentState { |
| 71 | Presented, // Buffer was latched and presented by SurfaceFlinger |
| 72 | Dropped, // Buffer was dropped by SurfaceFlinger |
| 73 | Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet |
| 74 | }; |
| 75 | |
| 76 | virtual ~SurfaceFrame() = default; |
| 77 | |
| 78 | virtual TimelineItem getPredictions() = 0; |
| 79 | virtual TimelineItem getActuals() = 0; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 80 | virtual nsecs_t getActualQueueTime() = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 81 | virtual PresentState getPresentState() = 0; |
| 82 | virtual PredictionState getPredictionState() = 0; |
| 83 | |
| 84 | virtual void setPresentState(PresentState state) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 85 | |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 86 | // Actual timestamps of the app are set individually at different functions. |
| 87 | // Start time (if the app provides) and Queue time are accessible after queueing the frame, |
Ady Abraham | 7f8a1e6 | 2020-09-28 16:09:35 -0700 | [diff] [blame^] | 88 | // whereas Acquire Fence time is available only during latch. |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 89 | virtual void setActualStartTime(nsecs_t actualStartTime) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 90 | virtual void setActualQueueTime(nsecs_t actualQueueTime) = 0; |
Ady Abraham | 7f8a1e6 | 2020-09-28 16:09:35 -0700 | [diff] [blame^] | 91 | virtual void setAcquireFenceTime(nsecs_t acquireFenceTime) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were |
| 96 | * presented |
| 97 | */ |
| 98 | class FrameTimeline { |
| 99 | public: |
| 100 | virtual ~FrameTimeline() = default; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 101 | virtual TokenManager* getTokenManager() = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 102 | |
| 103 | // Create a new surface frame, set the predictions based on a token and return it to the caller. |
| 104 | // Sets the PredictionState of SurfaceFrame. |
| 105 | virtual std::unique_ptr<SurfaceFrame> createSurfaceFrameForToken( |
| 106 | const std::string& layerName, std::optional<int64_t> token) = 0; |
| 107 | |
| 108 | // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be |
| 109 | // composited into one display frame. |
| 110 | virtual void addSurfaceFrame(std::unique_ptr<SurfaceFrame> surfaceFrame, |
| 111 | SurfaceFrame::PresentState state) = 0; |
| 112 | |
| 113 | // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on |
| 114 | // the token and sets the actualSfWakeTime for the current DisplayFrame. |
| 115 | virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime) = 0; |
| 116 | |
| 117 | // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence |
| 118 | // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in |
| 119 | // that vsync. |
| 120 | virtual void setSfPresent(nsecs_t sfPresentTime, |
| 121 | const std::shared_ptr<FenceTime>& presentFence) = 0; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 122 | |
| 123 | virtual void dump(std::string& result) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | namespace impl { |
| 127 | |
| 128 | using namespace std::chrono_literals; |
| 129 | |
| 130 | class TokenManager : public android::frametimeline::TokenManager { |
| 131 | public: |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 132 | TokenManager() : mCurrentToken(ISurfaceComposer::INVALID_VSYNC_ID + 1) {} |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 133 | ~TokenManager() = default; |
| 134 | |
| 135 | int64_t generateTokenForPredictions(TimelineItem&& predictions) override; |
| 136 | std::optional<TimelineItem> getPredictionsForToken(int64_t token); |
| 137 | |
| 138 | private: |
| 139 | // Friend class for testing |
| 140 | friend class android::frametimeline::FrameTimelineTest; |
| 141 | |
| 142 | void flushTokens(nsecs_t flushTime) REQUIRES(mMutex); |
| 143 | |
| 144 | std::unordered_map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex); |
| 145 | std::vector<std::pair<int64_t, nsecs_t>> mTokens GUARDED_BY(mMutex); |
| 146 | int64_t mCurrentToken GUARDED_BY(mMutex); |
| 147 | std::mutex mMutex; |
| 148 | static constexpr nsecs_t kMaxRetentionTime = |
| 149 | std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count(); |
| 150 | }; |
| 151 | |
| 152 | class SurfaceFrame : public android::frametimeline::SurfaceFrame { |
| 153 | public: |
| 154 | SurfaceFrame(const std::string& layerName, PredictionState predictionState, |
| 155 | TimelineItem&& predictions); |
| 156 | ~SurfaceFrame() = default; |
| 157 | |
| 158 | TimelineItem getPredictions() override { return mPredictions; }; |
| 159 | TimelineItem getActuals() override; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 160 | nsecs_t getActualQueueTime() override; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 161 | PresentState getPresentState() override; |
| 162 | PredictionState getPredictionState() override; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 163 | |
| 164 | void setActualStartTime(nsecs_t actualStartTime) override; |
| 165 | void setActualQueueTime(nsecs_t actualQueueTime) override; |
Ady Abraham | 7f8a1e6 | 2020-09-28 16:09:35 -0700 | [diff] [blame^] | 166 | void setAcquireFenceTime(nsecs_t acquireFenceTime) override; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 167 | void setPresentState(PresentState state) override; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 168 | void setActualPresentTime(nsecs_t presentTime); |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 169 | void dump(std::string& result); |
| 170 | |
| 171 | private: |
| 172 | const std::string mLayerName; |
| 173 | PresentState mPresentState GUARDED_BY(mMutex); |
| 174 | PredictionState mPredictionState GUARDED_BY(mMutex); |
| 175 | const TimelineItem mPredictions; |
| 176 | TimelineItem mActuals GUARDED_BY(mMutex); |
| 177 | nsecs_t mActualQueueTime; |
| 178 | std::mutex mMutex; |
| 179 | }; |
| 180 | |
| 181 | class FrameTimeline : public android::frametimeline::FrameTimeline { |
| 182 | public: |
| 183 | FrameTimeline(); |
| 184 | ~FrameTimeline() = default; |
| 185 | |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 186 | frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; } |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 187 | std::unique_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForToken( |
| 188 | const std::string& layerName, std::optional<int64_t> token) override; |
| 189 | void addSurfaceFrame(std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame, |
| 190 | SurfaceFrame::PresentState state) override; |
| 191 | void setSfWakeUp(int64_t token, nsecs_t wakeupTime) override; |
| 192 | void setSfPresent(nsecs_t sfPresentTime, |
| 193 | const std::shared_ptr<FenceTime>& presentFence) override; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 194 | void dump(std::string& result) override; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
| 197 | // Friend class for testing |
| 198 | friend class android::frametimeline::FrameTimelineTest; |
| 199 | |
| 200 | /* |
| 201 | * DisplayFrame should be used only internally within FrameTimeline. |
| 202 | */ |
| 203 | struct DisplayFrame { |
| 204 | DisplayFrame(); |
| 205 | |
| 206 | /* Usage of TimelineItem w.r.t SurfaceFlinger |
| 207 | * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates |
| 208 | * endTime Time when SurfaceFlinger sends a composited frame to Display |
| 209 | * presentTime Time when the composited frame was presented on screen |
| 210 | */ |
| 211 | TimelineItem surfaceFlingerPredictions; |
| 212 | TimelineItem surfaceFlingerActuals; |
| 213 | |
| 214 | // Collection of predictions and actual values sent over by Layers |
| 215 | std::vector<std::unique_ptr<SurfaceFrame>> surfaceFrames; |
| 216 | |
| 217 | PredictionState predictionState; |
| 218 | }; |
| 219 | |
| 220 | void flushPendingPresentFences() REQUIRES(mMutex); |
| 221 | void finalizeCurrentDisplayFrame() REQUIRES(mMutex); |
| 222 | |
| 223 | // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array |
| 224 | std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex); |
| 225 | std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>> |
| 226 | mPendingPresentFences GUARDED_BY(mMutex); |
| 227 | std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex); |
| 228 | TokenManager mTokenManager; |
| 229 | std::mutex mMutex; |
| 230 | static constexpr uint32_t kMaxDisplayFrames = 64; |
| 231 | // The initial container size for the vector<SurfaceFrames> inside display frame. Although this |
| 232 | // number doesn't represent any bounds on the number of surface frames that can go in a display |
| 233 | // frame, this is a good starting size for the vector so that we can avoid the internal vector |
| 234 | // resizing that happens with push_back. |
| 235 | static constexpr uint32_t kNumSurfaceFramesInitial = 10; |
| 236 | }; |
| 237 | |
| 238 | } // namespace impl |
| 239 | } // namespace android::frametimeline |