mobile/android/base/gfx/RenderTask.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/gfx/RenderTask.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.gfx;
    1.10 +
    1.11 +/**
    1.12 + * A class used to schedule a callback to occur when the next frame is drawn.
    1.13 + * Subclasses must redefine the internalRun method, not the run method.
    1.14 + */
    1.15 +public abstract class RenderTask {
    1.16 +    /**
    1.17 +     * Whether to run the task after the render, or before.
    1.18 +     */
    1.19 +    public final boolean runAfter;
    1.20 +
    1.21 +    /**
    1.22 +     * Time when this task has first run, in ns. Useful for tasks which run for a specific duration.
    1.23 +     */
    1.24 +    private long mStartTime;
    1.25 +
    1.26 +    /**
    1.27 +     * Whether we should initialise mStartTime on the next frame run.
    1.28 +     */
    1.29 +    private boolean mResetStartTime = true;
    1.30 +
    1.31 +    /**
    1.32 +     * The callback to run on each frame. timeDelta is the time elapsed since
    1.33 +     * the last call, in nanoseconds. Returns true if it should continue
    1.34 +     * running, or false if it should be removed from the task queue. Returning
    1.35 +     * true implicitly schedules a redraw.
    1.36 +     *
    1.37 +     * This method first initializes the start time if resetStartTime has been invoked,
    1.38 +     * then calls internalRun.
    1.39 +     *
    1.40 +     * Note : subclasses should override internalRun.
    1.41 +     *
    1.42 +     * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns.
    1.43 +     * @param currentFrameStartTime the startTime of the current frame, in ns.
    1.44 +     * @return true if animation should be run at the next frame, false otherwise
    1.45 +     * @see RenderTask#internalRun(long, long)
    1.46 +     */
    1.47 +    public final boolean run(long timeDelta, long currentFrameStartTime) {
    1.48 +        if (mResetStartTime) {
    1.49 +            mStartTime = currentFrameStartTime;
    1.50 +            mResetStartTime = false;
    1.51 +        }
    1.52 +        return internalRun(timeDelta, currentFrameStartTime);
    1.53 +    }
    1.54 +
    1.55 +    /**
    1.56 +     * Abstract method to be overridden by subclasses.
    1.57 +     * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns
    1.58 +     * @param currentFrameStartTime the startTime of the current frame, in ns.
    1.59 +     * @return true if animation should be run at the next frame, false otherwise
    1.60 +     */
    1.61 +    protected abstract boolean internalRun(long timeDelta, long currentFrameStartTime);
    1.62 +
    1.63 +    public RenderTask(boolean aRunAfter) {
    1.64 +        runAfter = aRunAfter;
    1.65 +    }
    1.66 +
    1.67 +    /**
    1.68 +     * Get the start time of this task.
    1.69 +     * It is the start time of the first frame this task was run on.
    1.70 +     * @return the start time in ns
    1.71 +     */
    1.72 +    public long getStartTime() {
    1.73 +        return mStartTime;
    1.74 +    }
    1.75 +
    1.76 +    /**
    1.77 +     * Schedule a reset of the recorded start time next time {@link RenderTask#run(long, long)} is run.
    1.78 +     * @see RenderTask#getStartTime()
    1.79 +     */
    1.80 +    public void resetStartTime() {
    1.81 +        mResetStartTime = true;
    1.82 +    }
    1.83 +}

mercurial