Added { willReadFrequently } to every canvas context, no matter where it comes from

This commit is contained in:
Richard Davey 2023-03-09 17:30:04 +00:00
parent f3028118d3
commit c8acb16fa7
9 changed files with 61 additions and 60 deletions

View file

@ -74,7 +74,7 @@ var GenerateTexture = function (config)
canvas.height = height; canvas.height = height;
} }
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d', { willReadFrequently: true });
if (clearCanvas) if (clearCanvas)
{ {

View file

@ -82,7 +82,7 @@ var CanvasPool = function ()
if (_disableContextSmoothing && canvasType === CONST.CANVAS) if (_disableContextSmoothing && canvasType === CONST.CANVAS)
{ {
Smoothing.disable(canvas.getContext('2d')); Smoothing.disable(canvas.getContext('2d', { willReadFrequently: false }));
} }
return canvas; return canvas;

View file

@ -1498,6 +1498,7 @@ var Graphics = new Class({
var texture; var texture;
var ctx; var ctx;
var willRead = { willReadFrequently: true };
if (typeof key === 'string') if (typeof key === 'string')
{ {
@ -1511,7 +1512,7 @@ var Graphics = new Class({
if (src instanceof HTMLCanvasElement) if (src instanceof HTMLCanvasElement)
{ {
ctx = src.getContext('2d'); ctx = src.getContext('2d', willRead);
} }
} }
else else
@ -1520,14 +1521,14 @@ var Graphics = new Class({
texture = sys.textures.createCanvas(key, width, height); texture = sys.textures.createCanvas(key, width, height);
ctx = texture.getSourceImage().getContext('2d'); ctx = texture.getSourceImage().getContext('2d', willRead);
} }
} }
else if (key instanceof HTMLCanvasElement) else if (key instanceof HTMLCanvasElement)
{ {
// Key is a Canvas, so draw to it // Key is a Canvas, so draw to it
ctx = key.getContext('2d'); ctx = key.getContext('2d', willRead);
} }
if (ctx) if (ctx)

View file

@ -144,7 +144,7 @@ var Text = new Class({
* @type {CanvasRenderingContext2D} * @type {CanvasRenderingContext2D}
* @since 3.0.0 * @since 3.0.0
*/ */
this.context = this.canvas.getContext('2d'); this.context = this.canvas.getContext('2d', { willReadFrequently: true });
/** /**
* The Text Style object. * The Text Style object.

View file

@ -187,7 +187,7 @@ var TileSprite = new Class({
* @type {CanvasRenderingContext2D} * @type {CanvasRenderingContext2D}
* @since 3.12.0 * @since 3.12.0
*/ */
this.context = this.canvas.getContext('2d'); this.context = this.canvas.getContext('2d', { willReadFrequently: false });
/** /**
* The Texture the TileSprite is using as its fill pattern. * The Texture the TileSprite is using as its fill pattern.
@ -272,7 +272,7 @@ var TileSprite = new Class({
* @type {CanvasRenderingContext2D} * @type {CanvasRenderingContext2D}
* @since 3.12.0 * @since 3.12.0
*/ */
this.fillContext = this.fillCanvas.getContext('2d'); this.fillContext = this.fillCanvas.getContext('2d', { willReadFrequently: false });
/** /**
* The texture that the Tile Sprite is rendered to, which is then rendered to a Scene. * The texture that the Tile Sprite is rendered to, which is then rendered to a Scene.

View file

@ -86,7 +86,7 @@ var Vector = require('../geometry/Vector');
render.mouse = options.mouse; render.mouse = options.mouse;
render.engine = options.engine; render.engine = options.engine;
render.canvas = render.canvas || _createCanvas(render.options.width, render.options.height); render.canvas = render.canvas || _createCanvas(render.options.width, render.options.height);
render.context = render.canvas.getContext('2d'); render.context = render.canvas.getContext('2d', { willReadFrequently: true });
render.textures = {}; render.textures = {};
render.bounds = render.bounds || { render.bounds = render.bounds || {
@ -1339,7 +1339,7 @@ var Vector = require('../geometry/Vector');
* @return {Number} pixel ratio * @return {Number} pixel ratio
*/ */
var _getPixelRatio = function(canvas) { var _getPixelRatio = function(canvas) {
var context = canvas.getContext('2d'), var context = canvas.getContext('2d', { willReadFrequently: true }),
devicePixelRatio = window.devicePixelRatio || 1, devicePixelRatio = window.devicePixelRatio || 1,
backingStorePixelRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio backingStorePixelRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio
|| context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio

View file

@ -33,7 +33,7 @@ var CanvasSnapshot = function (canvas, config)
if (getPixel) if (getPixel)
{ {
var context = canvas.getContext('2d'); var context = canvas.getContext('2d', { willReadFrequently: false });
var imageData = context.getImageData(x, y, 1, 1); var imageData = context.getImageData(x, y, 1, 1);
var data = imageData.data; var data = imageData.data;
@ -43,7 +43,7 @@ var CanvasSnapshot = function (canvas, config)
{ {
// Area Grab // Area Grab
var copyCanvas = CanvasPool.createWebGL(this, width, height); var copyCanvas = CanvasPool.createWebGL(this, width, height);
var ctx = copyCanvas.getContext('2d'); var ctx = copyCanvas.getContext('2d', { willReadFrequently: true });
if (width > 0 && height > 0) if (width > 0 && height > 0)
{ {

View file

@ -127,7 +127,7 @@ var DynamicTexture = new Class({
* @type {CanvasRenderingContext2D} * @type {CanvasRenderingContext2D}
* @since 3.7.0 * @since 3.7.0
*/ */
this.context = (isCanvas) ? source.getContext('2d') : null; this.context = (isCanvas) ? source.getContext('2d', { willReadFrequently: true }) : null;
/** /**
* Is this Dynamic Texture dirty or not? If not it won't spend time clearing or filling itself. * Is this Dynamic Texture dirty or not? If not it won't spend time clearing or filling itself.

View file

@ -397,7 +397,7 @@ var TextureManager = new Class({
var cd = textureFrame.canvasData; var cd = textureFrame.canvasData;
var canvas = CanvasPool.create2D(this, cd.width, cd.height); var canvas = CanvasPool.create2D(this, cd.width, cd.height);
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d', { willReadFrequently: true });
if (cd.width > 0 && cd.height > 0) if (cd.width > 0 && cd.height > 0)
{ {