Slide 1

Slide 1 text

WebKit Rendering Overview for HTML5 Developer @ariyahidayat San Francisco, April 2, 2013 1

Slide 2

Slide 2 text

whoami 2

Slide 3

Slide 3 text

WebKit 3

Slide 4

Slide 4 text

WebKit is Everywhere 4

Slide 5

Slide 5 text

KHTML, Apple, and Community 0 50000 100000 150000 0 3 6 9 12 Revisions Years ~3000 commits/month http://ariya.ofilabs.com/2011/11/one-hundred-thousand-and-counting.html 5

Slide 6

Slide 6 text

Heavy on TDD Source Code Tests Repository Checkout 6

Slide 7

Slide 7 text

Involvement: Social Layers Contributor Committer Reviewer accept or reject patches checks in reviewed patches after 10-20 patches after 80 patches 7

Slide 8

Slide 8 text

Big Players http://blog.bitergia.com/2013/02/06/report-on-the-activity-of-companies-in-the-webkit-project/ http://blog.bitergia.com/2013/03/01/reviewers-and-companies-in-webkit-project/ 8

Slide 9

Slide 9 text

Browser at a High Level User Interface Browser Engine Graphics Stack Data Persistence Render Engine JavaScript Engine Networking I/O http://www.html5rocks.com/en/tutorials/internals/howbrowserswork 9

Slide 10

Slide 10 text

WebKit Components Render Engine WebCore JavaScript Engine (JSC/V8) Client Interface HTML DOM CSS SVG Canvas 10

Slide 11

Slide 11 text

Platform Abstraction Client Interface Networking I/O Graphics Theme Events Clipboard Thread Geolocation Timer API Calls Events Port (Chrome, Safari, etc.) http://ariya.ofilabs.com/2011/06/your-webkit-port-is-special-just-like-every-other-port.html 11

Slide 12

Slide 12 text

Loader & Parser 12

Slide 13

Slide 13 text

From Contents to Pixels HTML Parser DOM CSS Parser HTML Style Sheets DOM Tree Style Rules Render Tree Render Layout Paint http://www.html5rocks.com/en/tutorials/internals/howbrowserswork 13

Slide 14

Slide 14 text

Loader: Resource, Document, Frame, Page https://www.webkit.org/blog/1188/how-webkit-loads-a-web-page/ 14

Slide 15

Slide 15 text

Browser Caches http://gent.ilcore.com/2011/02/chromes-10-caches.html HTTP Disk Cache HTTP Memory Cache DNS Host Cache Preconnect Domain Cache V8 Compilation Cache SSL Session Cache TCP Connections Cookies HTML5 Caches .... 15

Slide 16

Slide 16 text

HTML Tokenizer http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#tokenization ~70 states

Hello

tag text 16

Slide 17

Slide 17 text

HTML Parser https://www.webkit.org/blog/1273/the-html5-parsing-algorithm/ ~10,000 lines of code Work in progress: Threaded HTML Parser Tokenizer Tree Builder DOM Tree

Hello

tag text 17

Slide 18

Slide 18 text

Preloader Scanner Schedule concurrent loading of resources CSS @import HTML , <img> //ajax.googleapis.com/ajax/libs/ext-core/3/ext-core.js https://use.typekit.com/bqo8qzg.js /img/sencha-large.png /css/carousel.css /js/carousel.js http://cdn.sencha.io/img/home/promos/promos-20130328-bundle-webinar.png http://cdn.sencha.io/img/home/20130219-promo-senchacon.png http://cdn.sencha.io/img/home/december-2012/icon-news.png http://cdn.sencha.io/img/home/december-2012/icon-blog.png http://cdn.sencha.io/img/20130317-mz-pivot-grid.png http://cdn.sencha.io/img/20120607-whats-new-io.png http://cdn.sencha.io/img/20130317-development-enterprise.png http://cdn.sencha.io/img/home/december-2012/icon-twitter.png http://cdn.sencha.io/img/home/december-2012/customer-logos.png //platform.twitter.com/widgets.js /forum/clientscript/vbulletin_md5.js?v=403 /min/?g=main-js&1362779803&debug=1 sencha.com 18

Slide 19

Slide 19 text

Layout 19

Slide 20

Slide 20 text

DOM Tree

Hello

HTMLDocument HTMLBodyElement HTMLParagraphElement attributes, children, contents 20

Slide 21

Slide 21 text

DOM Tree vs Render Tree There is no Render* for display:none HTMLDocument HTMLBodyElement HTMLParagraphElement RenderRoot RenderBody RenderText tree structure/navigation metrics (box model) hit testing RenderStyle computed style many:1 21

