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 android.view;
18 
19 import android.annotation.NonNull;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Looper;
22 import android.os.MessageQueue;
23 
24 import com.android.internal.util.VirtualRefBasePtr;
25 
26 import java.lang.ref.WeakReference;
27 
28 /**
29  * Provides streaming access to frame stats information from the rendering
30  * subsystem to apps.
31  *
32  * @hide
33  */
34 public class FrameMetricsObserver {
35     @UnsupportedAppUsage
36     private MessageQueue mMessageQueue;
37 
38     private WeakReference<Window> mWindow;
39 
40     @UnsupportedAppUsage
41     private FrameMetrics mFrameMetrics;
42 
43     /* pacage */ Window.OnFrameMetricsAvailableListener mListener;
44     /** @hide */
45     public VirtualRefBasePtr mNative;
46 
47     /**
48      * Creates a FrameMetricsObserver
49      *
50      * @param looper the looper to use when invoking callbacks
51      */
FrameMetricsObserver(@onNull Window window, @NonNull Looper looper, @NonNull Window.OnFrameMetricsAvailableListener listener)52     FrameMetricsObserver(@NonNull Window window, @NonNull Looper looper,
53             @NonNull Window.OnFrameMetricsAvailableListener listener) {
54         if (looper == null) {
55             throw new NullPointerException("looper cannot be null");
56         }
57 
58         mMessageQueue = looper.getQueue();
59         if (mMessageQueue == null) {
60             throw new IllegalStateException("invalid looper, null message queue\n");
61         }
62 
63         mFrameMetrics = new FrameMetrics();
64         mWindow = new WeakReference<>(window);
65         mListener = listener;
66     }
67 
68     // Called by native on the provided Handler
69     @SuppressWarnings("unused")
70     @UnsupportedAppUsage
notifyDataAvailable(int dropCount)71     private void notifyDataAvailable(int dropCount) {
72         final Window window = mWindow.get();
73         if (window != null) {
74             mListener.onFrameMetricsAvailable(window, mFrameMetrics, dropCount);
75         }
76     }
77 }
78