blob: 38a6a21f1e9dd3775728dc41ea033dcdacc4fd65 [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 {
John Reckba6adf62015-02-19 14:36:50 -080033 kFlags = 0,
34 kIntendedVsync,
35 kVsync,
36 kOldestInputEvent,
37 kNewestInputEvent,
38 kHandleInputStart,
39 kAnimationStart,
40 kPerformTraversalsStart,
41 kDrawStart,
42 // End of UI frame info
43
44 kSyncStart,
45 kIssueDrawCommandsStart,
46 kSwapBuffers,
47 kFrameCompleted,
48
49 // Must be the last value!
50 kNumIndexes
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
John Reckc87be992015-02-20 10:57:22 -080055enum class FrameInfoFlags {
John Reckba6adf62015-02-19 14:36:50 -080056 kWindowLayoutChanged = 1 << 0,
57 kRTAnimation = 1 << 1,
58 kSurfaceCanvas = 1 << 2,
John Reck240ff622015-04-28 13:50:00 -070059 kSkippedFrame = 1 << 3,
John Reckc87be992015-02-20 10:57:22 -080060};
61MAKE_FLAGS_ENUM(FrameInfoFlags)
John Reckba6adf62015-02-19 14:36:50 -080062
63class ANDROID_API UiFrameInfoBuilder {
64public:
65 UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) {
66 memset(mBuffer, 0, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t));
67 }
68
69 UiFrameInfoBuilder& setVsync(nsecs_t vsyncTime, nsecs_t intendedVsync) {
John Reckc87be992015-02-20 10:57:22 -080070 set(FrameInfoIndex::kVsync) = vsyncTime;
71 set(FrameInfoIndex::kIntendedVsync) = intendedVsync;
John Reckbf3c6022015-06-02 15:55:00 -070072 // Pretend the other fields are all at vsync, too, so that naive
73 // duration calculations end up being 0 instead of very large
74 set(FrameInfoIndex::kHandleInputStart) = vsyncTime;
75 set(FrameInfoIndex::kAnimationStart) = vsyncTime;
76 set(FrameInfoIndex::kPerformTraversalsStart) = vsyncTime;
77 set(FrameInfoIndex::kDrawStart) = vsyncTime;
John Reckba6adf62015-02-19 14:36:50 -080078 return *this;
79 }
80
John Reckc87be992015-02-20 10:57:22 -080081 UiFrameInfoBuilder& addFlag(FrameInfoFlags flag) {
82 set(FrameInfoIndex::kFlags) |= static_cast<uint64_t>(flag);
John Reckba6adf62015-02-19 14:36:50 -080083 return *this;
84 }
85
86private:
John Reckc87be992015-02-20 10:57:22 -080087 inline int64_t& set(FrameInfoIndex index) {
88 return mBuffer[static_cast<int>(index)];
89 }
90
John Reckba6adf62015-02-19 14:36:50 -080091 int64_t* mBuffer;
92};
93
94class FrameInfo {
95public:
96 void importUiThreadInfo(int64_t* info);
97
98 void markSyncStart() {
John Reckc87be992015-02-20 10:57:22 -080099 set(FrameInfoIndex::kSyncStart) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800100 }
101
102 void markIssueDrawCommandsStart() {
John Reckc87be992015-02-20 10:57:22 -0800103 set(FrameInfoIndex::kIssueDrawCommandsStart) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800104 }
105
106 void markSwapBuffers() {
John Reckc87be992015-02-20 10:57:22 -0800107 set(FrameInfoIndex::kSwapBuffers) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800108 }
109
110 void markFrameCompleted() {
John Reckc87be992015-02-20 10:57:22 -0800111 set(FrameInfoIndex::kFrameCompleted) = systemTime(CLOCK_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800112 }
113
John Reck240ff622015-04-28 13:50:00 -0700114 void addFlag(FrameInfoFlags flag) {
115 set(FrameInfoIndex::kFlags) |= static_cast<uint64_t>(flag);
116 }
117
John Reckc87be992015-02-20 10:57:22 -0800118 int64_t operator[](FrameInfoIndex index) const {
John Reckba6adf62015-02-19 14:36:50 -0800119 if (index == FrameInfoIndex::kNumIndexes) return 0;
120 return mFrameInfo[static_cast<int>(index)];
121 }
122
123 int64_t operator[](int index) const {
John Reckc87be992015-02-20 10:57:22 -0800124 if (index < 0 || index >= static_cast<int>(FrameInfoIndex::kNumIndexes)) return 0;
125 return mFrameInfo[index];
John Reckba6adf62015-02-19 14:36:50 -0800126 }
127
128private:
John Reckc87be992015-02-20 10:57:22 -0800129 inline int64_t& set(FrameInfoIndex index) {
130 return mFrameInfo[static_cast<int>(index)];
131 }
132
133 int64_t mFrameInfo[static_cast<int>(FrameInfoIndex::kNumIndexes)];
John Reckba6adf62015-02-19 14:36:50 -0800134};
135
136} /* namespace uirenderer */
137} /* namespace android */
138
139#endif /* FRAMEINFO_H_ */