1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 #include "Properties.h"
20 #include "utils/Macros.h"
21 
22 #include <utils/Timers.h>
23 
24 #include <array>
25 #include <functional>
26 #include <tuple>
27 
28 namespace android {
29 namespace uirenderer {
30 
31 enum JankType {
32     kMissedVsync = 0,
33     kHighInputLatency,
34     kSlowUI,
35     kSlowSync,
36     kSlowRT,
37     kMissedDeadline,
38 
39     // must be last
40     NUM_BUCKETS,
41 };
42 
43 // For testing
44 class MockProfileData;
45 
46 // Try to keep as small as possible, should match ASHMEM_SIZE in
47 // GraphicsStatsService.java
48 class ProfileData {
49     PREVENT_COPY_AND_ASSIGN(ProfileData);
50 
51 public:
ProfileData()52     ProfileData() { reset(); }
53 
54     void reset();
55     void mergeWith(const ProfileData& other);
56     void dump(int fd) const;
57     uint32_t findPercentile(int percentile) const;
58     uint32_t findGPUPercentile(int percentile) const;
59 
60     void reportFrame(int64_t duration);
61     void reportGPUFrame(int64_t duration);
reportJank()62     void reportJank() { mJankFrameCount++; }
reportJankType(JankType type)63     void reportJankType(JankType type) { mJankTypeCounts[static_cast<int>(type)]++; }
64 
totalFrameCount()65     uint32_t totalFrameCount() const { return mTotalFrameCount; }
jankFrameCount()66     uint32_t jankFrameCount() const { return mJankFrameCount; }
statsStartTime()67     nsecs_t statsStartTime() const { return mStatStartTime; }
jankTypeCount(JankType type)68     uint32_t jankTypeCount(JankType type) const { return mJankTypeCounts[static_cast<int>(type)]; }
pipelineType()69     RenderPipelineType pipelineType() const { return mPipelineType; }
70 
71     struct HistogramEntry {
72         uint32_t renderTimeMs;
73         uint32_t frameCount;
74     };
75     void histogramForEach(const std::function<void(HistogramEntry)>& callback) const;
76     void histogramGPUForEach(const std::function<void(HistogramEntry)>& callback) const;
77 
HistogramSize()78     constexpr static int HistogramSize() {
79         return std::tuple_size<decltype(ProfileData::mFrameCounts)>::value +
80                std::tuple_size<decltype(ProfileData::mSlowFrameCounts)>::value;
81     }
82 
GPUHistogramSize()83     constexpr static int GPUHistogramSize() {
84         return std::tuple_size<decltype(ProfileData::mGPUFrameCounts)>::value;
85     }
86 
87     // Visible for testing
88     static uint32_t frameTimeForFrameCountIndex(uint32_t index);
89     static uint32_t frameTimeForSlowFrameCountIndex(uint32_t index);
90     static uint32_t GPUFrameTimeForFrameCountIndex(uint32_t index);
91 
92 private:
93     // Open our guts up to unit tests
94     friend class MockProfileData;
95 
96     std::array<uint32_t, NUM_BUCKETS> mJankTypeCounts;
97     // See comments on kBucket* constants for what this holds
98     std::array<uint32_t, 57> mFrameCounts;
99     // Holds a histogram of frame times in 50ms increments from 150ms to 5s
100     std::array<uint16_t, 97> mSlowFrameCounts;
101     // Holds a histogram of GPU draw times in 1ms increments. Frames longer than 25ms are placed in
102     // last bucket.
103     std::array<uint32_t, 26> mGPUFrameCounts;
104 
105     uint32_t mTotalFrameCount;
106     uint32_t mJankFrameCount;
107     nsecs_t mStatStartTime;
108 
109     // true if HWUI renders with Vulkan pipeline
110     RenderPipelineType mPipelineType;
111 };
112 
113 // For testing
114 class MockProfileData : public ProfileData {
115 public:
editJankTypeCounts()116     std::array<uint32_t, NUM_BUCKETS>& editJankTypeCounts() { return mJankTypeCounts; }
editFrameCounts()117     std::array<uint32_t, 57>& editFrameCounts() { return mFrameCounts; }
editSlowFrameCounts()118     std::array<uint16_t, 97>& editSlowFrameCounts() { return mSlowFrameCounts; }
editTotalFrameCount()119     uint32_t& editTotalFrameCount() { return mTotalFrameCount; }
editJankFrameCount()120     uint32_t& editJankFrameCount() { return mJankFrameCount; }
editStatStartTime()121     nsecs_t& editStatStartTime() { return mStatStartTime; }
122 };
123 
124 } /* namespace uirenderer */
125 } /* namespace android */
126