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 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 its 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 Dont yet Exist
  • I hope you liked it. by Royi Benyossef Thank you for listening

Top Related