Skip to content

Delegate canvas operations to main thread - #8185

Merged
kripken merged 13 commits into
emscripten-core:incomingfrom
VirtualTim:patch-2
Mar 8, 2019
Merged

kripken merged 13 commits into
emscripten-core:incomingfrom
VirtualTim:patch-2

Conversation

@VirtualTim

Copy link
Copy Markdown
Collaborator

This should fix canvas operations (ie emscripten_set_canvas_element_size) working on a pthread.
Previously this was throwing ReferenceError: document is not defined.

__findCanvasEventTarget will throw when called from a thread.
@VirtualTim

Copy link
Copy Markdown
Collaborator Author

I'm not sure if that fixes all the pthread/html5 issues, so I'll have a bit more of a look before taking this out of draft.

@VirtualTim

Copy link
Copy Markdown
Collaborator Author

Formatting change should change lines like

  function _emscripten_set_canvas_element_size_main_thread(target, width, height) {
  if (ENVIRONMENT_IS_PTHREAD) return _emscripten_proxy_to_main_thread_js(20, 1, target, width, height); return _emscripten_set_canvas_element_size_calling_thread(target, width, height); }function _emscripten_set_canvas_element_size(target, width, height) {
      var canvas = __findCanvasEventTarget(target);
      if (canvas) return _emscripten_set_canvas_element_size_calling_thread(target, width, height);
      else return _emscripten_set_canvas_element_size_main_thread(target, width, height);
    }

which have misleading indentation/whitespace to

  function _emscripten_set_canvas_element_size_main_thread(target, width, height) {
  if (ENVIRONMENT_IS_PTHREAD) return _emscripten_proxy_to_main_thread_js(20, 1, target, width, height);
   return _emscripten_set_canvas_element_size_calling_thread(target, width, height);
  }function _emscripten_set_canvas_element_size(target, width, height) {
      if (!ENVIRONMENT_IS_PTHREAD)
        var canvas = __findCanvasEventTarget(target);
      if (canvas)
        return _emscripten_set_canvas_element_size_calling_thread(target, width, height);
      else
        return _emscripten_set_canvas_element_size_main_thread(target, width, height);
    }

Still not perfect, but in my opinion a lot more readable.

@VirtualTim

VirtualTim commented Feb 27, 2019 •

Copy link
Copy Markdown
Collaborator Author

OK, I figured out what's going on with the failing test. emscripten_pthread_attr_settransferredcanvases can be used to transfer control of the canvas to a thread, so calls shouldn't be proxied in that case. It's definitely still a bug that's happening, I just need to adjust my fix. I'll do that tomorrow.

@kripken kripken 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.

Thanks for the PR!

optimally @juj should review this. But perhaps we should add a check on if something is in a pthread inside _findCanvasEventTarget? (return null if pthread) that way all methods calling that would just work, it seems?

We should also add testing for this.

Comment thread src/library_html5.js Resolved
Comment thread src/library_html5.js Resolved
Comment thread src/library_html5.js Outdated
if (canvas) return _emscripten_set_canvas_element_size_calling_thread(target, width, height);
else return _emscripten_set_canvas_element_size_main_thread(target, width, height);
if (!ENVIRONMENT_IS_PTHREAD)
var canvas = __findCanvasEventTarget(target);

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.

var for the canvas should be outside the if, for clarity (as it's used later down)

The pthreads check was a bad idea as threads can be given control over the canvas.
Resizing a canvas using PROXY_TO_PTHREAD will throw. This should stop that.
@VirtualTim

Copy link
Copy Markdown
Collaborator Author

I'll add a test for this tomorrow/Monday (assuming all the tests pass).

@kripken

kripken commented Mar 1, 2019

Copy link
Copy Markdown
Member

Thanks, looks good aside from the indentation issue, and adding testing.

Apparently the correct way is to address this in jsifier.js
var domElement = __specialEventTargets[target] || document.querySelector(__maybeCStringToJsString(target));
changed to 
var domElement = __specialEventTargets[target] || (typeof document !== 'undefined' ? document.querySelector(__maybeCStringToJsString(target)) : undefined);
so a small increase is expected.
@VirtualTim

Copy link
Copy Markdown
Collaborator Author

I don't know why those Firefox tests are failing. They look unrelated?
I've done all I wanted here, so I'll mark it ready for review.

Also I realise I should probably add myself to the authors, but if this goes through I'll add it in a separate PR.

@VirtualTim
VirtualTim marked this pull request as ready for review March 1, 2019 06:24
Comment thread src/library_html5.js Resolved
@kripken

kripken commented Mar 6, 2019

Copy link
Copy Markdown
Member

Thanks, I think this is good (the tests are random errors). Please just fix the code size issue mentioned above, and may as well add yourself to AUTHORS in this PR while updating it.

Comment thread tests/test_other.py Resolved
@kripken

kripken commented Mar 8, 2019

Copy link
Copy Markdown
Member

Great, thanks @VirtualTim!

@kripken
kripken merged commit 26e18ad into emscripten-core:incoming Mar 8, 2019
@VirtualTim
VirtualTim deleted the patch-2 branch March 11, 2019 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants