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: /** michael@0: * A class used to schedule a callback to occur when the next frame is drawn. michael@0: * Subclasses must redefine the internalRun method, not the run method. michael@0: */ michael@0: public abstract class RenderTask { michael@0: /** michael@0: * Whether to run the task after the render, or before. michael@0: */ michael@0: public final boolean runAfter; michael@0: michael@0: /** michael@0: * Time when this task has first run, in ns. Useful for tasks which run for a specific duration. michael@0: */ michael@0: private long mStartTime; michael@0: michael@0: /** michael@0: * Whether we should initialise mStartTime on the next frame run. michael@0: */ michael@0: private boolean mResetStartTime = true; michael@0: michael@0: /** michael@0: * The callback to run on each frame. timeDelta is the time elapsed since michael@0: * the last call, in nanoseconds. Returns true if it should continue michael@0: * running, or false if it should be removed from the task queue. Returning michael@0: * true implicitly schedules a redraw. michael@0: * michael@0: * This method first initializes the start time if resetStartTime has been invoked, michael@0: * then calls internalRun. michael@0: * michael@0: * Note : subclasses should override internalRun. michael@0: * michael@0: * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns. michael@0: * @param currentFrameStartTime the startTime of the current frame, in ns. michael@0: * @return true if animation should be run at the next frame, false otherwise michael@0: * @see RenderTask#internalRun(long, long) michael@0: */ michael@0: public final boolean run(long timeDelta, long currentFrameStartTime) { michael@0: if (mResetStartTime) { michael@0: mStartTime = currentFrameStartTime; michael@0: mResetStartTime = false; michael@0: } michael@0: return internalRun(timeDelta, currentFrameStartTime); michael@0: } michael@0: michael@0: /** michael@0: * Abstract method to be overridden by subclasses. michael@0: * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns michael@0: * @param currentFrameStartTime the startTime of the current frame, in ns. michael@0: * @return true if animation should be run at the next frame, false otherwise michael@0: */ michael@0: protected abstract boolean internalRun(long timeDelta, long currentFrameStartTime); michael@0: michael@0: public RenderTask(boolean aRunAfter) { michael@0: runAfter = aRunAfter; michael@0: } michael@0: michael@0: /** michael@0: * Get the start time of this task. michael@0: * It is the start time of the first frame this task was run on. michael@0: * @return the start time in ns michael@0: */ michael@0: public long getStartTime() { michael@0: return mStartTime; michael@0: } michael@0: michael@0: /** michael@0: * Schedule a reset of the recorded start time next time {@link RenderTask#run(long, long)} is run. michael@0: * @see RenderTask#getStartTime() michael@0: */ michael@0: public void resetStartTime() { michael@0: mResetStartTime = true; michael@0: } michael@0: }