视图(或图层)何时需要离屏渲染? [英] When does a view (or layer) require offscreen rendering?

查看:22
本文介绍了视图(或图层)何时需要离屏渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
这个周末我开始观看 2011 年的 WWDC 视频.我发现了关于 iOS 的非常有趣的话题.我最喜欢的是性能和图形,但我发现其中两个显然是矛盾的.当然,有些东西我没有得到.我正在谈论的课程是理解 UIKit 渲染 -121 和完善你的应用程序 -105.
不幸的是,2011 年的示例代码仍然无法下载,因此很难全面了解.在一次会议中,他们解释说,在滚动视图等可视化过程中,大多数情况下应该避免离屏渲染.他们修复了示例代码中的性能问题,几乎在 -drawRect 方法中绘制了所有内容.在另一个会话中,性能问题(在表格视图上)似乎是由于表格单元格的 -drawRect 方法中的代码过多.
首先我不清楚什么时候系统需要OffScreen渲染,我在视频中看到一些quartz函数如:cornerRadious、shadowOffset、shadowColor需要它,但是否存在一般规则?
其次,我不知道我是否理解得很好,但似乎当没有离屏渲染时添加层或视图是要走的路.我希望有人能对此有所了解..
谢谢,
安德里亚

Hello
this weekend I started to watch the 2011 WWDC videos. I've found really interesting topics about iOS. My favorites were about performance and graphics, but I've found two of them apparently in contradiction. Of course there is something that I didn't get. The sessions that I'm talking about are Understanding UIKit Rendering -121 and Polishing your app -105.
Unfortunately sample code from 2011 is still not downloadable, so is pretty hard to have an overall view. In one session they explain that most of times offscreen rendering should be avoided during visualization in scrollview etc. They fix the performance issues in the sample code almost drawing everything inside the -drawRect method. In the other session the performance issue (on a table view) seems to be due to too much code in the -drawRect method of the table's cells.
First is not clear to me when an OffScreen rendering is required by the system, I've seen in the video that some quartz function such as: cornerRadious, shadowOffset, shadowColor requires it, but does exist a general rule?
Second I don't know if I understood well, but it seems that when there is no offscreen rendering adding layers or views is the way to go. I hope someone could bring light about that..
Thanks,
Andrea

推荐答案

我不认为任何地方都写有规则,但希望这会有所帮助:

I don't think there is a rule written down anywhere, but hopefully this will help:

首先,让我们理清一些定义.我认为屏幕外渲染与屏幕渲染在大多数情况下并不是最重要的问题,因为屏幕外渲染可以和屏幕上一样快.主要问题是渲染是用硬件还是软件来完成.

First, let's clear up some definitions. I think offscreen vs onscreen rendering is not the overriding concern most of the time, because offscreen rendering can be as fast as onscreen. The main issue is whether the rendering is done in hardware or software.

使用图层和视图之间也几乎没有实际区别.视图只是 CALayer 的一个薄包装器,它们在大多数情况下不会带来显着的性能损失.如果您希望视图由 CAShapeLayer 或 CATileLayer 等支持,您可以使用 +layerClass 方法覆盖视图使用的图层类型.

There is also very little practical difference between using layers and views. Views are just a thin wrapper around CALayer and they don't introduce a significant performance penalty most of the time. You can override the type of layer used by a view using the +layerClass method if you want to have a view backed by a CAShapeLayer or CATileLayer, etc.

一般来说,在 iOS 上,像素效果和 Quartz/Core Graphics 绘图不是硬件加速的,其他大多数都是.

Generally, on iOS, pixel effects and Quartz / Core Graphics drawing are not hardware accelerated, and most other things are.

以下事情不是硬件加速的,这意味着它们需要在软件(屏幕外)中完成:

The following things are not hardware accelerated, which means that they need to be done in software (offscreen):

  1. 在 drawRect 中完成的任何事情.如果你的视图有一个 drawRect,即使是一个空的,绘制也不是在硬件中完成的,并且会有性能损失.

  1. Anything done in a drawRect. If your view has a drawRect, even an empty one, the drawing is not done in hardware, and there is a performance penalty.

任何将 shouldRasterize 属性设置为 YES 的图层.

Any layer with the shouldRasterize property set to YES.

