understanding hardware acceleration on mobile browsers

71
Understanding Hardware Acceleration on Mobile Browsers Ariya Hidayat

Upload: ariya-hidayat

Post on 11-Jan-2015

8.372 views

Category:

Technology


0 download

DESCRIPTION

GPU acceleration on mobile browsers, if it is leveraged correctly, can lead to a smooth and fluid applications, thus improving the user experience. There has been a lot of mentions and best practices of hardware acceleration these days, although so far it has been pretty general and hasn’t provided much technical direction apart from simple magical advice such as “use translate3d”. This talk sheds some more light on browser interactions with the GPU and explain what happens behind the scenes, covering the topic of acceleration of primitive drawing, the use of tiled backing store, and composited layer. Knowing the actual machinery behind hardware acceleration, you will be in the position to plan your strategy to improve the performance of your web application.

TRANSCRIPT

Page 1: Understanding Hardware Acceleration on Mobile Browsers

Understanding Hardware Acceleration on Mobile

Browsers

Ariya Hidayat

Page 2: Understanding Hardware Acceleration on Mobile Browsers
Page 3: Understanding Hardware Acceleration on Mobile Browsers

Magical AdviceUse translate3d for hardware

acceleration

Page 4: Understanding Hardware Acceleration on Mobile Browsers

Challenges

Page 5: Understanding Hardware Acceleration on Mobile Browsers

Game vs Web

Page 6: Understanding Hardware Acceleration on Mobile Browsers

Game

Animation

Physics

Textured objects

Text

Transformation

Page 7: Understanding Hardware Acceleration on Mobile Browsers

Large but Still Bounded

Page 8: Understanding Hardware Acceleration on Mobile Browsers

Web Page

Images

Text

Page 9: Understanding Hardware Acceleration on Mobile Browsers

Challenges

Predictable contents (assets) ✔

Mostly text ✔

Mostly images ✔

immediateInitial response

Page 10: Understanding Hardware Acceleration on Mobile Browsers

Browser Rendering

Page 11: Understanding Hardware Acceleration on Mobile Browsers

From Wave to Pixels

Layout engine

Network stack

GraphicsJavaScript engine

User interface

Page 12: Understanding Hardware Acceleration on Mobile Browsers

WebKit Components

Render Engine

WebCore

JavaScript Engine (JSC/V8)Client Interface

HTML

DOMCSS

SVG

Canvas

Page 13: Understanding Hardware Acceleration on Mobile Browsers

Platform Abstraction

Network Unicode Clipboard

Graphics Theme Events

Thread Geolocation Timer

Page 14: Understanding Hardware Acceleration on Mobile Browsers

Graphics Abstraction

iOS Android

CoreGraphicsSkia

Graphics stack

GraphicsContext

Page 15: Understanding Hardware Acceleration on Mobile Browsers

SoC = System-on-a-Chip

CPU GPU

Page 16: Understanding Hardware Acceleration on Mobile Browsers

GPU Workflow

Vertices

Matrix

Rendered Textured

Page 17: Understanding Hardware Acceleration on Mobile Browsers

Optimized for Games

Transformation

Textured triangles

“Fixed” geometry

Parallelism

Page 18: Understanding Hardware Acceleration on Mobile Browsers

Powerful Capabilities

http://epicgames.com/technology/epic-citadel http://www.rage.com/

Page 19: Understanding Hardware Acceleration on Mobile Browsers

http://www.trollquotes.org/619-super-spider-bat-troll-quote/

Page 20: Understanding Hardware Acceleration on Mobile Browsers

Fundamental Physical Limitations

Memory- Can’t store too much stuff

Speed- Quad-core CPU can do certain operations better

Bandwidth- Bottleneck can be in the data transfer

Page 21: Understanding Hardware Acceleration on Mobile Browsers

Traffic Congestion

Page 22: Understanding Hardware Acceleration on Mobile Browsers

Primitive Drawing

Page 23: Understanding Hardware Acceleration on Mobile Browsers

Path is Everything

Path

Triangle Rectangle

PolygonEllipse

Page 24: Understanding Hardware Acceleration on Mobile Browsers

Stroke = Outline

Solid Dashed Dotted Textured

