Slide 1

Slide 1 text

WebKit in you r living room Matt Seeley, Net fl ix UI Engineer

Slide 2

Slide 2 text

‘Yes’ I do watch movies all day long* * Actually, ‘no.’ Just after a big lunch. @innerhtml

Slide 3

Slide 3 text

Native + Web

Slide 4

Slide 4 text

450+ devices in 40+ countries * As of Q4 2011

Slide 5

Slide 5 text

CPU GPU Main memory Video memory Graphics driver CPU architecture Memory bus speed Network stack Devices capabilities vary

Slide 6

Slide 6 text

CPU GPU Main memory Video memory Graphics driver CPU architecture Memory bus speed Network stack Devices capabilities vary

Slide 7

Slide 7 text

• Device performance tip s • WebKit rendering basics • Accelerated compositing Topics:

Slide 8

Slide 8 text

Respond to user input

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Gallery

Slide 11

Slide 11 text

Bob

Slide 12

Slide 12 text

Video Input comes at various speeds

Slide 13

Slide 13 text

Video Sometimes input can come too fast

Slide 14

Slide 14 text

• Recycling of previous ro w • Load data or retrieve from cach e • XMLHttpReques t • JSON.parse( ) • DOM access for each bo x • Paint the row Activating a row could b e expensive, defer it

Slide 15

Slide 15 text

Video Defer & stagger activation

Slide 16

Slide 16 text

var activateTimeout; // @private function activateSync (/* id1 [, ..., idN]*/) { // perform greedy operation } // @public function activate (ids) { if (cfg.delay) { clearTimeout(activateTimeout); activateTimeout = setTimeout(function () { activateSync.apply(null, ids.splice(0, cfg.size)); if (ids.length) { activate(ids); } }, cfg.delay); // increase delay to stagger } else { activateSync.apply(null, ids); } } Work deferral & chunking

Slide 17

Slide 17 text

Fast Slow JSON.pars e XMLHttpReques t document. * element.* Cross this divide with care mostEverythingElse

Slide 18

Slide 18 text

Minding our memory

Slide 19

Slide 19 text

Total memory - Host processes - Netflix SDK - WebKit - Network buffer - Video buffer UI budget Understand the memory requirements of your components

Slide 20

Slide 20 text

Video memory contain s uncompressed images 1920 x 1080 8.3 MB 1280 x 720 3.7 MB

Slide 21

Slide 21 text

Memory saving techniques • Avoid unbounded growt h • Minimize number of throwaway object s • display:none; may clear video memor y • Pool elements and objects

Slide 22

Slide 22 text

Video Reuse instead of allocate & destroy

Slide 23

Slide 23 text

All that glitters...

Slide 24

Slide 24 text

Don’t do i n software what can b e handled by hardware.

Slide 25

Slide 25 text

-webkit-gradien t -webkit-box-shado w background-positio n background-repea t border Static images render faste r than CSS effects

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Use CSS effects for all transitions • Only use transitions on capable device s • Declarative, easy, supports multiple transform s • Rendering engine may optimiz e • Animations which don’t require repaints are hardware accelerated, ie: 
 -webkit-transition-property: 
 opacity, -webkit-transform;

Slide 28

Slide 28 text

Open sourced, hackable, port-able

Slide 29

Slide 29 text

Browser or Toolkit Networking, Graphics, Threading , Memory management, Disk access, etc... WebCore JavascriptCore

Slide 30

Slide 30 text

WebCore DOM tree construction Painting of render tree Parsing Parsing Render tree construction Layout o f render tree Render fl ow (abbreviated)

Slide 31

Slide 31 text

Toolkit or Browser port RenderLayer Layout & Paint GraphicsContext RenderLayer, one or more RenderObjects RenderObject sub-class

Slide 32

Slide 32 text

Accelerated compositing 1

Slide 33

Slide 33 text

Compositing laye r RenderLayer(s) fl attened into a single backing surface (bitmap); sometimes mapped to a GPU texture

Slide 34

Slide 34 text

Re d A compositing layer; number tracks number of recalculations . Yello w A container layer; no backing surface - children are layers . Cya n Overflow box; no backing surface - debugging tool . Gree n Tiled layer; created when layer width or height is > 1024px. Safari’s visual hints

Slide 35

Slide 35 text

defaults write com.apple.Safari \ IncludeInternalDebugMenu 1 Enable Safari’s Debug menu

Slide 36

Slide 36 text

Enable compositing border s in Chrome

Slide 37

Slide 37 text

• opacity • overflow • -webkit-transform • z-index • positio n • Element’s box overlaps a compositing layer Layer creation triggers

Slide 38

Slide 38 text

virtual bool requiresLayer() const { return isRoot() || isPositioned() || isRelPositioned() || isTransparent() || hasOverflowClip() || hasTransform() || hasMask() || hasReflection() || style()->specifiesColumns() ; } WebCore/rendering/RenderBoxModelObject.h Explicit layer creation

Slide 39

Slide 39 text

WebCore/rendering/RenderLayerCompositor.cpp Implicit layer creation bool RenderLayerCompositor::needsToBeComposited(const RenderLayer* layer) const { // edited.. . return requiresCompositingLayer(layer) || layer->mustOverlapCompositedLayers() || (inCompositingMode() && layer->isRootLayer()) ; } bool RenderLayerCompositor::requiresCompositingLayer(const RenderLayer* layer) const { RenderObject* renderer = layer->renderer() ; // edited... return requiresCompositingForTransform(renderer) || requiresCompositingForTransformingForVideo(renderer) || requiresCompositingForCanvas(renderer) || requiresCompositingForPlugin(renderer) || requiresCompositingForFrame(renderer) || (canRender3DTransforms() && /* edited */) || clipsCompositingDescendants(layer) || requiresCompositingForAnimation(renderer) || requiresCompositingForFullScreen(renderer) ; }

Slide 40

Slide 40 text

A (short) tale of two layers Animated gif with translateZ(0); Static gif wit h keyframes animation

Slide 41

Slide 41 text

translateZ(0) == zoom:1

Slide 42

Slide 42 text

• Change opacity • Change -webkit-transform • Hide layer offscreen • Change conten t • Change CSS box propertie s • Hide text content using text-indent • Hide layer with display* Safe Caution * May release video memory

Slide 43

Slide 43 text

...
Up & down layer composition

Slide 44

Slide 44 text

1 container laye r 2 compositing layers Up & down layer composition

Slide 45

Slide 45 text

2 container layer s 7+ compositing layers Left & right layer composition

Slide 46

Slide 46 text

.listRow { -webkit-transform: translateZ(0); } .listTitle { -webkit-transform: translateZ(0); } .listItems { -webkit-transform: translate(0, 30px); } .ct-seq0 { -webkit-transform: translateX(60px); } .listRow-animate-lr .ct-seq0 { /* Use Z to punch .ct-seq# into layers */ -webkit-transform: translateX(60px) translateZ(0); }

Slide 47

Slide 47 text

• Reduce the quantity of layer s • Keep layers as small as possibl e • Update layers infrequentl y • Tailor layer composition to purpos e • Trial and error; testing is important Accelerated compositing tips

Slide 48

Slide 48 text

mseeley@net fl ix @innerhtml