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 com.android.server.display.config; 18 19 import android.annotation.Nullable; 20 import android.util.Slog; 21 import android.util.Spline; 22 23 import java.math.BigDecimal; 24 import java.util.List; 25 import java.util.function.Function; 26 27 public class DisplayDeviceConfigUtils { 28 private static final String TAG = "DisplayDeviceConfigUtils"; 29 30 /** 31 * Create Spline from generic data 32 * @param points - points for Spline in format (x0, y0), (x1, y1) etc 33 * @param xExtractor - extract X component from generic data 34 * @param yExtractor - extract Y component from generic data 35 */ 36 @Nullable createSpline(List<T> points, Function<T, BigDecimal> xExtractor, Function<T, BigDecimal> yExtractor)37 public static <T> Spline createSpline(List<T> points, Function<T, BigDecimal> xExtractor, 38 Function<T, BigDecimal> yExtractor) { 39 int size = points.size(); 40 if (size == 0) { 41 return null; 42 } 43 44 float[] x = new float[size]; 45 float[] y = new float[size]; 46 47 int i = 0; 48 for (T point : points) { 49 x[i] = xExtractor.apply(point).floatValue(); 50 if (i > 0) { 51 if (x[i] <= x[i - 1]) { 52 Slog.e(TAG, "spline control points must be strictly increasing, ignoring " 53 + "configuration. x: " + x[i] + " <= " + x[i - 1]); 54 return null; 55 } 56 } 57 y[i] = yExtractor.apply(point).floatValue(); 58 ++i; 59 } 60 61 return Spline.createSpline(x, y); 62 } 63 64 /** 65 * Get the highest HDR/SDR ratio from the given map. 66 * @param points The map of brightness values to HDR/SDR ratios 67 * @param extractor Used to retrieve the ratio from the map element 68 * @return The highest HDR/SDR ratio 69 * @param <T> The type of the map elements 70 */ getHighestHdrSdrRatio(List<T> points, Function<T, BigDecimal> extractor)71 public static <T> float getHighestHdrSdrRatio(List<T> points, 72 Function<T, BigDecimal> extractor) { 73 float highestRatio = 1; 74 for (T point : points) { 75 float ratio = extractor.apply(point).floatValue(); 76 if (ratio > highestRatio) { 77 highestRatio = ratio; 78 } 79 } 80 return highestRatio; 81 } 82 } 83