1 /*
2  * Copyright (C) 2019 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.color;
18 
19 import static com.android.server.display.color.DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION;
20 
21 import android.content.Context;
22 import android.hardware.display.ColorDisplayManager;
23 import android.opengl.Matrix;
24 import android.util.Slog;
25 
26 import java.util.Arrays;
27 
28 /** Control the color transform for global device saturation. */
29 final class GlobalSaturationTintController extends TintController {
30 
31     private final float[] mMatrixGlobalSaturation = new float[16];
32 
33     @Override
setUp(Context context, boolean needsLinear)34     public void setUp(Context context, boolean needsLinear) {
35     }
36 
37     @Override
getMatrix()38     public float[] getMatrix() {
39         return Arrays.copyOf(mMatrixGlobalSaturation, mMatrixGlobalSaturation.length);
40     }
41 
42     @Override
setMatrix(int saturationLevel)43     public void setMatrix(int saturationLevel) {
44         if (saturationLevel < 0) {
45             saturationLevel = 0;
46         } else if (saturationLevel > 100) {
47             saturationLevel = 100;
48         }
49         Slog.d(ColorDisplayService.TAG, "Setting saturation level: " + saturationLevel);
50 
51         if (saturationLevel == 100) {
52             setActivated(false);
53             Matrix.setIdentityM(mMatrixGlobalSaturation, 0);
54         } else {
55             setActivated(true);
56             float saturation = saturationLevel * 0.01f;
57             float desaturation = 1.0f - saturation;
58             float[] luminance = {0.231f * desaturation, 0.715f * desaturation,
59                     0.072f * desaturation};
60             mMatrixGlobalSaturation[0] = luminance[0] + saturation;
61             mMatrixGlobalSaturation[1] = luminance[0];
62             mMatrixGlobalSaturation[2] = luminance[0];
63             mMatrixGlobalSaturation[4] = luminance[1];
64             mMatrixGlobalSaturation[5] = luminance[1] + saturation;
65             mMatrixGlobalSaturation[6] = luminance[1];
66             mMatrixGlobalSaturation[8] = luminance[2];
67             mMatrixGlobalSaturation[9] = luminance[2];
68             mMatrixGlobalSaturation[10] = luminance[2] + saturation;
69         }
70     }
71 
72     @Override
getLevel()73     public int getLevel() {
74         return LEVEL_COLOR_MATRIX_SATURATION;
75     }
76 
77     @Override
isAvailable(Context context)78     public boolean isAvailable(Context context) {
79         return ColorDisplayManager.isColorTransformAccelerated(context);
80     }
81 }
82