1 /*
2  * Copyright (C) 2023 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 package com.android.internal.jank;
17 
18 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__UNKNOWN_REFRESH_RATE;
19 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__VARIABLE_REFRESH_RATE;
20 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_30_HZ;
21 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_60_HZ;
22 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_90_HZ;
23 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_120_HZ;
24 import static com.android.internal.util.FrameworkStatsLog.UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_240_HZ;
25 
26 import android.annotation.IntDef;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 
31 /**
32  * Display refresh rate related functionality.
33  * @hide
34  */
35 public class DisplayRefreshRate {
36     public static final int UNKNOWN_REFRESH_RATE =
37             UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__UNKNOWN_REFRESH_RATE;
38     public static final int VARIABLE_REFRESH_RATE =
39             UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__VARIABLE_REFRESH_RATE;
40     public static final int REFRESH_RATE_30_HZ =
41             UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_30_HZ;
42     public static final int REFRESH_RATE_60_HZ =
43             UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_60_HZ;
44     public static final int REFRESH_RATE_90_HZ =
45             UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_90_HZ;
46     public static final int REFRESH_RATE_120_HZ =
47             UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_120_HZ;
48     public static final int REFRESH_RATE_240_HZ =
49             UIINTERACTION_FRAME_INFO_REPORTED__DISPLAY_REFRESH_RATE__RR_240_HZ;
50 
51     /** @hide */
52     @IntDef({
53         UNKNOWN_REFRESH_RATE,
54         VARIABLE_REFRESH_RATE,
55         REFRESH_RATE_30_HZ,
56         REFRESH_RATE_60_HZ,
57         REFRESH_RATE_90_HZ,
58         REFRESH_RATE_120_HZ,
59         REFRESH_RATE_240_HZ,
60     })
61     @Retention(RetentionPolicy.SOURCE)
62     public @interface RefreshRate {
63     }
64 
DisplayRefreshRate()65     private DisplayRefreshRate() {
66     }
67 
68     /**
69      * Computes the display refresh rate based off the frame interval.
70      */
71     @RefreshRate
getRefreshRate(long frameIntervalNs)72     public static int getRefreshRate(long frameIntervalNs) {
73         long rate = Math.round(1e9 / frameIntervalNs);
74         if (rate < 50) {
75             return REFRESH_RATE_30_HZ;
76         } else if (rate < 80) {
77             return REFRESH_RATE_60_HZ;
78         } else if (rate < 110) {
79             return REFRESH_RATE_90_HZ;
80         } else if (rate < 180) {
81             return REFRESH_RATE_120_HZ;
82         } else {
83             return REFRESH_RATE_240_HZ;
84         }
85     }
86 }
87