Slide 22

Slide 22 text

“Attach” Process Historical: Node is invisible, attach it to the main view to make it visible
42
View RenderObject RenderStyle tree structure/navigation metrics (box model) hit testing computed style Node/Element 1:1 many:1 another tree structure 22

Slide 23

Slide 23 text

It’s a forest! 23

Slide 24

Slide 24 text

http://www.webkit.org/coding/dom-element-attach.html 24

Slide 25

Slide 25 text

Style Recalc vs Layout • RecalcStyle refers to style resolution, i.e. solving style properties for each node. • Layout needs style information, e.g. the computed dimension defines the box geometry. • Layout will be deferred if there is painting in progress (not completed yet). • Layout is on-demand: triggered by style change. More complication when loading data data data data RecalcStyle RecalcStyle Layout Paint RecalcStyle Layout Paint preorder traversal might be incremental RenderObject specific 25

Slide 26

Slide 26 text

Effects of Style Recalc and Layout document.getElementById('box').style.top = '100px'; document.getElementById('box').style.backgroundColor = 'red'; Aggregated “dirty” area No layout necessary, metrics are not flagged as “changed” 26

Slide 27

Slide 27 text

Getting the Right Style HTMLDocument HTMLBodyElement HTMLParagraphElement DOM Tree p { color: blue } Stylesheet List RenderStyle CSS Style Selector: id, class, tags, ... 27

Slide 28

Slide 28 text

Minimizing Layout http://gent.ilcore.com/2011/03/how-not-to-trigger-layout-in-webkit.html http://code.google.com/p/chromium/source/search?q=%22-%3EupdateLayoutIgnorePendingStylesheets()%22 clientHeight clientLeft clientTop clientWidth offsetHeight offsetLeft offsetParent offsetTop offsetWidth scrollLeft scrollTop scrollWidth innerText outerText Element focus() getBoundingClientRect() getClientRects() scrollByLines() scrollByPages() scrollHeight scrollIntoView() scrollIntoViewIfNeeded() 28

Slide 29

Slide 29 text

Painting 29

Slide 30

Slide 30 text

Goals of Painting • “Visualize” all RenderObjects (again, tree traversal) • All painting commands go to GraphicsContext abstraction • Important • Clipped to the visible viewport • Back-to-front Optimizations Transparency & clipping 30

Slide 31

Slide 31 text

Graphics Abstraction Mac & iOS Chromium & Android Qt CoreGraphics Skia QPainter graphics stack GraphicsContext http://ariya.ofilabs.com/2011/06/your-webkit-port-is-special-just-like-every-other-port.html Gtk+ Cairo 31

Slide 32

Slide 32 text

CSS Painting: 10 Stages http://www.w3.org/TR/CSS2/zindex.html Appendix E. Elaborate description of Stacking Contexts • Background • Floats • Foreground • Selection • Outline Done by the render objects (in the render tree) 32

Slide 33

Slide 33 text

Painting Flow: Browser vs WebKit WebKit Browser Request painting, GraphicsContext User scrolls the viewport Style change Mark “dirty” area Sometimes to the backing store 33

Slide 34

Slide 34 text

Path is Everything 34

Slide 35

Slide 35 text

Stroke and Brush 35

Slide 36

Slide 36 text

Painting Complexity Polygon Multiple levels of transparency 36

Slide 37

Slide 37 text

Transformation Scaling Rotation Perspective 37

Slide 38

Slide 38 text

Hardware Acceleration 38

Slide 39

Slide 39 text

translate3d 39

Slide 40

Slide 40 text

SoC = System-on-a-Chip CPU GPU 40

Slide 41

Slide 41 text

Graphics Processing Unit “Fixed” geometry Transformation Textured triangles Parallelism 41

Slide 42

Slide 42 text

GPU Workflow Vertices Rendered Textured Matrix 42

Slide 43

Slide 43 text

Efficient Uses of GPU Drawing primitives Backing stores Layer & compositing 43

Slide 44

Slide 44 text

Responsive Interface Processor Rendering User interaction decoupled 44

Slide 45

Slide 45 text

Maps Tile 45

Slide 46

Slide 46 text

Rendering vs Interaction Web Page Backing Store Screen Rendering User interaction 46

Slide 47

Slide 47 text

Checkerboard Pattern 47

Slide 48

Slide 48 text

Pinch to Zoom when you pinch... 48

Slide 49

Slide 49 text

