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.<ref>https://www.html5rocks.com/en/tutorials/canvas/performance/][https://github.com/servo/servo/wiki/Offscreen-canvas-project</ref>

Background Introduction

Servo[1] 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[2].


Rust[3] 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 [4], 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 [5]<ref>https://hacks.mozilla.org/2016/01/webgl-off-the-main-thread/</ref>.

Project Description

Implement the OffscreenCanvas API: Servo has an implementation of the HTML Canvas API, we need to port this API to OffscreenCanvas. This API will provide a canvas that can be rendered off screen and will be available in both the window and worker contexts. The rendering will be done in a separate thread which makes the overall rendering of the webpage quicker.

Implement the OffscreenCanvasRenderingContext2D: Servo has an implementation of the CanvasRendering2D API, we need to port this to OffscreenCanvasRenderingContext2D. This interface will be used for drawing rectangles, text, images and other objects onto the canvas element. It provides the 2D rendering context for the drawing surface of a <canvas> element.

Steps:

  1. Create the OffscreenCanvas and OffscreenCanvasRenderingContext2d interfaces.
  2. Hide the new interfaces by default
  3. Enable the existing automated tests for this feature
  4. Implement the OffscreenCanvas constructor that creates a new canvas
  5. 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

  • Building

Servo is built with the Rust package manager, Cargo.

The following commands are steps to built servo in development mode, please note the resulting binary is very slow.

git clone https://github.com/jitendra29/servo.git

cd servo

./mach build --dev

./mach run tests/html/about-mozilla.html

  • Testing

Servo provides automated tests, adding the --release flag to create an optimized build:

./mach build --release

./mach run --release tests/html/about-mozilla.html

Notes:

  1. We have not added any new tests to the test suite as servo follows TDD and tests were previously written for OffscreenCanvas. We are adjusting some of the test expectations for the tests to pass our implementation.
  2. Our project cannot be tested from UI since it is basically improving some javascript features (OffsereenCanvas) in servo. However you can check that it doesn't break the existing code and the browser runs correctly by running a test page on servo after performing the build as mentioned above.

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

<references/>