blob: 23339cec174164f00153935b61ae8dcf62fd35ba [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
Chris Craik1b54fb22015-06-02 17:40:58 -070044 SyncStart,
45 IssueDrawCommandsStart,
46 SwapBuffers,
47 FrameCompleted,
John Reckba6adf62015-02-19 14:36:50 -080048
49 // Must be the last value!
Chris Craik1b54fb22015-06-02 17:40:58 -070050 NumIndexes
John Reckc87be992015-02-20 10:57:22 -080051};
John Reckba6adf62015-02-19 14:36:50 -080052
John Reck2a8bb052015-06-03 09:52:01 -070053extern const std::string FrameInfoNames[];
John Reck4db3d172015-06-02 15:58:43 -070054
Chris Craik1b54fb22015-06-02 17:40:58 -070055namespace FrameInfoFlags {
56 enum {
57 WindowLayoutChanged = 1 << 0,
58 RTAnimation = 1 << 1,
59 SurfaceCanvas = 1 << 2,
60 SkippedFrame = 1 << 3,
61 };
John Reckc87be992015-02-20 10:57:22 -080062};
John Reckba6adf62015-02-19 14:36:50 -080063
64class ANDROID_API UiFrameInfoBuilder {
65public:
66 UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) {
67 memset(mBuffer, 0, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t));
68 }
69
70 UiFrameInfoBuilder& setVsync(nsecs_t vsyncTime, nsecs_t intendedVsync) {
Chris Craik1b54fb22015-06-02 17:40:58 -070071 set(FrameInfoIndex::Vsync) = vsyncTime;
72 set(FrameInfoIndex::IntendedVsync) = intendedVsync;
John Reckbf3c6022015-06-02 15:55:00 -070073 // Pretend the other fields are all at vsync, too, so that naive
74 // duration calculations end up being 0 instead of very large
Chris Craik1b54fb22015-06-02 17:40:58 -070075 set(FrameInfoIndex::HandleInputStart) = vsyncTime;
76 set(FrameInfoIndex::AnimationStart) = vsyncTime;
77 set(FrameInfoIndex::PerformTraversalsStart) = vsyncTime;
78 set(FrameInfoIndex::DrawStart) = vsyncTime;
John Reckba6adf62015-02-19 14:36:50 -080079 return *this;
80 }
81
Chris Craik1b54fb22015-06-02 17:40:58 -070082 UiFrameInfoBuilder& addFlag(int frameInfoFlag) {
83 set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag);
John Reckba6adf62015-02-19 14:36:50 -080084 return *this;
85 }
86
87private:
John Reckc87be992015-02-20 10:57:22 -080088 inline int64_t& set(FrameInfoIndex index) {
89 return mBuffer[static_cast<int>(index)];
90 }
91
John Reckba6adf62015-02-19 14:36:50 -080092 int64_t* mBuffer;
93};
94
95class FrameInfo {
96public:
97 void importUiThreadInfo(int64_t* info);
98
99 void markSyncStart() {
Chris Craik1b54fb22015-06-02 17:40:58 -0700100 set(FrameInfoIndex::SyncStart) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800101 }
102
103 void markIssueDrawCommandsStart() {
Chris Craik1b54fb22015-06-02 17:40:58 -0700104 set(FrameInfoIndex::IssueDrawCommandsStart) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800105 }
106
107 void markSwapBuffers() {
Chris Craik1b54fb22015-06-02 17:40:58 -0700108 set(FrameInfoIndex::SwapBuffers) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800109 }
110
111 void markFrameCompleted() {
Chris Craik1b54fb22015-06-02 17:40:58 -0700112 set(FrameInfoIndex::FrameCompleted) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800113 }
114
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 Reck41300272015-06-03 14:42:34 -0700119 inline int64_t operator[](FrameInfoIndex index) const {
Chris Craik1b54fb22015-06-02 17:40:58 -0700120 if (index == FrameInfoIndex::NumIndexes) return 0;
John Reckba6adf62015-02-19 14:36:50 -0800121 return mFrameInfo[static_cast<int>(index)];
122 }
123
John Reck41300272015-06-03 14:42:34 -0700124 inline int64_t operator[](int index) const {
Chris Craik1b54fb22015-06-02 17:40:58 -0700125 if (index < 0 || index >= static_cast<int>(FrameInfoIndex::NumIndexes)) return 0;
John Reckc87be992015-02-20 10:57:22 -0800126 return mFrameInfo[index];
John Reckba6adf62015-02-19 14:36:50 -0800127 }
128
John Reck41300272015-06-03 14:42:34 -0700129 inline int64_t duration(FrameInfoIndex start, FrameInfoIndex end) const {
130 int64_t endtime = mFrameInfo[static_cast<int>(end)];
131 int64_t starttime = mFrameInfo[static_cast<int>(start)];
132 int64_t gap = endtime - starttime;
133 gap = starttime > 0 ? gap : 0;
134 return gap > 0 ? gap : 0;
135 }
136
137 inline int64_t totalDuration() const {
138 return duration(FrameInfoIndex::IntendedVsync, FrameInfoIndex::FrameCompleted);
139 }
140
John Reckba6adf62015-02-19 14:36:50 -0800141private:
John Reckc87be992015-02-20 10:57:22 -0800142 inline int64_t& set(FrameInfoIndex index) {
143 return mFrameInfo[static_cast<int>(index)];
144 }
145
Chris Craik1b54fb22015-06-02 17:40:58 -0700146 int64_t mFrameInfo[static_cast<int>(FrameInfoIndex::NumIndexes)];
John Reckba6adf62015-02-19 14:36:50 -0800147};
148
149} /* namespace uirenderer */
150} /* namespace android */
151
152#endif /* FRAMEINFO_H_ */