Description
We are encountering an issue when we have multiple plots with GL layers. When there are more then 16 WebGL contexts in use, the oldests will be "lost", showing a frowny. The console will also show "WARNING: Too many active WebGL contexts. Oldest context will be lost."
The browser limits the number of contexts to 16 per "site instance" by default and in our case it is not practical to change the browser options. Each GL layer (possibly each series, depending on the zlevel option) currently requires its own context.
I've been experimenting a bit and found that it is possible to use only one context and share it with all plots and layers. Details are below.
I've seen no issues with it so far, but I'm not familiar enough with this library. I'm sure the code has to be improved in places and checks have to be added for each (interface) function that uses the renderer. I'm not sure if there are cases where needed information is lost when recreating the renderer.
Hopefully you can have a look and see if you can incorporate this or something similar in a future version of the library. It would help a lot.
Details
Example files: webgl_context_lost.zip
- webgl_context_lost.html: Example HTML to easily create varying number of plots with varying number of GL layers.
- webgl_context_lost_LayerGL.patch: Git patch for LayerGL.js changes
- webgl_context_lost_release_2.0.7.patch: Patch for release 2.0.7
- webgl_context_lost_release_2.0.7_no_sharing.patch: Patch for release 2.0.7 without the shared renderer/context
My approach is to create a separate canvas element from the one of the renderer. After the renderer is done drawing, the result is copied to the separate canvas which is used for displaying.
Additionally, when trying to redraw, I check to see if the context is lost and, if so, simply reinstantiate the renderer.
In principle this also works without having the shared renderer/context, but the performance impact can be quite significant if, for example, you have a plot with more than 16 GL layers. In that case the renderer is recreated many times for each layer. You notice the difference clearly when using the attached example files and creating 1 plot with 17 layers. Toggling series and resizing is noticably affected.