blob: 0f38e847f4bd610d05978918f75745a5d3d7a0b2 [file] [log] [blame]
Andres Morales910beb82016-02-02 16:19:40 -08001/*
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
17package android.view;
18
19import android.annotation.NonNull;
Mathew Inwooda570dee2018-08-17 14:56:00 +010020import android.annotation.UnsupportedAppUsage;
Andres Morales910beb82016-02-02 16:19:40 -080021import android.os.Looper;
22import android.os.MessageQueue;
23
24import com.android.internal.util.VirtualRefBasePtr;
25
Andres Morales910beb82016-02-02 16:19:40 -080026import java.lang.ref.WeakReference;
Andres Morales910beb82016-02-02 16:19:40 -080027
28/**
29 * Provides streaming access to frame stats information from the rendering
30 * subsystem to apps.
31 *
32 * @hide
33 */
34public class FrameMetricsObserver {
Mathew Inwooda570dee2018-08-17 14:56:00 +010035 @UnsupportedAppUsage
Andres Morales910beb82016-02-02 16:19:40 -080036 private MessageQueue mMessageQueue;
37
38 private WeakReference<Window> mWindow;
39
Mathew Inwooda570dee2018-08-17 14:56:00 +010040 @UnsupportedAppUsage
Andres Morales910beb82016-02-02 16:19:40 -080041 private FrameMetrics mFrameMetrics;
42
John Reck8785ceb2018-10-29 16:45:58 -070043 /* pacage */ Window.OnFrameMetricsAvailableListener mListener;
44 /** @hide */
45 public VirtualRefBasePtr mNative;
Andres Morales910beb82016-02-02 16:19:40 -080046
47 /**
48 * Creates a FrameMetricsObserver
49 *
50 * @param looper the looper to use when invoking callbacks
51 */
52 FrameMetricsObserver(@NonNull Window window, @NonNull Looper looper,
Andres Moralesd908c622016-04-20 13:13:34 -070053 @NonNull Window.OnFrameMetricsAvailableListener listener) {
Andres Morales910beb82016-02-02 16:19:40 -080054 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")
Mathew Inwooda570dee2018-08-17 14:56:00 +010070 @UnsupportedAppUsage
Andres Morales910beb82016-02-02 16:19:40 -080071 private void notifyDataAvailable(int dropCount) {
72 final Window window = mWindow.get();
73 if (window != null) {
Andres Moralesd908c622016-04-20 13:13:34 -070074 mListener.onFrameMetricsAvailable(window, mFrameMetrics, dropCount);
Andres Morales910beb82016-02-02 16:19:40 -080075 }
76 }
77}