Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle vrdisplayactivate #317

Merged
merged 4 commits into from
Aug 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,6 @@ const _bindWindow = (window, newWindowCb) => {
const frameData = new window.VRFrameData();
const stageParameters = new window.VRStageParameters();
let timeout = null;
let numFrames = 0;
let numDirtyFrames = 0;
const dirtyFrameContexts = [];
const _checkDirtyFrameTimeout = () => {
Expand Down Expand Up @@ -1261,21 +1260,6 @@ const _bindWindow = (window, newWindowCb) => {
if (args.frame || args.minimalFrame) {
console.log('-'.repeat(80) + 'start frame');
}
if (window.document.readyState === 'complete' && (numFrames % FPS) === 0) {
const displays = window.navigator.getVRDisplaysSync();
const presentingDisplay = displays.find(display => display.isPresenting);
if (presentingDisplay) {
const e = new window.Event('vrdisplaycheck');
e.display = presentingDisplay;
window.dispatchEvent(e);
} else {
for (let i = 0; i < displays.length; i++) {
const e = new window.Event('vrdisplayactivate');
e.display = displays[i];
window.dispatchEvent(e);
}
}
}
window.tickAnimationFrame();
if (args.performance) {
const now = Date.now();
Expand All @@ -1299,7 +1283,6 @@ const _bindWindow = (window, newWindowCb) => {
if (args.frame || args.minimalFrame) {
console.log('-'.repeat(80) + 'end frame');
}
numFrames++;

// wait for next frame
const now = Date.now();
Expand Down
21 changes: 14 additions & 7 deletions src/DOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const _promiseSerial = async promiseFns => {
}
};
const _loadPromise = el => new Promise((accept, reject) => {
console.log('load promise', el.tagName);
const load = () => {
_cleanup();
accept();
Expand Down Expand Up @@ -1576,7 +1575,9 @@ class HTMLScriptElement extends HTMLLoadableElement {
this.dispatchEvent(e);
})
.finally(() => {
resource.setProgress(1);
setImmediate(() => {
resource.setProgress(1);
});
});
}

Expand All @@ -1589,7 +1590,7 @@ class HTMLScriptElement extends HTMLLoadableElement {

const resource = this.ownerDocument.resources.addResource();

process.nextTick(() => {
setImmediate(() => {
this.dispatchEvent(new Event('load', {target: this}));

resource.setProgress(1);
Expand Down Expand Up @@ -1803,7 +1804,9 @@ class HTMLIFrameElement extends HTMLSrcableElement {
this.dispatchEvent(new Event('load', {target: this}));
})
.finally(() => {
resource.setProgress(1);
setImmediate(() => {
resource.setProgress(1);
});
});
} else if (name === 'hidden') {
if (this.contentDocument) {
Expand Down Expand Up @@ -2118,7 +2121,9 @@ class HTMLImageElement extends HTMLSrcableElement {
this.dispatchEvent(e);
})
.finally(() => {
resource.setProgress(1);
setImmediate(() => {
resource.setProgress(1);
});
});
}
});
Expand Down Expand Up @@ -2216,7 +2221,9 @@ class HTMLAudioElement extends HTMLMediaElement {
this.dispatchEvent(e);
})
.finally(() => {
resource.setProgress(1);
setImmediate(() => {
resource.setProgress(1);
});
});
}
});
Expand Down Expand Up @@ -2269,7 +2276,7 @@ class HTMLVideoElement extends HTMLMediaElement {

const resource = this.ownerDocument.resources.addResource();

process.nextTick(() => {
setImmediate(() => {
this.dispatchEvent(new Event('canplay', {target: this}));
this.dispatchEvent(new Event('canplaythrough', {target: this}));

Expand Down
27 changes: 27 additions & 0 deletions src/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,33 @@ function initDocument (document, window) {

document.dispatchEvent(new Event('load', {target: document}));
window.dispatchEvent(new Event('load', {target: window}));

const _emitDisplays = () => {
const displays = window.navigator.getVRDisplaysSync();
const presentingDisplay = displays.find(display => display.isPresenting);
if (presentingDisplay) {
const e = new window.Event('vrdisplayactivate');
e.display = presentingDisplay;
window.dispatchEvent(e);
} else {
for (let i = 0; i < displays.length; i++) {
const e = new window.Event('vrdisplayactivate');
e.display = displays[i];
window.dispatchEvent(e);
}
}
};
if (document.resources.resources.length === 0) {
_emitDisplays();
} else {
const _update = () => {
if (document.resources.resources.length === 0) {
_emitDisplays();
document.resources.removeEventListener('update', _update);
}
};
document.resources.addEventListener('update', _update);
}
} else {
try {
await GlobalContext._runHtml(document, window);
Expand Down
2 changes: 1 addition & 1 deletion src/XR.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class XR extends EventEmitter {
requestDevice(name = 'VR') {
if (name === 'VR' && GlobalContext.nativeVr.VR_IsHmdPresent()) {
return Promise.resolve(_getXrDisplay(this._window));
} else if (name === 'AR' && GlobalContext.nativeMl.IsPresent()) {
} else if (name === 'AR' && GlobalContext.nativeMl && GlobalContext.nativeMl.IsPresent()) {
return Promise.resolve(_getXmDisplay(this._window));
} else {
return Promise.resolve(null);
Expand Down