/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{clamp as e}from"../../../../../../../core/mathUtils.js";import{disposeMaybe as t}from"../../../../../../../core/maybe.js";import{isSVG as i}from"../../../../../../../core/urlUtils.js";import{simplePipelineState as r}from"../../utils.js";import{Technique as s}from"../Technique.js";import{MagnifierShader as o}from"../shaders/MagnifierShader.js";import a from"../../../../../../webgl/Texture.js";import{TextureDescriptor as u}from"../../../../../../webgl/TextureDescriptor.js";class n extends s{constructor(){super(...arguments),this.type=21,this._resourcePixelRatio=1,this._position=[0,0,0,0],this.shaders={magnifier:new o}}updateResources(t,i,r,s){t.pixelRatio!==this._resourcePixelRatio&&this._destroyResources(),this._readbackTexture||this._initializeResources(t,i,r,s);const{context:o,pixelRatio:a}=t,{factor:u,offset:n,position:l}=s,{size:h}=t.state,c=s.size*a,p=1/u,x=Math.ceil(p*c);this._readbackTexture.resize(x,x);const m=a*h[0],d=a*h[1],_=.5*x,T=.5*x,f=e(a*l.x,_,m-_-1),b=e(d-a*l.y,T,d-T-1),g=f-_,y=b-T,k=this._readbackTexture;o.bindTexture(k,0),o.gl.copyTexImage2D(k.descriptor.target,0,k.descriptor.pixelFormat,g,y,x,x,0);const w=(f+n.x*a)/m*2-1,R=(b-n.y*a)/d*2-1,M=c/m*2,j=c/d*2;this._position[0]=w,this._position[1]=R,this._position[2]=M,this._position[3]=j}render(e,t){const{context:i,painter:s}=e;s.setPipelineState(r);const o={readbackTexture:{texture:this._readbackTexture,unit:0},maskTexture:{texture:this._maskTexture,unit:7},overlayTexture:{texture:this._overlayTexture,unit:6},drawPos:this._position,...t};s.submitDrawMesh(i,{shader:this.shaders.magnifier,uniforms:{config:o},defines:null,optionalAttributes:null,useComputeBuffer:!1},s.quadMesh)}shutdown(){this._destroyResources()}_initializeResources(e,t,r,s){const o=e.context;this._resourcePixelRatio=e.pixelRatio;const n=Math.ceil(s.size*e.pixelRatio);r.width=n,r.height=n;const l=new u(Math.ceil(n/s.factor));l.internalFormat=6408,l.wrapMode=33071,l.samplingMode=9728,l.flipped=!0,l.preMultiplyAlpha=!i(r.src)||!e.context.driverTest.svgPremultipliesAlpha.result,this._overlayTexture=new a(o,l,r),t.width=n,t.height=n,l.pixelFormat=l.internalFormat=6406,this._maskTexture=new a(o,l,t),l.pixelFormat=l.internalFormat=6408,l.samplingMode=9729,l.flipped=!1,this._readbackTexture=new a(o,l)}_destroyResources(){t(this._maskTexture),t(this._overlayTexture),t(this._readbackTexture),this._maskTexture=null,this._overlayTexture=null,this._readbackTexture=null}}export{n as MagnifierTechnique};