Page 25: Understanding Hardware Acceleration on Mobile Browsers

Brush = Fill

SolidNone Gradient Textured

Page 26: Understanding Hardware Acceleration on Mobile Browsers

Gradient = Non-uniform Pixel Values

Page 27: Understanding Hardware Acceleration on Mobile Browsers

Drawing Time: Solid vs Gradient

Only border

Solid color fill

Linear gradient fill

Radial gradient fill

14x slower(depending on the dimension)

Page 28: Understanding Hardware Acceleration on Mobile Browsers

Path Approximation

Curves

Polygon

Page 29: Understanding Hardware Acceleration on Mobile Browsers

Smooth via Antialiasing

Multiple levels of transparency

Perfect for parallelism

Page 30: Understanding Hardware Acceleration on Mobile Browsers

Shadow

Involved pixel “blending” Often obstructed

Page 31: Understanding Hardware Acceleration on Mobile Browsers

Drawing Time: Solid vs Blur Shadow

Solid shadow

Blur shadow

20x slower(depending on the blur radius)

Page 32: Understanding Hardware Acceleration on Mobile Browsers

CSS = Box Model

http://www.w3.org/TR/CSS2/zindex.html Appendix E. Elaborate description of Stacking Contexts

Page 33: Understanding Hardware Acceleration on Mobile Browsers

Shadow Abuse

12,000 pixels

Blur shadow

Page 34: Understanding Hardware Acceleration on Mobile Browsers

Transformation

Scaling

RotationPerspective

Page 35: Understanding Hardware Acceleration on Mobile Browsers

Text Rasterization

Page 36: Understanding Hardware Acceleration on Mobile Browsers

Font Atlas

Bye

Buffer

Page 37: Understanding Hardware Acceleration on Mobile Browsers

From Simple to Complex

Hello!

Simple shapeSolid color

CurvesGradient brushTextured stroke

Blur shadowSerif textRotated

Make it as an image

Page 38: Understanding Hardware Acceleration on Mobile Browsers

Library Instrumentation

platformInitsavePlatformStatetranslate 0,0translate 0,0clip 1,0 0x6.95322e-310fillRect 0,0 800x556 color ff ff ff ffrestorePlatformStateplatformDestroyplatformInitsavePlatformStatetranslate 0,0translate 0,0clip 1,0 0x6.95322e-310fillRect 0,0 800x556 color ff ff ff ffrestorePlatformStateplatformDestroyplatformInitsavePlatformStatetranslate 0,0translate 0,0clip 1,0 0x6.95322e-310fillRect 0,0 800x556 color ff ff ff fffillRect 0,0 800x556 color ff ff ff ff

Page 39: Understanding Hardware Acceleration on Mobile Browsers

Painting Performance

16:24:04.070 D/webcoreglue(  273): drawContent 11 ms16:24:04.110 D/webcoreglue(  273): drawContent 13 ms16:24:04.150 D/webcoreglue(  273): drawContent 13 ms16:24:04.190 D/webcoreglue(  273): drawContent 10 ms16:24:04.240 D/webcoreglue(  273): drawContent 10 ms16:24:04.280 D/webcoreglue(  273): drawContent 13 ms16:24:04.320 D/webcoreglue(  273): drawContent 13 ms16:24:04.360 D/webcoreglue(  273): drawContent 13 ms16:24:06.080 D/webcoreglue(  273): drawContent 12 ms16:24:06.140 D/webcoreglue(  273): drawContent 10 ms16:24:06.180 D/webcoreglue(  273): drawContent 13 ms16:24:06.230 D/webcoreglue(  273): drawContent 14 ms16:24:06.600 D/webcoreglue(  273): drawContent 26 ms16:24:06.640 D/webcoreglue(  273): drawContent 13 ms16:24:06.860 D/webcoreglue(  273): drawContent 33 ms16:24:06.890 D/webcoreglue(  273): drawContent 12 ms16:24:06.930 D/webcoreglue(  273): drawContent 13 ms16:24:06.960 D/webcoreglue(  273): drawContent 13 ms16:24:07.000 D/webcoreglue(  273): drawContent 13 ms

Page 40: Understanding Hardware Acceleration on Mobile Browsers

Backing Store

