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; 32 struct FrameTimestamps; 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); 41 void addFrame(nsecs_t refreshStartTime, sp<Fence> retireFence, 42 const Vector<sp<Layer>>& layers, sp<Fence> glDoneFence); 43 bool getFrameTimestamps(const Layer& layer, uint64_t frameNumber, 44 FrameTimestamps* outTimestamps); 45 46 protected: 47 static constexpr size_t MAX_FRAME_HISTORY = 8; 48 49 struct LayerRecord { 50 String8 name; // layer name 51 uint64_t frameNumber; // frame number for this layer 52 bool isGlesComposition; // was GLES composition used for this layer? 53 nsecs_t postedTime; // time when buffer was queued 54 nsecs_t acquireTime; // timestamp from the acquire fence 55 nsecs_t releaseTime; // timestamp from the release fence 56 sp<Fence> acquireFence; // acquire fence 57 sp<Fence> releaseFence; // release fence 58 LayerRecordLayerRecord59 LayerRecord(const String8& name, uint64_t frameNumber, 60 bool isGlesComposition, nsecs_t postedTime, 61 nsecs_t acquireTime, nsecs_t releaseTime, 62 sp<Fence> acquireFence, sp<Fence> releaseFence) : 63 name(name), frameNumber(frameNumber), 64 isGlesComposition(isGlesComposition), postedTime(postedTime), 65 acquireTime(acquireTime), releaseTime(releaseTime), 66 acquireFence(acquireFence), releaseFence(releaseFence) {}; LayerRecordLayerRecord67 LayerRecord() : name("uninitialized"), frameNumber(0), 68 isGlesComposition(false), postedTime(0), acquireTime(0), 69 releaseTime(0), acquireFence(Fence::NO_FENCE), 70 releaseFence(Fence::NO_FENCE) {}; 71 }; 72 73 struct FrameRecord { 74 // global SurfaceFlinger frame counter 75 uint64_t frameId; 76 // layer data for this frame 77 std::unordered_map<int32_t, LayerRecord> layers; 78 // timestamp for when SurfaceFlinger::handleMessageRefresh() was called 79 nsecs_t refreshStartTime; 80 // timestamp from the retire fence 81 nsecs_t retireTime; 82 // timestamp from the GLES composition completion fence 83 nsecs_t glesCompositionDoneTime; 84 // primary display retire fence for this frame 85 sp<Fence> retireFence; 86 // if GLES composition was done, the fence for its completion 87 sp<Fence> glesCompositionDoneFence; 88 FrameRecordFrameRecord89 FrameRecord() : frameId(0), layers(), refreshStartTime(0), 90 retireTime(0), glesCompositionDoneTime(0), 91 retireFence(Fence::NO_FENCE), 92 glesCompositionDoneFence(Fence::NO_FENCE) {} 93 }; 94 95 uint64_t mFrameCounter; 96 uint32_t mOffset; 97 FrameRecord mFrames[MAX_FRAME_HISTORY]; 98 Mutex mMutex; 99 100 void checkFencesForCompletion(); 101 }; 102 103 } 104 105 #endif // ANDROID_FRAMETRACKER_H 106