AndroidAndroidAndroidAndroid 硬件加速的硬件加速的硬件加速的硬件加速的2D2D2D2D绘图研究绘图研究绘图研究绘图研究
Android HW Accelerated 2D RenderingAndroid HW Ac...
2011-12-20 Roger, UC
Introduction
2011-12-20 Roger, UC
HW Accelerated 2D RenderingHW Accelerated 2D RenderingHW Accelerated 2D RenderingHW Accelerated 2D Re...
2011-12-20 4
Some Android Graphics True Facts...Some Android Graphics True Facts...Some Android Graphics True Facts...Some...
2011-12-20 Roger, UC
Controlling Hardware AccelerationControlling Hardware AccelerationControlling Hardware AccelerationCo...
2011-12-20 Roger, UC
Application LevelApplication LevelApplication LevelApplication Level
• Android 3.x 默认为false
• Android...
2011-12-20 Roger, UC
Activity/Window LevelActivity/Window LevelActivity/Window LevelActivity/Window Level
• Window Level 只...
2011-12-20 Roger, UC
View LevelView LevelView LevelView Level
• 将View的Layer Type设置为SW Layer,可以使View创建
一个内部的位图缓存,View的内容先绘制...
2011-12-20 Roger, UC
View Layer TypeView Layer TypeView Layer TypeView Layer Type
• 在Android 3.0+后,View一共有3种Layer Type
– N...
2011-12-20 Roger, UC
2D Rendering Mode2D Rendering Mode2D Rendering Mode2D Rendering Mode
• 在Android 3.0后,一共有5种主要的2D渲
染模式,...
2011-12-20 Roger, UC
Cont.Cont.Cont.Cont.
3.Window Surface非硬件加速,View的Layer Type为
None,在主线程(UI线程)中绘图(View.onDraw)
4.Window ...
2011-12-20 Roger, UC
CanvasCanvasCanvasCanvas
• Android3.0+后有3种不同的Canvas类型:
– android.graphics.Canvas
• 通过Bitmap生成的Canvas
...
2011-12-20 Roger, UC
Benchmark
2011-12-20 Roger, UC
https://github.com/rogeryi/0xbench_2d_hw
0xbench_2d_hw0xbench_2d_hw0xbench_2d_hw0xbench_2d_hw
2011-12-20 Roger, UC
TestTestTestTest
• 对应渲染模式1~4
– 2D (SV)
– 2D (HW)
– 2D (SW1)
– 2D (SW2)
• 测试项包括:
– Draw Canvas 填充整个
Ca...
2011-12-20 Roger, UC
SamsungSamsungSamsungSamsung
Galaxy TabGalaxy TabGalaxy TabGalaxy Tab
10.1 (3.2)10.1 (3.2)10.1 (3.2)1...
2011-12-20 Roger, UC
Nexus SNexus SNexus SNexus S
(4.0.3)(4.0.3)(4.0.3)(4.0.3)
Draw Circle2Draw Circle2Draw Circle2Draw Ci...
2011-12-20 Roger, UC
Basic ConclutionsBasic ConclutionsBasic ConclutionsBasic Conclutions
• 模式1总是比模式3要快,因为没有异步请求(invalidat...
2011-12-20 Roger, UC
Cont.Cont.Cont.Cont.
• 硬件加速绘图主要的优势在于
– 位图绘制,特别是带拉伸,旋转,扭曲
– 图层混合
• 硬件加速绘图主要的劣势在于文本绘制
• 对于图形填充(贴图平铺/渐变/...
2011-12-20 Roger, UC
Some TipsSome TipsSome TipsSome Tips
• 硬件加速绘图不是Silver Bullet,always profile
your bottleneck
• 避免在UI线程...
2011-12-20 Roger, UC
RenferenceRenferenceRenferenceRenference
• Android 3.0 Hardware Acceleration
• New Gingerbread API: S...
The EndThe EndThe EndThe End
Thank you for your listening
Yours Sincerely, Roger
Upcoming SlideShare
Loading in …5
×

Android Hardware Accelerated 2D Rendering

5,402 views

Published on

Introduce the new Hardware accelerated 2D rendering support since Android 3.0

Published in: Technology
2 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
5,402
On SlideShare
0
From Embeds
0
Number of Embeds
1,881
Actions
Shares
0
Downloads
78
Comments
2
Likes
1
Embeds 0
No embeds

No notes for slide

Android Hardware Accelerated 2D Rendering

  1. 1. AndroidAndroidAndroidAndroid 硬件加速的硬件加速的硬件加速的硬件加速的2D2D2D2D绘图研究绘图研究绘图研究绘图研究 Android HW Accelerated 2D RenderingAndroid HW Accelerated 2D RenderingAndroid HW Accelerated 2D RenderingAndroid HW Accelerated 2D Rendering RogerRogerRogerRoger yixx@ucweb.com roger2yi@gmail.com www.twitter.com/roger2yi https://plus.google.com/
  2. 2. 2011-12-20 Roger, UC Introduction
  3. 3. 2011-12-20 Roger, UC HW Accelerated 2D RenderingHW Accelerated 2D RenderingHW Accelerated 2D RenderingHW Accelerated 2D Rendering • 硬件加速的2D绘图主要包括两个方面 – 基本图元绘制(Primitive Drawing - Shape, Text,Image) – 图层混合(Layer/Surface Composite)
  4. 4. 2011-12-20 4 Some Android Graphics True Facts...Some Android Graphics True Facts...Some Android Graphics True Facts...Some Android Graphics True Facts... • 从Android 1.0开始,窗口混合就开 始使用硬件加速 – 右图包含了4个窗口,分别是 - 状 态栏,壁纸,lanucher和菜单 – 像通知栏的拖拉,Activities的切换 动画,菜单,对话框的弹出/消失 动画都使用了硬件加速进行窗口混 合 • 在Android 3.0之后,窗口内部的内 容绘制(Canvas)才开始支持硬件 加速
  5. 5. 2011-12-20 Roger, UC Controlling Hardware AccelerationControlling Hardware AccelerationControlling Hardware AccelerationControlling Hardware Acceleration • Application • Activity • Window • View
  6. 6. 2011-12-20 Roger, UC Application LevelApplication LevelApplication LevelApplication Level • Android 3.x 默认为false • Android 4.x 默认为true(可以通过系统设 置关闭)
  7. 7. 2011-12-20 Roger, UC Activity/Window LevelActivity/Window LevelActivity/Window LevelActivity/Window Level • Window Level 只能开不能关 • 可以在清单文件中设置Activity关闭,然后根据运 行时Intent传入的参数再打开窗口的硬件加速
  8. 8. 2011-12-20 Roger, UC View LevelView LevelView LevelView Level • 将View的Layer Type设置为SW Layer,可以使View创建 一个内部的位图缓存,View的内容先绘制到位图,然后再 把位图拷贝到Window Surface上 • 相当于关闭该View的硬件加速绘图
  9. 9. 2011-12-20 Roger, UC View Layer TypeView Layer TypeView Layer TypeView Layer Type • 在Android 3.0+后,View一共有3种Layer Type – None,默认类型,View的内容直接绘制到Window Surface上面 – SW Layer,创建一个内部的位图缓存,View的内容先 绘制到位图,然后再把位图拷贝到Window Surface上 – HW Layer,创建一个内部的硬件加速的帧缓存,View 的内容先绘制到帧缓存,然后再拷贝到Window Surface上,只有在Window是硬件加速时才支持,否 则自动变为SW Layer
  10. 10. 2011-12-20 Roger, UC 2D Rendering Mode2D Rendering Mode2D Rendering Mode2D Rendering Mode • 在Android 3.0后,一共有5种主要的2D渲 染模式,1主要用于游戏,2,3,4都可以 用于一般的View Hierachy渲染,5主要用 于动画和实时交互场景 1.通过SurfaceHolder获得Window或者SurfaceView的 Surface Canvas(SurfaceHolder.lockCanvas),在单 独的Render Thread中绘图 2.Window Surface硬件加速,View的Layer Type为 None,在主线程(UI线程)中绘图(View.onDraw)
  11. 11. 2011-12-20 Roger, UC Cont.Cont.Cont.Cont. 3.Window Surface非硬件加速,View的Layer Type为 None,在主线程(UI线程)中绘图(View.onDraw) 4.Window Surface硬件加速,View的Layer Type为SW, 在主线程(UI线程)中绘图(View.onDraw) 5.Window Surface硬件加速,View的Layer Type为HW, 在主线程(UI线程)中绘图(View.onDraw)
  12. 12. 2011-12-20 Roger, UC CanvasCanvasCanvasCanvas • Android3.0+后有3种不同的Canvas类型: – android.graphics.Canvas • 通过Bitmap生成的Canvas • 当View的设置为SW Layer时,View在onDraw中获得的 Canvas – android.view.Surface$CompatibleCanvas • 通过SurfaceHolder.lockCanvas获得的Canvas • 当Window非硬件加速时,Window中的View在onDraw中获得的 Canvas – android.view.GLES20RecordingCanvas (HWHWHWHW Accelerated!!!Accelerated!!!Accelerated!!!Accelerated!!!) • 当Window硬件加速时,Window中的View在onDraw中获得的 Canvas(View的 Layer Type为None或者HW) – isHardwareAccelerated用于判断Canvas是否使用硬件加速
  13. 13. 2011-12-20 Roger, UC Benchmark
  14. 14. 2011-12-20 Roger, UC https://github.com/rogeryi/0xbench_2d_hw 0xbench_2d_hw0xbench_2d_hw0xbench_2d_hw0xbench_2d_hw
  15. 15. 2011-12-20 Roger, UC TestTestTestTest • 对应渲染模式1~4 – 2D (SV) – 2D (HW) – 2D (SW1) – 2D (SW2) • 测试项包括: – Draw Canvas 填充整个 Canvas – Draw Circle2 圆形填充 – Draw Rect 矩形填充 – Draw Arc 弧形填充 – Draw Image 位图绘制 – Draw Text 文本绘制
  16. 16. 2011-12-20 Roger, UC SamsungSamsungSamsungSamsung Galaxy TabGalaxy TabGalaxy TabGalaxy Tab 10.1 (3.2)10.1 (3.2)10.1 (3.2)10.1 (3.2) Draw Circle2Draw Circle2Draw Circle2Draw Circle2 (texture/gradi(texture/gradi(texture/gradi(texture/gradi ent/color)ent/color)ent/color)ent/color) Draw RectDraw RectDraw RectDraw Rect (texture/gradi(texture/gradi(texture/gradi(texture/gradi ent/color)ent/color)ent/color)ent/color) Draw ArcDraw ArcDraw ArcDraw Arc (texture/gradi(texture/gradi(texture/gradi(texture/gradi ent/color)ent/color)ent/color)ent/color) Draw ImageDraw ImageDraw ImageDraw Image (w/o filter)(w/o filter)(w/o filter)(w/o filter) Draw TextDraw TextDraw TextDraw Text 2D (HW) 25/24.6/24.5 27.1/24/28 22.3/24/27 34/34 5 2D (SW1) 52.3/31/38 19.5/7/10.2 15/15.6/18.2 10.6/22 12 2D (SW2) 26.9/21/22 15.1/7/8.5 12/13.6/15.4 9.6/16.6 11 2D (SV) 51.7/38/46.8 19/10.5/13.8 13.4/21/25.8 11/25 13.1 Draw CanvasDraw CanvasDraw CanvasDraw Canvas (texture/gra(texture/gra(texture/gra(texture/gra dient/color)dient/color)dient/color)dient/color) 59.4/24/60 43.3/46/56 58.5/21/60 27.6/17/28
  17. 17. 2011-12-20 Roger, UC Nexus SNexus SNexus SNexus S (4.0.3)(4.0.3)(4.0.3)(4.0.3) Draw Circle2Draw Circle2Draw Circle2Draw Circle2 (texture/gradie(texture/gradie(texture/gradie(texture/gradie nt/color)nt/color)nt/color)nt/color) Draw RectDraw RectDraw RectDraw Rect (texture/gradie(texture/gradie(texture/gradie(texture/gradie nt/color)nt/color)nt/color)nt/color) Draw ArcDraw ArcDraw ArcDraw Arc (texture/gradi(texture/gradi(texture/gradi(texture/gradi ent/color)ent/color)ent/color)ent/color) Draw ImageDraw ImageDraw ImageDraw Image (w/o filter)(w/o filter)(w/o filter)(w/o filter) DrawDrawDrawDraw TextTextTextText 2D (HW) 30/30.0/31.2 45/16/45.7 40.5/13/44 28/28 3.2 2D (SW1) 52.6/49.4/50 37/17.7/28.6 47.5/29.7/43 28.5/49.2 8.5 2D (SW2) 43.7/36.4/39 27.4/16.6/22 35.6/24/32.4 21.4/32.2 8 2D (SV) 50.5/51.6/50 33.3/23.4/32 46/35.5/47.8 35.5/55.6 9 Draw CanvasDraw CanvasDraw CanvasDraw Canvas (texture/gradient(texture/gradient(texture/gradient(texture/gradient /color)/color)/color)/color) 55.5/55.6/54.5 42/42/53.8 55.7/55.9/55.4 48.2/42.8/41.4
  18. 18. 2011-12-20 Roger, UC Basic ConclutionsBasic ConclutionsBasic ConclutionsBasic Conclutions • 模式1总是比模式3要快,因为没有异步请求(invalidate/onDraw) 的延迟 • 模式3总是比模式4要快,模式4多了一次位图拷贝 • 模式2(硬件加速)跟模式3(非硬件加速)相比,有些快,有些慢 • 在硬件加速时,drawXXX方法实际只是将绘图指令存储到一个内部的 绘图指令序列中(DisplayList),实际的绘制发生在onDraw调用之 后 • 硬件加速的位图拉伸/旋转/扭曲绘制更平滑,filter bitmap flag总是打 开的
  19. 19. 2011-12-20 Roger, UC Cont.Cont.Cont.Cont. • 硬件加速绘图主要的优势在于 – 位图绘制,特别是带拉伸,旋转,扭曲 – 图层混合 • 硬件加速绘图主要的劣势在于文本绘制 • 对于图形填充(贴图平铺/渐变/纯色)情况比较复杂,一般来说需要 Alpha混合的话,硬件加速绘图会比较快 • 并且需要绘制的图形/位图越多,越复杂的场景,硬件加速的优势越 明显 • 充分利用硬件加速的图层混合(开启View的HW Layer)是实现60fps 的实时交互和动画的关键
  20. 20. 2011-12-20 Roger, UC Some TipsSome TipsSome TipsSome Tips • 硬件加速绘图不是Silver Bullet,always profile your bottleneck • 避免在UI线程或者渲染线程中进行IO和网络操作 (使用Strict Mode进行检测) • 避免频繁修改位图(Bitmap),图形 (Shape),避免在绘图方法中创建画笔(Paint) 和路径(Path),特别是处于硬件加速时
  21. 21. 2011-12-20 Roger, UC RenferenceRenferenceRenferenceRenference • Android 3.0 Hardware Acceleration • New Gingerbread API: StrictMode • How about some Android graphics true facts? • How about some Android graphics true facts? [More] • Understanding Hardware Acceleration on Mobile Browsers • Android 圖形系統 -- 設計與實做分析
  22. 22. The EndThe EndThe EndThe End Thank you for your listening Yours Sincerely, Roger

×