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.graphics.HardwareRendererObserver; 21 import android.os.Handler; 22 23 import java.lang.ref.WeakReference; 24 25 /** 26 * Provides streaming access to frame stats information from the rendering 27 * subsystem to apps. 28 * 29 * @hide 30 */ 31 public class FrameMetricsObserver 32 implements HardwareRendererObserver.OnFrameMetricsAvailableListener { 33 private final WeakReference<Window> mWindow; 34 private final FrameMetrics mFrameMetrics; 35 private final HardwareRendererObserver mObserver; 36 /*package*/ final Window.OnFrameMetricsAvailableListener mListener; 37 38 /** 39 * Creates a FrameMetricsObserver 40 * 41 * @param handler the Handler to use when invoking callbacks 42 */ FrameMetricsObserver(@onNull Window window, @NonNull Handler handler, @NonNull Window.OnFrameMetricsAvailableListener listener)43 FrameMetricsObserver(@NonNull Window window, @NonNull Handler handler, 44 @NonNull Window.OnFrameMetricsAvailableListener listener) { 45 mWindow = new WeakReference<>(window); 46 mListener = listener; 47 mFrameMetrics = new FrameMetrics(); 48 mObserver = new HardwareRendererObserver(this, mFrameMetrics.mTimingData, handler, 49 false /*waitForPresentTime*/); 50 } 51 52 /** 53 * Implementation of OnFrameMetricsAvailableListener 54 * @param dropCountSinceLastInvocation the number of reports dropped since the last time 55 * @Override 56 */ onFrameMetricsAvailable(int dropCountSinceLastInvocation)57 public void onFrameMetricsAvailable(int dropCountSinceLastInvocation) { 58 if (mWindow.get() != null) { 59 mListener.onFrameMetricsAvailable(mWindow.get(), mFrameMetrics, 60 dropCountSinceLastInvocation); 61 } 62 } 63 getRendererObserver()64 /*package*/ HardwareRendererObserver getRendererObserver() { 65 return mObserver; 66 } 67 } 68