• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      *
46      * @hide */
onHdrInfoChanged(IBinder displayToken, int numberOfHdrLayers, int maxW, int maxH, int flags)47     public abstract void onHdrInfoChanged(IBinder displayToken, int numberOfHdrLayers,
48             int maxW, int maxH, int flags);
49 
50     /**
51      * Registers this as an HDR info listener on the provided display
52      * @param displayToken
53      */
54     @RequiresPermission(android.Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
register(IBinder displayToken)55     public void register(IBinder displayToken) {
56         Objects.requireNonNull(displayToken);
57         synchronized (this) {
58             if (mRegisteredListeners.containsKey(displayToken)) {
59                 return;
60             }
61             long nativePtr = nRegister(displayToken);
62             Runnable destructor = sRegistry.registerNativeAllocation(this, nativePtr);
63             mRegisteredListeners.put(displayToken, destructor);
64         }
65     }
66 
67     /**
68      * Unregisters this as an HDR info listener on the provided display
69      * @param displayToken
70      */
71     @RequiresPermission(android.Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
unregister(IBinder displayToken)72     public void unregister(IBinder displayToken) {
73         Objects.requireNonNull(displayToken);
74         final Runnable destructor;
75         synchronized (this) {
76             destructor = mRegisteredListeners.remove(displayToken);
77         }
78         if (destructor != null) {
79             destructor.run();
80         }
81     }
82 
83     /**
84      * Unregisters this on all previously registered displays
85      */
86     @RequiresPermission(android.Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
unregisterAll()87     public void unregisterAll() {
88         final ArrayMap<IBinder, Runnable> toDestroy;
89         synchronized (this) {
90             toDestroy = mRegisteredListeners;
91             mRegisteredListeners = new ArrayMap<>();
92         }
93         for (Runnable destructor : toDestroy.values()) {
94             destructor.run();
95         }
96     }
97 
98     private ArrayMap<IBinder, Runnable> mRegisteredListeners = new ArrayMap<>();
99 
nGetDestructor()100     private static native long nGetDestructor();
nRegister(IBinder displayToken)101     private native long nRegister(IBinder displayToken);
102 }
103