blob: 385c970c9a33f92d9990084e47e28eee0a18cb7b [file] [log] [blame]
Pablo Ceballos40845df2016-01-25 17:41:15 -08001/*
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
29namespace android {
30
31class Layer;
Pablo Ceballosce796e72016-02-04 19:10:51 -080032struct FrameTimestamps;
Pablo Ceballos40845df2016-01-25 17:41:15 -080033/*
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 */
37class FenceTracker {
38public:
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);
Pablo Ceballosce796e72016-02-04 19:10:51 -080043 bool getFrameTimestamps(const Layer& layer, uint64_t frameNumber,
44 FrameTimestamps* outTimestamps);
Pablo Ceballos40845df2016-01-25 17:41:15 -080045
46protected:
Pablo Ceballos5045ab22016-05-17 17:52:08 -070047 static constexpr size_t MAX_FRAME_HISTORY = 8;
Pablo Ceballos40845df2016-01-25 17:41:15 -080048
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?
Brian Andersondbd0ea82016-07-22 09:38:59 -070053 // time the producer requested this frame be presented
54 nsecs_t requestedPresentTime;
Pablo Ceballos40845df2016-01-25 17:41:15 -080055 nsecs_t acquireTime; // timestamp from the acquire fence
56 nsecs_t releaseTime; // timestamp from the release fence
57 sp<Fence> acquireFence; // acquire fence
58 sp<Fence> releaseFence; // release fence
59
60 LayerRecord(const String8& name, uint64_t frameNumber,
Brian Andersondbd0ea82016-07-22 09:38:59 -070061 bool isGlesComposition, nsecs_t requestedPresentTime,
Pablo Ceballos40845df2016-01-25 17:41:15 -080062 nsecs_t acquireTime, nsecs_t releaseTime,
63 sp<Fence> acquireFence, sp<Fence> releaseFence) :
64 name(name), frameNumber(frameNumber),
Brian Andersondbd0ea82016-07-22 09:38:59 -070065 isGlesComposition(isGlesComposition),
66 requestedPresentTime(requestedPresentTime),
Pablo Ceballos40845df2016-01-25 17:41:15 -080067 acquireTime(acquireTime), releaseTime(releaseTime),
68 acquireFence(acquireFence), releaseFence(releaseFence) {};
69 LayerRecord() : name("uninitialized"), frameNumber(0),
Brian Andersondbd0ea82016-07-22 09:38:59 -070070 isGlesComposition(false), requestedPresentTime(0),
71 acquireTime(0), releaseTime(0), acquireFence(Fence::NO_FENCE),
Pablo Ceballos40845df2016-01-25 17:41:15 -080072 releaseFence(Fence::NO_FENCE) {};
73 };
74
75 struct FrameRecord {
76 // global SurfaceFlinger frame counter
77 uint64_t frameId;
78 // layer data for this frame
79 std::unordered_map<int32_t, LayerRecord> layers;
80 // timestamp for when SurfaceFlinger::handleMessageRefresh() was called
81 nsecs_t refreshStartTime;
82 // timestamp from the retire fence
83 nsecs_t retireTime;
84 // timestamp from the GLES composition completion fence
85 nsecs_t glesCompositionDoneTime;
86 // primary display retire fence for this frame
87 sp<Fence> retireFence;
88 // if GLES composition was done, the fence for its completion
89 sp<Fence> glesCompositionDoneFence;
90
91 FrameRecord() : frameId(0), layers(), refreshStartTime(0),
92 retireTime(0), glesCompositionDoneTime(0),
93 retireFence(Fence::NO_FENCE),
94 glesCompositionDoneFence(Fence::NO_FENCE) {}
95 };
96
97 uint64_t mFrameCounter;
98 uint32_t mOffset;
99 FrameRecord mFrames[MAX_FRAME_HISTORY];
100 Mutex mMutex;
101
102 void checkFencesForCompletion();
103};
104
105}
106
107#endif // ANDROID_FRAMETRACKER_H