scheduler.js (2146B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Frame schedulers: executing a frame's worth of mutation, and possibly 6 // waiting for a later frame. (These schedulers will halt the main thread, 7 // allowing background threads to continue working.) 8 9 var Scheduler = class { 10 constructor(perfMonitor) { 11 this._perfMonitor = perfMonitor; 12 } 13 14 start(loadMgr, timestamp) { 15 return loadMgr.start(timestamp); 16 } 17 tick(loadMgr, timestamp) {} 18 wait_for_next_frame(t0, tick_start, tick_end) {} 19 }; 20 21 // "Sync to vsync" scheduler: after the mutator is done for a frame, wait until 22 // the beginning of the next 60fps frame. 23 var VsyncScheduler = class extends Scheduler { 24 tick(loadMgr, timestamp) { 25 this._perfMonitor.before_mutator(timestamp); 26 gHost.start_turn(); 27 const completed = loadMgr.tick(timestamp); 28 gHost.end_turn(); 29 this._perfMonitor.after_mutator(timestamp); 30 return completed; 31 } 32 33 wait_for_next_frame(t0, tick_start, tick_end) { 34 // Compute how long until the next 60fps vsync event, and wait that long. 35 const elapsed = (tick_end - t0) / 1000; 36 const period = 1 / FPS; 37 const used = elapsed % period; 38 const delay = period - used; 39 gHost.suspend(delay); 40 this._perfMonitor.after_suspend(delay); 41 } 42 }; 43 44 // Try to maintain 60fps, but if we overrun a frame, do more processing 45 // immediately to make the next frame come up as soon as possible. 46 var OptimizeForFrameRate = class extends Scheduler { 47 tick(loadMgr, timestamp) { 48 this._perfMonitor.before_mutator(timestamp); 49 gHost.start_turn(); 50 const completed = loadMgr.tick(timestamp); 51 gHost.end_turn(); 52 this._perfMonitor.after_mutator(timestamp); 53 return completed; 54 } 55 56 wait_for_next_frame(t0, tick_start, tick_end) { 57 const next_frame_ms = round_up(tick_start, 1000 / FPS); 58 if (tick_end < next_frame_ms) { 59 const delay = (next_frame_ms - tick_end) / 1000; 60 gHost.suspend(delay); 61 this._perfMonitor.after_suspend(delay); 62 } 63 } 64 };