Progressive Rendering Crisp but slow Fast but blurry 49

Slide 50

Slide 50 text

Tile Transformation Panning = Translation Zooming = Scaling 50

Slide 51

Slide 51 text

Perceived Responsiveness User pinches Smooth scaled Blocky (but fast!) 51

Slide 52

Slide 52 text

Tiling System CPU GPU .... .... Texture upload 52

Slide 53

Slide 53 text

Per Element Backing Store = Layer http://techblog.netflix.com/2012/01/webkit-in-your-living-room.html http://aerotwist.com/blog/on-translate3d-and-layer-creation-hacks/ 3-D transform/perspective , , plugins CSS animation, filters .... 53

Slide 54

Slide 54 text

Mechanics of Animation Initialization Animation step “Hey, this is good stuff. Cache it as texture #42.” “Apply [operation] to texture #42.” 54

Slide 55

Slide 55 text

Element and “Logical” 3-D http://www.webkit.org/blog-files/leaves/ Compositor Tweening Can be in a separate thread 55

Slide 56

Slide 56 text

Debugging in Safari defaults write com.apple.Safari IncludeInternalDebugMenu 1 56

Slide 57

Slide 57 text

Compositing Indicators Composited layer Container layer No associated texture Overflow Texture (number = upload) 57

Slide 58

Slide 58 text

Frame Rate HUD http://ariya.ofilabs.com/2013/03/frame-rate-hud-on-chrome-for-android.html Frame rate chart GPU memory usage 58

Slide 59

Slide 59 text

Avoid Texture Reupload No reupload Upload Opacity Position Size Filter Everything else! “Hardware accelerated CSS” 59

Slide 60

Slide 60 text

Constant Upload = Bad http://codepen.io/ariya/full/xuwgy translate3d ??? 60

Slide 61

Slide 61 text

Transformation http://ariya.github.com/css/spinningcube/ http://dev.sencha.com/animator/demos/cogs/ 61

Slide 62

Slide 62 text

Fade In/Out http://dev.sencha.com/animator/demos/ions/ http://codepen.io/ariya/full/spzad 62

Slide 63

Slide 63 text

three.js Periodic Table http://mrdoob.github.com/three.js/examples/css3d_periodictable.html 63

Slide 64

Slide 64 text

Montage MovieShow http://montagejs.github.com/montage/samples/popcorn/ 64

Slide 65

Slide 65 text

Photon CSS Lighting http://photon.attasi.com 65

Slide 66

Slide 66 text

FPS View http://www.keithclark.co.uk/labs/css3-fps/ 66

Slide 67

Slide 67 text

Filter http://html.adobe.com/webstandards/csscustomfilters/cssfilterlab/ 67

Slide 68

Slide 68 text

Timer Latency Depending on user reaction Animation end triggers the JavaScript callback JavaScript code kicks the next animation Prepare both animation and hide the “wrong” one 68

Slide 69

Slide 69 text

Prepare and Reuse Hide the poster (=layer) offscreen Viewport 69

Slide 70

Slide 70 text

Avoid Overcapacity .... .... Texture upload Think of the GPU memory like a cache. 70

Slide 71

Slide 71 text

Color Transition @keyframes box { 0% { transform: background-color: blue; } 100% { transform: background-color: green; } } Need a new texture uploaded to the GPU for every in-between color 71

Slide 72

Slide 72 text

Color Transition Trick Blended with http://codepen.io/ariya/full/ofDIh 72

Slide 73

Slide 73 text

Traffic Congestion The speed of the car vs the traffic condition 73

Slide 74

Slide 74 text

Conclusion 74

Slide 75

Slide 75 text

Rendering Aspects 75

Slide 76

Slide 76 text

Run a thorough feature-cost analysis Don’t fight the rendering engine Never assume, always measure 76

Slide 77

Slide 77 text

More Stuff Jank Busters: http://www.youtube.com/watch?v=hAzhayTnhEI Continuous painting: http://updates.html5rocks.com/2013/02/Profiling-Long-Paint-Times-with- DevTools-Continuous-Painting-Mode Chrome GPU rendering benchmark (Telemetry): http://www.chromium.org/developers/design-documents/rendering-benchmarks Skia debugger: http://wesleyhales.com/blog/2013/02/18/Adventures-With-the-Skia-Debugger/ 77

Slide 78

Slide 78 text

Thank You [email protected] @AriyaHidayat ariya.ofilabs.com speakerdeck.com/ariya Credits: Some artworks are from http://openclipart.org/user-cliparts/nicubunu 78