W3C home > Mailing lists > Public > whatwg@whatwg.org > October 2013

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

From: Robert O'Callahan <robert@ocallahan.org>
Date: Mon, 21 Oct 2013 06:33:32 +0200
Message-ID: <CAOp6jLaXsFq55b=98V9S6Dyc9KbSqrVVeq++BSQaEG8kAkkxSw@mail.gmail.com>
To: Glenn Maynard <glenn@bluegoji.com>
Cc: whatwg <whatwg@whatwg.org>, Kenneth Russell <kbr@google.com>
On Sun, Oct 20, 2013 at 5:33 PM, Glenn Maynard <glenn@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  *
*
Received on Monday, 21 October 2013 04:33:56 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 17:00:12 UTC