任何带有遮罩或阴影的图层.

Any layer with a mask or drop shadow.

文本(任何类型,包括 UILabels、CATextLayers、Core Text 等).

Text (any kind, including UILabels, CATextLayers, Core Text, etc).

您使用 CGContext 自己绘制的任何图形(在屏幕上或屏幕外).

Any drawing you do yourself (either onscreen or offscreen) using a CGContext.

大多数其他东西都是硬件加速的,所以它们要快得多.但是,这可能并不意味着您认为它的作用.

Most other things are hardware accelerated, so they are much faster. However, this may not mean what you think it does.

与硬件加速绘图相比,上述任何类型的绘图都很慢,但它们不一定会减慢您的应用程序的速度,因为它们不需要每一帧都发生.例如,第一次在视图上绘制阴影很慢,但绘制后它会被缓存,并且只有在视图改变大小或形状时才会重新绘制.

Any of the above types of drawing are slow compared to hardware accelerated drawing, however they don't necessarily slow down your app because they don't need to happen every frame. For example, drawing a drop shadow on a view is slow the first time, but after it is drawn it is cached, and is only redrawn if the view changes size or shape.

光栅化视图或带有自定义 drawRect 的视图也是如此:视图通常不会在每一帧都重新绘制,它会绘制一次然后缓存,因此第一次设置视图后的性能不会更差,除非边界改变或者你调用 setNeedsDisplay .

The same goes for rasterised views or views with a custom drawRect: the view typically isn't redrawn every frame, it is drawn once and then cached, so the performance after the view is first set up is no worse, unless the bounds change or you call setNeedsDisplay on it.

为了获得良好的性能,诀窍是避免对每帧都改变的视图使用软件绘图.例如,如果您需要动画矢量形状,使用 CAShapeLayer 或 OpenGL 将获得比 drawRect 和 Core Graphics 更好的性能.但是如果你画了一个形状然后不需要改变它,它不会有太大的区别.

For good performance, the trick is to avoid using software drawing for views that change every frame. For example, if you need an animated vector shape you'll get better performance using CAShapeLayer or OpenGL than drawRect and Core Graphics. But if you draw a shape once and then don't need to change it, it won't make much difference.

同样,不要在动画视图上放置阴影,因为它会降低您的帧速率.但是视图上的阴影不会随帧变化而产生太大的负面影响.

Similarly, don't put a drop shadow on an animated view because it will slow down your frame rate. But a shadow on a view that doesn't change from frame to frame won't have much negative impact.

需要注意的另一件事是减慢视图设置时间.例如,假设您有一页文本,所有文本都带有阴影;由于文本和阴影都需要在软件中渲染,因此最初绘制需要很长时间,但是一旦绘制它就会很快.因此,您需要在应用程序加载时提前设置此视图,并将其副本保存在内存中,以便用户不必等待视图首次出现在屏幕上时就显示很长时间.

Another thing to watch out for is slowing down the view setup time. For example, suppose you have a page of text with drop shadows on all the text; this will take a very long time to draw initially since both the text and shadows all need to be rendered in software, but once drawn it will be fast. You will therefore want to set up this view in advance when your application loads, and keep a copy of it in memory so that the user doesn't have to wait ages for the view to display when it first appears on screen.

这可能是 WWDC 视频中明显矛盾的原因.对于不会每帧都改变的大型复杂视图,在软件中绘制一次(之后它们被缓存并且不需要重新绘制)将比让硬件每帧重新合成它们产生更好的性能,即使第一次画会比较慢.

This is probably the reason for the apparent contradiction in the WWDC videos. For large, complex views that don't change every frame, drawing them once in software (after which they are cached and don't need to be redrawn) will yield better performance than having the hardware re-composite them every frame, even though it will be slower to draw the first time.

但是对于必须不断重绘的视图,例如表格单元格(单元格被回收,因此每次一个单元格滚动到屏幕外时都必须重绘它们,并在它作为不同行滚动回另一侧时重新使用),软件绘图可能会减慢很多速度.

But for views that must be redrawn constantly, like table cells (the cells are recycled so they must be redrawn each time one cell scrolls offscreen and is re-used as it scrolls back onto the other side as a different row), software drawing may slow things down a lot.

这篇关于视图(或图层)何时需要离屏渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