離屏渲染是之在非當(dāng)前屏幕緩沖區(qū)進(jìn)行渲染,如果重寫了drawRect并使用了Core Graphics進(jìn)行了繪制操作就涉及到了cpu渲染,渲染得到的bitmap由GPU用于顯示。
CoreGraphics通常是線程安全的,所以可以進(jìn)行異步繪制
通常,GPU 的渲染性能要比 CPU 高效很多,同時(shí)對系統(tǒng)的負(fù)載和消耗也更低一些。
CPU離屏渲染:core graphics是cpu渲染
GPU離屏渲染:需要開辟新的緩沖區(qū)渲染,繪制的時(shí)候需要上下文切換。
CALayer直接添加陰影會導(dǎo)致離屏渲染,但是添加陰影路徑不會。離屏渲染直接結(jié)果有可能導(dǎo)致fps較低(fps越高越會得到流暢逼真的動畫)。
GPU渲染機(jī)制:
CPU 計(jì)算好顯示內(nèi)容提交到 GPU,GPU 渲染完成后將渲染結(jié)果放入幀緩沖區(qū),隨后視頻控制器會按照 VSync 信號逐行讀取幀緩沖區(qū)的數(shù)據(jù),經(jīng)過可能的數(shù)模轉(zhuǎn)換傳遞給顯示器顯示。
添加陰影:
方式一:直接添加(導(dǎo)致離屏渲染)
`
imgView.layer.shadowColor = [UIColor blackColor].CGColor;
imgView.layer.shadowOpacity = 0.8f;
imgView.layer.shadowRadius = 4.f;
imgView.layer.shadowOffset = CGSizeMake(4,4);
`
方式二:路徑
//路徑陰影
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(-5, -5)];
//添加直線
[path addLineToPoint:CGPointMake(paintingWidth /2, -15)];
[path addLineToPoint:CGPointMake(paintingWidth +5, -5)];
[path addLineToPoint:CGPointMake(paintingWidth +15, paintingHeight /2)];
[path addLineToPoint:CGPointMake(paintingWidth +5, paintingHeight +5)];
[path addLineToPoint:CGPointMake(paintingWidth /2, paintingHeight +15)];
[path addLineToPoint:CGPointMake(-5, paintingHeight +5)];
[path addLineToPoint:CGPointMake(-15, paintingHeight /2)];
[path addLineToPoint:CGPointMake(-5, -5)];
//設(shè)置陰影路徑
imgView.layer.shadowPath = path.CGPath;
設(shè)置圓角:
方式一:(直接設(shè)置,導(dǎo)致離屏渲染)
aView.layer.cornerRadius=8;
aView.layer.masksToBounds=YES;
方式二:(設(shè)置路徑,不會導(dǎo)致離屏渲染)
//設(shè)置所需的圓角位置以及大小UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:aView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10,10)];
CAShapeLayer*maskLayer =[[CAShapeLayer alloc] init];
maskLayer.frame=aView.bounds;
maskLayer.path=maskPath.CGPath;
aView.layer.mask= maskLayer;
概念:
光柵化概念:將圖轉(zhuǎn)為為一個(gè)個(gè)柵格組成的圖像
光柵化特點(diǎn):每個(gè)元素對應(yīng)幀緩沖區(qū)中的一像素
當(dāng)屬性是YES的時(shí)候,會生成位圖并且和其他內(nèi)容合成,當(dāng)為False的時(shí)候是直接生成。
shouldRasterize = YES
光柵化會導(dǎo)致離屏渲染,但是同時(shí)也會將渲染的圖片緩存,提高性能。因?yàn)榫彺妫鈻呕瘜蛹墢?fù)雜的視圖或者有復(fù)雜特效效果的圖層性能提升明顯。
如果圖層內(nèi)容經(jīng)常變化,緩存就會無效,這個(gè)時(shí)候離屏渲染會降低性能。
相當(dāng)于光柵化是把GPU的操作轉(zhuǎn)到CPU上了,生成位圖緩存,直接讀取復(fù)用。
問題:
drawrect為什么會導(dǎo)致離屏渲染?