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
17package android.view;
18
19import android.annotation.NonNull;
20import android.os.Looper;
21import android.os.MessageQueue;
22
23import com.android.internal.util.VirtualRefBasePtr;
24
25import java.lang.ref.WeakReference;
26
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
40    /* package */ Window.OnFrameMetricsAvailableListener mListener;
41    /* 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,
49            @NonNull Window.OnFrameMetricsAvailableListener listener) {
50        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) {
69            mListener.onFrameMetricsAvailable(window, mFrameMetrics, dropCount);
70        }
71    }
72}
73