Android Hardware Accelerated 2D Rendering
-
Upload
rogeryi -
Category
Technology
-
view
4.880 -
download
12
description
Transcript of Android Hardware Accelerated 2D Rendering
Android Android Android Android 硬件加速的硬件加速的硬件加速的硬件加速的2D2D2D2D绘图研究绘图研究绘图研究绘图研究Android HW Accelerated 2D RenderingAndroid HW Accelerated 2D RenderingAndroid HW Accelerated 2D RenderingAndroid HW Accelerated 2D Rendering
[email protected]@gmail.comwww.twitter.com/roger2yihttps://plus.google.com/
2011-12-20 Roger, UC
Introduction
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)
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)才开始支持硬件加速
2011-12-20 Roger, UC
Controlling Hardware AccelerationControlling Hardware AccelerationControlling Hardware AccelerationControlling Hardware Acceleration
• Application
• Activity
• Window
• View
2011-12-20 Roger, UC
Application LevelApplication LevelApplication LevelApplication Level
• Android 3.x 默认为false
• Android 4.x 默认为true(可以通过系统设置关闭)
2011-12-20 Roger, UC
Activity/Window LevelActivity/Window LevelActivity/Window LevelActivity/Window Level
• Window Level 只能开不能关
• 可以在清单文件中设置Activity关闭,然后根据运行时Intent传入的参数再打开窗口的硬件加速
2011-12-20 Roger, UC
View LevelView LevelView LevelView Level
• 将View的Layer Type设置为SW Layer,可以使View创建一个内部的位图缓存,View的内容先绘制到位图,然后再把位图拷贝到Window Surface上
• 相当于关闭该View的硬件加速绘图
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
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)
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)
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 (HW HW HW HW Accelerated!!!Accelerated!!!Accelerated!!!Accelerated!!!)
• 当Window硬件加速时,Window中的View在onDraw中获得的 Canvas(View的Layer Type为None或者HW)
– isHardwareAccelerated用于判断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 填充整个Canvas
– Draw Circle2 圆形填充
– Draw Rect 矩形填充
– Draw Arc 弧形填充
– Draw Image 位图绘制
– Draw Text 文本绘制
2011-12-20 Roger, UC
Samsung Samsung Samsung Samsung Galaxy Tab Galaxy Tab Galaxy Tab Galaxy Tab 10.1 (3.2)10.1 (3.2)10.1 (3.2)10.1 (3.2)
Draw Circle2 Draw Circle2 Draw Circle2 Draw Circle2 (texture/gradi(texture/gradi(texture/gradi(texture/gradient/color)ent/color)ent/color)ent/color)
Draw Rect Draw Rect Draw Rect Draw Rect (texture/gradi(texture/gradi(texture/gradi(texture/gradient/color)ent/color)ent/color)ent/color)
Draw Arc Draw Arc Draw Arc Draw Arc (texture/gradi(texture/gradi(texture/gradi(texture/gradient/color)ent/color)ent/color)ent/color)
Draw Image Draw Image Draw Image Draw 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 Canvas Draw Canvas Draw Canvas Draw Canvas (texture/gra(texture/gra(texture/gra(texture/gradient/color)dient/color)dient/color)dient/color)
59.4/24/60
43.3/46/56
58.5/21/60
27.6/17/28
2011-12-20 Roger, UC
Nexus S Nexus S Nexus S Nexus S (4.0.3)(4.0.3)(4.0.3)(4.0.3)
Draw Circle2 Draw Circle2 Draw Circle2 Draw Circle2 (texture/gradie(texture/gradie(texture/gradie(texture/gradient/color)nt/color)nt/color)nt/color)
Draw Rect Draw Rect Draw Rect Draw Rect (texture/gradie(texture/gradie(texture/gradie(texture/gradient/color)nt/color)nt/color)nt/color)
Draw Arc Draw Arc Draw Arc Draw Arc (texture/gradi(texture/gradi(texture/gradi(texture/gradient/color)ent/color)ent/color)ent/color)
Draw Image Draw Image Draw Image Draw Image (w/o filter)(w/o filter)(w/o filter)(w/o filter)
Draw Draw Draw Draw 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 Canvas Draw Canvas Draw Canvas Draw 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
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总是打开的
2011-12-20 Roger, UC
Cont.Cont.Cont.Cont.
• 硬件加速绘图主要的优势在于– 位图绘制,特别是带拉伸,旋转,扭曲– 图层混合
• 硬件加速绘图主要的劣势在于文本绘制
• 对于图形填充(贴图平铺/渐变/纯色)情况比较复杂,一般来说需要Alpha混合的话,硬件加速绘图会比较快
• 并且需要绘制的图形/位图越多,越复杂的场景,硬件加速的优势越明显
• 充分利用硬件加速的图层混合(开启View的HW Layer)是实现60fps的实时交互和动画的关键
2011-12-20 Roger, UC
Some TipsSome TipsSome TipsSome Tips
• 硬件加速绘图不是Silver Bullet,always profile your bottleneck
• 避免在UI线程或者渲染线程中进行IO和网络操作(使用Strict Mode进行检测)
• 避免频繁修改位图(Bitmap),图形(Shape),避免在绘图方法中创建画笔(Paint)和路径(Path),特别是处于硬件加速时
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 圖形系統 -- 設計與實做分析
The EndThe EndThe EndThe End
Thank you for your listeningYours Sincerely, Roger