Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 18 | |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 20 | import android.opengl.Matrix; |
| 21 | import android.os.IBinder; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 25 | import android.os.SystemProperties; |
| 26 | import android.util.Log; |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 27 | import android.util.Slog; |
| 28 | import android.util.SparseArray; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 29 | import com.android.internal.annotations.GuardedBy; |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 30 | import com.android.internal.app.ColorDisplayController; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 31 | import java.util.Arrays; |
| 32 | |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Manager for applying color transformations to the display. |
| 35 | */ |
| 36 | public class DisplayTransformManager { |
| 37 | |
| 38 | private static final String TAG = "DisplayTransformManager"; |
| 39 | |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 40 | private static final String SURFACE_FLINGER = "SurfaceFlinger"; |
| 41 | |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Color transform level used by Night display to tint the display red. |
| 44 | */ |
| 45 | public static final int LEVEL_COLOR_MATRIX_NIGHT_DISPLAY = 100; |
| 46 | /** |
Bryan Mawhinney | 462e29d | 2018-03-22 15:52:41 +0000 | [diff] [blame] | 47 | * Color transform level used to adjust the color saturation of the display. |
| 48 | */ |
| 49 | public static final int LEVEL_COLOR_MATRIX_SATURATION = 150; |
| 50 | /** |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 51 | * Color transform level used by A11y services to make the display monochromatic. |
| 52 | */ |
| 53 | public static final int LEVEL_COLOR_MATRIX_GRAYSCALE = 200; |
| 54 | /** |
| 55 | * Color transform level used by A11y services to invert the display colors. |
| 56 | */ |
| 57 | public static final int LEVEL_COLOR_MATRIX_INVERT_COLOR = 300; |
| 58 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 59 | private static final int SURFACE_FLINGER_TRANSACTION_COLOR_MATRIX = 1015; |
| 60 | private static final int SURFACE_FLINGER_TRANSACTION_DALTONIZER = 1014; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 61 | |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 62 | private static final String PERSISTENT_PROPERTY_SATURATION = "persist.sys.sf.color_saturation"; |
| 63 | private static final String PERSISTENT_PROPERTY_NATIVE_MODE = "persist.sys.sf.native_mode"; |
| 64 | |
| 65 | private static final int SURFACE_FLINGER_TRANSACTION_SATURATION = 1022; |
| 66 | private static final int SURFACE_FLINGER_TRANSACTION_NATIVE_MODE = 1023; |
| 67 | |
| 68 | private static final float COLOR_SATURATION_NATURAL = 1.0f; |
| 69 | private static final float COLOR_SATURATION_BOOSTED = 1.1f; |
| 70 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Map of level -> color transformation matrix. |
| 73 | */ |
| 74 | @GuardedBy("mColorMatrix") |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 75 | private final SparseArray<float[]> mColorMatrix = new SparseArray<>(3); |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Temporary matrix used internally by {@link #computeColorMatrixLocked()}. |
| 78 | */ |
| 79 | @GuardedBy("mColorMatrix") |
| 80 | private final float[][] mTempColorMatrix = new float[2][16]; |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 81 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Lock used for synchronize access to {@link #mDaltonizerMode}. |
| 84 | */ |
| 85 | private final Object mDaltonizerModeLock = new Object(); |
| 86 | @GuardedBy("mDaltonizerModeLock") |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 87 | private int mDaltonizerMode = -1; |
| 88 | |
| 89 | /* package */ DisplayTransformManager() { |
| 90 | } |
| 91 | |
| 92 | /** |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 93 | * Returns a copy of the color transform matrix set for a given level. |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 94 | */ |
| 95 | public float[] getColorMatrix(int key) { |
| 96 | synchronized (mColorMatrix) { |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 97 | final float[] value = mColorMatrix.get(key); |
| 98 | return value == null ? null : Arrays.copyOf(value, value.length); |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sets and applies a current color transform matrix for a given level. |
| 104 | * <p> |
| 105 | * Note: all color transforms are first composed to a single matrix in ascending order based |
| 106 | * on level before being applied to the display. |
| 107 | * |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 108 | * @param level the level used to identify and compose the color transform (low -> high) |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 109 | * @param value the 4x4 color transform matrix (in column-major order), or {@code null} to |
| 110 | * remove the color transform matrix associated with the provided level |
| 111 | */ |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 112 | public void setColorMatrix(int level, float[] value) { |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 113 | if (value != null && value.length != 16) { |
| 114 | throw new IllegalArgumentException("Expected length: 16 (4x4 matrix)" |
| 115 | + ", actual length: " + value.length); |
| 116 | } |
| 117 | |
| 118 | synchronized (mColorMatrix) { |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 119 | final float[] oldValue = mColorMatrix.get(level); |
| 120 | if (!Arrays.equals(oldValue, value)) { |
| 121 | if (value == null) { |
| 122 | mColorMatrix.remove(level); |
| 123 | } else if (oldValue == null) { |
| 124 | mColorMatrix.put(level, Arrays.copyOf(value, value.length)); |
| 125 | } else { |
| 126 | System.arraycopy(value, 0, oldValue, 0, value.length); |
| 127 | } |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 128 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 129 | // Update the current color transform. |
| 130 | applyColorMatrix(computeColorMatrixLocked()); |
| 131 | } |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the composition of all current color matrices, or {@code null} if there are none. |
| 137 | */ |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 138 | @GuardedBy("mColorMatrix") |
| 139 | private float[] computeColorMatrixLocked() { |
| 140 | final int count = mColorMatrix.size(); |
| 141 | if (count == 0) { |
| 142 | return null; |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 143 | } |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 144 | |
| 145 | final float[][] result = mTempColorMatrix; |
| 146 | Matrix.setIdentityM(result[0], 0); |
| 147 | for (int i = 0; i < count; i++) { |
| 148 | float[] rhs = mColorMatrix.valueAt(i); |
| 149 | Matrix.multiplyMM(result[(i + 1) % 2], 0, result[i % 2], 0, rhs, 0); |
| 150 | } |
| 151 | return result[count % 2]; |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns the current Daltonization mode. |
| 156 | */ |
| 157 | public int getDaltonizerMode() { |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 158 | synchronized (mDaltonizerModeLock) { |
| 159 | return mDaltonizerMode; |
| 160 | } |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets the current Daltonization mode. This adjusts the color space to correct for or simulate |
| 165 | * various types of color blindness. |
| 166 | * |
| 167 | * @param mode the new Daltonization mode, or -1 to disable |
| 168 | */ |
| 169 | public void setDaltonizerMode(int mode) { |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 170 | synchronized (mDaltonizerModeLock) { |
| 171 | if (mDaltonizerMode != mode) { |
| 172 | mDaltonizerMode = mode; |
| 173 | applyDaltonizerMode(mode); |
| 174 | } |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Propagates the provided color transformation matrix to the SurfaceFlinger. |
| 180 | */ |
| 181 | private static void applyColorMatrix(float[] m) { |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 182 | final IBinder flinger = ServiceManager.getService(SURFACE_FLINGER); |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 183 | if (flinger != null) { |
| 184 | final Parcel data = Parcel.obtain(); |
| 185 | data.writeInterfaceToken("android.ui.ISurfaceComposer"); |
| 186 | if (m != null) { |
| 187 | data.writeInt(1); |
| 188 | for (int i = 0; i < 16; i++) { |
| 189 | data.writeFloat(m[i]); |
| 190 | } |
| 191 | } else { |
| 192 | data.writeInt(0); |
| 193 | } |
| 194 | try { |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 195 | flinger.transact(SURFACE_FLINGER_TRANSACTION_COLOR_MATRIX, data, null, 0); |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 196 | } catch (RemoteException ex) { |
| 197 | Slog.e(TAG, "Failed to set color transform", ex); |
| 198 | } finally { |
| 199 | data.recycle(); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Propagates the provided Daltonization mode to the SurfaceFlinger. |
| 206 | */ |
| 207 | private static void applyDaltonizerMode(int mode) { |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 208 | final IBinder flinger = ServiceManager.getService(SURFACE_FLINGER); |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 209 | if (flinger != null) { |
| 210 | final Parcel data = Parcel.obtain(); |
| 211 | data.writeInterfaceToken("android.ui.ISurfaceComposer"); |
| 212 | data.writeInt(mode); |
| 213 | try { |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 214 | flinger.transact(SURFACE_FLINGER_TRANSACTION_DALTONIZER, data, null, 0); |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 215 | } catch (RemoteException ex) { |
| 216 | Slog.e(TAG, "Failed to set Daltonizer mode", ex); |
| 217 | } finally { |
| 218 | data.recycle(); |
| 219 | } |
| 220 | } |
| 221 | } |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 222 | |
| 223 | public static boolean isNativeModeEnabled() { |
| 224 | return SystemProperties.getBoolean(PERSISTENT_PROPERTY_NATIVE_MODE, false); |
| 225 | } |
| 226 | |
Christine Franks | 218e656 | 2017-11-27 10:20:14 -0800 | [diff] [blame] | 227 | public boolean setColorMode(int colorMode, float[] nightDisplayMatrix) { |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 228 | if (colorMode == ColorDisplayController.COLOR_MODE_NATURAL) { |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 229 | applySaturation(COLOR_SATURATION_NATURAL); |
| 230 | setNativeMode(false); |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 231 | } else if (colorMode == ColorDisplayController.COLOR_MODE_BOOSTED) { |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 232 | applySaturation(COLOR_SATURATION_BOOSTED); |
| 233 | setNativeMode(false); |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 234 | } else if (colorMode == ColorDisplayController.COLOR_MODE_SATURATED) { |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 235 | applySaturation(COLOR_SATURATION_NATURAL); |
| 236 | setNativeMode(true); |
| 237 | } |
Christine Franks | 218e656 | 2017-11-27 10:20:14 -0800 | [diff] [blame] | 238 | setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, nightDisplayMatrix); |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 239 | |
| 240 | updateConfiguration(); |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Propagates the provided saturation to the SurfaceFlinger. |
| 247 | */ |
| 248 | private void applySaturation(float saturation) { |
| 249 | SystemProperties.set(PERSISTENT_PROPERTY_SATURATION, Float.toString(saturation)); |
| 250 | final IBinder flinger = ServiceManager.getService(SURFACE_FLINGER); |
| 251 | if (flinger != null) { |
| 252 | final Parcel data = Parcel.obtain(); |
| 253 | data.writeInterfaceToken("android.ui.ISurfaceComposer"); |
| 254 | data.writeFloat(saturation); |
| 255 | try { |
| 256 | flinger.transact(SURFACE_FLINGER_TRANSACTION_SATURATION, data, null, 0); |
| 257 | } catch (RemoteException ex) { |
| 258 | Log.e(TAG, "Failed to set saturation", ex); |
| 259 | } finally { |
| 260 | data.recycle(); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Toggles native mode on/off in SurfaceFlinger. |
| 267 | */ |
| 268 | private void setNativeMode(boolean enabled) { |
| 269 | SystemProperties.set(PERSISTENT_PROPERTY_NATIVE_MODE, enabled ? "1" : "0"); |
| 270 | final IBinder flinger = ServiceManager.getService(SURFACE_FLINGER); |
| 271 | if (flinger != null) { |
| 272 | final Parcel data = Parcel.obtain(); |
| 273 | data.writeInterfaceToken("android.ui.ISurfaceComposer"); |
| 274 | data.writeInt(enabled ? 1 : 0); |
| 275 | try { |
| 276 | flinger.transact(SURFACE_FLINGER_TRANSACTION_NATIVE_MODE, data, null, 0); |
| 277 | } catch (RemoteException ex) { |
| 278 | Log.e(TAG, "Failed to set native mode", ex); |
| 279 | } finally { |
| 280 | data.recycle(); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | private void updateConfiguration() { |
| 286 | try { |
| 287 | ActivityManager.getService().updateConfiguration(null); |
| 288 | } catch (RemoteException e) { |
| 289 | Log.e(TAG, "Could not update configuration", e); |
| 290 | } |
| 291 | } |
Justin Klaassen | 22eb199 | 2016-07-11 20:52:23 -0700 | [diff] [blame] | 292 | } |