Page 41: Understanding Hardware Acceleration on Mobile Browsers

Maps

Tiles

Page 42: Understanding Hardware Acceleration on Mobile Browsers

Responsive Interface

Rendering

User interactionProcessor

Page 43: Understanding Hardware Acceleration on Mobile Browsers

Pinch to Zoom

when you pinch...

Page 44: Understanding Hardware Acceleration on Mobile Browsers

Rendering vs Interaction

Web Page

Rendering

User interaction

Page 45: Understanding Hardware Acceleration on Mobile Browsers

Checkerboard Pattern

Poor man’s indicator of performance

Page 46: Understanding Hardware Acceleration on Mobile Browsers

Progressive Rendering

Crisp but slow

Fast but blurry

Page 47: Understanding Hardware Acceleration on Mobile Browsers

Perceived Responsiveness

User pinches

Smooth scaled

Blocky (but fast!)

Page 48: Understanding Hardware Acceleration on Mobile Browsers

Tiling System

CPU GPU

........

Streamed

What happens if the page background changes color?

Page 49: Understanding Hardware Acceleration on Mobile Browsers

Tile Transformation

Panning = Translation Zooming = Scaling

Page 50: Understanding Hardware Acceleration on Mobile Browsers

Y U NOposition:fixed

Page 51: Understanding Hardware Acceleration on Mobile Browsers

Layer & Compositing

Page 52: Understanding Hardware Acceleration on Mobile Browsers

Typical Animation

opacity, movement, scaling, rotation, ...

Page 53: Understanding Hardware Acceleration on Mobile Browsers

Principles of Fluid Animation

1 At the beginning, push as many resources as possible to the GPU

2 During the animation, minimize CPU-GPU interaction

Page 54: Understanding Hardware Acceleration on Mobile Browsers

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

Page 55: Understanding Hardware Acceleration on Mobile Browsers

Mechanics of Animation

Initialization

Animation step

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

“Apply [operation] to texture #42.”

Page 56: Understanding Hardware Acceleration on Mobile Browsers

Elements = Layer

Page 57: Understanding Hardware Acceleration on Mobile Browsers

Logical 3-D

Page 58: Understanding Hardware Acceleration on Mobile Browsers

Compositing By Force

-webkit-transform: translateZ(0)

Not needed for CSS animation!

.someid { -webkit-animation-name: somekeyframeanimation; -webkit-animation-duration: 7s; -webkit-transform: translateZ(0);}

Don’t do that

Page 59: Understanding Hardware Acceleration on Mobile Browsers

Debugging in Safari

defaults write com.apple.Safari IncludeInternalDebugMenu 1

Page 60: Understanding Hardware Acceleration on Mobile Browsers

Compositing Indicators

Composited layer

Container layer

No associated texture

Overflow

Texture (number = upload)

Page 61: Understanding Hardware Acceleration on Mobile Browsers

Avoid Texture Reupload

No reupload Upload

OpacityPosition

SizeAnimation

Everything else!

“Hardware accelerated CSS”

Page 62: Understanding Hardware Acceleration on Mobile Browsers

Examples

Page 63: Understanding Hardware Acceleration on Mobile Browsers

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

Page 64: Understanding Hardware Acceleration on Mobile Browsers

Color Transition: The Trick

Blended with

Page 65: Understanding Hardware Acceleration on Mobile Browsers

Prepare and Reuse

Viewport

Hide the layer offscreen

Page 66: Understanding Hardware Acceleration on Mobile Browsers

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

Page 67: Understanding Hardware Acceleration on Mobile Browsers

Avoid Overcapacity

........

Texture upload

Think of the GPU memory like a cache.

Page 68: Understanding Hardware Acceleration on Mobile Browsers

Wrap-up

Page 69: Understanding Hardware Acceleration on Mobile Browsers

Hardware Acceleration

Drawing primitives Backing stores Layer & compositing

Page 70: Understanding Hardware Acceleration on Mobile Browsers

Traditional graphics programming has been

always full of tricks.

It will always be.

There is no such

Page 71: Understanding Hardware Acceleration on Mobile Browsers

Thank You

[email protected]

@AriyaHidayat

ariya.ofilabs.com

Wed 1:45 PM

Office Hour