1 /* 2 * Copyright (C) 2024 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 android.os; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.IntDef; 21 import android.annotation.IntRange; 22 import android.annotation.NonNull; 23 import android.os.health.SystemHealthManager; 24 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.RetentionPolicy; 27 28 /** 29 * Headroom request params used by {@link SystemHealthManager#getGpuHeadroom(GpuHeadroomParams)}. 30 * 31 * <p>This class is immutable and one should use the {@link Builder} to build a new instance. 32 */ 33 @FlaggedApi(Flags.FLAG_CPU_GPU_HEADROOMS) 34 public final class GpuHeadroomParams { 35 /** @hide */ 36 @IntDef(flag = false, prefix = {"GPU_HEADROOM_CALCULATION_TYPE_"}, value = { 37 GPU_HEADROOM_CALCULATION_TYPE_MIN, // 0 38 GPU_HEADROOM_CALCULATION_TYPE_AVERAGE, // 1 39 }) 40 @Retention(RetentionPolicy.SOURCE) 41 public @interface GpuHeadroomCalculationType { 42 } 43 44 /** 45 * The headroom calculation type bases on minimum value over a specified window. 46 */ 47 public static final int GPU_HEADROOM_CALCULATION_TYPE_MIN = 0; 48 49 /** 50 * The headroom calculation type bases on average value over a specified window. 51 */ 52 public static final int GPU_HEADROOM_CALCULATION_TYPE_AVERAGE = 1; 53 54 /** 55 * The minimum size of the window to compute the headroom over. 56 */ 57 public static final int GPU_HEADROOM_CALCULATION_WINDOW_MILLIS_MIN = 50; 58 59 /** 60 * The maximum size of the window to compute the headroom over. 61 */ 62 public static final int GPU_HEADROOM_CALCULATION_WINDOW_MILLIS_MAX = 10000; 63 64 /** 65 * @hide 66 */ 67 public final GpuHeadroomParamsInternal mInternal; 68 69 /** 70 * @hide 71 */ GpuHeadroomParams()72 private GpuHeadroomParams() { 73 mInternal = new GpuHeadroomParamsInternal(); 74 } 75 76 public static final class Builder { 77 private int mCalculationType = -1; 78 private int mCalculationWindowMillis = -1; 79 Builder()80 public Builder() { 81 } 82 83 /** 84 * Returns a new builder with the same values as this object. 85 */ Builder(@onNull GpuHeadroomParams params)86 public Builder(@NonNull GpuHeadroomParams params) { 87 if (params.mInternal.calculationType >= 0) { 88 mCalculationType = params.mInternal.calculationType; 89 } 90 if (params.mInternal.calculationWindowMillis >= 0) { 91 mCalculationWindowMillis = params.mInternal.calculationWindowMillis; 92 } 93 } 94 95 /** 96 * Sets the headroom calculation type. 97 * <p> 98 * 99 * @throws IllegalArgumentException if the type is invalid. 100 */ 101 @NonNull setCalculationType( @puHeadroomCalculationType int calculationType)102 public Builder setCalculationType( 103 @GpuHeadroomCalculationType int calculationType) { 104 switch (calculationType) { 105 case GPU_HEADROOM_CALCULATION_TYPE_MIN: 106 case GPU_HEADROOM_CALCULATION_TYPE_AVERAGE: { 107 mCalculationType = calculationType; 108 return this; 109 } 110 } 111 throw new IllegalArgumentException("Invalid calculation type: " + calculationType); 112 } 113 114 /** 115 * Sets the headroom calculation window size in milliseconds. 116 * <p> 117 * 118 * @param windowMillis the window size in milliseconds ranges from 119 * {@link SystemHealthManager#getGpuHeadroomCalculationWindowRange()}. 120 * The smaller the window size, the larger fluctuation in the headroom 121 * value should be expected. The default value can be retrieved from 122 * the {@link GpuHeadroomParams#getCalculationWindowMillis}. The device 123 * will try to use the closest feasible window size to this param. 124 * @throws IllegalArgumentException if the window is invalid. 125 */ 126 @NonNull setCalculationWindowMillis(@ntRangefrom = 1) int windowMillis)127 public Builder setCalculationWindowMillis(@IntRange(from = 1) int windowMillis) { 128 if (windowMillis <= 0) { 129 throw new IllegalArgumentException("Invalid calculation window: " + windowMillis); 130 } 131 mCalculationWindowMillis = windowMillis; 132 return this; 133 } 134 135 /** 136 * Builds the {@link GpuHeadroomParams} object. 137 */ 138 @NonNull build()139 public GpuHeadroomParams build() { 140 GpuHeadroomParams params = new GpuHeadroomParams(); 141 if (mCalculationType >= 0) { 142 params.mInternal.calculationType = (byte) mCalculationType; 143 } 144 if (mCalculationWindowMillis >= 0) { 145 params.mInternal.calculationWindowMillis = mCalculationWindowMillis; 146 } 147 return params; 148 } 149 } 150 151 /** 152 * Gets the headroom calculation type. 153 * <p> 154 * This will return the default value chosen by the device if not set. 155 */ getCalculationType()156 public @GpuHeadroomCalculationType int getCalculationType() { 157 @GpuHeadroomCalculationType int validatedType = switch ((int) mInternal.calculationType) { 158 case GPU_HEADROOM_CALCULATION_TYPE_MIN, 159 GPU_HEADROOM_CALCULATION_TYPE_AVERAGE -> mInternal.calculationType; 160 default -> GPU_HEADROOM_CALCULATION_TYPE_MIN; 161 }; 162 return validatedType; 163 } 164 165 /** 166 * Gets the headroom calculation window size in milliseconds. 167 * <p> 168 * This will return the default value chosen by the device if not set. 169 */ getCalculationWindowMillis()170 public int getCalculationWindowMillis() { 171 return mInternal.calculationWindowMillis; 172 } 173 174 @Override toString()175 public String toString() { 176 return "GpuHeadroomParams{" 177 + "calculationType=" + mInternal.calculationType 178 + ", calculationWindowMillis=" + mInternal.calculationWindowMillis 179 + '}'; 180 } 181 182 @Override equals(Object o)183 public boolean equals(Object o) { 184 if (this == o) return true; 185 if (o == null || getClass() != o.getClass()) return false; 186 GpuHeadroomParams that = (GpuHeadroomParams) o; 187 return mInternal.equals(that.mInternal); 188 } 189 190 @Override hashCode()191 public int hashCode() { 192 return mInternal.hashCode(); 193 } 194 } 195