Skip to content

Cleanup language around commit() being the only way to update OffscreenCanvas - #3872

Closed
fserb wants to merge 3 commits into
whatwg:mainfrom
fserb:ofc-cleanup
Closed

fserb wants to merge 3 commits into
whatwg:mainfrom
fserb:ofc-cleanup

Conversation

@fserb

@fserb fserb commented Aug 1, 2018 •

Copy link
Copy Markdown
Contributor

This is a follow up of: #3677
Also part of: #3587

@domenic I'm not very sure about the "current scope" language. Please advice.


💥 Error: Wattsi server error 💥

PR Preview failed to build. (Last tried on Jan 15, 2021, 7:58 AM UTC).

More

PR Preview relies on a number of web services to run. There seems to be an issue with the following one:

🚨 Wattsi Server - Wattsi Server is the web service used to build the WHATWG HTML spec.

🔗 Related URL

Command failed: /home/noderunner/wattsi/bin/wattsi /tmp/upload_2405d47f55e9294a597b9bb38851c94d (sha not provided) 4q79xl0f676 default /tmp/upload_2965616384b5c434d407a4dee9cb84f5

If you don't have enough information above to solve the error by yourself (or to understand to which web service the error is related to, if any), please file an issue.

Comment thread source
changed, and it cannot have a rendering context. The content of the placeholder canvas is
updated by calling the <code data-x="offscreencontext-commit">commit()</code> method of the
<code>OffscreenCanvas</code> object's rendering context.
updated on the rendering updated step of the current scope.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you trying to say here? It's updated whenever the browser chooses to update the rendering as per the event loop section? If so, maybe we should make it explicit when that happens relative to the events.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(What's also important here, does the user have to update it at that point or can they skip canvas updates while still performing layout?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to say: whenever the browser chooses to update the rendering of the OffscreenCanvas as per the event loop section. By "make it explicit" you mean on the rendering update section?

I'm not sure what you mean by your question. Could you clarify it?

The overall idea is:

  • canvas gets transferred to an offscreenCanvas.
  • OffscreenCanvas gets sent to a Worker.
  • During the rendering update of the Worker, the current OffscreenCanvas gets redraw (updated) AND the content of the original canvas (the placeholder canvas) gets updated.

Notice that the rendering update of the main document doesn't play a part here at all.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, by making it explicit I meant having this be a step in the "update the rendering" algorithm that does the drawing.

The placeholder canvas isn't in the "main thread" document?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The placeholder canvas is, but the update comes from the rendering of the offscreencanvas associated with it. I'll update the rendering algorithm to explicit "update placeholder canvases associated with..."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no rendering update of the worker though, right? That only appears once #3677 lands. I guess this PR is blocked on that one?

Comment thread source Outdated
data-x="offscreencontext-commit">commit()</code> method of the <code>OffscreenCanvas</code>
object's rendering context.</p>
<span data-x="intrinsic dimensions">intrinsic size</span> will only be updated during the
rendering update.</p>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is wrong here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@annevk

annevk commented Aug 10, 2018

Copy link
Copy Markdown
Member

So the placeholder canvas, which is on the main thread, gets updated based on the rendering update timing in the worker thread?

Is that not problematic for developers that it's not synchronized with the main thread's rendering updates? What if they have an animation going on as well?

@fserb

fserb commented Aug 15, 2018

Copy link
Copy Markdown
Contributor Author

Your first phrase is correct.

There's no expectation of synchronization between the main thread and the worker thread's rendering/animation. That's a feature of having OffscreenCanvas on a worker: if the main page is blocked/slow/hanging on something, the animation driven by OffscreenCanvas stays as smooth as the browser's rendering can make it.

They are supposed to be time-independent and not synchronizable (at least not easily animation synchronizable).

@domenic domenic added do not merge yet Pull request must not be merged per rationale in comment topic: canvas Canvas, OffscreenCanvas, the 2D and bitmaprenderer contexts, and ImageBitmap labels Aug 23, 2018
@domenic

domenic commented Aug 23, 2018

Copy link
Copy Markdown
Member

Marking as "do not merge yet" as this seems to be blocked on #3677 which defines the concept of "rendering updates" for workers. Once that lands we can fix up the phrasing. Otherwise the patch seems good.

@fserb

fserb commented Aug 27, 2018

Copy link
Copy Markdown
Contributor Author

Rebaselined since #3677 was pushed.

@annevk

annevk commented Aug 28, 2018

Copy link
Copy Markdown
Member

@fserb thanks, but it seems you still haven't made the relationship explicit. It also relies on an undefined term "scope". Basically, the specification needs to define everything from first principles.

@domenic

domenic commented Nov 9, 2018

Copy link
Copy Markdown
Member

@fserb to get this moving again, replace

updated on the rendering updated step of the current scope

and

as part of the rendering update of the scope containing the OffscreenCanvas.

and

updated during the rendering update.

all with

with

updated as part of the event loop's [update the rendering] step

