Upgrade to Pro — share decks privately, control downloads, hide ads and more …

View To Pixel (Android Summit & DroidconUk 2017)

View To Pixel (Android Summit & DroidconUk 2017)

Britt Barak

August 24, 2017
Tweet

More Decks by Britt Barak

Other Decks in Technology

Transcript

  1. So What Does That Mean? Measure/ Layout Draw Sync &

    Upload Execute Swap Buffers VSync/Misc
  2. 1. Steps will be called by order 2. A step

    will be called up to once per frame
  3. Order Is Guaranteed Choreographer.java void doFrame(long frameTimeNanos, int frame) {

    //... doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos); doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos); doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos); doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos); //… }
  4. Step 1: Measure Goal: obtain view size, including its descendants

    size, agreed by its parent. REF: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
  5. Multiple measure() Calls measure() may be called more than once

    For example: - Call once with UNSPECIFIED - If size too big/small- evaluate - Call with exact size
  6. Step 1: Measure - What’s View SIZE? - width &

    height (aka: drawing width & drawing height) - - Actual size on screen, at drawing time and after layout. REF: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
  7. Step 1: Measure - What’s View SIZE? - width &

    height (aka: drawing width & drawing height) - - Actual size on screen, at drawing time and after layout. - measured width & measured height - how big a view wants to be within its parent REF: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
  8. ViewTreeObserver • Provided by the view hierarchy • getViewTreeObserver() •

    Register listeners to view tree global changes https://developer.android.com/reference/android/view/ViewTreeObserver.html
  9. Step 2: Layout Goal : set position for view and

    all its children, respect parent’s constraints. REF: http://developer.android.com/reference/android/view/View.html#onLayout(boolean, int, int, int, int)
  10. Root View : RelativeLayout 1) By views’ requests • Then

    relativeLayout calculates by relations & weights 2) Determine final positions
  11. Root View : RelativeLayout Example: • Every view is RelativeLayout

    • Per Leaf change: 2*2*2 = 8 traversals X2 X2 X2
  12. It Usually Works Fine… But... • Root elements • Deep

    view hierarchy • Too many (i.e. list) Can hurt.
  13. What can we do? • Flatten hierarchy : ◦ Removing

    unneeded views ◦ Merge on include ◦ flatter layout • Beware of double layout taxation • Minimize requestLayout()
  14. Simple VS Complex Layouts • Simple : LinearLayout, FrameLayout, TableLayout

    ◦ → Nesting → Performance Problems • Complex : RelativeLayout , GridLayout ◦ → shouldn’t be @ top hierarchy ◦ → hard to use & maintain
  15. Constraint Layout • Api 9+ • Performance oriented: ◦ Reduce

    nesting ◦ Improve measure/layout ◦ Best practice : top level, without nesting layouts • User friendly: ◦ Expressive ◦ New layout Editor!
  16. Step3: Draw 1. Heavy onDraw() a. Object allocations 2. Overdraw

    a. Redundant backgrounds / transparency b. Wasteful onDraw - quickReject() - clipRect()
  17. GPU Profiling Tool • A graph per visible app •

    A bar per frame • 16ms benchmark • Crossing = skipping frame https://developer.android.com/studio/profile/dev-options-rendering.html
  18. Frame Metrics On Android N+ - Per-Frame data: - Windos.OnFrameMetricsAvailableListener()

    - Aggregated data: - FrameMetricsAggregator (Support lib v26)
  19. How Does A View Display On Screen? Screen GPU Polygons

    Textures CPU new Button() DisplayList