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.NonNull;
20 
21 /**
22  * Listener for sampling the frames per second for a SurfaceControl and its children.
23  * This should only be used by a system component that needs to listen to a SurfaceControl's
24  * tree's FPS when it is not actively submitting transactions for that SurfaceControl.
25  * Otherwise, ASurfaceTransaction_OnComplete callbacks should be used.
26  *
27  * @hide
28  */
29 public abstract class SurfaceControlFpsListener {
30     private long mNativeListener;
31 
SurfaceControlFpsListener()32     public SurfaceControlFpsListener() {
33         mNativeListener = nativeCreate(this);
34     }
35 
destroy()36     protected void destroy() {
37         if (mNativeListener == 0) {
38             return;
39         }
40         unregister();
41         nativeDestroy(mNativeListener);
42         mNativeListener = 0;
43     }
44 
45     @Override
finalize()46     protected void finalize() throws Throwable {
47         try {
48             destroy();
49         } finally {
50             super.finalize();
51         }
52     }
53 
54     /**
55      * Reports the fps from the registered SurfaceControl
56      */
onFpsReported(float fps)57     public abstract void onFpsReported(float fps);
58 
59     /**
60      * Registers the sampling listener for a particular task ID
61      */
register(int taskId)62     public void register(int taskId) {
63         if (mNativeListener == 0) {
64             return;
65         }
66 
67         nativeRegister(mNativeListener, taskId);
68     }
69 
70     /**
71      * Unregisters the sampling listener.
72      */
unregister()73     public void unregister() {
74         if (mNativeListener == 0) {
75             return;
76         }
77         nativeUnregister(mNativeListener);
78     }
79 
80     /**
81      * Dispatch the collected sample.
82      *
83      * Called from native code on a binder thread.
84      */
dispatchOnFpsReported( @onNull SurfaceControlFpsListener listener, float fps)85     private static void dispatchOnFpsReported(
86             @NonNull SurfaceControlFpsListener listener, float fps) {
87         listener.onFpsReported(fps);
88     }
89 
nativeCreate(SurfaceControlFpsListener thiz)90     private static native long nativeCreate(SurfaceControlFpsListener thiz);
nativeDestroy(long ptr)91     private static native void nativeDestroy(long ptr);
nativeRegister(long ptr, int taskId)92     private static native void nativeRegister(long ptr, int taskId);
nativeUnregister(long ptr)93     private static native void nativeUnregister(long ptr);
94 }
95