@greggman

Copy link
Copy Markdown

Anything holding this up? As it is the spec shows incorrect info which has bled over to MDN and mis-informing developers.

example 1

example 2

example 3

@domenic

domenic commented Jan 26, 2023

Copy link
Copy Markdown
Member

If you'd like to take over the PR and address the comments and merge conflicts, I think that would be great. I suspect @fserb has become busy.

@Kaiido

Kaiido commented Jan 26, 2023

Copy link
Copy Markdown
Member

I'd like to note that there are some edge cases that are being made weird when relying only on requestAnimationFrame (rAF) to commit to the placeholder canvas.

Example 1 and related: That user wanted to record the stream from a placeholder canvas, on which they draw from a Worker. captureStream() can only be called from the main thread, but they didn't need to actually present the canvas in the page, nor did they really need their drawing frequency to be synced with the monitor's refresh rate, and thus bound to rAF. However in these circumstances, the new drawings were never committed to the placeholder canvas. ctx.commit() would have solved this use case (arguably a pulling MediaStream too).

Example 2: That user did create a Worker only to render one image and then killed the Worker as soon as it performed the drawing operations. This caused an issue where the drawings weren't committed to the placeholder canvas yet, because rAF didn't fire before the Worker got terminated. I know creating a Worker for just one drawing is a very bad design, but that same issue could have happened even in more usual circumstances, if the user doesn't wait for after the next painting frame before terminating the Worker. (Somehow Firefox's current implementation seems to be able to handle that one, I'm curious how it is done).

I'm not saying we should absolutely keep commit() around just for these edge cases but it seems we should at least handle them in some specs words.

@greggman

Copy link
Copy Markdown

Example 1: How does that compare to video? To me, canvas from a worker is similar to video. You don't get much synchronization by default. To sync the main thread with canvas work from a worker AFAIK the solution is supposed to be that you call transferToImageBitmap and then postMessage that imagebitmap back to the main thread. The main thread then applies that to an ImageBitmapRenderingContext exactly when it wants to sync that particular output from the worker with things in the main page.

Example 2: sounds basically the same. If you want to synchronize, transfer an imagebitmap.

It's not clear how commit would solve either of these issues. There seems to be an assumption that if commit existed it would magically sync. If you call commit in the worker and then postMessage back to the main page, and in the main page, use the canvas that was committed it would magically sync but suspect it's not that simple.

@Kaiido

Kaiido commented Jan 26, 2023

Copy link
Copy Markdown
Member

There seems to be an assumption that if commit existed it would magically sync.

Yes that was the purpose of commit(), to synchronously grab the current image on the context so that it can be painted on the placeholder canvas. Which makes it indeed a bit of a shorthand for the long and tedious postMessage(transferToImageBitmap()); in another thread transferFromImageBitmap(evt.data.bitmap), with the added benefit that it shouldn't require the main thread to be free.

How does that compare to video?

This is mainly irrelevant. The point is not about having anything synced to some timer, the point is that the image never ever reaches the placeholder canvas nor the captured stream.

Example 2: sounds basically the same. If you want to synchronize, transfer an imagebitmap.

They don't want to synchronize, they want their drawings to be rendered on their placeholder canvas.

Using a bitmaprender might workaround these issues, just like waiting for requestAnimationFrame, but why should we go through such a complicated path? And anyway, the behavior using a placeholder canvas is not interoperable currently, it ought to at least make sense and be handled by the specs.

@greggman

Copy link
Copy Markdown

There seems to be an assumption that if commit existed it would magically sync.

Yes that was the purpose of commit(), to synchronously grab the current image on the context so that it can be painted on the placeholder canvas. Which makes it indeed a bit of a shorthand for the long and tedious postMessage(transferToImageBitmap()); in another thread transferFromImageBitmap(evt.data.bitmap), with the added benefit that it shouldn't require the main thread to be free.

How does that compare to video?

This is mainly irrelevant. The point is not about having anything synced to some timer, the point is that the image never ever reaches the placeholder canvas nor the captured stream.

Example 2: sounds basically the same. If you want to synchronize, transfer an imagebitmap.

They don't want to synchronize, they want their drawings to be rendered on their placeholder canvas.

Using a bitmaprender might workaround these issues, just like waiting for requestAnimationFrame, but why should we go through such a complicated path? And anyway, the behavior using a placeholder canvas is not interoperable currently, it ought to at least make sense and be handled by the specs.

All of that just sounds like bugs in the browser. You shouldn't need commit to solve this. commit provides nothing that wouldn't happen anyway by just exiting the current JavaScript event. That's what happens now. You exit the event and an implicit commit happens. If things were not working correctly that's a bug that should be filed (and tests added to the appropriate places like say the WPT)

A similar issue has come up in WebGPU where people were adding calls to onSubmittedWorkDone before doing things like sampling the canvas or passing it to videoframe but those were just bugs. The bugs were fixed and there's no reason to call onSubmittedWorkDone. The same should be true of here. No reason for commit

