/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{createResolver as t,onAbortOrThrow as e,throwIfNotAbortError as i}from"../../../core/promiseUtils.js";import{identity as s,translate as r,rotate as h,scaleByVec2 as o,multiply as u}from"../../../core/libs/gl-matrix-2/math/mat3.js";import{create as a}from"../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{fromValues as n}from"../../../core/libs/gl-matrix-2/factories/vec2f32.js";import{DisplayObject as l}from"./DisplayObject.js";import c from"./ImageryBitmapSource.js";import{SizedPixelFormat as d}from"../../webgl/enums.js";import _ from"../../webgl/Texture.js";import{TextureDescriptor as p}from"../../webgl/TextureDescriptor.js";function m(t){t instanceof ImageBitmap&&t.close()}function x(t){return t&&"render"in t}function g(t){const e=document.createElement("canvas");return e.width=t.width,e.height=t.height,t.render(e.getContext("2d")),e}function f(t){return x(t)?t instanceof c?t.getRenderedRasterPixels()?.renderedRasterPixels:g(t):t}class w extends l{constructor(t=null,e=!1){super(),this.blendFunction="standard",this._sourceWidth=0,this._sourceHeight=0,this._textureInvalidated=!1,this.stencilRef=0,this.coordScale=[1,1],this._height=void 0,this.pixelRatio=1,this.resolution=0,this.rotation=0,this._source=null,this._texture=null,this._width=void 0,this.x=0,this.y=0,this.immutable=e,this.source=t,this.requestRender=this.requestRender.bind(this)}destroy(){super.destroy(),this._texture&&(this._texture.dispose(),this._texture=null),this._source&&m(this._source),null!=this._uploadStatus&&(this._uploadStatus.controller.abort(),this._uploadStatus=null)}get isSourceScaled(){return this.width!==this._sourceWidth||this.height!==this._sourceHeight}get height(){return void 0!==this._height?this._height:this._sourceHeight}set height(t){this._height=t}get source(){return this._source}set source(t){null==t&&null==this._source||(this._source&&m(this._source),this._source=t,this.invalidateTexture(),this.requestRender())}get texture(){return this._texture}get width(){return void 0!==this._width?this._width:this._sourceWidth}set width(t){this._width=t}beforeRender(t){super.beforeRender(t),this.updateTexture(t)}async setSourceAsync(i,s){null!=this._uploadStatus&&this._uploadStatus.controller.abort();const r=new AbortController,h=t();return e(s,()=>r.abort()),e(r,t=>h.reject(t)),this._uploadStatus={controller:r,resolver:h},this.source=i,h.promise}invalidateTexture(){this._textureInvalidated||(this._textureInvalidated=!0,this._source instanceof HTMLImageElement?(this._sourceHeight=this._source.naturalHeight,this._sourceWidth=this._source.naturalWidth):this._source&&(this._sourceHeight=this._source.height,this._sourceWidth=this._source.width))}transitionStep(t,e){t>=64&&(this.fadeTransitionEnabled=!1),super.transitionStep(t,e)}setTransform(t){const e=s(this.transforms.displayViewScreenMat3),[i,a]=t.toScreenNoRotation([0,0],[this.x,this.y]),l=this.resolution/this.pixelRatio/t.resolution,c=l*this.width,d=l*this.height,_=Math.PI*this.rotation/180;r(e,e,n(i,a)),r(e,e,n(c/2,d/2)),h(e,e,-_),r(e,e,n(-c/2,-d/2)),o(e,e,n(c,d)),u(this.transforms.displayViewScreenMat3,t.displayViewMat3,e)}setSamplingProfile(t){this._texture&&(t.mips&&!this._texture.descriptor.hasMipmap&&this._texture.generateMipmap(),this._texture.setSamplingMode(t.samplingMode))}bind(t,e){this._texture&&t.bindTexture(this._texture,e)}async updateTexture({context:t,painter:e}){if(!this._textureInvalidated)return;if(this._textureInvalidated=!1,this._texture||(this._texture=this._createTexture(t)),!this.source)return void this._texture.setData(null);this._texture.resize(this._sourceWidth,this._sourceHeight);const s=f(this.source);try{if(null!=this._uploadStatus){const{controller:t,resolver:i}=this._uploadStatus,r={signal:t.signal},{width:h,height:o}=this,u=this._texture,a=e.textureUploadManager;await a.enqueueTextureUpdate({data:s,texture:u,width:h,height:o},r),i.resolve(),this._uploadStatus=null}else this._texture.setData(s);this.ready()}catch(r){i(r)}}onDetach(){this.destroy()}_createTransforms(){return{displayViewScreenMat3:a()}}_createTexture(t){const e=this.immutable,i=new p(this._sourceWidth,this._sourceHeight);return i.internalFormat=e?d.RGBA8:6408,i.wrapMode=33071,i.isImmutable=e,new _(t,i)}}export{w as Bitmap,x as isImageSource,g as rasterize};
