Skip to content

Synchronize WebGL contexts with page rendering - #21841

Closed
jdm wants to merge 3 commits into
servo:masterfrom
jdm:webgl-sync
Closed

jdm wants to merge 3 commits into
servo:masterfrom
jdm:webgl-sync

Conversation

@jdm

@jdm jdm commented Sep 29, 2018 •

Copy link
Copy Markdown
Member

The code that deals with a integrating a shared GL context with the display list that Webrender receives doesn't work right now. Webrender has an API that allows locking the context, but the period between the end of layout (when script starts running again) and when WR actually processes the display list means that the GL context can be modified so that what WR sees is not what was supposed to be composited. This change marks GL contexts for WebGL canvases as locked as soon as they are processed by layout, causing any future WebGL commands that affect the to context to be buffered and replayed once WebRender unlocks the GL context.



This change is Reviewable

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/webglrenderingcontext.rs, components/script/dom/node.rs, components/script/dom/webgl2renderingcontext.rs, components/script/dom/htmlcanvaselement.rs
  • @KiChjang: components/script/dom/webglrenderingcontext.rs, components/script/dom/node.rs, components/script/dom/webgl2renderingcontext.rs, components/script/dom/htmlcanvaselement.rs
  • @emilio: components/layout/construct.rs, components/layout/context.rs

@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!
  • These commits modify layout and script code, but no tests are modified. Please consider adding a test!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Sep 29, 2018
@jdm

jdm commented Sep 29, 2018

Copy link
Copy Markdown
Member Author

@bors-servo try=wpt

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Trying commit f693335 with merge c0c8602...

bors-servo pushed a commit that referenced this pull request Sep 29, 2018
Synchronize WebGL contexts with page rendering

The code that deals with a integrating a shared GL context with the display list that Webrender receives doesn't work right now. Webrender has an API that allows locking the context, but the period between the end of layout (when script starts running again) and when WR actually processes the display list means that the GL context can be modified so that what WR sees is not what was supposed to be composited. This change marks GL contexts for WebGL canvases as locked as soon as they are processed by layout, causing any future WebGL commands that affect the to context to be buffered and replayed once WebRender unlocks the GL context.

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #21838
- [x] These changes do not require tests because it's nondeterministic behaviour.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21841)
<!-- Reviewable:end -->
@jdm

jdm commented Sep 29, 2018

Copy link
Copy Markdown
Member Author

r? @emilio
Let me know if this is more than you want to deal with and I can find somebody else.

@highfive highfive assigned emilio and unassigned avadacatavra Sep 29, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-rel-wpt

@highfive highfive added the S-tests-failed The changes caused existing tests to fail. label Sep 29, 2018

@emilio emilio left a comment

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.

np, I think I can review this :)

Some comment below, I think it looks reasonable, though I think I need to re-page the locking / unlocking scheme is supposed to work to see if this is sound. We can probably take advantage that we're going to see us in person on Monday to double-check this :)

Thanks!

Comment thread components/canvas/webgl_thread.rs Outdated

