<commit_before>package openfl.display;


import haxe.Timer;
import lime.graphics.opengl.GL;
import openfl._internal.renderer.opengl.GLStage3D;
import openfl._internal.renderer.RenderSession;
import openfl.display.OpenGLView;
import openfl.display3D.Context3D;
import openfl.display3D.Context3DProfile;
import openfl.display3D.Context3DRenderMode;
import openfl.events.ErrorEvent;
import openfl.events.Event;
import openfl.events.EventDispatcher;
import openfl.Vector;

#if (js && html5)
import js.html.CanvasElement;
import js.html.CanvasRenderingContext2D;
import js.html.CSSStyleDeclaration;
import js.Browser;
#end

@:access(lime.graphics.opengl.GL)
@:access(openfl.display3D.Context3D)


class Stage3D extends EventDispatcher {
	
	
	public var context3D (default, null):Context3D;
	public var visible:Bool;
	public var x:Float;
	public var y:Float;
	
	private var __contextRequested:Bool;
	
	#if (js && html5)
	private var __canvas:CanvasElement;
	private var __context:CanvasRenderingContext2D;
	private var __style:CSSStyleDeclaration;
	#end
	
	
	public function new () {
		
		super ();
		
		visible = true;
		
	}
	
	
	public function requestContext3D (context3DRenderMode:Context3DRenderMode = AUTO, profile:Context3DProfile = BASELINE):Void {
		
		__contextRequested = true;
		
	}
	
	
	public function requestContext3DMatchingProfiles (profiles:Vector<Context3DProfile>):Void {
		
		requestContext3D ();
		
	}
	
	
	private function __createContext (renderSession:RenderSession):Void {
		
		
		
	}
	
	
	public function __renderDOM (stage:Stage, renderSession:RenderSession):Void {
		
		if (!visible) return;
		
		if (__contextRequested) {
			
			if (context3D == null) {
				
				#if (js && html5)
				
				__canvas = cast Browser.document.createElement ("canvas");
				__canvas.width = stage.stageWidth;
				__canvas.height = stage.stageHeight;
				
				var window = stage.window;
				var options = {
					
					alpha: false, 
					premultipliedAlpha: false, 
					antialias: false, 
					depth: Reflect.hasField (window.config, "depthBuffer") ? window.config.depthBuffer : true, 
					stencil: Reflect.hasField (window.config, "stencilBuffer") ? window.config.stencilBuffer : false
					
				}
				
				__context = cast __canvas.getContext ("webgl", options);
				
				if (__context == null) {
					
					__context = cast __canvas.getContext ("experimental-webgl", options);
					
				}
				
				if (__context != null) {
					
					#if debug
					__context = untyped WebGLDebugUtils.makeDebugContext (__context);
					#end
					
					// TODO: Need to handle renderSession/context better
					
					GL.context = cast __context;
					
					context3D = new Context3D (this, renderSession);
					
					renderSession.element.appendChild (__canvas);
					
					__style = __canvas.style;
					__style.setProperty ("position", "absolute", null);
					__style.setProperty ("top", "0", null);
					__style.setProperty ("left", "0", null);
					__style.setProperty (renderSession.transformOriginProperty, "0 0 0", null);
					__style.setProperty ("z-index", "-1", null);
					
					dispatchEvent (new Event (Event.CONTEXT3D_CREATE));
					
				} else {
					
					dispatchEvent (new ErrorEvent (ErrorEvent.ERROR));
					
				}
				
				#end
				
			}
			
			__contextRequested = false;
			
		}
		
		//if (context3D != null) {
			//
			//DOMStage3D.render (this, renderSession);
			//
		//}
		
	}
	
	
	public function __renderGL (stage:Stage, renderSession:RenderSession):Void {
		
		if (!visible) return;
		
		if (__contextRequested) {
			
			if (context3D == null) {
				
				context3D = new Context3D (this, renderSession);
				dispatchEvent (new Event (Event.CONTEXT3D_CREATE));
				
			}
			
			__contextRequested = false;
			
		}
		
		if (context3D != null) {
			
			GLStage3D.render (this, renderSession);
			
		}
		
	}
	
	
	public function __resize (width:Int, height:Int):Void {
		
		#if (js && html5)
		if (__canvas != null) {
			
			__canvas.width = width;
			__canvas.height = height;
			
		}<commit_msg>Temporary workaround for Stage3D blend mode<commit_after>package openfl.display;


import haxe.Timer;
import lime.graphics.opengl.GL;
import openfl._internal.renderer.opengl.GLStage3D;
import openfl._internal.renderer.RenderSession;
import openfl.display.OpenGLView;
import openfl.display3D.Context3D;
import openfl.display3D.Context3DBlendFactor;
import openfl.display3D.Context3DProfile;
import openfl.display3D.Context3DRenderMode;
import openfl.events.ErrorEvent;
import openfl.events.Event;
import openfl.events.EventDispatcher;
import openfl.Vector;

#if (js && html5)
import js.html.CanvasElement;
import js.html.CanvasRenderingContext2D;
import js.html.CSSStyleDeclaration;
import js.Browser;
#end

@:access(lime.graphics.opengl.GL)
@:access(openfl.display3D.Context3D)


class Stage3D extends EventDispatcher {
	
	
	public var context3D (default, null):Context3D;
	public var visible:Bool;
	public var x:Float;
	public var y:Float;
	
	private var __contextRequested:Bool;
	
	#if (js && html5)
	private var __canvas:CanvasElement;
	private var __context:CanvasRenderingContext2D;
	private var __style:CSSStyleDeclaration;
	#end
	
	
	public function new () {
		
		super ();
		
		visible = true;
		
	}
	
	
	public function requestContext3D (context3DRenderMode:Context3DRenderMode = AUTO, profile:Context3DProfile = BASELINE):Void {
		
		__contextRequested = true;
		
	}
	
	
	public function requestContext3DMatchingProfiles (profiles:Vector<Context3DProfile>):Void {
		
		requestContext3D ();
		
	}
	
	
	private function __createContext (renderSession:RenderSession):Void {
		
		
		
	}
	
	
	public function __renderDOM (stage:Stage, renderSession:RenderSession):Void {
		
		if (!visible) return;
		
		if (__contextRequested) {
			
			if (context3D == null) {
				
				#if (js && html5)
				
				__canvas = cast Browser.document.createElement ("canvas");
				__canvas.width = stage.stageWidth;
				__canvas.height = stage.stageHeight;
				
				var window = stage.window;
				var options = {
					
					alpha: false, 
					premultipliedAlpha: false, 
					antialias: false, 
					depth: Reflect.hasField (window.config, "depthBuffer") ? window.config.depthBuffer : true, 
					stencil: Reflect.hasField (window.config, "stencilBuffer") ? window.config.stencilBuffer : false
					
				}
				
				__context = cast __canvas.getContext ("webgl", options);
				
				if (__context == null) {
					
					__context = cast __canvas.getContext ("experimental-webgl", options);
					
				}
				
				if (__context != null) {
					
					#if debug
					__context = untyped WebGLDebugUtils.makeDebugContext (__context);
					#end
					
					// TODO: Need to handle renderSession/context better
					
					GL.context = cast __context;
					
					context3D = new Context3D (this, renderSession);
					
					renderSession.element.appendChild (__canvas);
					
					__style = __canvas.style;
					__style.setProperty ("position", "absolute", null);
					__style.setProperty ("top", "0", null);
					__style.setProperty ("left", "0", null);
					__style.setProperty (renderSession.transformOriginProperty, "0 0 0", null);
					__style.setProperty ("z-index", "-1", null);
					
					dispatchEvent (new Event (Event.CONTEXT3D_CREATE));
					
				} else {
					
					dispatchEvent (new ErrorEvent (ErrorEvent.ERROR));
					
				}
				
				#end
				
			}
			
			__contextRequested = false;
			
		}
		
		if (context3D != null) {
			
			// TODO: Better blend mode fix
			
			context3D.setBlendFactors (Context3DBlendFactor.ONE, Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			
			//DOMStage3D.render (this, renderSession);
			
		}
		
	}
	
	
	public function __renderGL (stage:Stage, renderSession:RenderSession):Void {
		
		if (!visible) return;
		
		if (__contextRequested) {
			
			if (context3D == null) {
				
				context3D = new Context3D (this, renderSession);
				dispatchEvent (new Event (Event.CONTEXT3D_CREATE));
				
			}
			
			__contextRequested = false;
			
		}
		
		if (context3D != null) {
			
			// TODO: Better blend mode fix
			
			context3D.setBlendFactors (Context3DBlendFactor.ONE, Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			
			GLStage3D.render (this, renderSession);
			
		}
		
	}
	
	
	public function __resize (width:Int, height:Int):Void {
		
		#if (js && html5)
		if (__canvas != null) {
			
			__canvas.width = width;
			__canvas.height = height;
			
		}
{'fraction_non_alphanumeric': 0.10486757337151038, 'fraction_numerical': 0.011333810546408972, 'mean_word_length': 3.1706467661691544, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686787', 'n_tokens_mistral': 3396, 'n_tokens_neox': 2593, 'n_words': 565}