custom views and hardware acceleration in android

50
Who? Shameless plug by Royi Benyossef (+royiby). Royi benyossef. Android developer since 2008. Android group leader at Vidmind. Android speaker and lecturer. Android Mentor at CampusTLV. Android GDE 2013, 2014. GDG Herzeliya co-founder.

Upload: droidcontlv

Post on 10-Jun-2015

599 views

Category:

Technology


4 download

DESCRIPTION

Hardware acceleration is a very important tool to know because it's maybe the only tool that not knowing it can damage existing code for no apparent reason in a way that could make your application start crashing or misbehaving without you changing a single character of code. Why would that happen? Very simple; since hardware acceleration is simply the translation of former software rendered canvas drawing code to OpenGL commands which run on the GPU so your code looks the same but it does not act the same and to make matters worse it will also behave differently on different devices and API levels. So why do we need it and how do we make it right again? Wait and see... this is what the session is all about :)

TRANSCRIPT

Page 1: Custom views and hardware acceleration in Android

Who?

Shameless plug by Royi Benyossef (+royiby).

Royi benyossef.

● Android developer since 2008.● Android group leader at Vidmind.● Android speaker and lecturer.● Android Mentor at CampusTLV. ● Android GDE 2013, 2014.● GDG Herzeliya co-founder.

Page 2: Custom views and hardware acceleration in Android

Learn to accel*(erate).

