Learn to accel(erate)

Download Learn to accel(erate)

Post on 23-Aug-2014

143 views

Category:

Mobile

2 download

Embed Size (px)

DESCRIPTION

Hardware acceleration is not a tool but an important feature you must be aware of as it can cause a lot of damage if unattended.

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 thats not how you write excel :P
  • Learn to accel(erate) by Royi Benyossef (+royiby). Motivation
  • 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.
  • Learn to accel(erate) by Royi Benyossef (+royiby). prolog - HW accel.
  • 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).
  • 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 items 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!
  • 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.
  • Learn to accel(erate) by Royi Benyossef (+royiby). Disable? why?
  • 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.
  • Learn to accel(erate) by Royi Benyossef (+royiby). Dont push it.
  • Check! Open the list: http://developer.android.com/guide/topics/graphics/hardware- accel.html#drawing-support. 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. * Heres Ran Nachmanys 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).
  • Tips, tricks and patterns Learn to accel(erate) by Royi Benyossef (+royiby).
  • 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 theyre changed. Bitmaps are reloaded to the GPU whenever theyre 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: Learn to accel(erate) by Royi Benyossef (+royiby).
  • HW accel. control Activity: 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