|
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/. */ |
|
5 |
|
6 package org.mozilla.gecko.gfx; |
|
7 |
|
8 /** |
|
9 * A class used to schedule a callback to occur when the next frame is drawn. |
|
10 * Subclasses must redefine the internalRun method, not the run method. |
|
11 */ |
|
12 public abstract class RenderTask { |
|
13 /** |
|
14 * Whether to run the task after the render, or before. |
|
15 */ |
|
16 public final boolean runAfter; |
|
17 |
|
18 /** |
|
19 * Time when this task has first run, in ns. Useful for tasks which run for a specific duration. |
|
20 */ |
|
21 private long mStartTime; |
|
22 |
|
23 /** |
|
24 * Whether we should initialise mStartTime on the next frame run. |
|
25 */ |
|
26 private boolean mResetStartTime = true; |
|
27 |
|
28 /** |
|
29 * The callback to run on each frame. timeDelta is the time elapsed since |
|
30 * the last call, in nanoseconds. Returns true if it should continue |
|
31 * running, or false if it should be removed from the task queue. Returning |
|
32 * true implicitly schedules a redraw. |
|
33 * |
|
34 * This method first initializes the start time if resetStartTime has been invoked, |
|
35 * then calls internalRun. |
|
36 * |
|
37 * Note : subclasses should override internalRun. |
|
38 * |
|
39 * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns. |
|
40 * @param currentFrameStartTime the startTime of the current frame, in ns. |
|
41 * @return true if animation should be run at the next frame, false otherwise |
|
42 * @see RenderTask#internalRun(long, long) |
|
43 */ |
|
44 public final boolean run(long timeDelta, long currentFrameStartTime) { |
|
45 if (mResetStartTime) { |
|
46 mStartTime = currentFrameStartTime; |
|
47 mResetStartTime = false; |
|
48 } |
|
49 return internalRun(timeDelta, currentFrameStartTime); |
|
50 } |
|
51 |
|
52 /** |
|
53 * Abstract method to be overridden by subclasses. |
|
54 * @param timeDelta the time between the beginning of last frame and the beginning of this frame, in ns |
|
55 * @param currentFrameStartTime the startTime of the current frame, in ns. |
|
56 * @return true if animation should be run at the next frame, false otherwise |
|
57 */ |
|
58 protected abstract boolean internalRun(long timeDelta, long currentFrameStartTime); |
|
59 |
|
60 public RenderTask(boolean aRunAfter) { |
|
61 runAfter = aRunAfter; |
|
62 } |
|
63 |
|
64 /** |
|
65 * Get the start time of this task. |
|
66 * It is the start time of the first frame this task was run on. |
|
67 * @return the start time in ns |
|
68 */ |
|
69 public long getStartTime() { |
|
70 return mStartTime; |
|
71 } |
|
72 |
|
73 /** |
|
74 * Schedule a reset of the recorded start time next time {@link RenderTask#run(long, long)} is run. |
|
75 * @see RenderTask#getStartTime() |
|
76 */ |
|
77 public void resetStartTime() { |
|
78 mResetStartTime = true; |
|
79 } |
|
80 } |