Google I/O 2013 - Android Graphics Performance

96

description

Engineers from the Android UI Graphics team will show some tips, tricks, tools, and techniques for getting the best performance and smoothest UI for your Android applications.

Transcript of Google I/O 2013 - Android Graphics Performance

Page 1: Google I/O 2013 - Android Graphics Performance
Page 2: Google I/O 2013 - Android Graphics Performance
Page 3: Google I/O 2013 - Android Graphics Performance

&Graphics Performance

Page 4: Google I/O 2013 - Android Graphics Performance

&Chet Haase Romain Guy

Page 5: Google I/O 2013 - Android Graphics Performance

Architecture1

Page 6: Google I/O 2013 - Android Graphics Performance

Reordering & merging

Page 7: Google I/O 2013 - Android Graphics Performance

Save Cancel

Maximize compatibility

Include metadata

Page 8: Google I/O 2013 - Android Graphics Performance
Page 9: Google I/O 2013 - Android Graphics Performance

Include metadata Maximize compatibility CancelSave

Order of drawing commands

Page 10: Google I/O 2013 - Android Graphics Performance

Include metadata Maximize compatibility CancelSave

1. Re-ordering

Page 11: Google I/O 2013 - Android Graphics Performance

Include metadata

Maximize compatibility

Cancel

Save

2. Merging

Page 12: Google I/O 2013 - Android Graphics Performance
Page 13: Google I/O 2013 - Android Graphics Performance

Multi-threading

Page 14: Google I/O 2013 - Android Graphics Performance
Page 15: Google I/O 2013 - Android Graphics Performance

Drawing

Page 16: Google I/O 2013 - Android Graphics Performance

Drawing

Shadows

Shadows

Shadows

Shadows

Page 17: Google I/O 2013 - Android Graphics Performance

Drawing

Paths

Paths

Shadows

Shadows

Shadows

Shadows

Page 18: Google I/O 2013 - Android Graphics Performance

Non-rectangular clipping

Page 19: Google I/O 2013 - Android Graphics Performance