(.by Royi Benyossef (+royiby

.the guide to HW accel

* I know that’s not how you write excel :P

Page 3: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

Motivation

what do we want?!

Page 4: Custom views and hardware acceleration in Android

Motivation

Learn to accel(erate) by Royi Benyossef (+royiby).

● Better looking UI.

● Smoother animations.

● Smaller mem. footprint.

● Smaller CPU load.

Page 5: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

prolog - HW accel.

HOW?

Hardware

accelera

tion

Page 6: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

prolog - HW accel.

What i

s that?

Page 7: Custom views and hardware acceleration in Android

Definition

Learn to accel(erate) by Royi Benyossef (+royiby).

Use the GPU:

● separate HW component

● Specific design (non-generic).

● separate resources.

● separate SW pipe impl.

Page 8: Custom views and hardware acceleration in Android

In Android

Learn to accel(erate) by Royi Benyossef (+royiby).

What does it do (when enabled)?

● canvas Drawing is done by the GPU in the

HW based drawing model rather than the

SW drawing model.

Page 9: Custom views and hardware acceleration in Android

SW drawing model

Learn to accel(erate) by Royi Benyossef (+royiby).

What happens?

1. Invalidate hierarchy.

2. Draw hierarchy.

Page 10: Custom views and hardware acceleration in Android

HW drawing model

Learn to accel(erate) by Royi Benyossef (+royiby).

What happens?

1. Invalidate hierarchy.

2. Record & update display lists.

3. Draw hierarchy.

Page 11: Custom views and hardware acceleration in Android

Are you kidding me?!

Learn to accel(erate) by Royi Benyossef (+royiby).

Not sure if stupid...

Or didn’t make his point yet...

Page 12: Custom views and hardware acceleration in Android

The point #1 part 1

Learn to accel(erate) by Royi Benyossef (+royiby).

In the SW model, when invalidate() is called:

1. the calling item is redrawn.

2. Any affiliated view to the item is redrawn.

Page 13: Custom views and hardware acceleration in Android

The point #1 part 2.

Learn to accel(erate) by Royi Benyossef (+royiby).

Impacts:

1. Non-invalidated views are refreshed (for

better or worse).

2. A lot of drawings all the time.

3. Command done immediately.

Page 14: Custom views and hardware acceleration in Android

The point #2 part 1

Learn to accel(erate) by Royi Benyossef (+royiby).

In the HW model, when invalidate() is called:

1. the calling item’s drawing is saved to the

display list.

2. The display list is updated.

3. Draw diff.

Page 15: Custom views and hardware acceleration in Android

The point #2 part 2.

Learn to accel(erate) by Royi Benyossef (+royiby).

Impacts:

1. Non-invalidated views are NOT refreshed.

2. Command is done on the HW sync signal.

Page 16: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

Yay!

Accelera

te all t

he

thin

gs!!!

Page 17: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

Not yet.

Page 18: Custom views and hardware acceleration in Android

by API level

Learn to accel(erate) by Royi Benyossef (+royiby).

Availability & default status:

● API < 11 - Not

available.

● API >= 11 (HC 3.x) - Available, off.

● API >= 14 (ICS 4.x) - Available, on.

Page 19: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

Wait….

There’s a disable

API!

Page 20: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

Disable? why?

If it’

s so

good?

Why is

n’t it

always

on?

Page 21: Custom views and hardware acceleration in Android

Default? why?

Learn to accel(erate) by Royi Benyossef (+royiby).

Why not always on?

● Increased RAM usage.

● Not all operations are supported (API level

dependant).

● Sick strange things.

Page 22: Custom views and hardware acceleration in Android

When is it safe?

Learn to accel(erate) by Royi Benyossef (+royiby).

When do i have nothing to worry about?

● using only standard views.

● Min API level >= 17.

Page 23: Custom views and hardware acceleration in Android

Worst case scenario?

Learn to accel(erate) by Royi Benyossef (+royiby).

What might happen?

● Invisible UI elements.

● Exceptions.

● Badly rendered pixels.

Page 24: Custom views and hardware acceleration in Android

scenario example #1

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 25: Custom views and hardware acceleration in Android

scenario example #2

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 26: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

Wait….

What if that means no more custom views?!?!

Page 27: Custom views and hardware acceleration in Android

Learn to accel(erate) by Royi Benyossef (+royiby).

Don’t push it.

So…?What to do?

Page 28: Custom views and hardware acceleration in Android

Check!

● Open the list: http://developer.android.com/guide/topics/graphics/hardware-accel.html#drawing-supp

ort

.

● Check.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 29: Custom views and hardware acceleration in Android

Test #1

● On as many devices as possible.

● HW accel. turned on.

* Device library - http://www.campustelaviv.com/hackspace/

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 30: Custom views and hardware acceleration in Android

Test #2

● Systrace.

● tracer.

● GLtrace.

* Here’s Ran Nachmany’s great session on that:

https://www.youtube.com/watch?v=aEFxqdPgHJk

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 31: Custom views and hardware acceleration in Android

And...

Learn to accel(erate) by Royi Benyossef (+royiby).

If bugs w

ere

found...

Page 32: Custom views and hardware acceleration in Android

Tips, tricks and patterns

Learn to accel(erate) by Royi Benyossef (+royiby).

Getting st

arted!

Page 33: Custom views and hardware acceleration in Android

design & impl. correctly

Reduce the number of views.

Less views means smaller display lists and less to draw (for HW and

SW drawing models).

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 34: Custom views and hardware acceleration in Android

design & impl. correctly

Avoid overdraw.

● Rule of thumb - never draw more than X2.5 of the number of

available pixels.

● Merge/remove layers when you fail at #1.

● Transparent pixels in bitmaps count!

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 35: Custom views and hardware acceleration in Android

design & impl. correctly

New is bad!

● Never create new Objects in the rendering functions (common

mistakes including new Path or Paint objects).

● Doing #1 causes the garbage collector to run a lot (a very

expensive action).

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 36: Custom views and hardware acceleration in Android

design & impl. correctly

Beware of modifications.

● Shapes/circles/paths create new texture masks whenever

they’re changed.

● Bitmaps are reloaded to the GPU whenever they’re altered.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 37: Custom views and hardware acceleration in Android

design & impl. correctly

Alpha belongs w/ HW accel.

● Alpha manipulations are created in an off-screen buffer which

should be dealt in a hardware layer type.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 38: Custom views and hardware acceleration in Android

design & impl. correctly

go easy on GPU loading.

private class PieView extends View {

public PieView(Context context) {

super(context);

if (!isInEditMode()) {

setLayerType(View.LAYER_TYPE_HARDWARE, null);

}

}

...

}

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 39: Custom views and hardware acceleration in Android

HW accel. control

Control is allowed in the following resolutions:

● Application.

● Activity.

● Window.

● View & layer.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 40: Custom views and hardware acceleration in Android

HW accel. control

● Application:

<application android:hardwareAccelerated="true" ...>

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 41: Custom views and hardware acceleration in Android

HW accel. control

● Activity:

<application android:hardwareAccelerated="true">

<activity ... />

<activity android:hardwareAccelerated="false" />

</application>

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 42: Custom views and hardware acceleration in Android

HW accel. control

● Window:

getWindow().setFlags(

WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,

WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED

);

Note: You currently cannot disable hardware acceleration at the window

level.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 43: Custom views and hardware acceleration in Android

HW accel. control

● View & layer:myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Note: You currently cannot enable hardware acceleration at the view level. View

layers have other functions besides disabling hardware acceleration. See

View layers for more information about their uses.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 44: Custom views and hardware acceleration in Android

accel. control: layers

LAYER_TYPE_NONE - not backed by any off screen buffer, rendered

in the SW model.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 45: Custom views and hardware acceleration in Android

accel. control: layers

LAYER_TYPE_HARDWARE - if HW accel. is on, layer is backed by HW

texture buffer and rendered in the HW model, else it is the same as in

the case of LAYER_TYPE_SOFTWARE.

*Best suited for performance:

● drawing can be delayed until invalidate is called.

● Some animations such as alpha animations can be added directly

to the HW texture without redrawing,

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 46: Custom views and hardware acceleration in Android

accel. control: layers

LAYER_TYPE_SOFTWARE - backed by a Bitmap as if it was a buffer,

rendered in the SW model.

*Best suited for compatibility:

● API - if your min API is less than 11.

● hardware - some GPUs excel in different kinds of animations and

have poor performance in other kinds of animations.

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 47: Custom views and hardware acceleration in Android

accel. control: getters

How do you determine dynamically whether a view is HW accelerated:

● View.isHardwareAccelerated()

● Canvas.isHardwareAccelerated()

Prefer Canvas.isHardwareAccelerated() especially within the drawing

code since a view attached to a hardware accel. window can still be

drawn in the SW model (like when it’s drawn to a bitmap for caching).

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 48: Custom views and hardware acceleration in Android

● HW accel. - good but not all the time.● GPUs - have strong and weak points; make

sure you use the strong points and avoid the weak ones (dah!).

● Less is more - create only what you need and when you need it and avoid new instances whenever possible.

● Analyze and adapt - your custom views at all times.

Key points

Learn to accel(erate) by Royi Benyossef (+royiby).

Page 49: Custom views and hardware acceleration in Android

“We Should Be Building Great Things. Things That Don’t yet Exist”

Page 50: Custom views and hardware acceleration in Android

I hope you liked it.

by Royi Benyossef

Thank you for listening