michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import org.mozilla.gecko.mozglue.RobocopTarget; michael@0: michael@0: import android.os.SystemClock; michael@0: import android.util.Log; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.List; michael@0: michael@0: public class PanningPerfAPI { michael@0: private static final String LOGTAG = "GeckoPanningPerfAPI"; michael@0: michael@0: // make this large enough to avoid having to resize the frame time michael@0: // list, as that may be expensive and impact the thing we're trying michael@0: // to measure. michael@0: private static final int EXPECTED_FRAME_COUNT = 2048; michael@0: michael@0: private static boolean mRecordingFrames = false; michael@0: private static List mFrameTimes; michael@0: private static long mFrameStartTime; michael@0: michael@0: private static boolean mRecordingCheckerboard = false; michael@0: private static List mCheckerboardAmounts; michael@0: private static long mCheckerboardStartTime; michael@0: michael@0: private static void initialiseRecordingArrays() { michael@0: if (mFrameTimes == null) { michael@0: mFrameTimes = new ArrayList(EXPECTED_FRAME_COUNT); michael@0: } else { michael@0: mFrameTimes.clear(); michael@0: } michael@0: if (mCheckerboardAmounts == null) { michael@0: mCheckerboardAmounts = new ArrayList(EXPECTED_FRAME_COUNT); michael@0: } else { michael@0: mCheckerboardAmounts.clear(); michael@0: } michael@0: } michael@0: michael@0: @RobocopTarget michael@0: public static void startFrameTimeRecording() { michael@0: if (mRecordingFrames || mRecordingCheckerboard) { michael@0: Log.e(LOGTAG, "Error: startFrameTimeRecording() called while already recording!"); michael@0: return; michael@0: } michael@0: mRecordingFrames = true; michael@0: initialiseRecordingArrays(); michael@0: mFrameStartTime = SystemClock.uptimeMillis(); michael@0: } michael@0: michael@0: @RobocopTarget michael@0: public static List stopFrameTimeRecording() { michael@0: if (!mRecordingFrames) { michael@0: Log.e(LOGTAG, "Error: stopFrameTimeRecording() called when not recording!"); michael@0: return null; michael@0: } michael@0: mRecordingFrames = false; michael@0: return mFrameTimes; michael@0: } michael@0: michael@0: public static void recordFrameTime() { michael@0: // this will be called often, so try to make it as quick as possible michael@0: if (mRecordingFrames) { michael@0: mFrameTimes.add(SystemClock.uptimeMillis() - mFrameStartTime); michael@0: } michael@0: } michael@0: michael@0: public static boolean isRecordingCheckerboard() { michael@0: return mRecordingCheckerboard; michael@0: } michael@0: michael@0: @RobocopTarget michael@0: public static void startCheckerboardRecording() { michael@0: if (mRecordingCheckerboard || mRecordingFrames) { michael@0: Log.e(LOGTAG, "Error: startCheckerboardRecording() called while already recording!"); michael@0: return; michael@0: } michael@0: mRecordingCheckerboard = true; michael@0: initialiseRecordingArrays(); michael@0: mCheckerboardStartTime = SystemClock.uptimeMillis(); michael@0: } michael@0: michael@0: @RobocopTarget michael@0: public static List stopCheckerboardRecording() { michael@0: if (!mRecordingCheckerboard) { michael@0: Log.e(LOGTAG, "Error: stopCheckerboardRecording() called when not recording!"); michael@0: return null; michael@0: } michael@0: mRecordingCheckerboard = false; michael@0: michael@0: // We take the number of values in mCheckerboardAmounts here, as there's michael@0: // the possibility that this function is called while recordCheckerboard michael@0: // is still executing. As values are added to this list last, we use michael@0: // this number as the canonical number of recordings. michael@0: int values = mCheckerboardAmounts.size(); michael@0: michael@0: // The score will be the sum of all the values in mCheckerboardAmounts, michael@0: // so weight the checkerboard values by time so that frame-rate and michael@0: // run-length don't affect score. michael@0: long lastTime = 0; michael@0: float totalTime = mFrameTimes.get(values - 1); michael@0: for (int i = 0; i < values; i++) { michael@0: long elapsedTime = mFrameTimes.get(i) - lastTime; michael@0: mCheckerboardAmounts.set(i, mCheckerboardAmounts.get(i) * elapsedTime / totalTime); michael@0: lastTime += elapsedTime; michael@0: } michael@0: michael@0: return mCheckerboardAmounts; michael@0: } michael@0: michael@0: public static void recordCheckerboard(float amount) { michael@0: // this will be called often, so try to make it as quick as possible michael@0: if (mRecordingCheckerboard) { michael@0: mFrameTimes.add(SystemClock.uptimeMillis() - mCheckerboardStartTime); michael@0: mCheckerboardAmounts.add(amount); michael@0: } michael@0: } michael@0: }