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 
17 package com.android.internal.display;
18 
19 import static android.hardware.display.DisplayManager.DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED;
20 
21 import android.content.Context;
22 import android.hardware.display.DisplayManager;
23 import android.util.Log;
24 import android.view.Display;
25 
26 /**
27  * Constants and utility methods for refresh rate settings.
28  */
29 public class RefreshRateSettingsUtils {
30 
31     private static final String TAG = "RefreshRateSettingsUtils";
32 
33     public static final float DEFAULT_REFRESH_RATE = 60f;
34 
35     /**
36      * Find the highest refresh rate among all the modes of the default display.
37      *
38      * This method will acquire DisplayManager.mLock, so calling it while holding other locks
39      * should be done with care.
40      * @param context The context
41      * @return The highest refresh rate
42      */
findHighestRefreshRateForDefaultDisplay(Context context)43     public static float findHighestRefreshRateForDefaultDisplay(Context context) {
44         final DisplayManager dm = context.getSystemService(DisplayManager.class);
45         final Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
46         if (display == null) {
47             Log.w(TAG, "No valid default display device");
48             return DEFAULT_REFRESH_RATE;
49         }
50 
51         float maxRefreshRate = DEFAULT_REFRESH_RATE;
52         for (Display.Mode mode : display.getSupportedModes()) {
53             if (mode.getRefreshRate() > maxRefreshRate) {
54                 maxRefreshRate = mode.getRefreshRate();
55             }
56         }
57         return maxRefreshRate;
58     }
59 
60     /**
61      * Find the highest refresh rate among all the modes of all the displays.
62      *
63      * This method will acquire DisplayManager.mLock, so calling it while holding other locks
64      * should be done with care.
65      * @param context The context
66      * @return The highest refresh rate
67      */
findHighestRefreshRateAmongAllDisplays(Context context)68     public static float findHighestRefreshRateAmongAllDisplays(Context context) {
69         final DisplayManager dm = context.getSystemService(DisplayManager.class);
70         final Display[] displays = dm.getDisplays(DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED);
71         if (displays.length == 0) {
72             Log.w(TAG, "No valid display devices");
73             return DEFAULT_REFRESH_RATE;
74         }
75 
76         float maxRefreshRate = DEFAULT_REFRESH_RATE;
77         for (Display display : displays) {
78             for (Display.Mode mode : display.getSupportedModes()) {
79                 if (mode.getRefreshRate() > maxRefreshRate) {
80                     maxRefreshRate = mode.getRefreshRate();
81                 }
82             }
83         }
84         return maxRefreshRate;
85     }
86 }
87