// Class: openfl._internal.renderer.opengl.GLTextureBase

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;
function openfl__$internal_renderer_opengl_GLCompressedTextureFormats() {return require("./../../../../openfl/_internal/renderer/opengl/GLCompressedTextureFormats");}
function lime__$internal_graphics_ImageCanvasUtil() {return require("./../../../../lime/_internal/graphics/ImageCanvasUtil");}
function lime_graphics_ImageType() {return require("./../../../../lime/graphics/ImageType");}
function openfl__$internal_renderer_opengl_GLUtils() {return require("./../../../../openfl/_internal/renderer/opengl/GLUtils");}

// Constructor

var GLTextureBase = function(){}

// Meta

GLTextureBase.__name__ = ["openfl","_internal","renderer","opengl","GLTextureBase"];
GLTextureBase.prototype = {
	
};
GLTextureBase.prototype.__class__ = GLTextureBase.prototype.constructor = $hxClasses["openfl._internal.renderer.opengl.GLTextureBase"] = GLTextureBase;

// Init



// Statics

GLTextureBase.create = function(textureBase,renderer) {
	var gl = renderer.__context.webgl;
	textureBase.__textureID = gl.createTexture();
	textureBase.__textureContext = renderer.__context;
	if(GLTextureBase.__supportsBGRA == null) {
		GLTextureBase.__textureInternalFormat = gl.RGBA;
		var bgraExtension = null;
		if(bgraExtension != null) {
			GLTextureBase.__supportsBGRA = true;
			GLTextureBase.__textureFormat = bgraExtension.BGRA_EXT;
			if(renderer.__context.type == "opengles") {
				GLTextureBase.__textureInternalFormat = bgraExtension.BGRA_EXT;
			}
		} else {
			GLTextureBase.__supportsBGRA = false;
			GLTextureBase.__textureFormat = gl.RGBA;
		}
	}
	if(GLTextureBase.__compressedTextureFormats == null) {
		GLTextureBase.__compressedTextureFormats = new (openfl__$internal_renderer_opengl_GLCompressedTextureFormats().default)(renderer.__context);
	}
	textureBase.__internalFormat = GLTextureBase.__textureInternalFormat;
	textureBase.__format = GLTextureBase.__textureFormat;
}
GLTextureBase.dispose = function(textureBase,renderer) {
	var gl = renderer.__context.webgl;
	if(textureBase.__alphaTexture != null) {
		textureBase.__alphaTexture.dispose();
	}
	gl.deleteTexture(textureBase.__textureID);
}
GLTextureBase.getImage = function(textureBase,renderer,bitmapData) {
	var image = bitmapData.image;
	if(!bitmapData.__isValid || image == null) {
		return null;
	}
	(lime__$internal_graphics_ImageCanvasUtil().default).sync(image,false);
	var gl = renderer.__context.webgl;
	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,0);
		image = image.clone();
		image.set_premultiplied(true);
	}
	if(image.get_format() != 0) {
		image = image.clone();
		image.set_format(0);
		image.buffer.premultiplied = true;
	}
	return image;
}
GLTextureBase.setSamplerState = function(textureBase,renderer,state) {
	if(!state.equals(textureBase.__samplerState)) {
		var gl = renderer.__context.webgl;
		renderer.bindTexture(textureBase.__textureTarget,textureBase.__textureID);
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		gl.texParameteri(textureBase.__textureTarget,gl.TEXTURE_MIN_FILTER,state.minFilter);
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		gl.texParameteri(textureBase.__textureTarget,gl.TEXTURE_MAG_FILTER,state.magFilter);
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		gl.texParameteri(textureBase.__textureTarget,gl.TEXTURE_WRAP_S,state.wrapModeS);
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		gl.texParameteri(textureBase.__textureTarget,gl.TEXTURE_WRAP_T,state.wrapModeT);
		(openfl__$internal_renderer_opengl_GLUtils().default).CheckGLError();
		var tmp = state.lodBias != 0.0;
		textureBase.__samplerState = state;
		textureBase.__samplerState.__samplerDirty = false;
	}
}
GLTextureBase.__supportsBGRA = null
GLTextureBase.__compressedTextureFormats = null

// Export

exports.default = GLTextureBase;