// Class: openfl.display3D.textures.RectangleTexture

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../../hxClasses_stub").default;
var $import = require("./../../../import_stub").default;
var $extend = require("./../../../extend_stub").default;
function openfl_display3D_textures_TextureBase() {return require("./../../../openfl/display3D/textures/TextureBase");}
function lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$() {return require("./../../../lime/graphics/_WebGLRenderContext/WebGLRenderContext_Impl_");}
function openfl_utils__$ByteArray_ByteArray_$Impl_$() {return require("./../../../openfl/utils/_ByteArray/ByteArray_Impl_");}
function openfl_display3D_Context3D() {return require("./../../../openfl/display3D/Context3D");}
function lime_graphics_ImageType() {return require("./../../../lime/graphics/ImageType");}

// Constructor

var RectangleTexture = function(context,width,height,format,optimizeForRenderToTexture) {
	(openfl_display3D_textures_TextureBase().default).call(this,context);
	this.__width = width;
	this.__height = height;
	this.__optimizeForRenderToTexture = optimizeForRenderToTexture;
	this.__textureTarget = this.__context.gl.TEXTURE_2D;
	this.uploadFromTypedArray(null);
	if(optimizeForRenderToTexture) {
		this.__getGLFramebuffer(true,0,0);
	}
}

// Meta

RectangleTexture.__name__ = ["openfl","display3D","textures","RectangleTexture"];
RectangleTexture.__super__ = (openfl_display3D_textures_TextureBase().default);
RectangleTexture.prototype = $extend((openfl_display3D_textures_TextureBase().default).prototype, {
	uploadFromBitmapData: function(source) {
		if(source == null) {
			return;
		}
		var image = this.__getImage(source);
		if(image == null) {
			return;
		}
		if(image.buffer != null && image.buffer.data == null && image.buffer.get_src() != null) {
			var gl = this.__context.gl;
			this.__context.__bindGLTexture2D(this.__textureID);
			(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,this.__textureTarget,0,this.__internalFormat,this.__format,gl.UNSIGNED_BYTE,image.buffer.get_src());
			this.__context.__bindGLTexture2D(null);
			return;
		}
		this.uploadFromTypedArray(image.get_data());
	},
	uploadFromByteArray: function(data,byteArrayOffset) {
		if(byteArrayOffset == 0) {
			this.uploadFromTypedArray(data.b);
			return;
		}
		var buffer = (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).toArrayBuffer(data);
		var this1;
		if(buffer != null) {
			this1 = new Uint8Array(buffer,byteArrayOffset);
		} else {
			this1 = null;
		}
		this.uploadFromTypedArray(this1);
	},
	uploadFromTypedArray: function(data) {
		var gl = this.__context.gl;
		this.__context.__bindGLTexture2D(this.__textureID);
		(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,this.__textureTarget,0,this.__internalFormat,this.__width,this.__height,0,this.__format,gl.UNSIGNED_BYTE,data);
		this.__context.__bindGLTexture2D(null);
	},
	__setSamplerState: function(state) {
		if((openfl_display3D_textures_TextureBase().default).prototype.__setSamplerState.call(this,state)) {
			var gl = this.__context.gl;
			if((openfl_display3D_Context3D().default).GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT != 0) {
				var aniso;
				var _g = state.filter;
				switch(_g) {
				case "anisotropic16x":
					aniso = 16;
					break;
				case "anisotropic2x":
					aniso = 2;
					break;
				case "anisotropic4x":
					aniso = 4;
					break;
				case "anisotropic8x":
					aniso = 8;
					break;
				default:
					aniso = 1;
				}
				if(aniso > (openfl_display3D_Context3D().default).GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT) {
					aniso = (openfl_display3D_Context3D().default).GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT;
				}
				gl.texParameterf(gl.TEXTURE_2D,(openfl_display3D_Context3D().default).GL_TEXTURE_MAX_ANISOTROPY_EXT,aniso);
			}
			return true;
		}
		return false;
	},
	__uploadFromImage: function(image) {
		var gl = this.__context.gl;
		var internalFormat;
		var format;
		if(image.buffer.bitsPerPixel == 1) {
			internalFormat = gl.ALPHA;
			format = gl.ALPHA;
		} else {
			internalFormat = (openfl_display3D_textures_TextureBase().default).__textureInternalFormat;
			format = (openfl_display3D_textures_TextureBase().default).__textureFormat;
		}
		this.__context.__bindGLTexture2D(this.__textureID);
		if(image.type != (lime_graphics_ImageType().default).DATA && !image.get_premultiplied()) {
			gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,1);
		} else if(!image.get_premultiplied() && image.get_transparent()) {
			gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,1);
		}
		if(image.type == (lime_graphics_ImageType().default).DATA) {
			(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,gl.TEXTURE_2D,0,internalFormat,image.buffer.width,image.buffer.height,0,format,gl.UNSIGNED_BYTE,image.get_data());
		} else {
			(lime_graphics__$WebGLRenderContext_WebGLRenderContext_$Impl_$().default).texImage2D(gl,gl.TEXTURE_2D,0,internalFormat,format,gl.UNSIGNED_BYTE,image.get_src());
		}
		this.__context.__bindGLTexture2D(null);
	}
});
RectangleTexture.prototype.__class__ = RectangleTexture.prototype.constructor = $hxClasses["openfl.display3D.textures.RectangleTexture"] = RectangleTexture;

// Init



// Statics




// Export

exports.default = RectangleTexture;