var g=Object.defineProperty;var S=(i,t,e)=>t in i?g(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var a=(i,t,e)=>S(i,typeof t!="symbol"?t+"":t,e);import{U as C}from"./pixi-C9-BRZ0j.js";const $=300,v=["drawElements","drawArrays","drawElementsInstanced","drawArraysInstanced"];class m{constructor(t){a(this,"app");a(this,"el");a(this,"drawCalls",0);a(this,"lastFrameDrawCalls",0);a(this,"samples",[]);a(this,"startMs",performance.now());a(this,"lastSampleMs",performance.now());a(this,"glRestore",[]);a(this,"tick",t=>this.onAfterRender(t));this.app=t,this.el=m.createElement(),document.body.appendChild(this.el),this.wrapDrawCalls(),this.app.ticker.add(this.tick,void 0,C.UTILITY),this.render()}onAfterRender(t){this.lastFrameDrawCalls=this.drawCalls,this.drawCalls=0;const e=performance.now();e-this.lastSampleMs>=1e3&&(this.lastSampleMs=e,this.pushSample(),this.render())}pushSample(){this.samples.push({fps:this.app.ticker.FPS,draw:this.lastFrameDrawCalls,tex:this.managedTextureCount()}),this.samples.length>$&&this.samples.shift()}managedTextureCount(){var e;const t=this.app.renderer.texture;return((e=t==null?void 0:t.managedTextures)==null?void 0:e.length)??0}stats(t){if(this.samples.length===0)return{min:0,avg:0,max:0};let e=1/0,n=-1/0,l=0;for(const p of this.samples){const o=t(p);o<e&&(e=o),o>n&&(n=o),l+=o}return{min:e,avg:l/this.samples.length,max:n}}render(){const t=this.stats(r=>r.fps),e=this.stats(r=>r.draw),n=this.stats(r=>r.tex).max,l=this.samples.length?this.samples[this.samples.length-1].tex:this.managedTextureCount(),p=(performance.now()-this.startMs)/1e3,o=Math.floor(p/60),u=Math.floor(p%60).toString().padStart(2,"0"),c=this.app.renderer,w=this.glRestore.length>0,s=r=>Math.round(r).toString(),d=r=>r.toFixed(1),f=this.app.ticker.maxFPS,x=f===0?"off":s(f);this.el.textContent=[`FPS  cur ${s(this.app.ticker.FPS)}  cap ${x}  min ${s(t.min)}  avg ${d(t.avg)}  max ${s(t.max)}`,w?`DRAW cur ${s(this.lastFrameDrawCalls)}  min ${s(e.min)}  avg ${d(e.avg)}  max ${s(e.max)}`:"DRAW n/a (non-WebGL renderer)",`TEX  cur ${l}  max ${s(n)}`,`DPR ${window.devicePixelRatio}  RES ${c.resolution}  ${s(c.width)}x${s(c.height)}`,`elapsed ${o}:${u}  (5-min window)`].join(`
`)}wrapDrawCalls(){const t=this.app.renderer.gl;if(t)for(const e of v){const n=t[e];if(typeof n!="function")continue;const l=n.bind(t);t[e]=(...p)=>{this.drawCalls++,l(...p)},this.glRestore.push(()=>{t[e]=n})}}static createElement(){const t=document.createElement("div");return t.id="perf-overlay",Object.assign(t.style,{position:"fixed",top:"0",left:"0",zIndex:"2147483647",padding:"4px 6px",font:"11px/1.35 ui-monospace, Menlo, Consolas, monospace",whiteSpace:"pre",color:"#0f0",background:"rgba(0, 0, 0, 0.6)",pointerEvents:"none",userSelect:"none",textShadow:"0 1px 1px #000"}),t}destroy(){this.app.ticker.remove(this.tick);for(const t of this.glRestore)t();this.glRestore=[],this.el.remove()}}let h;function E(i){return h||(h=new m(i)),()=>{h==null||h.destroy(),h=void 0}}export{E as mountPerfOverlay};
