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