/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{clone as e}from"../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{resampleHermite as t}from"../../../support/screenshotUtils.js";import{DataType as r}from"../../../webgl/enums.js";import{ensureAttachmentMaxSize as s}from"../../../webgl/FramebufferObject.js";import{createEmptyImageData as i}from"../../../../core/imageUtils.js";class n{constructor(e,t){this.parameters=e,this.fbos=t}}class o{constructor(e,t,r){this._rctx=e,this._renderFunctions=t,this._forceCameraHook=r,this.supersample=!0,this._screenshotQueue=new Array}destroy(){this._rctx=null}async takeScreenshot(e){await this._renderFunctions.prepareOverlay(),this._renderFunctions.requestRenderScene(0);const t=Promise.withResolvers();return this._screenshotQueue.push({settings:e,resolver:t}),t.promise}update(e,t){for(const r of this._screenshotQueue){if(null==this._rctx){r.resolver.reject();continue}const s={...r.settings,pixelRatio:r.settings.pixelRatio*e.parameters.camera.pixelRatio},i=this._renderScreenshot(e,s,t);r.resolver.resolve(i)}this._screenshotQueue.length=0}_renderScreenshotOverlay(e,t,r){e.width=t.width,e.height=t.height;const s=e.getContext("2d",{willReadFrequently:!0}),i=r.pixelRatio;return s.save(),s.translate(0,t.height),s.scale(1,-1),r.region&&s.translate(-r.region.x,-r.region.y),s.scale(i,i),t=this._renderFunctions.renderOverlay(e,r.disableDecorations,t),s.restore(),t}_readbackScreenshot(e,t){return e.resample?this._readbackScreenshotResampled({...e,resample:e.resample},t):this._readbackScreenshotImmediate(e,t)}_readbackScreenshotResampled(e,s){const{framebufferWidth:n,framebufferHeight:o,region:a,resample:h}=e,c=this._ensureScreenshotEncodeCanvas();let l=i(n,o,c);this._rctx.gl.readPixels(0,0,n,o,6408,r.UNSIGNED_BYTE,new Uint8Array(l.data.buffer)),s(),l=this._renderScreenshotOverlay(c,l,{...e,region:void 0});const d=i(a.width,a.height,c);return t(l,d,!0,h.region.x,o-(h.region.y+h.region.height),h.region.width,h.region.height)}_readbackScreenshotImmediate(e,t){const{framebufferHeight:s,region:n}=e,o=this._ensureScreenshotEncodeCanvas(),a=i(n.width,n.height,o);return this._rctx.gl.readPixels(n.x,s-(n.y+n.height),n.width,n.height,6408,r.UNSIGNED_BYTE,new Uint8Array(a.data.buffer)),t(),this._renderScreenshotOverlay(o,a,e)}_renderScreenshot(t,r,i){const n=t.parameters.camera,{framebufferWidth:o,framebufferHeight:a,ignorePadding:h,pixelRatio:c,disableDecorations:l,olidColor:d}=r,f={width:o,height:a};s(f,Math.min(this._rctx.parameters.maxTextureSize,this._rctx.parameters.maxRenderbufferSize));let u=!1;const m=f.width!==n.fullWidth||f.height!==n.fullHeight,g=h&&n.pixelRatio!==c,_=m||l||g||d;let p=null,b=null;if(_){const t=n.clone();if(h){const r=e(t.padding);for(let e=0;e<4;e++)r[e]=Math.round(r[e]/t.pixelRatio*c);t.padding=r}t.fullWidth=f.width,t.fullHeight=f.height,t.pixelRatio=c;const r=n.fovX-t.fovX,s=n.fovY-t.fovY;r<0&&r<s?t.fovX=n.fovX:s<0&&s<r&&(t.fovY=n.fovY);const o={camera:t,contentCamera:t,mode:2,alignPixelEnabled:!0,contentPixelRatio:t.pixelRatio};this._forceCameraHook(o),u=!0;const a=this._renderFunctions.renderScene(o,i,d?2:1,l);b=a.color,p=a.olid}const x=()=>{this._rctx.bindFramebuffer(null),b?.release()};this._rctx.bindFramebuffer(b?.fbo);const v=this._readbackScreenshot(r,x);let S=null;if(d){const e=()=>{this._rctx.bindFramebuffer(null),p?.release()};this._rctx.bindFramebuffer(p?.fbo),S=this._readbackScreenshot(r,e),this._rctx.bindFramebuffer(null)}if(_&&!this._rctx.contextAttributes.alpha)for(let e=3;e<v.data.length;e+=4)v.data[e]=255;if(S&&!this._rctx.contextAttributes.alpha)for(let e=3;e<S.data.length;e+=4)S.data[e]=255;return u&&this._forceCameraHook(t.parameters),[v,S]}_ensureScreenshotEncodeCanvas(){return this._screenshotEncodeCanvas??=document.createElement("canvas"),this._screenshotEncodeCanvas}}export{n as ScreenshotContext,o as ScreenshotManager};
