blob: 9e81de0e09bb9bd5310db054dc4ede47fe18e954 [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;
Andres Morales910beb82016-02-02 16:19:40 -080020import android.os.Looper;
21import android.os.MessageQueue;
22
23import com.android.internal.util.VirtualRefBasePtr;
24
Andres Morales910beb82016-02-02 16:19:40 -080025import java.lang.ref.WeakReference;
Andres Morales910beb82016-02-02 16:19:40 -080026
27/**
28 * Provides streaming access to frame stats information from the rendering
29 * subsystem to apps.
30 *
31 * @hide
32 */
33public class FrameMetricsObserver {
34 private MessageQueue mMessageQueue;
35
36 private WeakReference<Window> mWindow;
37
38 private FrameMetrics mFrameMetrics;
39
Andres Moralesd908c622016-04-20 13:13:34 -070040 /* package */ Window.OnFrameMetricsAvailableListener mListener;
Andres Morales910beb82016-02-02 16:19:40 -080041 /* package */ VirtualRefBasePtr mNative;
42
43 /**
44 * Creates a FrameMetricsObserver
45 *
46 * @param looper the looper to use when invoking callbacks
47 */
48 FrameMetricsObserver(@NonNull Window window, @NonNull Looper looper,
Andres Moralesd908c622016-04-20 13:13:34 -070049 @NonNull Window.OnFrameMetricsAvailableListener listener) {
Andres Morales910beb82016-02-02 16:19:40 -080050 if (looper == null) {
51 throw new NullPointerException("looper cannot be null");
52 }
53
54 mMessageQueue = looper.getQueue();
55 if (mMessageQueue == null) {
56 throw new IllegalStateException("invalid looper, null message queue\n");
57 }
58
59 mFrameMetrics = new FrameMetrics();
60 mWindow = new WeakReference<>(window);
61 mListener = listener;
62 }
63
64 // Called by native on the provided Handler
65 @SuppressWarnings("unused")
66 private void notifyDataAvailable(int dropCount) {
67 final Window window = mWindow.get();
68 if (window != null) {
Andres Moralesd908c622016-04-20 13:13:34 -070069 mListener.onFrameMetricsAvailable(window, mFrameMetrics, dropCount);
Andres Morales910beb82016-02-02 16:19:40 -080070 }
71 }
72}