美文网首页图像iOS 性能
image 优化之 —— image copy 字节对齐

image 优化之 —— image copy 字节对齐

作者: 介和 | 来源:发表于2018-12-28 20:21 被阅读0次

    Core Animation在图像数据非字节对齐的情况下渲染前会先拷贝一份图像数据,官方文档没有对这次拷贝行为作说明,模拟器和Instrument里有高亮显示“copied images”的功能,但似乎它有bug,即使某张图片没有被高亮显示出渲染时被copy,从调用堆栈上也还是能看到调用了CA::Render::copy_image方法:

            那什么是字节对齐呢?

            按我的理解,为了性能,底层渲染图像时不是一个像素一个像素渲染,而是一块一块渲染,数据是一块块地取,就可能遇到这一块连续的内存数据里结尾的数据不是图像的内容,是内存里其他的数据,可能越界读取导致一些奇怪的东西混入,所以,在渲染之前CoreAnimation要把数据拷贝一份进行处理,确保每一块都是图像数据,对于不足一块的数据置空。大致图示:(pixel是图像像素数据,data是内存里其他数据)

             块的大小应该是跟CPU cache line有关,ARMv7是32byte,A9是64byte,在A9下CoreAnimation应该是按64byte作为一块数据去读取和渲染,让图像数据对齐64byte就可以避免CoreAnimation再拷贝一份数据进行修补。FastImageCache做的字节对齐就是这个事情。

    FastImageCache , FICUtilities.m 文件 中 提到:

    // Core Animation will make a copy of any image that a client application provides whose backing store isn't properly byte-aligned.

    // This copy operation can be prohibitively expensive, so we want to avoid this by properly aligning any UIImages we're working with.

    // To produce a UIImage that is properly aligned, we need to ensure that the backing store's bytes per row is a multiple of 64.

    相关文章

      网友评论

        本文标题:image 优化之 —— image copy 字节对齐

        本文链接:https://www.haomeiwen.com/subject/hwgelqtx.html