mobile/android/base/gfx/PanningPerfAPI.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko.gfx;
     8 import org.mozilla.gecko.mozglue.RobocopTarget;
    10 import android.os.SystemClock;
    11 import android.util.Log;
    13 import java.util.ArrayList;
    14 import java.util.List;
    16 public class PanningPerfAPI {
    17     private static final String LOGTAG = "GeckoPanningPerfAPI";
    19     // make this large enough to avoid having to resize the frame time
    20     // list, as that may be expensive and impact the thing we're trying
    21     // to measure.
    22     private static final int EXPECTED_FRAME_COUNT = 2048;
    24     private static boolean mRecordingFrames = false;
    25     private static List<Long> mFrameTimes;
    26     private static long mFrameStartTime;
    28     private static boolean mRecordingCheckerboard = false;
    29     private static List<Float> mCheckerboardAmounts;
    30     private static long mCheckerboardStartTime;
    32     private static void initialiseRecordingArrays() {
    33         if (mFrameTimes == null) {
    34             mFrameTimes = new ArrayList<Long>(EXPECTED_FRAME_COUNT);
    35         } else {
    36             mFrameTimes.clear();
    37         }
    38         if (mCheckerboardAmounts == null) {
    39             mCheckerboardAmounts = new ArrayList<Float>(EXPECTED_FRAME_COUNT);
    40         } else {
    41             mCheckerboardAmounts.clear();
    42         }
    43     }
    45     @RobocopTarget
    46     public static void startFrameTimeRecording() {
    47         if (mRecordingFrames || mRecordingCheckerboard) {
    48             Log.e(LOGTAG, "Error: startFrameTimeRecording() called while already recording!");
    49             return;
    50         }
    51         mRecordingFrames = true;
    52         initialiseRecordingArrays();
    53         mFrameStartTime = SystemClock.uptimeMillis();
    54     }
    56     @RobocopTarget
    57     public static List<Long> stopFrameTimeRecording() {
    58         if (!mRecordingFrames) {
    59             Log.e(LOGTAG, "Error: stopFrameTimeRecording() called when not recording!");
    60             return null;
    61         }
    62         mRecordingFrames = false;
    63         return mFrameTimes;
    64     }
    66     public static void recordFrameTime() {
    67         // this will be called often, so try to make it as quick as possible
    68         if (mRecordingFrames) {
    69             mFrameTimes.add(SystemClock.uptimeMillis() - mFrameStartTime);
    70         }
    71     }
    73     public static boolean isRecordingCheckerboard() {
    74         return mRecordingCheckerboard;
    75     }
    77     @RobocopTarget
    78     public static void startCheckerboardRecording() {
    79         if (mRecordingCheckerboard || mRecordingFrames) {
    80             Log.e(LOGTAG, "Error: startCheckerboardRecording() called while already recording!");
    81             return;
    82         }
    83         mRecordingCheckerboard = true;
    84         initialiseRecordingArrays();
    85         mCheckerboardStartTime = SystemClock.uptimeMillis();
    86     }
    88     @RobocopTarget
    89     public static List<Float> stopCheckerboardRecording() {
    90         if (!mRecordingCheckerboard) {
    91             Log.e(LOGTAG, "Error: stopCheckerboardRecording() called when not recording!");
    92             return null;
    93         }
    94         mRecordingCheckerboard = false;
    96         // We take the number of values in mCheckerboardAmounts here, as there's
    97         // the possibility that this function is called while recordCheckerboard
    98         // is still executing. As values are added to this list last, we use
    99         // this number as the canonical number of recordings.
   100         int values = mCheckerboardAmounts.size();
   102         // The score will be the sum of all the values in mCheckerboardAmounts,
   103         // so weight the checkerboard values by time so that frame-rate and
   104         // run-length don't affect score.
   105         long lastTime = 0;
   106         float totalTime = mFrameTimes.get(values - 1);
   107         for (int i = 0; i < values; i++) {
   108             long elapsedTime = mFrameTimes.get(i) - lastTime;
   109             mCheckerboardAmounts.set(i, mCheckerboardAmounts.get(i) * elapsedTime / totalTime);
   110             lastTime += elapsedTime;
   111         }
   113         return mCheckerboardAmounts;
   114     }
   116     public static void recordCheckerboard(float amount) {
   117         // this will be called often, so try to make it as quick as possible
   118         if (mRecordingCheckerboard) {
   119             mFrameTimes.add(SystemClock.uptimeMillis() - mCheckerboardStartTime);
   120             mCheckerboardAmounts.add(amount);
   121         }
   122     }
   123 }

mercurial