1 /* 2 * Copyright (C) 2020 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.os; 18 19 import static com.android.internal.os.PowerProfile.POWER_GROUP_DISPLAY_AMBIENT; 20 21 import android.os.BatteryConsumer; 22 import android.os.BatteryStats; 23 import android.os.BatteryUsageStats; 24 import android.os.BatteryUsageStatsQuery; 25 26 /** 27 * Estimates power consumed by the ambient display 28 */ 29 public class AmbientDisplayPowerCalculator extends PowerCalculator { 30 private final UsageBasedPowerEstimator[] mPowerEstimators; 31 32 @Override isPowerComponentSupported(@atteryConsumer.PowerComponent int powerComponent)33 public boolean isPowerComponentSupported(@BatteryConsumer.PowerComponent int powerComponent) { 34 return powerComponent == BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY; 35 } 36 AmbientDisplayPowerCalculator(PowerProfile powerProfile)37 public AmbientDisplayPowerCalculator(PowerProfile powerProfile) { 38 final int numDisplays = powerProfile.getNumDisplays(); 39 mPowerEstimators = new UsageBasedPowerEstimator[numDisplays]; 40 for (int display = 0; display < numDisplays; display++) { 41 mPowerEstimators[display] = new UsageBasedPowerEstimator( 42 powerProfile.getAveragePowerForOrdinal(POWER_GROUP_DISPLAY_AMBIENT, display)); 43 } 44 } 45 46 /** 47 * Ambient display power is the additional power the screen takes while in ambient display/ 48 * screen doze/always-on display (interchangeable terms) mode. 49 */ 50 @Override calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)51 public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, 52 long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) { 53 final long measuredEnergyUC = batteryStats.getScreenDozeMeasuredBatteryConsumptionUC(); 54 final int powerModel = getPowerModel(measuredEnergyUC, query); 55 final long durationMs = calculateDuration(batteryStats, rawRealtimeUs, 56 BatteryStats.STATS_SINCE_CHARGED); 57 final double powerMah = calculateTotalPower(powerModel, batteryStats, rawRealtimeUs, 58 measuredEnergyUC); 59 builder.getAggregateBatteryConsumerBuilder( 60 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE) 61 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY, durationMs) 62 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY, 63 powerMah, powerModel); 64 } 65 calculateDuration(BatteryStats batteryStats, long rawRealtimeUs, int statsType)66 private long calculateDuration(BatteryStats batteryStats, long rawRealtimeUs, int statsType) { 67 return batteryStats.getScreenDozeTime(rawRealtimeUs, statsType) / 1000; 68 } 69 calculateTotalPower(@atteryConsumer.PowerModel int powerModel, BatteryStats batteryStats, long rawRealtimeUs, long consumptionUC)70 private double calculateTotalPower(@BatteryConsumer.PowerModel int powerModel, 71 BatteryStats batteryStats, long rawRealtimeUs, long consumptionUC) { 72 switch (powerModel) { 73 case BatteryConsumer.POWER_MODEL_MEASURED_ENERGY: 74 return uCtoMah(consumptionUC); 75 case BatteryConsumer.POWER_MODEL_POWER_PROFILE: 76 default: 77 return calculateEstimatedPower(batteryStats, rawRealtimeUs); 78 } 79 } 80 calculateEstimatedPower(BatteryStats batteryStats, long rawRealtimeUs)81 private double calculateEstimatedPower(BatteryStats batteryStats, long rawRealtimeUs) { 82 final int numDisplays = mPowerEstimators.length; 83 double power = 0; 84 for (int display = 0; display < numDisplays; display++) { 85 final long dozeTime = batteryStats.getDisplayScreenDozeTime(display, rawRealtimeUs) 86 / 1000; 87 power += mPowerEstimators[display].calculatePower(dozeTime); 88 } 89 return power; 90 } 91 } 92