/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{fromValues as e}from"../../../../../core/libs/gl-matrix-2/factories/vec4f32.js";import{FeatureTile as t}from"../FeatureTile.js";import r from"./WGLBrush.js";import{background as o}from"../shaders/BackgroundPrograms.js";import{tileInfo as i}from"../shaders/TileInfoPrograms.js";import{PrimitiveType as s}from"../../../../webgl/enums.js";import{createProgram as n}from"../../../../webgl/ProgramTemplate.js";import l from"../../../../webgl/Texture.js";import{TextureDescriptor as a}from"../../../../webgl/TextureDescriptor.js";import{VertexArrayObject as f}from"../../../../webgl/VertexArrayObject.js";import{Pos2b as m}from"../../../../webgl/VertexAttributeLayouts.js";import{VertexBuffer as c}from"../../../../webgl/VertexBuffer.js";const u=512,d=512,h=16,g=8,_=(d-2*g)/5;class x extends r{constructor(){super(...arguments),this._color=e(1,0,0,1)}dispose(){this._outlineProgram?.dispose(),this._outlineProgram=null,this._tileInfoProgram?.dispose(),this._tileInfoProgram=null,this._outlineVertexArrayObject?.dispose(),this._outlineVertexArrayObject=null,this._tileInfoVertexArrayObject?.dispose(),this._tileInfoVertexArrayObject=null,this._ctx=null}prepareState({context:e}){e.setBlendingEnabled(!0),e.setBlendFunctionSeparate(1,771,1,771),e.setColorMask(!0,!0,!0,!0),e.setStencilWriteMask(0),e.setStencilTestEnabled(!1)}draw(e,r){const{context:o,requestRender:i,allowDelayedRender:n}=e;if(!r.isReady&&r instanceof t&&r.hasData)return;if(this._loadWGLResources(o),n&&null!=i&&(!this._outlineProgram.compiled||!this._tileInfoProgram.compiled))return void i();o.bindVAO(this._outlineVertexArrayObject),o.useProgram(this._outlineProgram),this._outlineProgram.setUniformMatrix3fv("u_dvsMat3",r.transforms.displayViewScreenMat3),this._outlineProgram.setUniform2f("u_coord_range",r.rangeX,r.rangeY),this._outlineProgram.setUniform1f("u_depth",0),this._outlineProgram.setUniform4fv("u_color",this._color),o.drawArrays(s.LINE_STRIP,0,4);const l=this._getTexture(o,r);l?(o.bindVAO(this._tileInfoVertexArrayObject),o.useProgram(this._tileInfoProgram),o.bindTexture(l,0),this._tileInfoProgram.setUniformMatrix3fv("u_dvsMat3",r.transforms.displayViewScreenMat3),this._tileInfoProgram.setUniform1f("u_depth",0),this._tileInfoProgram.setUniform2f("u_coord_ratio",r.rangeX/r.width,r.rangeY/r.height),this._tileInfoProgram.setUniform2f("u_delta",0,0),this._tileInfoProgram.setUniform2f("u_dimensions",l.descriptor.width,l.descriptor.height),o.drawArrays(s.TRIANGLE_STRIP,0,4),o.bindVAO(null)):o.bindVAO(null)}_loadWGLResources(e){if(this._outlineProgram&&this._tileInfoProgram)return;const t=new Int8Array([0,0,1,0,1,1,0,1]),r=new c(e,m,t);this._outlineVertexArrayObject=new f(e,r),this._outlineProgram=n(e,o,this._outlineVertexArrayObject.locations);const s=new Int8Array([0,0,1,0,0,1,1,1]),l=new c(e,m,s);this._tileInfoVertexArrayObject=new f(e,l),this._tileInfoProgram=n(e,i,this._tileInfoVertexArrayObject.locations)}_getTexture(e,t){if(!this._ctx){const e=document.createElement("canvas");e.width=u,e.height=d,this._ctx=e.getContext("2d")}if(!t.tileDebugInfoTexture){const r=new a(u,d);r.wrapMode=33071,r.samplingMode=9729,r.isImmutable=!0,t.tileDebugInfoTexture=new l(e,r)}const r=this._ctx;r.clearRect(0,0,r.canvas.width,r.canvas.height),r.textAlign="left",r.textBaseline="top",r.font=h-2+"px sans-serif",r.lineWidth=2,r.fillStyle="white",r.strokeStyle="black";const{debugSlot:o}=t;let i=g+_*o;const s=`${o}) ${t.key.id} (${t.constructor.name})`;r.strokeText(s,g,i),r.fillText(s,g,i),i+=h;const{debugInfo:n}=t;if(n){const{length:e,minOrderedLength:t,minUnorderedLength:o,triangleCount:s}=n.display;if(e>0){const t=`Length: ${e}`;r.strokeText(t,g,i),r.fillText(t,g,i),i+=h}if(t){const e=`Min ordered length: ${t}`;r.strokeText(e,g,i),r.fillText(e,g,i),i+=h}if(o){const e=`Min unordered length: ${o}`;r.strokeText(e,g,i),r.fillText(e,g,i),i+=h}if(s>0){s>1e5&&(r.fillStyle="red",r.strokeStyle="white");const e=`Triangle count: ${s}`;r.strokeText(e,g,i),r.fillText(e,g,i),i+=h}const{bytesUsed:l,bytesReserved:a}=n.memory;if(r.fillStyle="white",r.strokeStyle="black",l>0||a>0){const e=`Memory usage: ${l} of ${a} bytes`;r.strokeText(e,g,i),r.fillText(e,g,i),i+=h}}return t.tileDebugInfoTexture.setData(r.canvas),t.tileDebugInfoTexture}}export{x as default};
