openfl / lime

A foundational Haxe framework for cross-platform development
https://lime.openfl.org/
MIT License
756 stars 370 forks source link

Expose willReadFrequently attribute for Canvas2D to improve performance and avoid warnings #1690

Open Poobslag opened 1 year ago

Poobslag commented 1 year ago

I am developing an HTML5 game with HaxeFlixel and get numerous warnings in Chrome's developer console:

ImageCanvasUtil.hx:212 Canvas2D: Multiple readback operations using getImageData are faster with the willReadFrequently attribute set to true. See: https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-will-read-frequently

This 'willReadFrequently' attribute is not set by ImageCanvasUtil.hx and I do not see any way to set this attribute with the current API.

player-03 commented 1 year ago

Pretty sure that would be a net performance loss. Faster reading at the expense of slower rendering.

Unless you're doing something that specifically requires reading from an image but not rendering it directly? Something like BitmapData.copyPixels()?

Poobslag commented 1 year ago

Pretty sure that would be a net performance loss. Faster reading at the expense of slower rendering.

That is a reasonable prediction and I don't know enough about Canvas2D to debate it. I am simply relaying the warning as communicated by Chrome's developer console, some of the HaxeFlixel demos report many, many Canvas2D warnings with this message.

image

The FlxPieDial Demo reports 33 warnings for example. Most demos report zero. TurnBasedRPG reports one if you play it through to the end.

player-03 commented 1 year ago

Are these warnings mainly during setup? Looks like FlxPieDial makes multiple calls to copyPixels(), but I'm pretty sure that's only during setup.

If copyPixels() and similar operations are in fact the culprit, we may want to add an argument to the BitmapData constructor. Let you set it per bitmap. Maybe call the argument willReadFrequently like in JS, or maybe useSoftwareRenderer to be explicit about the effects.

JS was the only one to print a warning, but the hardware/software tradeoff should be comparable on all targets. If we decide to add this, we should make it cross-platform.