Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.gfx; |
michael@0 | 7 | |
michael@0 | 8 | /** |
michael@0 | 9 | * A class used to schedule a callback to occur when the next frame is drawn. |
michael@0 | 10 | * Subclasses must redefine the internalRun method, not the run method. |
michael@0 | 11 | */ |
michael@0 | 12 | public abstract class RenderTask { |
michael@0 | 13 | /** |
michael@0 | 14 | * Whether to run the task after the render, or before. |
michael@0 | 15 | */ |
michael@0 | 16 | public final boolean runAfter; |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * Time when this task has first run, in ns. Useful for tasks which run for a specific duration. |
michael@0 | 20 | */ |
michael@0 | 21 | private long mStartTime; |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * Whether we should initialise mStartTime on the next frame run. |
michael@0 | 25 | */ |
michael@0 | 26 | private boolean mResetStartTime = true; |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * The callback to run on each frame. timeDelta is the time elapsed since |
michael@0 | 30 | * the last call, in nanoseconds. Returns true if it should continue |
michael@0 | 31 | * running, or false if it should be removed from the task queue. Returning |
michael@0 | 32 | * true implicitly schedules a redraw. |
michael@0 | 33 | * |
michael@0 | 34 | * This method first initializes the start time if resetStartTime has been invoked, |
michael@0 | 35 | * then calls internalRun. |
michael@0 | 36 | * |
michael@0 | 37 | * Note : subclasses should override internalRun. |
michael@0 | 38 | * |
michael@0 | 39 | * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns. |
michael@0 | 40 | * @param currentFrameStartTime the startTime of the current frame, in ns. |
michael@0 | 41 | * @return true if animation should be run at the next frame, false otherwise |
michael@0 | 42 | * @see RenderTask#internalRun(long, long) |
michael@0 | 43 | */ |
michael@0 | 44 | public final boolean run(long timeDelta, long currentFrameStartTime) { |
michael@0 | 45 | if (mResetStartTime) { |
michael@0 | 46 | mStartTime = currentFrameStartTime; |
michael@0 | 47 | mResetStartTime = false; |
michael@0 | 48 | } |
michael@0 | 49 | return internalRun(timeDelta, currentFrameStartTime); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * Abstract method to be overridden by subclasses. |
michael@0 | 54 | * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns |
michael@0 | 55 | * @param currentFrameStartTime the startTime of the current frame, in ns. |
michael@0 | 56 | * @return true if animation should be run at the next frame, false otherwise |
michael@0 | 57 | */ |
michael@0 | 58 | protected abstract boolean internalRun(long timeDelta, long currentFrameStartTime); |
michael@0 | 59 | |
michael@0 | 60 | public RenderTask(boolean aRunAfter) { |
michael@0 | 61 | runAfter = aRunAfter; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Get the start time of this task. |
michael@0 | 66 | * It is the start time of the first frame this task was run on. |
michael@0 | 67 | * @return the start time in ns |
michael@0 | 68 | */ |
michael@0 | 69 | public long getStartTime() { |
michael@0 | 70 | return mStartTime; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Schedule a reset of the recorded start time next time {@link RenderTask#run(long, long)} is run. |
michael@0 | 75 | * @see RenderTask#getStartTime() |
michael@0 | 76 | */ |
michael@0 | 77 | public void resetStartTime() { |
michael@0 | 78 | mResetStartTime = true; |
michael@0 | 79 | } |
michael@0 | 80 | } |