/// Handles a request to prevent a GL context from being modified until further notice.
fn handle_prevent_modification(&mut self, context_id: WebGLContextId) {
if !self.buffered_contexts.contains_key(&context_id) {

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.

nit: This can use entry instead to avoid two hash table lookups.

Comment thread components/canvas/webgl_thread.rs Outdated
let target_id = match msg {
WebGLMsg::CreateContext(..) |
WebGLMsg::Lock(..) |
WebGLMsg::Unlock(..) |

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.

We should probably assert that we don't receive lock messages while it's unlocked, or similar. Otherwise the code below in handle_unlock won't work. Though I think this is a pre-existing issue.

Comment thread components/canvas/webgl_thread.rs Outdated
{
let data = Self::make_current_if_needed(context_id, &self.contexts, &mut self.bound_context_id)
.expect("WebGLContext not found in a WebGLMsg::Unlock message");
let info = self.cached_context_info.get_mut(&context_id).unwrap();

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.

nit: while you're touching this, using the indexing operator may give a nicer error message if it fails.

Comment thread components/canvas/webgl_thread.rs Outdated
/// Texture ids and sizes used in DOM to texture outputs.
dom_outputs: FnvHashMap<webrender_api::PipelineId, DOMToTextureData>,
/// Tracking GL contexts that must not be modified but can still be
/// targeted by WebGL commands.

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.

This should document that this map only has keys for contexts that are currently locked, and for which a PreventModification message has been sent.

Comment thread components/layout_thread/lib.rs Outdated
guards.clone(),
true,
data.reflow_goal.needs_display(),
&map);

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.

nit: paren on the next line.

Comment thread components/layout_thread/lib.rs Outdated
guards,
false,
goal.needs_display(),
&snapshots

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.

nit: I think rustfmt would add a trailing comma here.

Comment thread components/canvas/webgl_thread.rs Outdated
}

/// Handles a request to prevent a GL context from being modified until further notice.
fn handle_prevent_modification(&mut self, context_id: WebGLContextId) {

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.

Should this assert that the context is locked? Otherwise there's no guarantee that the context would be unlocked, right?

@emilio

emilio commented Oct 1, 2018

Copy link
Copy Markdown
Member

Another thing to check is whether WebRender is guaranteed to give us an unlock() message. I'd expect it to not do so if the image is culled (because the element is off-screen for example).

@MortimerGoro

Copy link
Copy Markdown
Contributor

As an optimization we could start the msg buffering only when a Draw operation arrives (e.g. glDrawElements, glDrawArrays or glClear). It should be safe to run other methods.

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #21543) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added the S-needs-rebase There are merge conflict errors. label Oct 9, 2018
@jdm

jdm commented Oct 23, 2018

Copy link
Copy Markdown
Member Author

The concern about webrender only calling lock/unlock when the canvas is rendered onscreen is real. Firefox uses a queue of shared textures (instead of one shared context) to ensure that it can continue performing webgl operations while webrender will always see consistent texture contents. To avoid completely rewriting our webgl setup, I'm going to try delaying all webgl command execution until the webrender image handler lock is called; the webgl thread can buffer all commands and group them by frames. For a canvas with preserveDrawingBuffer: false, we can discard all buffered frames except the most recent complete one, otherwise we will need to execute all of them.

@jdm

jdm commented Nov 9, 2018

Copy link
Copy Markdown
Member Author

This runs into problems where JS will block on compositing. I'm going to try using textures like Gecko instead of a single offscreen framebuffer so we can just tell webrender "this is the frame; draw it" and keep working on a separate texture.

@MortimerGoro

Copy link
Copy Markdown
Contributor

That's a good idea. We are also going to need render to texture (SurfaceTexture) for WebVR integration with FxR.

jdm added 3 commits November 22, 2018 10:28
…ed dirty.

Refactors the WebGL thread and canvas rendering context to reflect the fact that
WebRender image keys are immutable. Any readback that is necessary occurs right
before the draw buffer is swapped.
@jdm

jdm commented Nov 22, 2018

Copy link
Copy Markdown
Member Author

Huh. In a release build on my pixel 2, there is way more flickering with these changes than without them. That's... unexpected.

@jdm jdm added the S-blocked-on-external Something, somewhere else, needs to happen before this PR can be merged. label Nov 22, 2018
@jdm jdm removed the S-needs-rebase There are merge conflict errors. label Nov 22, 2018
@jdm

jdm commented Nov 23, 2018

Copy link
Copy Markdown
Member Author

This is definitely a huge improvement in debug builds, at least.

@jdm

jdm commented Nov 26, 2018

Copy link
Copy Markdown
Member Author

@bors-servo try=wpt

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Trying commit 3c229f1 with merge 40dec74...

bors-servo pushed a commit that referenced this pull request Nov 26, 2018
Synchronize WebGL contexts with page rendering

The code that deals with a integrating a shared GL context with the display list that Webrender receives doesn't work right now. Webrender has an API that allows locking the context, but the period between the end of layout (when script starts running again) and when WR actually processes the display list means that the GL context can be modified so that what WR sees is not what was supposed to be composited. This change marks GL contexts for WebGL canvases as locked as soon as they are processed by layout, causing any future WebGL commands that affect the to context to be buffered and replayed once WebRender unlocks the GL context.

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #21838
- [x] These changes do not require tests because it's nondeterministic behaviour.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21841)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-rel-css

@highfive highfive added the S-tests-failed The changes caused existing tests to fail. label Nov 26, 2018
@jdm

jdm commented Nov 26, 2018

Copy link
Copy Markdown
Member Author

Current theory about the pixel 2 brokenness:

  1. WR locks the external image and fetches the texture id for the complete frame
  2. the canvas thread processes the lock message and returns the current texture id for the complete frame
  3. the canvas thread receives a message to present the current frame and swaps the textures, resetting the previously complete texture to black
  4. WR draws the texture id that has now been reset to black

More specifically, the locking mechanism currently implemented is broken by the switch to use multiple textures. The swapping code should only run when the texture is unlocked; otherwise we should just reset the current frame and skip displaying it, I suppose.

@jdm

jdm commented Nov 29, 2018

Copy link
Copy Markdown
Member Author

The flickering is not helped by dealing with the issue described in the previous comment. It is helped by commenting out the call to reset_draw_buffer_contents in servo/surfman#131.

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #22385) made this pull request unmergeable. Please resolve the merge conflicts.

@jdm

jdm commented Nov 7, 2019

Copy link
Copy Markdown
Member Author

No longer necessary with the introduction of #24482!

@jdm jdm closed this Nov 7, 2019
@jdm
jdm deleted the webgl-sync branch November 7, 2019 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed. S-blocked-on-external Something, somewhere else, needs to happen before this PR can be merged. S-needs-rebase There are merge conflict errors. S-tests-failed The changes caused existing tests to fail.

Projects

Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

WebGL shared contexts and Webrender aren't synchronized

6 participants