Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | #ifndef ANDROID_FENCETRACKER_H |
| 18 | #define ANDROID_FENCETRACKER_H |
| 19 | |
| 20 | #include <ui/Fence.h> |
| 21 | #include <utils/Mutex.h> |
| 22 | #include <utils/RefBase.h> |
| 23 | #include <utils/String8.h> |
| 24 | #include <utils/Timers.h> |
| 25 | #include <utils/Vector.h> |
| 26 | |
| 27 | #include <unordered_map> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class Layer; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 32 | struct FrameTimestamps; |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 33 | /* |
| 34 | * Keeps a circular buffer of fence/timestamp data for the last N frames in |
| 35 | * SurfaceFlinger. Gets timestamps for fences after they have signaled. |
| 36 | */ |
| 37 | class FenceTracker { |
| 38 | public: |
| 39 | FenceTracker(); |
| 40 | void dump(String8* outString); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 41 | void addFrame(nsecs_t refreshStartTime, sp<Fence> presentFence, |
| 42 | sp<Fence> retireFence, const Vector<sp<Layer>>& layers, |
| 43 | sp<Fence> glDoneFence); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 44 | bool getFrameTimestamps(const Layer& layer, uint64_t frameNumber, |
| 45 | FrameTimestamps* outTimestamps); |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 46 | |
| 47 | protected: |
Pablo Ceballos | 5045ab2 | 2016-05-17 17:52:08 -0700 | [diff] [blame] | 48 | static constexpr size_t MAX_FRAME_HISTORY = 8; |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 49 | |
| 50 | struct LayerRecord { |
| 51 | String8 name; // layer name |
| 52 | uint64_t frameNumber; // frame number for this layer |
| 53 | bool isGlesComposition; // was GLES composition used for this layer? |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 54 | // time the producer requested this frame be presented |
| 55 | nsecs_t requestedPresentTime; |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 56 | nsecs_t acquireTime; // timestamp from the acquire fence |
| 57 | nsecs_t releaseTime; // timestamp from the release fence |
| 58 | sp<Fence> acquireFence; // acquire fence |
| 59 | sp<Fence> releaseFence; // release fence |
| 60 | |
| 61 | LayerRecord(const String8& name, uint64_t frameNumber, |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 62 | bool isGlesComposition, nsecs_t requestedPresentTime, |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 63 | nsecs_t acquireTime, nsecs_t releaseTime, |
| 64 | sp<Fence> acquireFence, sp<Fence> releaseFence) : |
| 65 | name(name), frameNumber(frameNumber), |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 66 | isGlesComposition(isGlesComposition), |
| 67 | requestedPresentTime(requestedPresentTime), |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 68 | acquireTime(acquireTime), releaseTime(releaseTime), |
| 69 | acquireFence(acquireFence), releaseFence(releaseFence) {}; |
| 70 | LayerRecord() : name("uninitialized"), frameNumber(0), |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 71 | isGlesComposition(false), requestedPresentTime(0), |
| 72 | acquireTime(0), releaseTime(0), acquireFence(Fence::NO_FENCE), |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 73 | releaseFence(Fence::NO_FENCE) {}; |
| 74 | }; |
| 75 | |
| 76 | struct FrameRecord { |
| 77 | // global SurfaceFlinger frame counter |
| 78 | uint64_t frameId; |
| 79 | // layer data for this frame |
| 80 | std::unordered_map<int32_t, LayerRecord> layers; |
| 81 | // timestamp for when SurfaceFlinger::handleMessageRefresh() was called |
| 82 | nsecs_t refreshStartTime; |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 83 | // timestamp from the present fence |
| 84 | nsecs_t presentTime; |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 85 | // timestamp from the retire fence |
| 86 | nsecs_t retireTime; |
| 87 | // timestamp from the GLES composition completion fence |
| 88 | nsecs_t glesCompositionDoneTime; |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 89 | // primary display present fence for this frame |
| 90 | sp<Fence> presentFence; |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 91 | // primary display retire fence for this frame |
| 92 | sp<Fence> retireFence; |
| 93 | // if GLES composition was done, the fence for its completion |
| 94 | sp<Fence> glesCompositionDoneFence; |
| 95 | |
| 96 | FrameRecord() : frameId(0), layers(), refreshStartTime(0), |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 97 | presentTime(0), retireTime(0), glesCompositionDoneTime(0), |
| 98 | presentFence(Fence::NO_FENCE), retireFence(Fence::NO_FENCE), |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 99 | glesCompositionDoneFence(Fence::NO_FENCE) {} |
| 100 | }; |
| 101 | |
| 102 | uint64_t mFrameCounter; |
| 103 | uint32_t mOffset; |
| 104 | FrameRecord mFrames[MAX_FRAME_HISTORY]; |
| 105 | Mutex mMutex; |
| 106 | |
| 107 | void checkFencesForCompletion(); |
| 108 | }; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | #endif // ANDROID_FRAMETRACKER_H |