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