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.util.Log; 21 import android.os.Looper; 22 import android.os.MessageQueue; 23 24 import com.android.internal.util.VirtualRefBasePtr; 25 26 import java.lang.NullPointerException; 27 import java.lang.ref.WeakReference; 28 import java.lang.SuppressWarnings; 29 30 /** 31 * Provides streaming access to frame stats information from the rendering 32 * subsystem to apps. 33 * 34 * @hide 35 */ 36 public 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 */ FrameMetricsObserver(@onNull Window window, @NonNull Looper looper, @NonNull Window.OnFrameMetricsAvailableListener listener)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") notifyDataAvailable(int dropCount)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