blob: 51674fbd557eaee6d303fbe9d64a14ccde672074 [file] [log] [blame]
John Reckba6adf62015-02-19 14:36:50 -08001/*
2 * Copyright (C) 2015 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#ifndef FRAMEINFO_H_
17#define FRAMEINFO_H_
18
19#include "utils/Macros.h"
20
21#include <cutils/compiler.h>
22#include <utils/Timers.h>
23
24#include <memory.h>
John Reck4db3d172015-06-02 15:58:43 -070025#include <string>
John Reckba6adf62015-02-19 14:36:50 -080026
27namespace android {
28namespace uirenderer {
29
30#define UI_THREAD_FRAME_INFO_SIZE 9
31
John Reckc87be992015-02-20 10:57:22 -080032enum class FrameInfoIndex {
Chris Craik1b54fb22015-06-02 17:40:58 -070033 Flags = 0,
34 IntendedVsync,
35 Vsync,
36 OldestInputEvent,
37 NewestInputEvent,
38 HandleInputStart,
39 AnimationStart,
40 PerformTraversalsStart,
41 DrawStart,
John Reckba6adf62015-02-19 14:36:50 -080042 // End of UI frame info
43
John Reckbe3fba02015-07-06 13:49:58 -070044 SyncQueued,
45
Chris Craik1b54fb22015-06-02 17:40:58 -070046 SyncStart,
47 IssueDrawCommandsStart,
48 SwapBuffers,
49 FrameCompleted,
John Reckba6adf62015-02-19 14:36:50 -080050
John Reck2d5b8d72016-07-28 15:36:11 -070051 DequeueBufferDuration,
52 QueueBufferDuration,
53
Stan Iliev7203e1f2019-07-25 13:12:02 -040054 GpuCompleted,
55
John Reckba6adf62015-02-19 14:36:50 -080056 // Must be the last value!
John Reck65ddb152016-08-02 09:38:26 -070057 // Also must be kept in sync with FrameMetrics.java#FRAME_STATS_COUNT
Chris Craik1b54fb22015-06-02 17:40:58 -070058 NumIndexes
John Reckc87be992015-02-20 10:57:22 -080059};
John Reckba6adf62015-02-19 14:36:50 -080060
John Reck2a8bb052015-06-03 09:52:01 -070061extern const std::string FrameInfoNames[];
John Reck4db3d172015-06-02 15:58:43 -070062
Chris Craik1b54fb22015-06-02 17:40:58 -070063namespace FrameInfoFlags {
John Reck1bcacfd2017-11-03 10:12:19 -070064enum {
65 WindowLayoutChanged = 1 << 0,
66 RTAnimation = 1 << 1,
67 SurfaceCanvas = 1 << 2,
68 SkippedFrame = 1 << 3,
69};
John Reckc87be992015-02-20 10:57:22 -080070};
John Reckba6adf62015-02-19 14:36:50 -080071
72class ANDROID_API UiFrameInfoBuilder {
73public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070074 explicit UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) {
John Reckba6adf62015-02-19 14:36:50 -080075 memset(mBuffer, 0, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t));
76 }
77
78 UiFrameInfoBuilder& setVsync(nsecs_t vsyncTime, nsecs_t intendedVsync) {
Chris Craik1b54fb22015-06-02 17:40:58 -070079 set(FrameInfoIndex::Vsync) = vsyncTime;
80 set(FrameInfoIndex::IntendedVsync) = intendedVsync;
John Reckbf3c6022015-06-02 15:55:00 -070081 // Pretend the other fields are all at vsync, too, so that naive
82 // duration calculations end up being 0 instead of very large
Chris Craik1b54fb22015-06-02 17:40:58 -070083 set(FrameInfoIndex::HandleInputStart) = vsyncTime;
84 set(FrameInfoIndex::AnimationStart) = vsyncTime;
85 set(FrameInfoIndex::PerformTraversalsStart) = vsyncTime;
86 set(FrameInfoIndex::DrawStart) = vsyncTime;
John Reckba6adf62015-02-19 14:36:50 -080087 return *this;
88 }
89
Chris Craik1b54fb22015-06-02 17:40:58 -070090 UiFrameInfoBuilder& addFlag(int frameInfoFlag) {
91 set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag);
John Reckba6adf62015-02-19 14:36:50 -080092 return *this;
93 }
94
95private:
John Reck1bcacfd2017-11-03 10:12:19 -070096 inline int64_t& set(FrameInfoIndex index) { return mBuffer[static_cast<int>(index)]; }
John Reckc87be992015-02-20 10:57:22 -080097
John Reckba6adf62015-02-19 14:36:50 -080098 int64_t* mBuffer;
99};
100
101class FrameInfo {
102public:
103 void importUiThreadInfo(int64_t* info);
104
Jerome Gaillarde218c692019-06-14 12:58:57 +0100105 void markSyncStart() { set(FrameInfoIndex::SyncStart) = systemTime(SYSTEM_TIME_MONOTONIC); }
John Reckba6adf62015-02-19 14:36:50 -0800106
107 void markIssueDrawCommandsStart() {
Jerome Gaillarde218c692019-06-14 12:58:57 +0100108 set(FrameInfoIndex::IssueDrawCommandsStart) = systemTime(SYSTEM_TIME_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800109 }
110
Jerome Gaillarde218c692019-06-14 12:58:57 +0100111 void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(SYSTEM_TIME_MONOTONIC); }
John Reckba6adf62015-02-19 14:36:50 -0800112
Jerome Gaillarde218c692019-06-14 12:58:57 +0100113 void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); }
John Reckba6adf62015-02-19 14:36:50 -0800114
Chris Craik1b54fb22015-06-02 17:40:58 -0700115 void addFlag(int frameInfoFlag) {
116 set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag);
John Reck240ff622015-04-28 13:50:00 -0700117 }
118
John Reck1bcacfd2017-11-03 10:12:19 -0700119 const int64_t* data() const { return mFrameInfo; }
Andres Morales06f5bc72015-12-15 15:21:31 -0800120
John Reck1bcacfd2017-11-03 10:12:19 -0700121 inline int64_t operator[](FrameInfoIndex index) const { return get(index); }
John Reckba6adf62015-02-19 14:36:50 -0800122
John Reck41300272015-06-03 14:42:34 -0700123 inline int64_t operator[](int index) const {
Chris Craik1b54fb22015-06-02 17:40:58 -0700124 if (index < 0 || index >= static_cast<int>(FrameInfoIndex::NumIndexes)) return 0;
John Reckc87be992015-02-20 10:57:22 -0800125 return mFrameInfo[index];
John Reckba6adf62015-02-19 14:36:50 -0800126 }
127
John Reck41300272015-06-03 14:42:34 -0700128 inline int64_t duration(FrameInfoIndex start, FrameInfoIndex end) const {
John Reckbe3fba02015-07-06 13:49:58 -0700129 int64_t endtime = get(end);
130 int64_t starttime = get(start);
John Reck41300272015-06-03 14:42:34 -0700131 int64_t gap = endtime - starttime;
132 gap = starttime > 0 ? gap : 0;
John Reck1bcacfd2017-11-03 10:12:19 -0700133 if (end > FrameInfoIndex::SyncQueued && start < FrameInfoIndex::SyncQueued) {
John Reckbe3fba02015-07-06 13:49:58 -0700134 // Need to subtract out the time spent in a stalled state
135 // as this will be captured by the previous frame's info
John Reck1bcacfd2017-11-03 10:12:19 -0700136 int64_t offset = get(FrameInfoIndex::SyncStart) - get(FrameInfoIndex::SyncQueued);
John Reckbe3fba02015-07-06 13:49:58 -0700137 if (offset > 0) {
138 gap -= offset;
139 }
140 }
John Reck41300272015-06-03 14:42:34 -0700141 return gap > 0 ? gap : 0;
142 }
143
144 inline int64_t totalDuration() const {
145 return duration(FrameInfoIndex::IntendedVsync, FrameInfoIndex::FrameCompleted);
146 }
147
Stan Iliev7203e1f2019-07-25 13:12:02 -0400148 inline int64_t gpuDrawTime() const {
149 // GPU start time is approximated to the moment before swapBuffer is invoked.
150 // We could add an EGLSyncKHR fence at the beginning of the frame, but that is an overhead.
151 int64_t endTime = get(FrameInfoIndex::GpuCompleted);
152 return endTime > 0 ? endTime - get(FrameInfoIndex::SwapBuffers) : -1;
153 }
154
John Reck1bcacfd2017-11-03 10:12:19 -0700155 inline int64_t& set(FrameInfoIndex index) { return mFrameInfo[static_cast<int>(index)]; }
John Reckc87be992015-02-20 10:57:22 -0800156
John Reckbe3fba02015-07-06 13:49:58 -0700157 inline int64_t get(FrameInfoIndex index) const {
158 if (index == FrameInfoIndex::NumIndexes) return 0;
159 return mFrameInfo[static_cast<int>(index)];
160 }
161
162private:
Chris Craik1b54fb22015-06-02 17:40:58 -0700163 int64_t mFrameInfo[static_cast<int>(FrameInfoIndex::NumIndexes)];
John Reckba6adf62015-02-19 14:36:50 -0800164};
165
166} /* namespace uirenderer */
167} /* namespace android */
168
169#endif /* FRAMEINFO_H_ */