Understanding Webkit Rendering

82
Understanding WebKit Rendering Ariya Hidayat Sunday, October 7, 12

Transcript of Understanding Webkit Rendering

Page 1: Understanding Webkit Rendering

Understanding WebKit Rendering

Ariya Hidayat

Sunday, October 7, 12

Page 2: Understanding Webkit Rendering

whoami

Sunday, October 7, 12

Page 3: Understanding Webkit Rendering

Sunday, October 7, 12

Page 4: Understanding Webkit Rendering

WebKit

Sunday, October 7, 12

Page 5: Understanding Webkit Rendering

WebKit is Everywhere

Sunday, October 7, 12

Page 6: Understanding Webkit Rendering

Browser at a High Level

User Interface

Browser Engine

Graphics Stack

Data Persistence

Render Engine

JavaScript Engine

Networking I/O

Sunday, October 7, 12

Page 7: Understanding Webkit Rendering

WebKit Components

Render Engine

WebCore

JavaScript Engine (JSC/V8)Client Interface

HTML

DOMCSS

SVG

Canvas

Sunday, October 7, 12

Page 8: Understanding Webkit Rendering

Platform Abstraction

Client Interface

Networking I/O

GraphicsThemeEventsClipboard

Thread Geolocation Timer

API CallsEvents

Port(Chrome, Safari, etc.)

Sunday, October 7, 12

Page 9: Understanding Webkit Rendering

Layout

Sunday, October 7, 12

Page 10: Understanding Webkit Rendering

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

Page 11: Understanding Webkit Rendering

DOM Tree

<html><body><p>Hello</p></body></html>

HTMLDocument

HTMLBodyElement

HTMLParagraphElement attributes, children, contents

Sunday, October 7, 12

Page 12: Understanding Webkit Rendering

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

Page 13: Understanding Webkit Rendering

DOM Tree vs Render Tree

HTMLDocument

HTMLBodyElement

HTMLParagraphElement

RenderRoot

RenderBody

RenderText

There is no Render* for display:none

Sunday, October 7, 12

Page 14: Understanding Webkit Rendering

“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

Page 15: Understanding Webkit Rendering

Sunday, October 7, 12

Page 16: Understanding Webkit Rendering

It’s a forest!

Sunday, October 7, 12

Page 17: Understanding Webkit Rendering

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

Page 18: Understanding Webkit Rendering

Getting the Right Style

HTMLDocumentHTMLBodyElement

HTMLParagraphElement

DOM Tree

p { color: blue }

Stylesheet List

RenderStyle

CSS Style Selector: id, class, tags, ...

Sunday, October 7, 12

Page 19: Understanding Webkit Rendering

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

Page 20: Understanding Webkit Rendering

Painting

Sunday, October 7, 12

Page 21: Understanding Webkit Rendering

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

Page 22: Understanding Webkit Rendering

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

Page 23: Understanding Webkit Rendering

Graphics Abstraction

Mac & iOSChromium

Android Qt

CoreGraphics

Skia

QPainter

graphics stack

GraphicsContext

RenderThemeNative controls,e.g. form elements

Sunday, October 7, 12

Page 24: Understanding Webkit Rendering

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

Page 25: Understanding Webkit Rendering

Path is Everything

Sunday, October 7, 12

Page 26: Understanding Webkit Rendering

Stroke = Outline

Solid Dashed Dotted Textured

Sunday, October 7, 12

Page 27: Understanding Webkit Rendering

Brush = Fill

SolidNone Gradient Textured

Sunday, October 7, 12

Page 28: Understanding Webkit Rendering

Gradient = Non-uniform Pixel Values

Sunday, October 7, 12

Page 29: Understanding Webkit Rendering

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

Page 30: Understanding Webkit Rendering

Path Approximation

Curves

Polygon

Sunday, October 7, 12

Page 31: Understanding Webkit Rendering

Antialiasing

Multiple levels of transparency

Sunday, October 7, 12

Page 32: Understanding Webkit Rendering

Transformation

Scaling

RotationPerspective

Sunday, October 7, 12

Page 33: Understanding Webkit Rendering

Text Rasterization

Sunday, October 7, 12

Page 34: Understanding Webkit Rendering

Font Atlas

Bye

Buffer

Sunday, October 7, 12

Page 35: Understanding Webkit Rendering

Simple to Complex

Hello!

Simple shapeSolid color Curves

Gradient brushTextured stroke

Blur shadowSerif textRotated

Sunday, October 7, 12

Page 36: Understanding Webkit Rendering

Simple to Complex

Hello!

Simple shapeSolid color Curves

Gradient brushTextured stroke

Blur shadowSerif textRotatedMake it as an image

Sunday, October 7, 12

Page 37: Understanding Webkit Rendering

Hardware Acceleration

Sunday, October 7, 12

Page 38: Understanding Webkit Rendering

Game vs Web

Sunday, October 7, 12

Page 39: Understanding Webkit Rendering

Game

Animation

Physics

Textured objects

Text

Transformation

Sunday, October 7, 12

Page 40: Understanding Webkit Rendering

Large Area, but Still Bounded

