FrameInfo.h revision 4db3d17debef68f72d23999d69ae68b75f59dda3
1/*
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>
25#include <string>
26
27namespace android {
28namespace uirenderer {
29
30#define UI_THREAD_FRAME_INFO_SIZE 9
31
32enum class FrameInfoIndex {
33    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
51};
52
53extern std::string FrameInfoNames[];
54
55enum class FrameInfoFlags {
56    kWindowLayoutChanged = 1 << 0,
57    kRTAnimation = 1 << 1,
58    kSurfaceCanvas = 1 << 2,
59    kSkippedFrame = 1 << 3,
60};
61MAKE_FLAGS_ENUM(FrameInfoFlags)
62
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) {
70        set(FrameInfoIndex::kVsync) = vsyncTime;
71        set(FrameInfoIndex::kIntendedVsync) = intendedVsync;
72        return *this;
73    }
74
75    UiFrameInfoBuilder& addFlag(FrameInfoFlags flag) {
76        set(FrameInfoIndex::kFlags) |= static_cast<uint64_t>(flag);
77        return *this;
78    }
79
80private:
81    inline int64_t& set(FrameInfoIndex index) {
82        return mBuffer[static_cast<int>(index)];
83    }
84
85    int64_t* mBuffer;
86};
87
88class FrameInfo {
89public:
90    void importUiThreadInfo(int64_t* info);
91
92    void markSyncStart() {
93        set(FrameInfoIndex::kSyncStart) = systemTime(CLOCK_MONOTONIC);
94    }
95
96    void markIssueDrawCommandsStart() {
97        set(FrameInfoIndex::kIssueDrawCommandsStart) = systemTime(CLOCK_MONOTONIC);
98    }
99
100    void markSwapBuffers() {
101        set(FrameInfoIndex::kSwapBuffers) = systemTime(CLOCK_MONOTONIC);
102    }
103
104    void markFrameCompleted() {
105        set(FrameInfoIndex::kFrameCompleted) = systemTime(CLOCK_MONOTONIC);
106    }
107
108    void addFlag(FrameInfoFlags flag) {
109        set(FrameInfoIndex::kFlags) |= static_cast<uint64_t>(flag);
110    }
111
112    int64_t operator[](FrameInfoIndex index) const {
113        if (index == FrameInfoIndex::kNumIndexes) return 0;
114        return mFrameInfo[static_cast<int>(index)];
115    }
116
117    int64_t operator[](int index) const {
118        if (index < 0 || index >= static_cast<int>(FrameInfoIndex::kNumIndexes)) return 0;
119        return mFrameInfo[index];
120    }
121
122private:
123    inline int64_t& set(FrameInfoIndex index) {
124        return mFrameInfo[static_cast<int>(index)];
125    }
126
127    int64_t mFrameInfo[static_cast<int>(FrameInfoIndex::kNumIndexes)];
128};
129
130} /* namespace uirenderer */
131} /* namespace android */
132
133#endif /* FRAMEINFO_H_ */
134