[whatwg] Synchronizing Canvas updates in a worker to DOM changes in the UI thread

Robert O'Callahan robert at ocallahan.org
Sun Oct 20 21:33:32 PDT 2013


On Sun, Oct 20, 2013 at 5:33 PM, Glenn Maynard <glenn at bluegoji.com> wrote:

> Example:
>
> <canvas id=canvas explicitpresent>
> <script>
> var canvas = document.querySelector("#canvas");
> var worker = createWorker();
> worker.postMessage({
>     cmd: "init",
>     canvas: canvas.getWorkerCanvas(),
> });
>
> worker.onmessage = function(e)
> {
>     // The worker told us that a frame has been committed.  Present it for
> display.
>     canvas.present();
>
>     // Tell the worker that it should start rendering the next frame.
>     worker.postMessage({cmd: "update"});
>
>     // Do any DOM changes here, to synchronize them with displaying the new
> canvas.
>     updateUI();
> }
> </script>
>
> Worker:
>
> onmessage = function(e)
> {
>     // On initialization only:
>     if(e.data.cmd == "init")
>         canvas = e.data.canvas;
>
>     // Render our scene.
>     renderFrame(canvas);
>
>     // Commit the scene.
>     canvas.commit();
>
>     // Tell the main thread that the frame is ready.
>     postMessage("present");
> }
> function renderFrame(workerCanvas) { }
>

To me, passing the image data explicitly in an ImageBuffer along with the
"present" message seems like a better fit to the workers message-passing
model than this proposal, where the data is stored as hidden state in the
canvas element with (effectively) a setter in the worker and a getter in
the main thread, and that setting and getting has to be coordinated with
postMessage for synchronization. The relationship between a "commit" and
its "present" has to be deduced by reasoning about the timing of messages,
rather than by just reasoning about JS data flow through postMessage.

This proposal also requires that whenever a worker is going to return image
data to the main thread, the main thread must start things off by creating
a canvas element. It's also not possible for a worker to spawn off
sub-workers to do drawing (at least, not without some really ugly
coordination with the main thread.)

Rob
-- 
Jtehsauts  tshaei dS,o n" Wohfy  Mdaon  yhoaus  eanuttehrotraiitny  eovni
le atrhtohu gthot sf oirng iyvoeu rs ihnesa.r"t sS?o  Whhei csha iids  teoa
stiheer :p atroa lsyazye,d  'mYaonu,r  "sGients  uapr,e  tfaokreg iyvoeunr,
'm aotr  atnod  sgaoy ,h o'mGee.t"  uTph eann dt hwea lmka'n?  gBoutt  uIp
waanndt  wyeonut  thoo mken.o w  *
*



More information about the whatwg mailing list