The difference between commit and transferToImageBitmap is you actually get to choose when to sync the frame with transferToImageBitmap. With commit (and implicit commit) you don't. For example if you render something or change some HTML or CSS in the main thread and want to sync changes to the canvas from a worker, commit (nor implicit commit) will never do that.

@Kaiido

Kaiido commented Jan 26, 2023 •

Copy link
Copy Markdown
Member

You exit the event and an implicit commit happens

I don't think this is true. Currently you need to wait for a rendering opportunity for this commit to happen. In current specs texts for the Worker event loop it's if "the user agent believes that it would benefit from having its rendering updated at this time". But terminating a worker also disregards all queued tasks*, so calling drawOnCtx(); self.close() would kill the Worker before it gets a chance to execute the implicit commit that's part of the "update the rendering" steps. *(Though to be fair it doesn't explicitly talk about animation frame callbacks, but I'm quite confident they are to be included).
The point is that, in this PR, we need to handle these edge cases that commit could handle but that the current model doesn't. This might be by forcing the UA to enter the "update the rendering" steps of the Worker's event loop when it's being terminated, this might be another solution, but it's needed.

@greggman

Copy link
Copy Markdown

This is the wrong place to address this. Each type of context has it's own needs. If CanvasRenderingContext2D has issues here those issues need to be addressed in the CanvasRenderingContext2D part of the spec, not the HTMLCanvasElement part of the spec.

As one solution, you could make it so that all CanvasRenderingContext2D commands that affect the contents of the canvas "if not already queued, queue a task/microtask to update the canvas". That task would hold references to whatever it needs to make sure the canvas gets updated even if the worker exits, the same way a message send from a worker that closes is still delivered.

@Kaiido

Kaiido commented Jan 26, 2023

Copy link
Copy Markdown
Member

I'll have to disagree, here seems like the exact place we need to handle that.
This PR changes where the placeholder content gets updated to be on the "update the rendering" steps of the OffscreenCanvas context. As currently written, this "update the rendering" step can be missed when closing the Worker.
This is not a 2D thing, this is what the changes in this PR do, and this concerns any context type since this concerns the placeholder canvas.

For the closing Worker case, queuing a task doesn't work, tasks are disregarded. But I guess it could be as simple as adding a clause "or if the closing flag is set to true" when determining if this update the rendering step should be ran, though maybe it needs to be more complex to avoid the animation frame callbacks. But I'm not even sure that would be correct either.

@greggman

greggman commented Jan 27, 2023 •

Copy link
Copy Markdown

It is not the place to handle it. You can not go forcing an API call across contexts. CanvasRenderingContext2D, WebGLRendingingContext, WebGL2RenderingContext, OffscreenCanvasRenderingContext2D, WebGPURenderingContext, ImageBitmapRenderingContext. They are all disparte APIs. At this level you don't know what implications will be there for the other APIs. For example what if the user calls commit 10 times in a row, 10 times in a row with draw calls between each both of those would make zero sense in WebGPU and all the issues around how to handle it would require a bunch of specification unrelated to any other context. So no, it does not belong here.

Also tasks may not run after close but microtasks do

@Kaiido

Kaiido commented Jan 27, 2023

Copy link
Copy Markdown
Member

Did you read the changes made in that PR? They are about when to update the content of the placeholder canvas, so that it happens in the OffscreenCanvas's scope's "update the rendering" step. That concerns the placeholder canvas, the one that stays in the main thread.
My proposed change doesn't affect much of your concerns, this PR already asks that this content be updated in this "update the rendering" step. If you think this is bad, then we have a bigger issue.

@Kaiido

Kaiido commented Dec 6, 2023

Copy link
Copy Markdown
Member

I've got a fix ready for this, but I can't push a new commit on top of this PR.
What's the proper etiquette here? Should I open a new PR or can someone give me write access to this PR?

@domenic

domenic commented Dec 6, 2023

Copy link
Copy Markdown
Member

A new PR is probably the right way to do this, given that it's so old :)

Kaiido added a commit to Kaiido/html that referenced this pull request Dec 6, 2023
Takes over whatwg#3872
This removes the `OffscreenCanvas.commit()` method which has became obsoleted by whatwg#3677

This mainly handles the review comments left on the original PR.
Note that I "hide" the merge-commit to make it clear what's changed from current upstream.
@domenic domenic closed this in 95cb883 Apr 18, 2024
lozy219 pushed a commit to lozy219/html that referenced this pull request Mar 10, 2025
This goes beyond whatwg#3872 and removes the OffscreenCanvas contexts' commit(), method which has been superseded by requestAnimationFrame() since whatwg#3677 and is thus not strictly necessary anymore and might conflict with this new model.

Closes whatwg#3872.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: canvas Canvas, OffscreenCanvas, the 2D and bitmaprenderer contexts, and ImageBitmap

Development

Successfully merging this pull request may close these issues.

5 participants