blob: 3b429d189c02b3592725e62d3a1ecf3c16311838 [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);
Brian Anderson069b3652016-07-22 10:32:47 -070041 void addFrame(nsecs_t refreshStartTime, sp<Fence> presentFence,
42 sp<Fence> retireFence, const Vector<sp<Layer>>& layers,
43 sp<Fence> glDoneFence);
Pablo Ceballosce796e72016-02-04 19:10:51 -080044 bool getFrameTimestamps(const Layer& layer, uint64_t frameNumber,
45 FrameTimestamps* outTimestamps);
Pablo Ceballos40845df2016-01-25 17:41:15 -080046
47protected:
Pablo Ceballos5045ab22016-05-17 17:52:08 -070048 static constexpr size_t MAX_FRAME_HISTORY = 8;
Pablo Ceballos40845df2016-01-25 17:41:15 -080049
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 Andersondbd0ea82016-07-22 09:38:59 -070054 // time the producer requested this frame be presented
55 nsecs_t requestedPresentTime;
Pablo Ceballos40845df2016-01-25 17:41:15 -080056 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 Andersondbd0ea82016-07-22 09:38:59 -070062 bool isGlesComposition, nsecs_t requestedPresentTime,
Pablo Ceballos40845df2016-01-25 17:41:15 -080063 nsecs_t acquireTime, nsecs_t releaseTime,
64 sp<Fence> acquireFence, sp<Fence> releaseFence) :
65 name(name), frameNumber(frameNumber),
Brian Andersondbd0ea82016-07-22 09:38:59 -070066 isGlesComposition(isGlesComposition),
67 requestedPresentTime(requestedPresentTime),
Pablo Ceballos40845df2016-01-25 17:41:15 -080068 acquireTime(acquireTime), releaseTime(releaseTime),
69 acquireFence(acquireFence), releaseFence(releaseFence) {};
70 LayerRecord() : name("uninitialized"), frameNumber(0),
Brian Andersondbd0ea82016-07-22 09:38:59 -070071 isGlesComposition(false), requestedPresentTime(0),
72 acquireTime(0), releaseTime(0), acquireFence(Fence::NO_FENCE),
Pablo Ceballos40845df2016-01-25 17:41:15 -080073 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 Anderson069b3652016-07-22 10:32:47 -070083 // timestamp from the present fence
84 nsecs_t presentTime;
Pablo Ceballos40845df2016-01-25 17:41:15 -080085 // timestamp from the retire fence
86 nsecs_t retireTime;
87 // timestamp from the GLES composition completion fence
88 nsecs_t glesCompositionDoneTime;
Brian Anderson069b3652016-07-22 10:32:47 -070089 // primary display present fence for this frame
90 sp<Fence> presentFence;
Pablo Ceballos40845df2016-01-25 17:41:15 -080091 // 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 Anderson069b3652016-07-22 10:32:47 -070097 presentTime(0), retireTime(0), glesCompositionDoneTime(0),
98 presentFence(Fence::NO_FENCE), retireFence(Fence::NO_FENCE),
Pablo Ceballos40845df2016-01-25 17:41:15 -080099 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