Understanding Webkit Rendering
-
Upload
ariya-hidayat -
Category
Technology
-
view
18.154 -
download
5
Transcript of Understanding Webkit Rendering
Understanding WebKit Rendering
Ariya Hidayat
Sunday, October 7, 12
whoami
Sunday, October 7, 12
Sunday, October 7, 12
WebKit
Sunday, October 7, 12
WebKit is Everywhere
Sunday, October 7, 12
Browser at a High Level
User Interface
Browser Engine
Graphics Stack
Data Persistence
Render Engine
JavaScript Engine
Networking I/O
Sunday, October 7, 12
WebKit Components
Render Engine
WebCore
JavaScript Engine (JSC/V8)Client Interface
HTML
DOMCSS
SVG
Canvas
Sunday, October 7, 12
Platform Abstraction
Client Interface
Networking I/O
GraphicsThemeEventsClipboard
Thread Geolocation Timer
API CallsEvents
Port(Chrome, Safari, etc.)
Sunday, October 7, 12
Layout
Sunday, October 7, 12
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
Sunday, October 7, 12
DOM Tree
<html><body><p>Hello</p></body></html>
HTMLDocument
HTMLBodyElement
HTMLParagraphElement attributes, children, contents
Sunday, October 7, 12
HTML Parsing
TokenizerLoader
Tree BuilderMay come in chunks
Section 12.2 in HTML Specification
http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#parsing
DOM Tree
<html><body><p>Hello</p></body></html>
tag
text
Sunday, October 7, 12
DOM Tree vs Render Tree
HTMLDocument
HTMLBodyElement
HTMLParagraphElement
RenderRoot
RenderBody
RenderText
There is no Render* for display:none
Sunday, October 7, 12
“Attach” Process
Historical: Node is invisible, attach it to the main view to make it visible
<div>42</div>
View
RenderObject RenderStyletree structure/navigation
metrics (box model)hit testing
computed style
Node/Element1:1 many:1
another tree structure
Sunday, October 7, 12
Sunday, October 7, 12
It’s a forest!
Sunday, October 7, 12
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”
Sunday, October 7, 12
Getting the Right Style
HTMLDocumentHTMLBodyElement
HTMLParagraphElement
DOM Tree
p { color: blue }
Stylesheet List
RenderStyle
CSS Style Selector: id, class, tags, ...
Sunday, October 7, 12
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
clientHeightclientLeftclientTopclientWidthoffsetHeightoffsetLeftoffsetParentoffsetTopoffsetWidthscrollLeftscrollTopscrollWidthinnerTextouterText
Element
focus()getBoundingClientRect()getClientRects()scrollByLines()scrollByPages()scrollHeightscrollIntoView()scrollIntoViewIfNeeded()
Sunday, October 7, 12
Painting
Sunday, October 7, 12
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
Sunday, October 7, 12
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)
Sunday, October 7, 12
Graphics Abstraction
Mac & iOSChromium
Android Qt
CoreGraphics
Skia
QPainter
graphics stack
GraphicsContext
RenderThemeNative controls,e.g. form elements
Sunday, October 7, 12
Painting Flow: Browser vs WebKit
WebKitBrowser
Request painting, GraphicsContext
User scrollsthe viewport
Style change
Mark “dirty” area
Sometimes to the backing store
Sunday, October 7, 12
Path is Everything
Sunday, October 7, 12
Stroke = Outline
Solid Dashed Dotted Textured
Sunday, October 7, 12
Brush = Fill
SolidNone Gradient Textured
Sunday, October 7, 12
Gradient = Non-uniform Pixel Values
Sunday, October 7, 12
Drawing Time: Solid vs Gradient
Only border
Solid color fill
Linear gradient fill
Radial gradient fill
14x slower(depending on the dimension)
Sunday, October 7, 12
Path Approximation
Curves
Polygon
Sunday, October 7, 12
Antialiasing
Multiple levels of transparency
Sunday, October 7, 12
Transformation
Scaling
RotationPerspective
Sunday, October 7, 12
Text Rasterization
Sunday, October 7, 12
Font Atlas
Bye
Buffer
Sunday, October 7, 12
Simple to Complex
Hello!
Simple shapeSolid color Curves
Gradient brushTextured stroke
Blur shadowSerif textRotated
Sunday, October 7, 12
Simple to Complex
Hello!
Simple shapeSolid color Curves
Gradient brushTextured stroke
Blur shadowSerif textRotatedMake it as an image
Sunday, October 7, 12
Hardware Acceleration
Sunday, October 7, 12
Game vs Web
Sunday, October 7, 12
Game
Animation
Physics
Textured objects
Text
Transformation
Sunday, October 7, 12
Large Area, but Still Bounded
Sunday, October 7, 12
Web Page
Images
Text
Sunday, October 7, 12
Challenges
Predictable contents (assets) ✔Mostly text ✔Mostly images ✔
immediateInitial response
Sunday, October 7, 12
SoC = System-on-a-Chip
CPU GPU
Sunday, October 7, 12
GPU Workflow
Vertices
Matrix
Rendered Textured
Sunday, October 7, 12
Optimized for Games
Transformation
Textured triangles
“Fixed” geometry
Parallelism
Sunday, October 7, 12
Powerful Capabilities
http://epicgames.com/technology/epic-citadel
http://www.rage.com/
Sunday, October 7, 12
http://www.trollquotes.org/619-super-spider-bat-troll-quote/Sunday, October 7, 12
Fundamental Physical Limitations
• Memory: Can’t store too much stuff
• Speed: Quad-core CPU can do certain operations better
• Bandwidth: Data transfer can be the bottleneck
Sunday, October 7, 12
Traffic Congestion
Sunday, October 7, 12
Efficient Use of GPU
Drawing primitives
Backing stores
Layer & compositing
Sunday, October 7, 12
Maps
Tile
Sunday, October 7, 12
Pinch to Zoom
when you pinch...
Sunday, October 7, 12
Responsive Interface
Processor
Rendering
User interaction
decoupled
Sunday, October 7, 12
Rendering vs Interaction
Web Page
Backing StoreScreen
Rendering
User interaction
Sunday, October 7, 12
Checkerboard Pattern
Sunday, October 7, 12
Progressive Rendering
Crisp but slow
Fast but blurry
Sunday, October 7, 12
Perceived Responsiveness
User pinches
Smooth scaled
Blocky (but fast!)
Sunday, October 7, 12
Tiling System
CPU GPU
........
Texture upload
Sunday, October 7, 12
Tile Transformation
Panning = Translation Zooming = Scaling
Sunday, October 7, 12
Checkerboard Pattern
Sunday, October 7, 12
Typical Animation
opacity, movement, scaling, rotation, ...
Sunday, October 7, 12
Principles of Fluid Animation
At the beginning, push as many resources as possible to the GPU
During the animation, minimize CPU-GPU interaction
1
2
Sunday, October 7, 12
Immediate vs Retained
draw the shape at (x, y)x = x + 10
blit the buffer at (x, y)
x = x + 10
For every animation tick...
At the beginning...
draw the shape onto a buffer
Off main thread
Sunday, October 7, 12
Mechanics of Animation
Initialization
Animation step
“Hey, this is good stuff. Cache it as texture
“Apply [operation] to texture #42.”
Sunday, October 7, 12
Elements = Layer
Sunday, October 7, 12
Logical 3-D
Sunday, October 7, 12
Compositing By Force
-webkit-transform: translateZ(0)
Not needed for CSS
.someid { -webkit-animation-name: somekeyframeanimation;
-webkit-animation-duration: 7s; -webkit-transform: translateZ(0);
}
Don’t do that
Sunday, October 7, 12
Magical AdviceUse translate3d for
hardware acceleration
Sunday, October 7, 12
Magical AdviceUse translate3d for
hardware acceleration
Sunday, October 7, 12
Debugging in Safari
defaults write com.apple.Safari IncludeInternalDebugMenu 1
Sunday, October 7, 12
Compositing Indicators
Composited layer
Container layer
No associated texture
Overflow
Texture (number = upload)
Sunday, October 7, 12
Avoid Texture Reupload
No reupload Upload
OpacityPosition
SizeAnimation
Everything else!
“Hardware accelerated CSS”
Sunday, October 7, 12
Examples
Sunday, October 7, 12
Color Transition
@-webkit-keyframes box { 0% { -webkit-transform: background-color: blue; } 100% { -webkit-transform: background-color: green; }}
Need a new texture uploaded to the GPU for every in-between color
Sunday, October 7, 12
Color Transition: The Trick
Blended with
http://jsfiddle.net/79AVnSunday, October 7, 12
Prepare and Reuse
Viewport
Hide the layer offscreen
Sunday, October 7, 12
Timer Latency
Depending on user
Animation end triggers the JavaScript callbackJavaScript code kicks the next animation
Prepare both animation and hide the
“wrong” one
Sunday, October 7, 12
Avoid Overcapacity
........
Think of the GPU memory like a cache.
Sunday, October 7, 12
Conclusion
Sunday, October 7, 12
Rendering Aspects
Sunday, October 7, 12
There is No Silver Bullet
Sunday, October 7, 12