尝试和大家一起探讨以下问题:
- view绘制渲染机制和runloop什么关系?
- 所谓的列表卡顿,到底是什么原因引发的?
- 我们经常在drawrect方法里绘制代码,但该方法是谁调用的 何时调用的?
- drawrect方法内为何第一行代码往往要获取图形的上下文?
- layer的代理必须是view吗,可以是vc吗,为何CALayerDelegate 不能主动遵循?
- view绘制机制和CPU之间关系?
- view渲染机制和GPU之间关系?
- 所有的切圆角都很浪费性能吗?
- 离屏渲染很nb吗?
- 那些绘制API都是哪个类提供的 我如何系统的学习它?
- 如何优化CPU /GPU使用率?
view绘制渲染机制和runloop什么关系?
代码示例
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ZYYView *view = [[ZYYView alloc] init];
view.backgroundColor = [UIColor whiteColor];
view.bounds = CGRectMake(0, 0, 100, 100);
view.center = CGPointMake(100, 100);
[self.view addSubview:view];
}
@end
@implementation ZYYView
- (void)drawRect:(CGRect)rect {
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,200));
CGContextSetRGBFillColor(con, 0, 0, 1, 1);
CGContextFillPath(con);
}
@end
堆栈展示
底层原理
当在操作 UI 时,比如改变了 Frame、更新了 UIView/CALayer 的层次时,或者手动调用了 UIView/CALayer 的 setNeedsLayout/setNeedsDisplay方法后,这个 UIView/CALayer 就被标记为待处理,并被提交到一个全局的容器去。
苹果注册了一个 Observer 监听 BeforeWaiting(即将进入休眠) 和 Exit (即将退出Loop) 事件,回调去执行一个很长的函数:
_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv()。这个函数里会遍历所有待处理的 UIView/CAlayer 以执行实际的绘制和调整,并更新 UI 界面。
这个函数内部的调用栈大概是这样的:
_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv()
QuartzCore:CA::Transaction::observer_callback:
CA::Transaction::commit();
CA::Context::commit_transaction();
CA::Layer::layout_and_display_if_needed();
CA::Layer::layout_if_needed();
[CALayer layoutSublayers];
[UIView layoutSubviews];
CA::Layer::display_if_needed();
[CALayer display];
[UIView drawRect];
我们上图的堆栈信息 截图 ,看到巴拉巴拉一大堆调用堆栈信息,其实这就是个函数做的孽 。如何不能理解,那直接看下面的流程图吧。
流程图
<svg height="1359.75" version="1.1" width="1014.421875" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative; top: -0.671875px;"><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="stt" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,305.0547,18.3125)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">程序启动</tspan></text></svg> <text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op1t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,268.5547,125.5)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">UIApplicationMain()</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op2t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,162.6172,232.6875)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">主线程:我是UI线程不能停,Runloop来和我一起吧。</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op3t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,241.0938,339.875)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">MainRunloop create and run </tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op4t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,154.3359,447.0625)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">MainRunloop:我想睡觉了,observer,你那边有事吗?</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op5t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,4,554.25)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">observer:我去检查一下_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv()</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op6t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,205.9922,661.4375)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">我去看看 图层树中有没有待处理的对象</tspan></text><path fill="#ffffff" stroke="#000000" d="M33.75,16.875L0,33.75L67.5,67.5L135,33.75L67.5,0L0,33.75" stroke-width="2" font-family="sans-serif" font-weight="normal" id="cond" class="flowchart" transform="matrix(1,0,0,1,277.5547,754.3125)" style="font-family: sans-serif; font-weight: normal;"></path><text x="38.75" y="33.75" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="condt" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,277.5547,754.3125)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">有没有?</tspan><tspan dy="18" x="38.75"></tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op7t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,162.4609,890.125)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">CPU:我在更新图层树,一会交给Core Animation运走</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op8t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,118.6328,997.3125)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">Core Animation:把待处理的图层对象 通过IPC发送到渲染服务进程</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op9t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,228.4844,1104.5)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">GPU:渲染服务进程开始渲染工作</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op10t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,205.8906,1211.6875)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">GPU:Compositing\Offscreen Rendering </tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op11t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,297.5547,1318.875)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">展示到屏幕 </tspan></text><text x="20" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="sub1t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,602.7969,768.625)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">告诉runloop 让它睡会吧。有东西,我在叫你observer。</tspan></text><path fill="none" stroke="#000000" d="M345.0546875,57.1875C345.0546875,57.1875,345.0546875,109.31428690254688,345.0546875,122.49869882418716" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,164.375C345.0546875,164.375,345.0546875,216.50178690254688,345.0546875,229.68619882418716" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,271.5625C345.0546875,271.5625,345.0546875,323.6892869025469,345.0546875,336.87369882418716" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,378.75C345.0546875,378.75,345.0546875,430.8767869025469,345.0546875,444.06119882418716" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,485.9375C345.0546875,485.9375,345.0546875,538.0642869025469,345.0546875,551.2486988241872" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,593.125C345.0546875,593.125,345.0546875,645.2517869025469,345.0546875,658.4361988241872" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,700.3125C345.0546875,700.3125,345.0546875,739.9665999412537,345.0546875,751.3129390846007" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,821.8125C345.0546875,821.8125,345.0546875,873.9392869025469,345.0546875,887.1236988241872" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="350.0546875" y="831.8125" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">yes</tspan></text><path fill="none" stroke="#000000" d="M412.5546875,788.0625C412.5546875,788.0625,575.5197486574762,788.0625,599.7894477068073,788.0625" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="417.5546875" y="778.0625" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">no</tspan></text><path fill="none" stroke="#000000" d="M345.0546875,929C345.0546875,929,345.0546875,981.1267869025469,345.0546875,994.3111988241872" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,1036.1875C345.0546875,1036.1875,345.0546875,1088.3142869025469,345.0546875,1101.4986988241872" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,1143.375C345.0546875,1143.375,345.0546875,1195.5017869025469,345.0546875,1208.6861988241872" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M345.0546875,1250.5625C345.0546875,1250.5625,345.0546875,1302.6892869025469,345.0546875,1315.8736988241872" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path>
所谓的列表卡顿,到底是什么原因引发的?
iOS的mainRunloop是一个60fps的回调,也就是说每16.7ms会绘制一次屏幕,这个时间段内要完成view的缓冲区创建,view内容的绘制(如果重写了drawRect),这些CPU的工作。然后将这个缓冲区交给GPU渲染,这个过程又包括多个view的拼接(compositing),纹理的渲染(Texture)等,最终显示在屏幕上。整个过程就是我们上面画的流程图。 因此,如果在16.7ms内完不成这些操作,比如,CPU做了太多的工作,或者view层次过于多,图片过于大,导致GPU压力太大,就会导致“卡”的现象,也就是丢帧
我们经常在drawrect方法里绘制代码,但该方法是谁调用的 何时调用的?
产品绘图需求
首先我们假设有这样一个需求:实现下面的椭圆效果:
代码示例
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ZYYView *view = [[ZYYView alloc] init];
view.backgroundColor = [UIColor whiteColor];
view.bounds = CGRectMake(0, 0, 100, 100);
view.center = CGPointMake(100, 100);
[self.view addSubview:view];
}
@end
@implementation ZYYView
- (void)drawRect:(CGRect)rect {
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,200));
CGContextSetRGBFillColor(con, 0, 0, 1, 1);
CGContextFillPath(con);
}
@end
堆栈展示
底层原理
1、 在[ZYYView drawRect:] 方法之前,先调用了 [UIView(CALayerDelegate) drawLayer:inContext:] 和 [CALayer drawInContext:]
2、如果 [self.view addSubview:view]; 被注销掉 则 drawRect 不执行。可以肯定 drawRect
方法是由 addSubview 函数触发的。
流程图
<svg height="323.5" version="1.1" width="355.578125" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative; top: -0.4375px;"><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="stt" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,73.2344,4)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">[self.view addSubview:view]</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op1t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,82.5313,96.875)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">[CALayer drawInContext:]</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op2t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,4,189.75)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">[UIView(CALayerDelegate) drawLayer:inContext:]</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="et" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,99.9531,282.625)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">[ZYYView drawRect:]</tspan></text></svg>
drawrect方法内为何第一行代码总要获取图形的上下文
代码示例
CGContextRef con = UIGraphicsGetCurrentContext();
堆栈展示
底层原理
每一个UIView都有一个layer,每一个layer都有个content,这个content指向的是一块缓存,叫做backing store
当UIView被绘制时(从 CA::Transaction::commit:以后),CPU执行drawRect,通过context将数据写入backing store
当backing store写完后,通过render server交给GPU去渲染,将backing store中的bitmap数据显示在屏幕上
所以在 drawRect 方法中 要首先获取 context
layer的代理必须是view吗,可以是vc吗?为何CALayerDelegate 不能主动遵循?
代码示例
代码1
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ZYYLayer *layer = [ZYYLayer layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.position = CGPointMake(100, 100);
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
}
@end
@implementation ZYYLayer
- (void)drawInContext:(CGContextRef)ctx {
CGContextAddEllipseInRect(ctx, CGRectMake(0,0,100,200));
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
CGContextFillPath(ctx);
}
@end
代码二
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CALayer *layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.position = CGPointMake(100, 100);
layer.delegate = self;
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextAddEllipseInRect(ctx, CGRectMake(0,0,100,200));
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
CGContextFillPath(ctx);
}
@end
图标展示
综合以上2种不同的绘制函数加上uiview下的drawrect方法 一起区别 :
编号 | 所在的类或类别 | 方法 | 出现范围 | 可以使用的API | viewDidLoad | 优先级 |
---|---|---|---|---|---|---|
1 | UIView(UIViewRendering) | drawRect | 自定义view类 | UIkit 、CoreGraphics | 3 | |
2 | CALayer | drawInContext | 自定义layer类 | CoreGraphics | [layer setNeedsDisplay] | 1 |
3 | NSObject (CALayerDelegate) | drawLayer:inContext | vc类、自定义layer、view类 | UIkit、CoreGraphics | [layer setNeedsDisplay] layer.delegate = self | 2 |
底层原理
不能再将某个UIView设置为CALayer的delegate,因为UIView对象已经是它内部根层的delegate,再次设置为其他层的delegate就会出问题。
在设置代理的时候,它并不要求我们遵守协议,说明这个方法为非正式协议,就不需要再额外的显示遵守协议了
view绘制机制和CPU之间关系
创建对象
性能瓶颈:
创建对象会分配内存,对象过多,比较消耗 CPU 资源 。
优化方案:
1、尽量用轻量的对象代替重量的对象,可以对性能有所优化。比如 CALayer 比 UIView 要轻量,如果不需要响应触摸事件,用 CALayer 显示会更加合适。如果对象不涉及 UI 操作,则尽量放到后台线程去创建,但如果是包含了 CALayer 的控件,都只能在主线程创建和操作。
2、通过 Storyboard 创建视图对象时,其资源消耗会比直接通过代码创建对象要大非常多。
3、使用懒加载,尽量推迟对象创建的时间,并把对象的创建分散到多个任务中去。
调整对象
调整对象视图层级
性能瓶颈:
对象的调整也经常是消耗 CPU 资源的地方。视图层次调整时,UIView、CALayer 之间会出现很多方法调用与通知。
优化方案:
尽量的避免或者减少调整视图层次、添加和移除视图。
调整对象布局计算
性能瓶颈:视图布局的计算是 App 中最为常见的消耗 CPU 资源的地方
优化方案:不论通过何种技术对视图进行布局,其最终都会落到对 UIView.frame/bounds/center 等属性的调整上。对这些属性的调整非常消耗资源,所以尽量提前计算好布局,如果一次性可以调整好对应属性,就不要多次、频繁的计算和调整这些属性。
调整对象文本计算
性能瓶颈:如果一个界面中包含大量文本(比如微博微信朋友圈等),文本的宽高计算会占用很大一部分资源。
优化方案:用 [NSAttributedString boundingRectWithSize:options:context:] 来计算文本宽高,用 -[NSAttributedString drawWithRect:options:context:] 来绘制文本,记住放到后台线程进行以避免阻塞主线程。
图像的绘制
流程图
<svg height="4560.33984375" version="1.1" width="1263.32421875" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative; top: -0.078125px;"><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="stt" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,325.5547,175.5313)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">自定义ZYYView</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op1t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,330.9375,439.9375)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">初始化、坐标</tspan></text><text x="70.923828125" y="65.923828125" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="c1t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,254.0898,657.8574)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.330078125">确认 alloc setFrame?</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op2t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,278.1328,1015.2363)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">[self.view addSubview:view];</tspan></text></svg> <path fill="#ffffff" stroke="#000000" d="M60.603515625,30.3017578125L0,60.603515625L121.20703125,121.20703125L242.4140625,60.603515625L121.20703125,0L0,60.603515625" stroke-width="2" font-family="sans-serif" font-weight="normal" id="c2" class="flowchart" transform="matrix(1,0,0,1,264.7305,1238.4766)" style="font-family: sans-serif; font-weight: normal;"></path><text x="65.603515625" y="60.603515625" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="c2t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,264.7305,1238.4766)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.337890625">确认 addsubview ?</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op3t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,196.6172,1585.2148)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">(隐式) 此view的layer的CALayerDelegate设置成此view</tspan></text><text x="20" y="28.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="sub3t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,127.1094,1840.6211)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="-3.671875">1、首先CPU会为layer分配一块内存用来绘制bitmap,叫做backing store</tspan><tspan dy="18" x="20">2、layer创建指向这块bitmap缓冲区的指针,叫做CGContextRef</tspan><tspan dy="18" x="20"></tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op4t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,224.4375,2123.0273)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">调用此view的self.layer的drawInContext:方法</tspan><tspan dy="18" x="10"></tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op5t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,221.6016,2387.4336)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">执行 - (void)drawInContext:(CGContextRef)ctx</tspan></text><path fill="#ffffff" stroke="#000000" d="M190.96875,95.484375L0,190.96875L381.9375,381.9375L763.875,190.96875L381.9375,0L0,190.96875" stroke-width="2" font-family="sans-serif" font-weight="normal" id="c3" class="flowchart" transform="matrix(1,0,0,1,4,2480.3086)" style="font-family: sans-serif; font-weight: normal;"></path><text x="195.96875" y="190.96875" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="c3t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,4,2480.3086)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">if([self.delegate responseToSelector:@selector(drawLayer:inContext:)])</tspan><tspan dy="18" x="195.96875"></tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op6t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,150.5156,3087.7773)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">执行(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx</tspan></text><path fill="#ffffff" stroke="#000000" d="M129.09375,64.546875L0,129.09375L258.1875,258.1875L516.375,129.09375L258.1875,0L0,129.09375" stroke-width="2" font-family="sans-serif" font-weight="normal" id="c4" class="flowchart" transform="matrix(1,0,0,1,127.75,3242.5273)" style="font-family: sans-serif; font-weight: normal;"></path><text x="134.09375" y="129.09375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="c4t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,127.75,3242.5273)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">是否调用[super drawLayer:layer inContext:ctx] </tspan><tspan dy="18" x="134.09375"></tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op7t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,259.0391,3726.2461)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">执行 - (void)drawRect:(CGRect)rect</tspan><tspan dy="18" x="10"></tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op8t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,181.6406,3990.6523)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">使用 UIkit绘制API 或者 CoreGraphics绘制API,绘制bitmap</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="op9t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,256.6641,4255.0586)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">将layer的content指向生成的bitmap</tspan></text><text x="10" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="et" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,295.6328,4519.4648)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">交付layer的content属性</tspan><tspan dy="18" x="10"></tspan></text><text x="20" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="sub2t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,853.668,1279.6426)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">drawrect 方法不调用</tspan><tspan dy="18" x="20"></tspan></text><text x="20" y="19.4375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" id="sub1t" class="flowchartt" font-size="14px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,1082.4961,704.3438)" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">drawrect 方法不调用</tspan><tspan dy="18" x="20"></tspan></text><path fill="none" stroke="#000000" d="M385.9375,214.40625C385.9375,214.40625,385.9375,410.25966608710587,385.9375,436.9408687669411" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,478.8125C385.9375,478.8125,385.9375,631.4584060381167,385.9375,654.8621240537263" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,789.705078125C385.9375,789.705078125,385.9375,985.5584942121059,385.9375,1012.2396968919411" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="390.9375" y="799.705078125" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.330078125">yes</tspan></text><path fill="none" stroke="#000000" d="M517.78515625,723.78125C517.78515625,723.78125,1035.272804287728,723.78125,1079.4917195253267,723.78125" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="522.78515625" y="713.78125" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.328125">no</tspan></text><path fill="none" stroke="#000000" d="M385.9375,1054.111328125C385.9375,1054.111328125,385.9375,1211.6257607354783,385.9375,1235.4682773903846" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,1359.68359375C385.9375,1359.68359375,385.9375,1555.5370098371059,385.9375,1582.218212516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="390.9375" y="1369.68359375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.33203125">yes</tspan></text><path fill="none" stroke="#000000" d="M507.14453125,1299.080078125C507.14453125,1299.080078125,816.8033232688904,1299.080078125,850.6722536459565,1299.080078125" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="512.14453125" y="1289.080078125" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.330078125">no</tspan></text><path fill="none" stroke="#000000" d="M385.9375,1624.08984375C385.9375,1624.08984375,385.9375,1811.5368828047067,385.9375,1837.6220420481595" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,1897.49609375C385.9375,1897.49609375,385.9375,2093.349509837106,385.9375,2120.030712516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,2161.90234375C385.9375,2161.90234375,385.9375,2357.755759837106,385.9375,2384.436962516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,2426.30859375C385.9375,2426.30859375,385.9375,2465.9626936912537,385.9375,2477.3090328346007" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,2862.24609375C385.9375,2862.24609375,385.9375,3058.099509837106,385.9375,3084.780712516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="390.9375" y="2872.24609375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.33203125">yes</tspan></text><path fill="none" stroke="#000000" d="M767.875,2671.27734375C767.875,2671.27734375,792.875,2671.27734375,792.875,2671.27734375C792.875,2671.27734375,792.875,4494.46484375,792.875,4494.46484375C792.875,4494.46484375,385.9375,4494.46484375,385.9375,4494.46484375C385.9375,4494.46484375,385.9375,4509.83828830719,385.9375,4516.474091524258" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="772.875" y="2661.27734375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.33203125">no</tspan></text><path fill="none" stroke="#000000" d="M385.9375,3126.65234375C385.9375,3126.65234375,385.9375,3221.3582960292697,385.9375,3239.531850348485" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,3500.71484375C385.9375,3500.71484375,385.9375,3696.568259837106,385.9375,3723.249462516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="390.9375" y="3510.71484375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.33203125">yes</tspan></text><path fill="none" stroke="#000000" d="M644.125,3371.62109375C644.125,3371.62109375,669.125,3371.62109375,669.125,3371.62109375C669.125,3371.62109375,669.125,4494.46484375,669.125,4494.46484375C669.125,4494.46484375,385.9375,4494.46484375,385.9375,4494.46484375C385.9375,4494.46484375,385.9375,4509.83828830719,385.9375,4516.474091524258" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><text x="649.125" y="3361.62109375" text-anchor="start" font="10px "Arial"" stroke="none" fill="#000000" font-size="14px" font-family="sans-serif" font-weight="normal" style="text-anchor: start; font-style: normal; font-variant-caps: normal; font-weight: normal; font-size: 14px; line-height: normal; font-family: sans-serif;"><tspan dy="5.33203125">no</tspan></text><path fill="none" stroke="#000000" d="M385.9375,3765.12109375C385.9375,3765.12109375,385.9375,3960.974509837106,385.9375,3987.655712516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,4029.52734375C385.9375,4029.52734375,385.9375,4225.380759837106,385.9375,4252.061962516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path><path fill="none" stroke="#000000" d="M385.9375,4293.93359375C385.9375,4293.93359375,385.9375,4489.787009837106,385.9375,4516.468212516941" stroke-width="2" marker-end="url(#raphael-marker-endblock33)" font-family="sans-serif" font-weight="normal" style="font-family: sans-serif; font-weight: normal;"></path>
底层原理
我们回过头思考 图形的上下文 CGContextRef的创建历程。
• addsubview 的时候 触发的
• CPU会为layer分配一块内存用来绘制bitmap,叫做backing store
• layer创建指向这块bitmap缓冲区的指针,叫做CGContextRef
• 通过CoreGraphic的api,也叫Quartz2D,绘制bitmap
• 将layer的content指向生成的bitmap
其实 CGContextRef 的创建过程 就是CPU的工作过程
CPU 将view变成了bitmap 完成自己工作,剩下就是GPU的工作了。
view渲染机制和GPU之间关系
GPU功能
GPU处理的单位是Texture
基本上我们控制GPU都是通过OpenGL来完成的,但是从bitmap到Texture之间需要一座桥梁,Core Animation正好充当了这个角色:
Core Animation对OpenGL的api有一层封装,当我们的要渲染的layer已经有了bitmap content的时候,这个content一般来说是一个CGImageRef,CoreAnimation会创建一个OpenGL的Texture并将CGImageRef(bitmap)和这个Texture绑定,通过TextureID来标识。
这个对应关系建立起来之后,剩下的任务就是GPU如何将Texture渲染到屏幕上了。
GPU工作模式:
整个过程也就是一件事:CPU将准备好的bitmap放到RAM里,GPU去搬这快内存到VRAM中处理。
而这个过程GPU所能承受的极限大概在16.7ms完成一帧的处理,所以最开始提到的60fps其实就是GPU能处理的最高频率。
GPU性能瓶颈
因此,GPU的挑战有两个:
• 将数据从RAM搬到VRAM中
• 将Texture渲染到屏幕上
这两个中瓶颈基本在第二点上。渲染Texture基本要处理这么几个问题:
Compositing:
Compositing是指将多个纹理拼到一起的过程,对应UIKit,是指处理多个view合到一起的情况,如
[self.view addsubview : subview]
如果view之间没有叠加,那么GPU只需要做普通渲染即可。 如果多个view之间有叠加部分,GPU需要做blending。
加入两个view大小相同,一个叠加在另一个上面,那么计算公式如下:
R = S+D*(1-Sa)
R: 为最终的像素值
S: 代表 上面的Texture(Top Texture)
D: 代表下面的Texture(lower Texture)
Sa代表Texture的alpha值。
其中S,D都已经pre-multiplied各自的alpha值。
假如Top Texture(上层view)的alpha值为1,即不透明。那么它会遮住下层的Texture。即,R = S。是合理的。 假如Top Texture(上层view)的alpha值为0.5,S 为 (1,0,0),乘以alpha后为(0.5,0,0)。D为(0,0,1)。 得到的R为(0.5,0,0.5)。
基本上每个像素点都需要这么计算一次。
因此,view的层级很复杂,或者view都是半透明的(alpha值不为1)都会带来GPU额外的计算工作。
应用应当尽量减少视图数量和层次,并在不透明的视图里标明 opaque 属性以避免无用的 Alpha 通道合成。
Size
这个问题,主要是处理image带来的,假如内存里有一张400x400的图片,要放到100x100的imageview里,如果不做任何处理,直接丢进去,问题就大了,这意味着,GPU需要对大图进行缩放到小的区域显示,需要做像素点的sampling,这种smapling的代价很高,又需要兼顾pixel alignment。计算量会飙升。
shouldRasterize
其中shouldRasterize(光栅化)是比较特别的一种:
光栅化概念:将图转化为一个个栅格组成的图象。
光栅化特点:每个元素对应帧缓冲区中的一像素。
shouldRasterize = YES在其他属性触发离屏渲染的同时,会将光栅化后的内容缓存起来,如果对应的layer及其sublayers没有发生改变,在下一帧的时候可以直接复用。shouldRasterize = YES,这将隐式的创建一个位图,各种阴影遮罩等效果也会保存到位图中并缓存起来,从而减少渲染的频度(不是矢量图)。
相当于光栅化是把GPU的操作转到CPU上了,生成位图缓存,直接读取复用。
当你使用光栅化时,你可以开启“Color Hits Green and Misses Red”来检查该场景下光栅化操作是否是一个好的选择。绿色表示缓存被复用,红色表示缓存在被重复创建。
如果光栅化的层变红得太频繁那么光栅化对优化可能没有多少用处。位图缓存从内存中删除又重新创建得太过频繁,红色表明缓存重建得太迟。可以针对性的选择某个较小而较深的层结构进行光栅化,来尝试减少渲染时间。
注意:
对于经常变动的内容,这个时候不要开启,否则会造成性能的浪费
例如我们日程经常打交道的TableViewCell,因为TableViewCell的重绘是很频繁的(因为Cell的复用),如果Cell的内容不断变化,则Cell需要不断重绘,如果此时设置了cell.layer可光栅化。则会造成大量的离屏渲染,降低图形性能。
Offscreen Rendering And Mask(离屏渲染)
GPU屏幕渲染有以下两种方式:
On-Screen Rendering
意为当前屏幕渲染,指的是GPU的渲染操作是在当前用于显示的屏幕缓冲区中进行。
Off-Screen Rendering
意为离屏渲染,指的是GPU在当前屏幕缓冲区以外新开辟一个缓冲区进行渲染操作。
设置了以下属性时,都会触发离屏绘制:
shouldRasterize(光栅化)
masks(遮罩)
shadows(阴影)
edge antialiasing(抗锯齿)
group opacity(不透明)
复杂形状设置圆角等
渐变
为什么会使用离屏渲染
当使用圆角,阴影,遮罩的时候,图层属性的混合体被指定为在未预合成之前不能直接在屏幕中绘制,所以就需要屏幕外渲染被唤起。
屏幕外渲染并不意味着软件绘制,但是它意味着图层必须在被显示之前在一个屏幕外上下文中被渲染(不论CPU还是GPU)。
性能瓶颈:
如果我们对layer做这样的操作:
label.layer.cornerRadius = 5.0f;
label.layer.masksToBounds = YES;
会产生offscreen rendering,它带来的最大的问题是,当渲染这样的layer的时候,需要额外开辟内存,绘制好radius,mask,然后再将绘制好的bitmap重新赋值给layer。所以当使用离屏渲染的时候会很容易造成性能消耗,屏幕外缓冲区跟当前屏幕缓冲区上下文切换是很耗性能的。
优化方案:
1、因此继续性能的考虑,Quartz提供了优化的api:
label.layer.cornerRadius = 5.0f;
label.layer.masksToBounds = YES;
label.layer.shouldRasterize = YES;
label.layer.rasterizationScale = label.layer.contentsScale;
简单的说,这是一种cache机制。
2、只需要圆角的某些场合,也可以用一张已经绘制好的圆角图片覆盖到原本视图上面来模拟相同的视觉效果。
3、最彻底的解决办法,就是把需要显示的图形在后台线程绘制为图片,避免使用圆角、阴影、遮罩等属性.
同样GPU的性能也可以通过instrument去衡量:
红色代表GPU需要做额外的工作来渲染View,绿色代表GPU无需做额外的工作来处理bitmap。