Successfully reported this slideshow.

Google I/O 2011, Android Accelerated Rendering

29

Share

Upcoming SlideShare
Graphiti presentation
Graphiti presentation
Loading in …3
×
1 of 119
1 of 119

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Google I/O 2011, Android Accelerated Rendering

  1. 1. Android Rendering Romain Guy @romainguy Chet Haase @chethaase May 11, 2011 Feedback goo.gl/wI57L Hashtags #io2011, #Android
  2. 2. Android Accelerated Rendering Romain Guy @romainguy Chet Haase @chethaase May 11, 2011 Feedback goo.gl/wI57L Hashtags #io2011, #Android
  3. 3. Hardware OpenGL
  4. 4. Why now?
  5. 5. memcpy (MiB/s) Pixel count 1100 825 550 275 0 G1 Droid N1 NS XOOM
  6. 6. UI on the GPU
  7. 7. on the GPU UI
  8. 8. Software rendering
  9. 9. Hardware rendering
  10. 10. Drawing a list
  11. 11. Drawing a list
  12. 12. Drawing a button
  13. 13. Drawing a button
  14. 14. Drawing a path
  15. 15. Drawing a path
  16. 16. Drawing a path
  17. 17. attribute vec4 position; attribute vec2 texCoords; uniform mat4 transform; uniform mat4 screenSpace; varying vec2 outTexCoords; varying vec2 linear; void main(void) { outTexCoords = texCoords; linear = vec2((screenSpace * position).x, 0.5); gl_Position = transform * position; } Text+gradient vertex shader
  18. 18. precision mediump float; varying vec2 outTexCoords; varying vec2 linear; uniform sampler2D sampler; uniform sampler2D gradientSampler; void main(void) { lowp vec4 color; vec4 gradient = texture2D(gradientSampler, linear); color = gradient * texture2D(sampler, outTexCoords).a; gl_FragColor = color; } Text+gradient fragment shader
  19. 19. ?
  20. 20. android:hardwareAccelerated="true"
  21. 21. Hardware acceleration control
  22. 22. 1 <application android:hardwareAccelerated="true"> 2 <activity ... /> 3 <activity android:hardwareAccelerated="false" /> 4 </application>
  23. 23. getWindow().setFlags(     WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,     WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
  24. 24. view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  25. 25. View.isHardwareAccelerated(); Canvas.isHardwareAccelerated(); GPU aware code
  26. 26. Caveats
  27. 27. Canvas clipPath clipRegion drawPicture drawPoints drawPosText drawTextOnPath drawVertices Paint setLinearText setMaskFilter setRasterizer Unsupported operations
  28. 28. 3D transforms, XOR, Diff, Canvas clipRect ReverseDiff ignored drawBitmapMesh Colors array ignored drawLines No anti-aliasing setDrawFilter Ignored Paint setDither Ignored setFilterBitmap Filtering always on setShadowLayer Works with text only Limitations
  29. 29. ComposeShader Cannot contain another ComposeShader Cannot contain two shaders of the same type Limitations
  30. 30. view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Workaround
  31. 31. New drawing model
  32. 32. ViewRoot ViewGroup View View Old model
  33. 33. ViewRoot ViewGroup View View invalidate() Old model
  34. 34. ViewRoot ViewGroup View View invalidate() Old model
  35. 35. ViewRoot ViewGroup invalidate() View View invalidate() Old model
  36. 36. ViewRoot ViewGroup invalidate() View View invalidate() Old model
  37. 37. ViewRoot invalidate() ViewGroup invalidate() View View invalidate() Old model
  38. 38. ViewRoot invalidate() ViewGroup invalidate() View View invalidate() Old model
  39. 39. ViewRoot ViewGroup View View Old model
  40. 40. ViewRoot draw() ViewGroup View View Old model
  41. 41. ViewRoot draw() ViewGroup View View Old model
  42. 42. ViewRoot draw() ViewGroup draw() View View Old model
  43. 43. ViewRoot draw() ViewGroup draw() View View Old model
  44. 44. • No logic! • List of drawing commands • Cached when a View changes DisplayList
  45. 45. Save 3 DrawPatch Save 3 ClipRect 20.00, 4.00, 99.00, 44.00, 1 Translate 20.00, 12.00 DrawText 9, 18, 9, 0.00, 19.00, 0x17e898 Restore RestoreToCount 0 DisplayList to draw a Button
  46. 46. ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  47. 47. ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A invalidate() New model
  48. 48. ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A mark dirty invalidate() New model
  49. 49. ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A mark dirty invalidate() New model
  50. 50. ViewRoot ViewGroup DisplayList invalidate() View A View B DisplayList B DisplayList A mark dirty invalidate() New model
  51. 51. ViewRoot invalidate() ViewGroup DisplayList invalidate() View A View B DisplayList B DisplayList A mark dirty invalidate() New model
  52. 52. rebuild ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  53. 53. rebuild ViewRoot ViewGroup rebuild DisplayList View A View B DisplayList B DisplayList A New model
  54. 54. ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  55. 55. draw() ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  56. 56. 1 background.draw(); 2 panel.draw(); 3 selector.draw(); 4 wifi.draw(); 5 turnOnWifi.draw(); 6 checkBox.draw(); Old model
  57. 57. 1 selector.draw(); 2 drawDisplayLists(); New model
  58. 58. Old model New model View.invalidate() Intersects a dirty View When does View.draw() run?
  59. 59. ViewRoot ViewGroup View View Old model
  60. 60. ViewRoot ViewGroup invalidate() View View Old model
  61. 61. ViewRoot ViewGroup invalidate() View View Old model
  62. 62. ViewRoot invalidate() ViewGroup invalidate() View View Old model
  63. 63. ViewRoot invalidate() ViewGroup invalidate() View View Old model
  64. 64. ViewRoot ViewGroup View View Old model
  65. 65. ViewRoot draw() ViewGroup View View Old model
  66. 66. ViewRoot draw() ViewGroup View View Old model
  67. 67. ViewRoot draw() ViewGroup draw() View View Old model
  68. 68. ViewRoot draw() ViewGroup draw() draw() View View Old model
  69. 69. ViewRoot ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  70. 70. ViewRoot invalidate() ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  71. 71. ViewRoot invalidate() ViewGroup DisplayList mark dirty View A View B DisplayList B DisplayList A New model
  72. 72. ViewRoot invalidate() ViewGroup DisplayList mark dirty View A View B DisplayList B DisplayList A New model
  73. 73. ViewRoot invalidate() invalidate() ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  74. 74. ViewRoot draw() invalidate() ViewGroup DisplayList View A View B DisplayList B DisplayList A New model
  75. 75. ViewRoot invalidate() ViewGroup DisplayList rebuild View A View B DisplayList B DisplayList A New model
  76. 76. ViewRoot invalidate() ViewGroup DisplayList rebuild View A View B DisplayList B DisplayList A New model
  77. 77. View.setLayerType(int type, Paint p)
  78. 78. Button draw() DisplayList Display Save DrawPatch ClipRect Translate DrawText Restore
  79. 79. Button draw() draw() DisplayList Layer Display Save DrawPatch ClipRect Translate DrawText Restore
  80. 80. view.setLayerType(View.LAYER_TYPE_NONE, null) 3 types of layers
  81. 81. view.setLayerType(View.LAYER_TYPE_SOFTWARE, null) Software layer
  82. 82. view.setLayerType(View.LAYER_TYPE_HARDWARE, null) Hardware layer
  83. 83. 1. Performance Hardware layers
  84. 84. Drawing a ListView Hardware layer DisplayList Software Time in ms 0.009 2.1 10.3 Measured when drawing a ListView with Android 3.0 on a Motorola XOOM
  85. 85. 2. Visual effects Hardware and software layers
  86. 86. ColorMatrix m = new ColorMatrix(); m.setSaturation(0.0f); Paint p = new Paint(); p.setColorFilter(new ColorMatrixColorFilter(m)); page.setLayerType(LAYER_TYPE_HARDWARE, p);
  87. 87. 3. Compatibility Software layers
  88. 88. 4. Animations Hardware and software layers
  89. 89. Opacity Position Size Orientation Origin alpha x scaleX rotation pivotX y scaleY rotationX pivotY translationX rotationY translationY Layers-friendly properties
  90. 90. ViewRoot ViewGroup DisplayList View A View B Layer B DisplayList A Layers-friendly properties
  91. 91. ViewRoot ViewGroup DisplayList View A View B Layer B DisplayList A setRotationY(45) Layers-friendly properties
  92. 92. ViewRoot ViewGroup DisplayList invalidate() View A View B Layer B DisplayList A setRotationY(45) Layers-friendly properties
  93. 93. ViewRoot mark dirty ViewGroup DisplayList invalidate() View A View B Layer B DisplayList A setRotationY(45) Layers-friendly properties
  94. 94. ViewRoot mark dirty ViewGroup DisplayList invalidate() View A View B Layer B DisplayList A setRotationY(45) Layers-friendly properties
  95. 95. Save 3 DrawDisplayList A DrawLayer B Restore Parent DisplayList
  96. 96. viewB.setRotationY(45) Save 3 DrawDisplayList A DrawLayer B Restore Parent DisplayList
  97. 97. viewB.setRotationY(45) Save 3 Save 3 DrawDisplayList A DrawDisplayList A DrawLayer B Rotate 0.0, 45.0, 0.0 Restore DrawLayer B Restore Parent DisplayList
  98. 98. view.setLayerType(View.LAYER_TYPE_HARDWARE, null); ObjectAnimator.ofFloat(view, "rotationY", 180).start(); Animating a complex View efficiently
  99. 99. view.setLayerType(View.LAYER_TYPE_HARDWARE, null); ObjectAnimator animator = ObjectAnimator.ofFloat( view, "rotationY", 180); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) {     view.setLayerType(View.LAYER_TYPE_NONE, null);   } }); animator.start();
  100. 100. Tips & tricks
  101. 101. 1. Don’t use too many views Keep your hierarchy flat
  102. 102. 2. Be careful of setAlpha() Without hardware layers, it costs 2x the fill-rate
  103. 103. 3. Reuse rendering objects Don’t create new Paints, Bitmaps, etc. in draw()
  104. 104. 4. Don’t modify Bitmaps often Every change causes a texture upload
  105. 105. 5. Don’t modify Paths often Every change causes a new rasterization
  106. 106. 6. Avoid overdraw GPUs are fill-rate limited
  107. 107. 7. Profile DDMS and traceview are your friends
  108. 108. Android blog d.android.com Romain’s blog curious-creature.org Chet’s blog graphics-geek.blogspot.com
  109. 109. Feedback http://goo.gl/wI57L Hashtags #io2011, #Android
  110. 110. Q&A Feedback http://goo.gl/wI57L Hashtags #io2011, #Android

