1 /*
2  * Copyright 2021 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.view;
18 
19 import android.annotation.RequiresPermission;
20 import android.os.IBinder;
21 import android.util.ArrayMap;
22 
23 import libcore.util.NativeAllocationRegistry;
24 
25 import java.util.Objects;
26 
27 /**
28  * Allows for the monitoring of layers with HDR content
29  *
30  * @hide */
31 public abstract class SurfaceControlHdrLayerInfoListener {
32     private static final NativeAllocationRegistry sRegistry =
33             NativeAllocationRegistry.createMalloced(
34                     SurfaceControlHdrLayerInfoListener.class.getClassLoader(), nGetDestructor());
35 
36     /**
37      * Callback when the HDR information about the given display has changed
38      *
39      * @param displayToken The display this callback is about
40      * @param numberOfHdrLayers How many HDR layers are visible on the display
41      * @param maxW The width of the HDR layer with the largest area
42      * @param maxH The height of the HDR layer with the largest area
43      * @param flags Additional metadata flags, currently always 0
44      *              TODO(b/182312559): Add some flags
45      * @param maxDesiredHdrSdrRatio The max desired HDR/SDR ratio. Unbounded if the ratio is
46      *                              positive infinity.
47      *
48      * @hide
49      */
onHdrInfoChanged(IBinder displayToken, int numberOfHdrLayers, int maxW, int maxH, int flags, float maxDesiredHdrSdrRatio)50     public abstract void onHdrInfoChanged(IBinder displayToken, int numberOfHdrLayers,
51             int maxW, int maxH, int flags, float maxDesiredHdrSdrRatio);
52 
53     /**
54      * Registers this as an HDR info listener on the provided display
55      * @param displayToken
56      */
57     @RequiresPermission(android.Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
register(IBinder displayToken)58     public void register(IBinder displayToken) {
59         Objects.requireNonNull(displayToken);
60         synchronized (this) {
61             if (mRegisteredListeners.containsKey(displayToken)) {
62                 return;
63             }
64             long nativePtr = nRegister(displayToken);
65             Runnable destructor = sRegistry.registerNativeAllocation(this, nativePtr);
66             mRegisteredListeners.put(displayToken, destructor);
67         }
68     }
69 
70     /**
71      * Unregisters this as an HDR info listener on the provided display
72      * @param displayToken
73      */
74     @RequiresPermission(android.Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
unregister(IBinder displayToken)75     public void unregister(IBinder displayToken) {
76         Objects.requireNonNull(displayToken);
77         final Runnable destructor;
78         synchronized (this) {
79             destructor = mRegisteredListeners.remove(displayToken);
80         }
81         if (destructor != null) {
82             destructor.run();
83         }
84     }
85 
86     /**
87      * Unregisters this on all previously registered displays
88      */
89     @RequiresPermission(android.Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
unregisterAll()90     public void unregisterAll() {
91         final ArrayMap<IBinder, Runnable> toDestroy;
92         synchronized (this) {
93             toDestroy = mRegisteredListeners;
94             mRegisteredListeners = new ArrayMap<>();
95         }
96         for (Runnable destructor : toDestroy.values()) {
97             destructor.run();
98         }
99     }
100 
101     private ArrayMap<IBinder, Runnable> mRegisteredListeners = new ArrayMap<>();
102 
nGetDestructor()103     private static native long nGetDestructor();
nRegister(IBinder displayToken)104     private native long nRegister(IBinder displayToken);
105 }
106