UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 4.38 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import{createResolver as e}from"../../../../core/promiseUtils.js";import{clone as t}from"../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{RenderRequestType as r}from"../lib/basicInterfaces.js";import{RendererTarget as i}from"../lib/rendererUtils.js";import{RenderState as s}from"../../../support/RenderState.js";import{resampleHermite as o}from"../../../support/screenshotUtils.js";import{PixelFormat as n,DataType as a}from"../../../webgl/enums.js";import{ensureAttachmentMaxSize as c}from"../../../webgl/FramebufferObject.js";import{createEmptyImageData as h}from"../../../../core/imageUtils.js";class l{constructor(e,t){this.parameters=e,this.fbos=t}}class d{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(t){await this._renderFunctions.prepareOverlay(),this._renderFunctions.requestRenderScene(r.BACKGROUND);const i=e();return this._screenshotQueue.push({settings:t,resolver:i}),i.promise}update(e,t){for(const r of this._screenshotQueue){if(null==this._rctx){r.resolver.reject();continue}const i={...r.settings,pixelRatio:r.settings.pixelRatio*e.parameters.camera.pixelRatio},s=this._renderScreenshot(e,i,t);r.resolver(s)}this._screenshotQueue.length=0}_renderScreenshotOverlay(e,t,r){e.width=t.width,e.height=t.height;const i=e.getContext("2d"),s=r.pixelRatio;return i.save(),i.translate(0,t.height),i.scale(1,-1),r.region&&i.translate(-r.region.x,-r.region.y),i.scale(s,s),t=this._renderFunctions.renderOverlay(e,r.disableDecorations,t),i.restore(),t}_readbackScreenshot(e,t){return e.resample?this._readbackScreenshotResampled({...e,resample:e.resample},t):this._readbackScreenshotImmediate(e,t)}_readbackScreenshotResampled(e,t){const{framebufferWidth:r,framebufferHeight:i,region:s,resample:c}=e,l=this._ensureScreenshotEncodeCanvas();let d=h(r,i,l);this._rctx.gl.readPixels(0,0,r,i,n.RGBA,a.UNSIGNED_BYTE,new Uint8Array(d.data.buffer)),t(),d=this._renderScreenshotOverlay(l,d,{...e,region:void 0});const f=h(s.width,s.height,l);return o(d,f,!0,c.region.x,i-(c.region.y+c.region.height),c.region.width,c.region.height)}_readbackScreenshotImmediate(e,t){const{framebufferHeight:r,region:i}=e,s=this._ensureScreenshotEncodeCanvas(),o=h(i.width,i.height,s);return this._rctx.gl.readPixels(i.x,r-(i.y+i.height),i.width,i.height,n.RGBA,a.UNSIGNED_BYTE,new Uint8Array(o.data.buffer)),t(),this._renderScreenshotOverlay(s,o,e)}_renderScreenshot(e,r,o){const n=e.parameters.camera,a={width:r.framebufferWidth,height:r.framebufferHeight};c(a,Math.min(this._rctx.parameters.maxTextureSize,this._rctx.parameters.maxRenderbufferSize));let h=!1;const l=a.width!==n.fullWidth||a.height!==n.fullHeight,d=r.ignorePadding&&n.pixelRatio!==r.pixelRatio,f=l||r.disableDecorations||d||r.objectAndLayerIdColor;let m=null,u=null;if(f){const c=n.clone();if(r.ignorePadding){const e=t(c.padding);for(let t=0;t<4;t++)e[t]=Math.round(e[t]/c.pixelRatio*r.pixelRatio);c.padding=e}c.fullWidth=a.width,c.fullHeight=a.height,c.pixelRatio=r.pixelRatio;const l=n.fovX-c.fovX,d=n.fovY-c.fovY;l<0&&l<d?c.fovX=n.fovX:d<0&&d<l&&(c.fovY=n.fovY);const f={camera:c,contentCamera:c,mode:s.IDLE,alignPixelEnabled:!0,contentPixelRatio:c.pixelRatio,forcedAnimationTime:e.parameters.forcedAnimationTime};this._forceCameraHook(f),h=!0;const p=this._renderFunctions.renderScene(f,o,r.objectAndLayerIdColor?i.ObjectAndLayerID:i.Screenshot,r.disableDecorations);u=p.screen,m=p.olid}const p=()=>{this._rctx.bindFramebuffer(null),u?.release()};this._rctx.bindFramebuffer(u?.fbo);const g=this._readbackScreenshot(r,p);let b=null;if(r.objectAndLayerIdColor){const e=()=>{this._rctx.bindFramebuffer(null),m?.release()};this._rctx.bindFramebuffer(m?.fbo),b=this._readbackScreenshot(r,e),this._rctx.bindFramebuffer(null)}if(f&&!this._rctx.contextAttributes.alpha)for(let t=3;t<g.data.length;t+=4)g.data[t]=255;if(b&&!this._rctx.contextAttributes.alpha)for(let t=3;t<b.data.length;t+=4)b.data[t]=255;return h&&this._forceCameraHook(e.parameters),[g,b]}_ensureScreenshotEncodeCanvas(){return this._screenshotEncodeCanvas??=document.createElement("canvas"),this._screenshotEncodeCanvas}}export{l as ScreenshotContext,d as ScreenshotManager};