Editor's Notes

  • \n
  • \n
  • \n
  • Since Android 1.0: hardware OpenGL ES 1.x and 2.x support\nWindows composition\nGames\nRich media (live wallpapers)\n
  • But the UI remains in software\n
  • Aren&amp;#x2019;t we done?\nFaster CPUs, multi-cores, etc.\n
  • New, richer experiences\nMore smoother animations\nMore and more pixels (tablets, hdpi screens)\n
  • Software cannot keep up\nGraph from 2008 to 2011\nYellow bar increase at the same rate or faster rate\n
  • Android 3.0 has many new features\nFavorite new UI toolkit feature: GPU support\nAll 2D ops run with OpenGL on the GPU\nAll standard apps run on the GPU\n
  • Android 3.0 has many new features\nFavorite new UI toolkit feature: GPU support\nAll 2D ops run with OpenGL on the GPU\nAll standard apps run on the GPU\n
  • Android 3.0 has many new features\nFavorite new UI toolkit feature: GPU support\nAll 2D ops run with OpenGL on the GPU\nAll standard apps run on the GPU\n
  • Android 3.0 has many new features\nFavorite new UI toolkit feature: GPU support\nAll 2D ops run with OpenGL on the GPU\nAll standard apps run on the GPU\n
  • Android 3.0 has many new features\nFavorite new UI toolkit feature: GPU support\nAll 2D ops run with OpenGL on the GPU\nAll standard apps run on the GPU\n
  • How does it work?\nHere&amp;#x2019;s the software rendering pipeline (pre-3.0 or apps without h/w in 3.0+)\n
  • The new hw accelerated pipeline translates Canvas calls to OpenGL\nOpenGL primitives are rasterized by the GPU\n
  • We have points, lines, triangles and textures\nEach draw call is a combo of those\nHere is a ListView, you can notice the fades at top/bottom\n
  • A simple button, 9patches are great in OpenGL\nResult is a simple mesh + texture\n
  • A path is rendered using a combination of geometry (quad) and an alpha texture\nColor is applied in the fragment shader\nText is drawn in a similar way\n
  • A path is rendered using a combination of geometry (quad) and an alpha texture\nColor is applied in the fragment shader\nText is drawn in a similar way\n
  • A path is rendered using a combination of geometry (quad) and an alpha texture\nColor is applied in the fragment shader\nText is drawn in a similar way\n
  • A path is rendered using a combination of geometry (quad) and an alpha texture\nColor is applied in the fragment shader\nText is drawn in a similar way\n
  • A path is rendered using a combination of geometry (quad) and an alpha texture\nColor is applied in the fragment shader\nText is drawn in a similar way\n
  • A path is rendered using a combination of geometry (quad) and an alpha texture\nColor is applied in the fragment shader\nText is drawn in a similar way\n
  • A path is rendered using a combination of geometry (quad) and an alpha texture\nColor is applied in the fragment shader\nText is drawn in a similar way\n
  • Here&amp;#x2019;s an example of the vertex shader used to draw text filled with a gradient\nShaders are generated at runtime by the pipeline\nSeveral millions of shaders can be thus generated\n
  • Drawing text with a gradient requires 2 textures:\n- an alpha texture for text\n- the gradient texture\n
  • To enable 2D h/w acceleration, put this in AndroidManifest.xml\nThat&amp;#x2019;s all you need to do\n
  • To enable 2D h/w acceleration, put this in AndroidManifest.xml\nThat&amp;#x2019;s all you need to do\n
  • Hardware acceleration can be enabled/disabled at several levels\nLet&amp;#x2019;s see how it can be done at each level\n
  • Here, enabled for the app, disabled for an activity\n
  • It can also be enabled for a specific window\n(for instance in Activity.onCreate())\n
  • H/w acceleration can be disabled per View\nWe&amp;#x2019;ll talk more about layers later\n
  • If you write custom views, checking the hw accel. status might be important\nCanvas.isHardwareAccelerated() is the preferred method\nView.isHA() &amp;&amp; !Canvas.isHA() is possible (e.g. bitmap cache)\n
  • Because of the nature of the 2D API and what GL can do, not everything will work\nSome features are missing, some are limited\nAll standard views/drawables will work\n
  • Some ops will be supported in future releases (drawPoints for instance)\nIf these ops are needed, draw in a Bitmap, or use a software layer\n
  • Some limitations will be lifted in future releases (AA lines for instance)\n
  • ComposeShaders are very useful\nCurrent constraints\n
  • An easy workaround is to use a software layer\nYou can also use a Bitmap, or Canvas.isHardwareAccelerated()\n
  • The new rendering pipeline changes the way rendering works\nIt&amp;#x2019;s still an immediate mode\nBut with a special kind of caching\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • In the old model, invalidate()ing a View meant redrawing each parent\nThis executes a lot of Java code\nBut the logic remains the same for the parents!\nExpensive!\n
  • DisplayList are simplified lists of drawing commands\nOne DisplayList per View, cached when a View changes\nTo redraw a View, we just re-issue its DisplayList\n
  • Here is an example of a DisplayList: this draws a button\nSeveral 100s of lines of Java code are required to produce this\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • A DisplayList can reference other DisplayLists\nNote that only one DisplayList is rebuilt, nothing else is touched!\nMuch less code is executed\nDrawing a display list = native code only\n\n
  • Let&amp;#x2019;s examine what happens when enabling WiFi in Settings\nWe&amp;#x2019;re going to tap the Wi-Fi list item and look at what happens in both models\n\n
  • Let&amp;#x2019;s examine what happens when enabling WiFi in Settings\nWe&amp;#x2019;re going to tap the Wi-Fi list item and look at what happens in both models\n\n
  • Let&amp;#x2019;s examine what happens when enabling WiFi in Settings\nWe&amp;#x2019;re going to tap the Wi-Fi list item and look at what happens in both models\n\n
  • Let&amp;#x2019;s examine what happens when enabling WiFi in Settings\nWe&amp;#x2019;re going to tap the Wi-Fi list item and look at what happens in both models\n\n
  • Let&amp;#x2019;s examine what happens when enabling WiFi in Settings\nWe&amp;#x2019;re going to tap the Wi-Fi list item and look at what happens in both models\n\n
  • When the selector turns blue, here are all the views that need to redraw\nEach View executes all of its Java drawing logic\nLot of code!\n
  • With the new model, only one piece of Java code is executed\nDrawing display lists is done in native\nOnly a series of draw calls, no logic whatsoever\n
  • If you change a View, call invalidate() on it\nDO NOT rely on calling invalidate() on a parent or sibling\nIt may have worked before, it won&amp;#x2019;t anymore\nIt was incorrect before, but worked anyway\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the old model, invalidate()ing a View meant redrawing intersecting Views (children for instance)\nNotice how both leaves are drawn even though they are not dirty\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • In the new model, invalidating a parent redraws that parent only\nNote how the children are not redrawn\n\n
  • We briefly mentioned layers earlier, for compatibility\nBut layers can do much more, they are very useful and efficient\nThe option Paint can be used to control compositing\n
  • Even with display lists, drawing is expensive\nA display list can have many operations\nAnimating a View requires to rerun these operations on every frame\nAnimating complex Views can be more efficient\n
  • With a layer (or offscreen cache/bitmap), drawing happens once (or when the view invalidates)\nOnly the layer is drawn on screen, it&amp;#x2019;s just a texture\nIt&amp;#x2019;s *very* efficient, especially for the GPU\nAnd layers have other advantages too\n
  • The type of layer LAYER_TYPE_NONE disables caching\nDefault value, View is not backed by an off-screen buffer\n
  • The View is rendered into a Bitmap, just like setDrawingCacheEnabled(true)\nForces software rendering, good for compatibility\nExpensive to refresh a software layer (sw rendering + texture upload)\nUse only if necessary! (costs Dalvik heap memory!!!!)\n\n
  • The View is rendered into an FBO, using OpenGL/HW acceleration\nReally fast, costs only GPU memory\nOptimized for blending\n
  • Hardware layers render View into a hardware texture\nDrawing code runs only when View.invalidate() is called\nSome animations are applied directly onto the layer\n
  • Perf comparison of \nDrawing a hardware layer is much faster, recommended when animating a complex View\nBut layers cost memory and refreshing a layer costs 2x the fillrate\n
  • Use the optional Paint to apply visual treatments:\nopacity, color filter, blending modes etc.\nYou can tint a view, make it grayscale, etc.\n
  • Launcher modified to display the icons in gray\nThe pages of icons are backed by hardware layers\nVery easy to do\n
  • Here is the code used in the previous screenshot\nJust sets a ColorFilter on the paint used by the hardware layer\nEasy!\n
  • Forces a View to render in software\nWorkaround for hw limitations or GL pipeline limitations\n
  • Related to performance, but worth talking about in detail\nSome View properties affect layers directly, which make them very cheap\n
  • When changing one of these properties, the View does not invalidate\nThe layer is not redrawn, instead the parent&amp;#x2019;s DisplayList is updated\nThis is a very efficient way to animate Views\nThese properties are also DisplayLists friendly\n
  • When setting one of these properties, the parent is invalidated, and the parent&amp;#x2019;s DisplayList is dirtied\nRebuilding the parent&amp;#x2019;s display list is usually very cheap, since it refers to DisplayLists/Layers\n
  • When setting one of these properties, the parent is invalidated, and the parent&amp;#x2019;s DisplayList is dirtied\nRebuilding the parent&amp;#x2019;s display list is usually very cheap, since it refers to DisplayLists/Layers\n
  • When setting one of these properties, the parent is invalidated, and the parent&amp;#x2019;s DisplayList is dirtied\nRebuilding the parent&amp;#x2019;s display list is usually very cheap, since it refers to DisplayLists/Layers\n
  • When setting one of these properties, the parent is invalidated, and the parent&amp;#x2019;s DisplayList is dirtied\nRebuilding the parent&amp;#x2019;s display list is usually very cheap, since it refers to DisplayLists/Layers\n
  • When setting one of these properties, the parent is invalidated, and the parent&amp;#x2019;s DisplayList is dirtied\nRebuilding the parent&amp;#x2019;s display list is usually very cheap, since it refers to DisplayLists/Layers\n
  • When setting one of these properties, the parent is invalidated, and the parent&amp;#x2019;s DisplayList is dirtied\nRebuilding the parent&amp;#x2019;s display list is usually very cheap, since it refers to DisplayLists/Layers\n
  • Here is a simplified version of the parent&amp;#x2019;s display list\nDoes NOT execute() viewB.draw()\n
  • Here is a simplified version of the parent&amp;#x2019;s display list\nDoes NOT execute() viewB.draw()\n
  • Here is a simplified version of the parent&amp;#x2019;s display list\nDoes NOT execute() viewB.draw()\n
  • If you want to animated a complex View (a ListView for instance), use layers\nThis is an example of a 3D rotation optimized with layers\nBut layers are costly to maintain (memory, refresh/fillrate)\n
  • Keep layers only for the duration of the animation\nEasy to do with an AnimatorListener\nSaves time and space\n
  • The new rendering pipeline changes the way rendering works\nIt&amp;#x2019;s still an immediate mode\nBut with a special kind of caching\n
  • The more views you have, the longer is takes to draw\nViewGroups in particular are very costly\nKeep your hierarchy flat\n
  • Changing the alpha of a View costs 2x the fillrate, if you are not using layers\nAvoid it on large views (half the size of the screen) or make sure you use hardware layers\n
  • Do NOT create Paints, Bitmaps, Shaders, etc. in draw methods\nCreating new ones is costly and will defeat internal caches and optimizations\nIt also avoids unnecessary GCs\n
  • Changing a Bitmap, even one pixel, is expensive\nIt will cause a texture upload, which is a copy to GPU memory\nAvoid doing it if you can\n
  • Paths are rendered using texture\nChanging a path causes the generation of an offscreen bitmap, software rasterization and texture upload\n
  • On current hardware, no more than 2.5x times the entire screen\nRemove obscured views (useless background bitmap for instance)\n
  • Profile profile profile\nWe&amp;#x2019;ve spent weeks profiling our code to optimize it on tablets\nNasty surprises sometimes (new bitmaps created on every frame, etc.)\nAlways profile your code before blaming someone else!\n
  • \n
  • \n
  • \n
  • ×