|
| 1 | +import { XRSessionSpector } from "../../polyfill/XRSessionSpector"; |
| 2 | +import { XRWebGLBindingSpector } from "../../polyfill/XRWebGLBindingSpector"; |
| 3 | +import { XRWebGLLayerSpector } from "../../polyfill/XRWebGLLayerSpector"; |
| 4 | +import { OriginFunctionHelper } from "../utils/originFunctionHelper"; |
| 5 | +import { TimeSpy } from "./timeSpy"; |
| 6 | + |
| 7 | +export class XRSpy { |
| 8 | + public currentXRSession: XRSessionSpector | undefined; |
| 9 | + private timeSpy: TimeSpy; |
| 10 | + constructor(timeSpy: TimeSpy) { |
| 11 | + this.timeSpy = timeSpy; |
| 12 | + this.init(); |
| 13 | + } |
| 14 | + |
| 15 | + public spyXRSession(session: XRSessionSpector) { |
| 16 | + if (this.currentXRSession) { |
| 17 | + this.unspyXRSession(); |
| 18 | + } |
| 19 | + for (const Spy of TimeSpy.getRequestAnimationFrameFunctionNames()) { |
| 20 | + OriginFunctionHelper.resetOriginFunction(this.timeSpy.getSpiedScope(), Spy); |
| 21 | + } |
| 22 | + this.timeSpy.spyRequestAnimationFrame("requestAnimationFrame", session); |
| 23 | + this.currentXRSession = session; |
| 24 | + } |
| 25 | + |
| 26 | + public unspyXRSession() { |
| 27 | + if (!this.currentXRSession) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + OriginFunctionHelper.resetOriginFunction(this.currentXRSession, "requestAnimationFrame"); |
| 32 | + this.currentXRSession = undefined; |
| 33 | + // listen to the regular frames again. |
| 34 | + for (const Spy of TimeSpy.getRequestAnimationFrameFunctionNames()) { |
| 35 | + this.timeSpy.spyRequestAnimationFrame(Spy, this.timeSpy.getSpiedScope()); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private init(): void { |
| 40 | + if (!navigator.xr) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + (window as any).XRWebGLLayer = XRWebGLLayerSpector; |
| 45 | + (window as any).XRWebGLBinding = XRWebGLBindingSpector; |
| 46 | + |
| 47 | + // polyfill request session so Spector gets access to the session object. |
| 48 | + const existingRequestSession = navigator.xr.requestSession; |
| 49 | + Object.defineProperty(navigator.xr, "requestSessionInternal", { writable: true }); |
| 50 | + (navigator.xr as any).requestSessionInternal = existingRequestSession; |
| 51 | + |
| 52 | + const newRequestSession = ( |
| 53 | + sessionMode: XRSessionMode, |
| 54 | + sessionInit?: any |
| 55 | + ): Promise<XRSession> => { |
| 56 | + const modifiedSessionPromise = (mode: XRSessionMode, init?: any): Promise<XRSession> => { |
| 57 | + return (navigator.xr as any).requestSessionInternal(mode, init).then((session: XRSession) => { |
| 58 | + // listen to the XR Session here! When we do that, we'll stop listening to window.requestAnimationFrame |
| 59 | + // and start listening to session.requestAnimationFrame |
| 60 | + |
| 61 | + // Feed the gl context through the session |
| 62 | + const spectorSession = session as XRSessionSpector; |
| 63 | + spectorSession._updateRenderState = session.updateRenderState; |
| 64 | + spectorSession.updateRenderState = async ( |
| 65 | + renderStateInit?: XRRenderStateInit |
| 66 | + ): Promise<void> => { |
| 67 | + if (renderStateInit.baseLayer) { |
| 68 | + const polyfilledBaseLayer = |
| 69 | + renderStateInit.baseLayer as XRWebGLLayerSpector; |
| 70 | + spectorSession.glContext = polyfilledBaseLayer.getContext(); |
| 71 | + } |
| 72 | + |
| 73 | + if (renderStateInit.layers) { |
| 74 | + for (const layer of renderStateInit.layers) { |
| 75 | + const layerAny: any = layer; |
| 76 | + if (layerAny.glContext) { |
| 77 | + spectorSession.glContext = layerAny.glContext; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return spectorSession._updateRenderState(renderStateInit); |
| 82 | + }; |
| 83 | + |
| 84 | + this.spyXRSession(spectorSession); |
| 85 | + session.addEventListener("end", () => { |
| 86 | + this.unspyXRSession(); |
| 87 | + }); |
| 88 | + return Promise.resolve(session); |
| 89 | + }); |
| 90 | + }; |
| 91 | + return modifiedSessionPromise(sessionMode, sessionInit); |
| 92 | + }; |
| 93 | + |
| 94 | + |
| 95 | + Object.defineProperty(navigator.xr, "requestSession", { writable: true }); |
| 96 | + (navigator.xr as any).requestSession = newRequestSession; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments