/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../../../core/CircularArray.js";import{EventEmitter as t}from"../../../../core/Evented.js";import has from"../../../../core/has.js";import{createDisjointTimerQuery as s}from"../../../webgl/capabilities/DisjointTimerQuery.js";const n=!!has("esri-2d-profiler");class r{constructor(r,i){if(this._events=new t,this._entries=new Map,this._timings=new e(10),this._currentContainer=null,this._currentPass=null,this._currentBrush=null,this._currentSummary=null,!n)return;this._ext=s(r.gl,{}),this._debugOutput=i;const o=r.gl;if(!this.enableCommandLogging)return;let a;for(a in o)if("function"==typeof o[a]){const e=o[a],t=a.includes("draw");o[a]=(...s)=>(this._events.emit("command",{container:this._currentContainer,pass:this._currentPass,brush:this._currentBrush,method:a,args:s,isDrawCommand:t}),this._currentSummary&&(this._currentSummary.commands++,t&&this._currentSummary.drawCommands++),e.apply(o,s))}}get enableCommandLogging(){return"object"==typeof n&&n.commandLogging}get enableTimeLogging(){return"object"==typeof n&&n.timeLogging}get lastTime(){return this._timings.peekLast()}recordContainerStart(e){n&&(this._currentContainer=e)}recordContainerEnd(){n&&(this._currentContainer=null)}recordPassStart(e){n&&(this._currentPass=e,this._initSummary())}recordPassEnd(){n&&(this._currentPass=null,this._emitSummary())}recordBrushStart(e){n&&(this._currentBrush=e)}recordBrushEnd(){n&&(this._currentBrush=null)}recordStart(e){if(n&&null!=this._ext){if(this._entries.has(e)){const t=this._entries.get(e),s=this._ext.resultAvailable(t.query),n=this._ext.disjoint();if(s&&!n){const s=this._ext.getResult(t.query)/1e6;let n=0;if(null!=this._timings.enqueue(s)&&this.enableTimeLogging){const e=this._timings.entries,t=e.length;let s=0;for(const n of e)s+=n;n=s/t}const r=s.toFixed(2),i=n?n.toFixed(2):"--";this.enableCommandLogging?(this.enableTimeLogging?console.groupCollapsed(`Frame report for ${e}, ${r} ms (${i} last 10 avg)\n              ${t.commandsLen} Commands (${t.drawCommands} draw)`):console.groupCollapsed(`Frame report for ${e}\n              ${t.commandsLen} Commands (${t.drawCommands} draw)`),console.log("RenderPass breakdown: "),console.table(t.summaries),console.log("Commands: ",t.commands),console.groupEnd()):this.enableTimeLogging&&console.log(`Frame report for ${e}, ${r} ms (${i} last 10 avg)`),this.enableTimeLogging&&(this._debugOutput.innerHTML=`${r} (${i})`)}for(const e of t.handles)e.remove();this._ext.deleteQuery(t.query),this._entries.delete(e)}const t={name:e,query:this._ext.createQuery(),commands:[],commandsLen:0,drawCommands:0,summaries:[],handles:[]};this.enableCommandLogging&&(t.handles.push(this._events.on("command",e=>{t.commandsLen++,t.commands.push(e),e.isDrawCommand&&t.drawCommands++})),t.handles.push(this._events.on("summary",e=>{t.summaries.push(e)}))),this._ext.beginTimeElapsed(t.query),this._entries.set(e,t)}}recordEnd(e){n&&null!=this._ext&&this._entries.has(e)&&this._ext.endTimeElapsed()}_initSummary(){this.enableCommandLogging&&(this._currentSummary={container:this._currentContainer,pass:this._currentPass,drawCommands:0,commands:0})}_emitSummary(){this.enableCommandLogging&&this._currentSummary&&this._events.emit("summary",this._currentSummary)}}export{r as Profiler};
