+ All Categories
Home > Software > Salt and pepper — native code in the browser Browser using Google native Client

Salt and pepper — native code in the browser Browser using Google native Client

Date post: 15-Jan-2015
Category:
Upload: mayflower-gmbh
View: 330 times
Download: 1 times
Share this document with a friend
Description:
Wie man nativen Code in Webseiten direkt einbindet. Ein Ansatz.
Popular Tags:
31
SALT AND PEPPER RUNNING NATIVE CODE IN THE BROWSER WITH NACL Christian Speckner, Mayflower GmbH
Transcript
Page 1: Salt and pepper — native code in the browser Browser using Google native Client

SALT ANDPEPPER

RUNNING NATIVE CODE IN THEBROWSER WITH NACL

Christian Speckner, Mayflower GmbH

Page 2: Salt and pepper — native code in the browser Browser using Google native Client

NATIVE CODE IN THEBROWSER?

Page 3: Salt and pepper — native code in the browser Browser using Google native Client

[BOCHS DEMO]

Page 4: Salt and pepper — native code in the browser Browser using Google native Client

SERIOUSLY THOUGH:The Web is transforming into a Platform for Application

Development!

Page 5: Salt and pepper — native code in the browser Browser using Google native Client

SO, WHY NATIVE CODE?Existing codebases: lots of tried-and-tested C / C++code out thereEfficient memory managementSpeed

Page 6: Salt and pepper — native code in the browser Browser using Google native Client

WHAT IS NACL?

Page 7: Salt and pepper — native code in the browser Browser using Google native Client

SANDBOXED EXECUTIONENVIRONMENT FOR NATIVE

CODECode is generated by dedicated toolchain (gcc based)Static code analysis and verification before executionNo access to OS or memory outside the sandboxOS functionality only via browser API

Page 8: Salt and pepper — native code in the browser Browser using Google native Client

PORTABLE NATIVE CLIENT(PNACL)

Compiles to LLVM bytecodeLLVM / clang toolchainTranslated to machine code by the browser at runtimeOnly a single, architecture independent binary

Page 9: Salt and pepper — native code in the browser Browser using Google native Client

THE BIG PICTURE

Page 10: Salt and pepper — native code in the browser Browser using Google native Client

COMPATIBILITYSO FAR ONLY CHROME :(

Supported platforms: x86, ARM, MIPSChrome on Windows, Linux, OSX, ChromeOSNo mobile systems yetNACL and PNACL for packaged appsPNACL for the web

Page 11: Salt and pepper — native code in the browser Browser using Google native Client

API

Page 12: Salt and pepper — native code in the browser Browser using Google native Client

PEPPERSimilar to the pepper plugin API (PPAPI)C and OOP C++ wrapperAsynchroneous, callback basedAccess to HTML5 filesystemVideo, including OpenGL ES 2.0AudioHTTP, TCP / UDP sockets (packaged apps only)Communication with Javascript via postMessage stylemessages

Page 13: Salt and pepper — native code in the browser Browser using Google native Client

POSIX IS AVAILABLE AS WELLVirtual filesystem

Resides in memoryHTML5 filesystem and server can be mountedEnables standard POSIX file operations

Network SocketsPOSIX threads

→ Lots of existing POSIX-complient code just compiles outof the box!

Page 14: Salt and pepper — native code in the browser Browser using Google native Client

NACL-PORTSCollection of opensource libraries and applications ported

to run on NaClBoostBulletffmpegImageMagickOpenCVSDL...

Most require only minor patches to the build system!

Page 15: Salt and pepper — native code in the browser Browser using Google native Client

HOW DOES A NACL APPLOOK LIKE?(WARNING: C++ AHEAD)

Page 16: Salt and pepper — native code in the browser Browser using Google native Client

C++: BIRD'S EYE VIEWclass Instance : public pp::Instance {// ...};

class Module : public pp::Module {// ...};

namespace pp { Module* CreateModule() { return new ::Module(); }}

Page 17: Salt and pepper — native code in the browser Browser using Google native Client

C++: MODULE CLASSclass Module : public pp::Module { public: virtual pp::Instance* CreateInstance(PP_Instance instance) { return new Instance(instance); }};

Page 18: Salt and pepper — native code in the browser Browser using Google native Client

C++: INSTANCE CLASSclass Instance : public pp::Instance { public: Instance(PP_Instance instance) : pp::Instance(instance) {}

virtual void HandleMessage(const pp::Var& msg) { std::stringstream ss; ss << "Echo: " << msg.AsString(); PostMessage(ss.str(); }};

Page 19: Salt and pepper — native code in the browser Browser using Google native Client

MANIFESTDescribes the structure of the NACL module for the

browser{ "program": { "portable": { "pnacl-translate": { "url": "demo.pexe" } } }}

Page 20: Salt and pepper — native code in the browser Browser using Google native Client

HTML<script>

</script>

<embed width=0 height=0 src="demo.nmf"type="application/x-pnacl" onload="onModuleLoad()" onmessage="onMessage()"/>

function onModuleLoad() { naclModule = document.querySelector('embed'); naclModule.addEventListener('message', function(e) { // ... }); }

function onClick() { if (naclModule) { // ... naclModule.postMessage(msg); } }

Page 21: Salt and pepper — native code in the browser Browser using Google native Client

[DEMO]

Page 22: Salt and pepper — native code in the browser Browser using Google native Client

USEFUL FOR WHAT?

Page 23: Salt and pepper — native code in the browser Browser using Google native Client

NUMBER CRUNCHINGAudio / Video processingCustom video / audio decoding & playbackImage filtering editing (Google+ photo editor!)

Page 24: Salt and pepper — native code in the browser Browser using Google native Client

PACKAGING EXITINGAPPLICATIONS

Browser gamesChrome store packaged appsChromeOS / Chromebooks!

Page 25: Salt and pepper — native code in the browser Browser using Google native Client

COMPARISION TOASM.JS / EMSCRIPTEN

Page 26: Salt and pepper — native code in the browser Browser using Google native Client

ASM.JSBytecode-like subset of JS

JIT can optimize very wellStatic type informationNo garbage collection

Firefox has a dedicated JIT codepath: Odinmonkey

Page 27: Salt and pepper — native code in the browser Browser using Google native Client

Compiles C / C++ to Asm.js via LLVM / clang

EMSCRIPTEN

Page 28: Salt and pepper — native code in the browser Browser using Google native Client

Compilation to native codeNaCl: Ahead-of-timeAsm.js: Just-in-time

PerformanceNaCl: Almost native (Google: 80%-90%), predictableAsm.js: 2x - 3x native, highly dependent on browser

ThreadsNaCl: full POSIX threadsAsm.js: No threading

CompatibilityNaCl: Just Chrome atmAsm.js: Everything that runs JS (performance varies)

Page 29: Salt and pepper — native code in the browser Browser using Google native Client

PEPPER.JSImplements Pepper API in Javascript

Allows to compile applications for both NaCl and JS viaEmscriptenCan be used to target both NaCl and other browsers viaAsm.jsObviously still no threads :(

Page 30: Salt and pepper — native code in the browser Browser using Google native Client

WRAP-UPPowerful technologyRuns existing C / C++ code almost unmodified in thebrowserSupports threadingUseful for both background processing and visibleinteractionPerfect package existing applicatons as Chrome appsCurrently only Chrome :(

Page 31: Salt and pepper — native code in the browser Browser using Google native Client

THAT'S ALL —

THANK YOU FOR YOURATTENTION!


Recommended