Custom views and hardware acceleration in Android

Post on 10-Jun-2015

598 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

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

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.

Learn to accel*(erate).

(.by Royi Benyossef (+royiby

.the guide to HW accel

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

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

Motivation

what do we want?!

Motivation

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

● Better looking UI.

● Smoother animations.

● Smaller mem. footprint.

● Smaller CPU load.

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

prolog - HW accel.

HOW?

Hardware

accelera

tion

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

prolog - HW accel.

What i

s that?

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.

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.

SW drawing model

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

What happens?

1. Invalidate hierarchy.

2. Draw hierarchy.

HW drawing model

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

What happens?

1. Invalidate hierarchy.

2. Record & update display lists.

3. Draw hierarchy.

Are you kidding me?!

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

Not sure if stupid...

Or didn’t make his point yet...

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.

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.

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.

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.

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

Yay!

Accelera

te all t

he

thin

gs!!!

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

Not yet.

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.

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

Wait….

There’s a disable

API!

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

Disable? why?

If it’

s so

good?

Why is

n’t it

always

on?

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.

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.

Worst case scenario?

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

What might happen?

● Invisible UI elements.

● Exceptions.

● Badly rendered pixels.

scenario example #1

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

scenario example #2

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

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

Wait….

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

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

Don’t push it.

So…?What to do?

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).

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).

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).

And...

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

If bugs w

ere

found...

Tips, tricks and patterns

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

Getting st

arted!

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).

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).

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).

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).

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).

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).

HW accel. control

Control is allowed in the following resolutions:

● Application.

● Activity.

● Window.

● View & layer.

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

HW accel. control

● Application:

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

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

HW accel. control

● Activity:

<application android:hardwareAccelerated="true">

<activity ... />

<activity android:hardwareAccelerated="false" />

</application>

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

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).

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).

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).

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).

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).

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).

● 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).

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

I hope you liked it.

by Royi Benyossef

Thank you for listening

top related