/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../chunks/_rollupPluginBabelHelpers","../../../../core/maybe","../../../../core/promiseUtils","../../../../chunks/vec4f64","../lib/AutoDisposable","../lib/basicInterfaces","../../../support/screenshotUtils","../../../webgl/enums","../../../webgl/FramebufferObject"],(function(e,t,r,n,i,s,o,a,c,h){"use strict";let l=function(e){function s(t,r,n,i,s,o){var a;return(a=e.call(this)||this)._rctx=t,a._renderScene=r,a._requestRenderScene=n,a._renderOverlay=i,a._forceCameraHook=s,a._disposeOffscreenBuffers=o,a.supersample=!0,a._screenshotQueue=new Array,a}t._inheritsLoose(s,e);var l=s.prototype;return l.dispose=function(){this._rctx=null},l.takeScreenshot=function(e){this._requestRenderScene(o.RenderRequestType.BACKGROUND);const t=n.createResolver();return this._screenshotQueue.push({settings:e,resolver:t}),t.promise},l.update=function(e){for(const t of this._screenshotQueue){if(this.isDisposed){t.resolver.reject();continue}const r={...t.settings,pixelRatio:t.settings.pixelRatio*e.viewCamera.pixelRatio},n=this._renderScreenshot(e,r);t.resolver(n)}this._screenshotQueue.length=0},l._renderScreenshotOverlay=function(e,t,n){if(r.isNone(this._renderOverlay))return t;e.width=t.width,e.height=t.height;const i=e.getContext("2d"),s=n.pixelRatio;return i.save(),i.translate(0,t.height),i.scale(1,-1),n.region&&i.translate(-n.region.x,-n.region.y),i.scale(s,s),t=this._renderOverlay(e,t),i.restore(),t},l._readbackScreenshot=function(e,t){return e.resample?this._readbackScreenshotResampled(e,t):this._readbackScreenshotImmediate(e,t)},l._readbackScreenshotResampled=function(e,t){const{framebufferWidth:r,framebufferHeight:n,region:i,resample:s}=e,o=this._ensureScreenshotEncodeCanvas();let h=a.createEmptyImageData(r,n,o);this._rctx.gl.readPixels(0,0,r,n,c.PixelFormat.RGBA,c.DataType.UNSIGNED_BYTE,new Uint8Array(h.data.buffer)),t(),h=this._renderScreenshotOverlay(o,h,{...e,region:null});const l=a.createEmptyImageData(i.width,i.height,o);return a.resampleHermite(h,l,!0,s.region.x,n-(s.region.y+s.region.height),s.region.width,s.region.height)},l._readbackScreenshotImmediate=function(e,t){const{framebufferHeight:r,region:n}=e,i=this._ensureScreenshotEncodeCanvas(),s=a.createEmptyImageData(n.width,n.height,i);return this._rctx.gl.readPixels(n.x,r-(n.y+n.height),n.width,n.height,c.PixelFormat.RGBA,c.DataType.UNSIGNED_BYTE,new Uint8Array(s.data.buffer)),t(),this._renderScreenshotOverlay(i,s,e)},l._renderScreenshot=function(e,t){let n=null;const s=e.viewCamera,{framebufferWidth:a,framebufferHeight:l}=t;let u=!1;const f=t.disableDecorations&&e.frameHasDecorations,d=a!==s.fullWidth||l!==s.fullHeight,g=t.ignorePadding&&s.pixelRatio!==t.pixelRatio,p=d||f||g;if(p){const e=s.clone();if(t.ignorePadding){const r=i.clone(e.padding);for(let n=0;n<4;n++)r[n]=Math.round(r[n]/e.pixelRatio*t.pixelRatio);e.padding=r}e.fullWidth=a,e.fullHeight=l,e.pixelRatio=t.pixelRatio;const r=s.fovX-e.fovX,f=s.fovY-e.fovY;r<0&&r<f?e.fovX=s.fovX:f<0&&f<r&&(e.fovY=s.fovY),this._forceCameraHook(e),u=!0,n=new h.FramebufferObject(this._rctx,{width:e.fullWidth,height:e.fullHeight,colorTarget:c.TargetType.TEXTURE,depthStencilTarget:c.DepthStencilTargetType.DEPTH_STENCIL_RENDER_BUFFER}),this._renderScene(n,e,t.disableDecorations?o.Decorations.OFF:o.Decorations.ON),this._disposeOffscreenBuffers()}const _=()=>{this._rctx.bindFramebuffer(null),r.disposeMaybe(n)},m=this._readbackScreenshot(t,_);if(_(),p&&!this._rctx.contextAttributes.alpha)for(let r=3;r<m.data.length;r+=4)m.data[r]=255;return u&&this._forceCameraHook(s),m},l._ensureScreenshotEncodeCanvas=function(){return this._screenshotEncodeCanvas||(this._screenshotEncodeCanvas=document.createElement("canvas")),this._screenshotEncodeCanvas},s}(s.AutoDisposable);e.ScreenshotManager=l,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
