summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/RenderTask.java
blob: 80cbf77f0a94ae423852575bd64f7afc8c896592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.gfx;

/**
 * A class used to schedule a callback to occur when the next frame is drawn.
 * Subclasses must redefine the internalRun method, not the run method.
 */
public abstract class RenderTask {
    /**
     * Whether to run the task after the render, or before.
     */
    public final boolean runAfter;

    /**
     * Time when this task has first run, in ns. Useful for tasks which run for a specific duration.
     */
    private long mStartTime;

    /**
     * Whether we should initialise mStartTime on the next frame run.
     */
    private boolean mResetStartTime = true;

    /**
     * The callback to run on each frame. timeDelta is the time elapsed since
     * the last call, in nanoseconds. Returns true if it should continue
     * running, or false if it should be removed from the task queue. Returning
     * true implicitly schedules a redraw.
     *
     * This method first initializes the start time if resetStartTime has been invoked,
     * then calls internalRun.
     *
     * Note : subclasses should override internalRun.
     *
     * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns.
     * @param currentFrameStartTime the startTime of the current frame, in ns.
     * @return true if animation should be run at the next frame, false otherwise
     * @see RenderTask#internalRun(long, long)
     */
    public final boolean run(long timeDelta, long currentFrameStartTime) {
        if (mResetStartTime) {
            mStartTime = currentFrameStartTime;
            mResetStartTime = false;
        }
        return internalRun(timeDelta, currentFrameStartTime);
    }

    /**
     * Abstract method to be overridden by subclasses.
     * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns
     * @param currentFrameStartTime the startTime of the current frame, in ns.
     * @return true if animation should be run at the next frame, false otherwise
     */
    protected abstract boolean internalRun(long timeDelta, long currentFrameStartTime);

    public RenderTask(boolean aRunAfter) {
        runAfter = aRunAfter;
    }

    /**
     * Get the start time of this task.
     * It is the start time of the first frame this task was run on.
     * @return the start time in ns
     */
    public long getStartTime() {
        return mStartTime;
    }

    /**
     * Schedule a reset of the recorded start time next time {@link RenderTask#run(long, long)} is run.
     * @see RenderTask#getStartTime()
     */
    public void resetStartTime() {
        mResetStartTime = true;
    }
}