@Overrideprotected void onDraw(Canvas canvas) { // Clip with a shape Path clip = getPath(); canvas.clipPath(clip);

// Draw the content for (int i = 0; i < mLines,length; i++) { TextLine line = mLines[i]; canvas.drawText(line.text, line.x, line.y, mPaint); }}

Page 20: Google I/O 2013 - Android Graphics Performance

Developer Tools2

Page 21: Google I/O 2013 - Android Graphics Performance

Overdraw

Page 22: Google I/O 2013 - Android Graphics Performance

Overdraw

Page 23: Google I/O 2013 - Android Graphics Performance

Overdraw

Page 24: Google I/O 2013 - Android Graphics Performance

OverdrawBlueGreenRedDeep Red

1x2x3x4x

Page 25: Google I/O 2013 - Android Graphics Performance

0

3

6

9

12

15

Tim

e in

ms

Frames

Update display lists Process display lists Swap buffers

Page 26: Google I/O 2013 - Android Graphics Performance
Page 27: Google I/O 2013 - Android Graphics Performance
Page 28: Google I/O 2013 - Android Graphics Performance

performTraversals

draw

getDL drawDisplayList

systrace

flush drawing commands

Page 29: Google I/O 2013 - Android Graphics Performance

android:sdk $ cd platform-tools/

android:platform-tools $ ⏎ ./systrace.py gfx view freq sched

Page 30: Google I/O 2013 - Android Graphics Performance
Page 31: Google I/O 2013 - Android Graphics Performance

import android.os.Trace;

@Overridepublic View getView(int pos, View view, ViewGroup parent) { Trace.beginSection("getView"); if (view == null) { view = createView(); }

// Trace time spent binding data Trace.beginSection("bind"); bindView(pos, view); Trace.endSection();

Trace.endSection(); return view;}

Page 32: Google I/O 2013 - Android Graphics Performance

android:sdk $ cd platform-tools/

android:platform-tools $ ⏎ ./systrace.py -a com.example.myapp

Page 33: Google I/O 2013 - Android Graphics Performance

Tips & Tricks3

Page 34: Google I/O 2013 - Android Graphics Performance

Overdraw demo

Page 35: Google I/O 2013 - Android Graphics Performance

Trilinear filtering

Page 36: Google I/O 2013 - Android Graphics Performance
Page 37: Google I/O 2013 - Android Graphics Performance

Off On

Page 38: Google I/O 2013 - Android Graphics Performance

private void loadData() { // Load bitmap Bitmap b = getBitmap();

// Enable trilinear filtering b.setHasMipMap(true);}

Page 39: Google I/O 2013 - Android Graphics Performance

<bitmap android:mipMap="true" android:src="@drawable/my_drawable" />

Page 40: Google I/O 2013 - Android Graphics Performance

Canvas layers

Page 41: Google I/O 2013 - Android Graphics Performance

@Overrideprotected void onDraw(Canvas canvas) { // Create a clipped layer canvas.save(); canvas.saveLayer(x, y, width, height, Canvas.CLIP_TO_LAYER_SAVE_FLAG);

// Draw stuff canvas.drawBitmap(bugDroid, 0.0f, 0.0f, null); canvas.restore();}

Page 42: Google I/O 2013 - Android Graphics Performance

saveLayer()

Page 43: Google I/O 2013 - Android Graphics Performance
Page 44: Google I/O 2013 - Android Graphics Performance
Page 45: Google I/O 2013 - Android Graphics Performance

@Overrideprotected void onDraw(Canvas canvas) { // Create an unclipped layer canvas.save(); canvas.saveLayer(x, y, width, height, 0);

// Draw stuff canvas.drawBitmap(bugDroid, 0.0f, 0.0f, null); canvas.restore();}

Page 46: Google I/O 2013 - Android Graphics Performance

saveLayer()

Page 47: Google I/O 2013 - Android Graphics Performance
Page 48: Google I/O 2013 - Android Graphics Performance
Page 49: Google I/O 2013 - Android Graphics Performance

Using alpha with care

Page 50: Google I/O 2013 - Android Graphics Performance
Page 51: Google I/O 2013 - Android Graphics Performance

view.setAlpha(0.5f);View.ALPHA.set(view, 0.5f);ObjectAnimation.ofFloat(view, "alpha", 0.5f)view.animate().alpha(0.5f);view.setAnimation(new AlphaAnimation(1.0f, 0.5f));

Page 52: Google I/O 2013 - Android Graphics Performance

view.setAlpha(0.5f);View.ALPHA.set(view, 0.5f);ObjectAnimation.ofFloat(view, "alpha", 0.5f)view.animate().alpha(0.5f);view.setAnimation(new AlphaAnimation(1.0f, 0.5f));

Canvas.saveLayerAlpha(l, t, r, b, 127, Canvas.CLIP_TO_LAYER_SAVE_FLAG);

==

Page 53: Google I/O 2013 - Android Graphics Performance
Page 54: Google I/O 2013 - Android Graphics Performance
Page 55: Google I/O 2013 - Android Graphics Performance
Page 56: Google I/O 2013 - Android Graphics Performance
Page 57: Google I/O 2013 - Android Graphics Performance

lternatives

Page 58: Google I/O 2013 - Android Graphics Performance

// Not thistextView.setAlpha(alpha);

// But thisint newTextColor = (int) (0xFF * alpha) << 24 | baseTextColor & 0xFFFFFF;textView.setTextColor(newTextColor);

Page 59: Google I/O 2013 - Android Graphics Performance

// Not thisimageView.setAlpha(alpha);

// But thisimageView.setImageAlpha((int) (alpha * 255));

Page 60: Google I/O 2013 - Android Graphics Performance

// Not thiscustomView.setAlpha(alpha);

// But thisint alpha = (int) (255 * slider.getProgress() / 100.0f);paint.setAlpha(alpha);canvas.draw*(..., paint);

Page 61: Google I/O 2013 - Android Graphics Performance

// Or use a layerview.setLayerType(View.LAYER_TYPE_HARDWARE, null);

// Transient layerview.animate().alpha(0).withLayer();

Page 62: Google I/O 2013 - Android Graphics Performance

// API level 16+@Overridepublic boolean hasOverlappingRendering() { // Don't lie to us! return false;}

Page 63: Google I/O 2013 - Android Graphics Performance

640 dp

400

dp

Canvas

Page 64: Google I/O 2013 - Android Graphics Performance

@Overrideprotected void onDraw(Canvas canvas) { // Get the dimensions of the Canvas int w = canvas.getWidth(); int h = canvas.getHeight();

canvas.drawRect(0, 0, w, h, mPaint);}

Page 65: Google I/O 2013 - Android Graphics Performance

1280 px

800

px

View

300

px

600 px

Page 66: Google I/O 2013 - Android Graphics Performance
Page 67: Google I/O 2013 - Android Graphics Performance

With hardware rendering

600x300 px (size of the View)

Page 68: Google I/O 2013 - Android Graphics Performance

With hardware rendering

With software rendering

600x300 px

1280x800 px

(size of the View)

(size of the window)

Page 69: Google I/O 2013 - Android Graphics Performance

✂Clipping

Page 70: Google I/O 2013 - Android Graphics Performance

@Overrideprotected void onDraw(Canvas canvas) { // Keep the jellybeans canvas.clipRect(l, t, r, b); // Rotate the jar canvas.rotate(-30.0f, pX, pY); // Draw the jar canvas.drawBitmap(mJellyBeans, x, y, null);}

Page 71: Google I/O 2013 - Android Graphics Performance
Page 72: Google I/O 2013 - Android Graphics Performance

1. Clip

Page 73: Google I/O 2013 - Android Graphics Performance

2. Rotate

Page 74: Google I/O 2013 - Android Graphics Performance

3. Draw

Page 75: Google I/O 2013 - Android Graphics Performance
Page 76: Google I/O 2013 - Android Graphics Performance

@Overrideprotected void onDraw(Canvas canvas) { // Rotate the jar canvas.rotate(-30.0f, pX, pY); // Keep the jellybeans canvas.clipRect(l, t, r, b); // Draw the jar canvas.drawBitmap(mJellyBeans, x, y, null);}

Page 77: Google I/O 2013 - Android Graphics Performance
Page 78: Google I/O 2013 - Android Graphics Performance

1. Rotate

Page 79: Google I/O 2013 - Android Graphics Performance

2. Clip

Page 80: Google I/O 2013 - Android Graphics Performance

3. Draw

Page 81: Google I/O 2013 - Android Graphics Performance
Page 82: Google I/O 2013 - Android Graphics Performance

Sten

cil buf

fer

Page 83: Google I/O 2013 - Android Graphics Performance

Sten

cil buf

fer

Page 84: Google I/O 2013 - Android Graphics Performance

640 px

400

px

View

Page 85: Google I/O 2013 - Android Graphics Performance

Invalidate

640 px

400

px

(170,125)

(470,275)

Page 86: Google I/O 2013 - Android Graphics Performance

@Overrideprotected void onDraw(Canvas canvas) { // Query the current clip Rect clip = canvas.getClipBounds(); // ??? Log.d("I/O", "clip = " + clip);}

Page 87: Google I/O 2013 - Android Graphics Performance
Page 88: Google I/O 2013 - Android Graphics Performance

With hardware rendering0, 0, 640, 400(bounds of the View)

Page 89: Google I/O 2013 - Android Graphics Performance

With hardware rendering

With software rendering

0, 0, 640, 400

170,125, 470, 275

(bounds of the View)

(bounds of the dirty rect)

Page 90: Google I/O 2013 - Android Graphics Performance

Reordering barriers

Page 91: Google I/O 2013 - Android Graphics Performance

Non-rectangular clips

Page 92: Google I/O 2013 - Android Graphics Performance

saveLayer()

Page 93: Google I/O 2013 - Android Graphics Performance

More infoParleys.com

For Butter or WorseGoogle I/O 2012

Various Android GUI & performance talks

Accelerated Android RenderingGoogle I/O 2011

Page 94: Google I/O 2013 - Android Graphics Performance

More infoRomain’s Tips & Trickswww.curious-creature.org

Chet’s Tips & Tricks

goo.gl/y9JZrAndroid Performance Case Study

graphics-geek.blogspot.com

Page 96: Google I/O 2013 - Android Graphics Performance

Developers