CSC/ECE 517 Fall 2017/M1753 OffscreenCanvas API

From Expertiza_Wiki
Jump to navigation Jump to search

The HTML specification defines a <canvas> element that use a 2D or 3D rendering context and draw graphics. The biggest limitation of <canvas> is that all of the javascript that manipulates the <canvas> has to run on the main thread, as the applications we build increase in complexity, developers inadvertently hit the performance wall. A new specification was recently developed that defines a canvas that can be used without being associated with an in-page canvas element, allowing for more efficient rendering. The OffscreenCanvas API achieves pre-rendering that using a separate off-screen canvas to render temporary image, and then rendering the off-screen canvas back to the main thread.[1][2]

Background Introduction

Servo[3] is a modern web browser engine designed to maximize the benefits of multi-core-based parallel processing. Parallelism is undoubtedly the highlight feature of the servo, besides other characteristics like GPU rendering, embedding, modularity and memory safety are also stressed. Written in the Rust language, servo was aimed for the application on Android and ARM processors, but now its applicable scope has expanded to Linux, Mac OS X, Windows, Android and Firefox OS (also known as Gonk) and other operating systems[4].


Rust[5] is a programming language that emphasizes security, concurrency and speed. The three keywords, safety, control of memory layout, and concurrency are the core features of rust, and also the primary factors that distinguish rust from other programming languages. Rust performs most of its memory management and security checks at compile time, which makes it possible to predict the time and space requirements of the program and be embedded into low-level code , such as device driver and operating system.Rust is developed on GitHub/Git [6], and every commit to the source code is documented by the version control system.


The OffscreenCanvas API supports a canvas to be rendered off screen. The performance of a webpage can be limited by using <canvas> since all of the javascript that manipulates the <canvas> has to run on the main thread, situation is not optimistic when the canvas operations are complex. The OffscreenCanvas API provides a way to interact with the same canvas APIs but in a different thread. This allows rendering to progress no matter what is going on in the main thread [7].

Project Description

Implement the OffscreenCanvas API: Servo has an implementation of the HTML Canvas API. Our project is to implement the OffscreenCanvas API which provides a canvas that can be rendered off screen and is available in both the window and worker contexts.


Steps

  • Create the OffscreenCanvas and OffscreenCanvasRenderingContext2d interfaces.
  • Hide the new interfaces by default
  • Enable the existing automated tests for this feature
  • Implement the OffscreenCanvas constructor that creates a new canvas
  • Implement the OffscreenCanvas.getContext ignoring the WebGL requirements

Implementation

Create Interfaces

Create the OffscreenCanvas and OffscreenCanvasRenderingContext2d interfaces with stub method implementations.

  To add the interfaces, the following steps are to be followed: 
   * adding the new IDL file at components/script/dom/webidls/Foo.webidl;
   * creating components/script/dom/foo.rs;
   * listing foo.rs in components/script/dom/mod.rs;  
   * defining the DOM struct Foo with a #[dom_struct] attribute, a superclass or Reflector member, and other members as appropriate;
   * implementing the dom::bindings::codegen::Bindings::FooBindings::FooMethods trait for Foo;
   * adding/updating the match arm in create_element in components/script/dom/create.rs (only applicable to new types inheriting from HTMLElement)
  Hide newly created interfaces by default:
   * Add corresponding attribute to the interface file as well as resources/prefs.json
  Enable Tests:
   * Add 'offscreen-canvas' directory to tests folder.
   * Enable test preference for offscreen-canvas by adding '__dir__.ini' file to the newly created directory.
   * Run tests and change expected results.
  Implement OffscreenCanvas Constructor: 
    Syntax: 
      var offscreen = new OffscreenCanvas(256, 256);
   
  Implement the getContext() method for OffscreenCanvas element. 
    Syntax: 
       canvas.getContext(contextType, contextAttributes); 

Testing Details

Pull Request

Future Work

  • extract all relevant canvas operations from CanvasRenderingContext2d into an implementation shared with OffscreenCanvasRenderingContext2d
    • create a trait that abstracts away any operation that currently uses self.canvas in the 2d canvas rendering context, since the offscreen canvas rendering context has no associated <canvas> element
  • implement the convertToBlob API to allow testing the contents of the canvas
  • support offscreen webgl contexts in a similar fashion to the 2d context, by sharing relevant code between OffscreenCanvasRenderingContext2d and WebGLRenderingContext

References

1. https://html.spec.whatwg.org/multipage/canvas.html#the-offscreen-2d-rendering-context
2. https://html.spec.whatwg.org/multipage/structured-data.html#transferable
3. https://html.spec.whatwg.org/multipage/canvas.html#the-offscreencanvas-interface
4. https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
5. https://developer.mozilla.org/en-US/docs/Web/API/Comment
6. https://developer.mozilla.org/en-US/docs/Web/API/Comment/Comment
7. https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
8. https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
9. http://doc.servo.org/script/dom/index.html#adding-a-new-dom-interface