UNPKG

7.8 kBJavaScriptView Raw
1"use strict";
2var constants = require("@pixi/constants"), extensions = require("@pixi/extensions"), settings = require("@pixi/settings");
3let CONTEXT_UID_COUNTER = 0;
4class ContextSystem {
5 /** @param renderer - The renderer this System works for. */
6 constructor(renderer) {
7 this.renderer = renderer, this.webGLVersion = 1, this.extensions = {}, this.supports = {
8 uint32Indices: !1
9 }, this.handleContextLost = this.handleContextLost.bind(this), this.handleContextRestored = this.handleContextRestored.bind(this);
10 }
11 /**
12 * `true` if the context is lost
13 * @readonly
14 */
15 get isLost() {
16 return !this.gl || this.gl.isContextLost();
17 }
18 /**
19 * Handles the context change event.
20 * @param {WebGLRenderingContext} gl - New WebGL context.
21 */
22 contextChange(gl) {
23 this.gl = gl, this.renderer.gl = gl, this.renderer.CONTEXT_UID = CONTEXT_UID_COUNTER++;
24 }
25 init(options) {
26 if (options.context)
27 this.initFromContext(options.context);
28 else {
29 const alpha = this.renderer.background.alpha < 1, premultipliedAlpha = options.premultipliedAlpha;
30 this.preserveDrawingBuffer = options.preserveDrawingBuffer, this.useContextAlpha = options.useContextAlpha, this.powerPreference = options.powerPreference, this.initFromOptions({
31 alpha,
32 premultipliedAlpha,
33 antialias: options.antialias,
34 stencil: !0,
35 preserveDrawingBuffer: options.preserveDrawingBuffer,
36 powerPreference: options.powerPreference
37 });
38 }
39 }
40 /**
41 * Initializes the context.
42 * @protected
43 * @param {WebGLRenderingContext} gl - WebGL context
44 */
45 initFromContext(gl) {
46 this.gl = gl, this.validateContext(gl), this.renderer.gl = gl, this.renderer.CONTEXT_UID = CONTEXT_UID_COUNTER++, this.renderer.runners.contextChange.emit(gl);
47 const view = this.renderer.view;
48 view.addEventListener !== void 0 && (view.addEventListener("webglcontextlost", this.handleContextLost, !1), view.addEventListener("webglcontextrestored", this.handleContextRestored, !1));
49 }
50 /**
51 * Initialize from context options
52 * @protected
53 * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
54 * @param {object} options - context attributes
55 */
56 initFromOptions(options) {
57 const gl = this.createContext(this.renderer.view, options);
58 this.initFromContext(gl);
59 }
60 /**
61 * Helper class to create a WebGL Context
62 * @param canvas - the canvas element that we will get the context from
63 * @param options - An options object that gets passed in to the canvas element containing the
64 * context attributes
65 * @see https://developer.mozilla.org/en/docs/Web/API/HTMLCanvasElement/getContext
66 * @returns {WebGLRenderingContext} the WebGL context
67 */
68 createContext(canvas, options) {
69 let gl;
70 if (settings.settings.PREFER_ENV >= constants.ENV.WEBGL2 && (gl = canvas.getContext("webgl2", options)), gl)
71 this.webGLVersion = 2;
72 else if (this.webGLVersion = 1, gl = canvas.getContext("webgl", options) || canvas.getContext("experimental-webgl", options), !gl)
73 throw new Error("This browser does not support WebGL. Try using the canvas renderer");
74 return this.gl = gl, this.getExtensions(), this.gl;
75 }
76 /** Auto-populate the {@link PIXI.ContextSystem.extensions extensions}. */
77 getExtensions() {
78 const { gl } = this, common = {
79 loseContext: gl.getExtension("WEBGL_lose_context"),
80 anisotropicFiltering: gl.getExtension("EXT_texture_filter_anisotropic"),
81 floatTextureLinear: gl.getExtension("OES_texture_float_linear"),
82 s3tc: gl.getExtension("WEBGL_compressed_texture_s3tc"),
83 s3tc_sRGB: gl.getExtension("WEBGL_compressed_texture_s3tc_srgb"),
84 // eslint-disable-line camelcase
85 etc: gl.getExtension("WEBGL_compressed_texture_etc"),
86 etc1: gl.getExtension("WEBGL_compressed_texture_etc1"),
87 pvrtc: gl.getExtension("WEBGL_compressed_texture_pvrtc") || gl.getExtension("WEBKIT_WEBGL_compressed_texture_pvrtc"),
88 atc: gl.getExtension("WEBGL_compressed_texture_atc"),
89 astc: gl.getExtension("WEBGL_compressed_texture_astc"),
90 bptc: gl.getExtension("EXT_texture_compression_bptc")
91 };
92 this.webGLVersion === 1 ? Object.assign(this.extensions, common, {
93 drawBuffers: gl.getExtension("WEBGL_draw_buffers"),
94 depthTexture: gl.getExtension("WEBGL_depth_texture"),
95 vertexArrayObject: gl.getExtension("OES_vertex_array_object") || gl.getExtension("MOZ_OES_vertex_array_object") || gl.getExtension("WEBKIT_OES_vertex_array_object"),
96 uint32ElementIndex: gl.getExtension("OES_element_index_uint"),
97 // Floats and half-floats
98 floatTexture: gl.getExtension("OES_texture_float"),
99 floatTextureLinear: gl.getExtension("OES_texture_float_linear"),
100 textureHalfFloat: gl.getExtension("OES_texture_half_float"),
101 textureHalfFloatLinear: gl.getExtension("OES_texture_half_float_linear")
102 }) : this.webGLVersion === 2 && Object.assign(this.extensions, common, {
103 // Floats and half-floats
104 colorBufferFloat: gl.getExtension("EXT_color_buffer_float")
105 });
106 }
107 /**
108 * Handles a lost webgl context
109 * @param {WebGLContextEvent} event - The context lost event.
110 */
111 handleContextLost(event) {
112 event.preventDefault(), setTimeout(() => {
113 this.gl.isContextLost() && this.extensions.loseContext && this.extensions.loseContext.restoreContext();
114 }, 0);
115 }
116 /** Handles a restored webgl context. */
117 handleContextRestored() {
118 this.renderer.runners.contextChange.emit(this.gl);
119 }
120 destroy() {
121 const view = this.renderer.view;
122 this.renderer = null, view.removeEventListener !== void 0 && (view.removeEventListener("webglcontextlost", this.handleContextLost), view.removeEventListener("webglcontextrestored", this.handleContextRestored)), this.gl.useProgram(null), this.extensions.loseContext && this.extensions.loseContext.loseContext();
123 }
124 /** Handle the post-render runner event. */
125 postrender() {
126 this.renderer.objectRenderer.renderingToScreen && this.gl.flush();
127 }
128 /**
129 * Validate context.
130 * @param {WebGLRenderingContext} gl - Render context.
131 */
132 validateContext(gl) {
133 const attributes = gl.getContextAttributes(), isWebGl2 = "WebGL2RenderingContext" in globalThis && gl instanceof globalThis.WebGL2RenderingContext;
134 isWebGl2 && (this.webGLVersion = 2), attributes && !attributes.stencil && console.warn("Provided WebGL context does not have a stencil buffer, masks may not render correctly");
135 const hasuint32 = isWebGl2 || !!gl.getExtension("OES_element_index_uint");
136 this.supports.uint32Indices = hasuint32, hasuint32 || console.warn("Provided WebGL context does not support 32 index buffer, complex graphics may not render correctly");
137 }
138}
139ContextSystem.defaultOptions = {
140 /**
141 * {@link PIXI.IRendererOptions.context}
142 * @default null
143 * @memberof PIXI.settings.RENDER_OPTIONS
144 */
145 context: null,
146 /**
147 * {@link PIXI.IRendererOptions.antialias}
148 * @default false
149 * @memberof PIXI.settings.RENDER_OPTIONS
150 */
151 antialias: !1,
152 /**
153 * {@link PIXI.IRendererOptions.premultipliedAlpha}
154 * @default true
155 * @memberof PIXI.settings.RENDER_OPTIONS
156 */
157 premultipliedAlpha: !0,
158 /**
159 * {@link PIXI.IRendererOptions.preserveDrawingBuffer}
160 * @default false
161 * @memberof PIXI.settings.RENDER_OPTIONS
162 */
163 preserveDrawingBuffer: !1,
164 /**
165 * {@link PIXI.IRendererOptions.powerPreference}
166 * @default default
167 * @memberof PIXI.settings.RENDER_OPTIONS
168 */
169 powerPreference: "default"
170}, /** @ignore */
171ContextSystem.extension = {
172 type: extensions.ExtensionType.RendererSystem,
173 name: "context"
174};
175extensions.extensions.add(ContextSystem);
176exports.ContextSystem = ContextSystem;
177//# sourceMappingURL=ContextSystem.js.map