Sunday, October 7, 12

Page 41: Understanding Webkit Rendering

Web Page

Images

Text

Sunday, October 7, 12

Page 42: Understanding Webkit Rendering

Challenges

Predictable contents (assets) ✔Mostly text ✔Mostly images ✔

immediateInitial response

Sunday, October 7, 12

Page 43: Understanding Webkit Rendering

SoC = System-on-a-Chip

CPU GPU

Sunday, October 7, 12

Page 44: Understanding Webkit Rendering

GPU Workflow

Vertices

Matrix

Rendered Textured

Sunday, October 7, 12

Page 45: Understanding Webkit Rendering

Optimized for Games

Transformation

Textured triangles

“Fixed” geometry

Parallelism

Sunday, October 7, 12

Page 47: Understanding Webkit Rendering

http://www.trollquotes.org/619-super-spider-bat-troll-quote/Sunday, October 7, 12

Page 48: Understanding Webkit Rendering

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

Page 49: Understanding Webkit Rendering

Traffic Congestion

Sunday, October 7, 12

Page 50: Understanding Webkit Rendering

Efficient Use of GPU

Drawing primitives

Backing stores

Layer & compositing

Sunday, October 7, 12

Page 51: Understanding Webkit Rendering

Maps

Tile

Sunday, October 7, 12

Page 52: Understanding Webkit Rendering

Pinch to Zoom

when you pinch...

Sunday, October 7, 12

Page 53: Understanding Webkit Rendering

Responsive Interface

Processor

Rendering

User interaction

decoupled

Sunday, October 7, 12

Page 54: Understanding Webkit Rendering

Rendering vs Interaction

Web Page

Backing StoreScreen

Rendering

User interaction

Sunday, October 7, 12

Page 55: Understanding Webkit Rendering

Checkerboard Pattern

Sunday, October 7, 12

Page 56: Understanding Webkit Rendering

Progressive Rendering

Crisp but slow

Fast but blurry

Sunday, October 7, 12

Page 57: Understanding Webkit Rendering

Perceived Responsiveness

User pinches

Smooth scaled

Blocky (but fast!)

Sunday, October 7, 12

Page 58: Understanding Webkit Rendering

Tiling System

CPU GPU

........

Texture upload

Sunday, October 7, 12

Page 59: Understanding Webkit Rendering

Tile Transformation

Panning = Translation Zooming = Scaling

Sunday, October 7, 12

Page 60: Understanding Webkit Rendering

Checkerboard Pattern

Sunday, October 7, 12

Page 61: Understanding Webkit Rendering

Typical Animation

opacity, movement, scaling, rotation, ...

Sunday, October 7, 12

Page 62: Understanding Webkit Rendering

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

Page 63: Understanding Webkit Rendering

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

Page 64: Understanding Webkit Rendering

Mechanics of Animation

Initialization

Animation step

“Hey, this is good stuff. Cache it as texture

“Apply [operation] to texture #42.”

Sunday, October 7, 12

Page 65: Understanding Webkit Rendering

Elements = Layer

Sunday, October 7, 12

Page 66: Understanding Webkit Rendering

Logical 3-D

Sunday, October 7, 12

Page 67: Understanding Webkit Rendering

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

Page 68: Understanding Webkit Rendering

Magical AdviceUse translate3d for

hardware acceleration

Sunday, October 7, 12

Page 69: Understanding Webkit Rendering

Magical AdviceUse translate3d for

hardware acceleration

Sunday, October 7, 12

Page 70: Understanding Webkit Rendering

Debugging in Safari

defaults write com.apple.Safari IncludeInternalDebugMenu 1

Sunday, October 7, 12

Page 71: Understanding Webkit Rendering

Compositing Indicators

Composited layer

Container layer

No associated texture

Overflow

Texture (number = upload)

Sunday, October 7, 12

Page 72: Understanding Webkit Rendering

Avoid Texture Reupload

No reupload Upload

OpacityPosition

SizeAnimation

Everything else!

“Hardware accelerated CSS”

Sunday, October 7, 12

Page 73: Understanding Webkit Rendering

Examples

Sunday, October 7, 12

Page 74: Understanding Webkit Rendering

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

Page 75: Understanding Webkit Rendering

Color Transition: The Trick

Blended with

http://jsfiddle.net/79AVnSunday, October 7, 12

Page 76: Understanding Webkit Rendering

Prepare and Reuse

Viewport

Hide the layer offscreen

Sunday, October 7, 12

Page 77: Understanding Webkit Rendering

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

Page 78: Understanding Webkit Rendering

Avoid Overcapacity

........

Think of the GPU memory like a cache.

Sunday, October 7, 12

Page 79: Understanding Webkit Rendering

Conclusion

Sunday, October 7, 12

Page 80: Understanding Webkit Rendering

Rendering Aspects

Sunday, October 7, 12

Page 81: Understanding Webkit Rendering

There is No Silver Bullet

Sunday, October 7, 12

Page 82: Understanding Webkit Rendering

Thank You

[email protected]

@AriyaHidayat

ariya.ofilabs.com

Sunday, October 7, 12