ios GUI系統綜述:iOS的圖形繪制、動畫與runloop


一、一條業務pipeline:

一個連接核心:coreanimation

 

二、兩個進程:

1、app進程;

2、render進程;

  • 首先,由 app 處理事件(Handle Events),如:用戶的點擊操作,在此過程中 app 可能需要更新 視圖樹,相應地,圖層樹 也會被更新。
  • 其次,app 通過 CPU 完成對顯示內容的計算,如:視圖的創建、布局計算、圖片解碼、文本繪制等。在完成對顯示內容的計算之后,app 對圖層進行打包,並在下一次 RunLoop 時將其發送至 Render Server,即完成了一次 Commit Transaction 操作。
  • Render Server 主要執行 Open GL、Core Graphics 相關程序,並調用 GPU

https://www.cnblogs.com/feng9exe/p/10896042.html

 

兩個循環:事件循環與顯示循環;

 

三、3個硬件

在 iOS 系統中,圖像內容展示到屏幕的過程需要 CPU 和 GPU 共同參與。

CPU 負責計算顯示內容,比如視圖的創建、布局計算、圖片解碼、文本繪制等。

隨后 CPU 會將計算好的內容提交到 GPU 去,由 GPU 進行變換、合成、渲染。

之后 GPU 會把渲染結果提交到幀緩沖區去,等待下一次 VSync 信號到來時顯示到屏幕上。

https://www.cnblogs.com/feng9exe/p/10898875.html

1、cpu

布局、繪圖、解碼、打包提交;

2、gpu

合成;

3、screen:

顯示;

 

三、三個協調

1、runloop:協調物理屏幕、系統、應用進程;

2、core animation協調app進程、render進程;

3、display loop協調 gpu緩存與顯示過程;

 

四、四個要素:

組件、路由、渲染、事件;

 

 

五、流程梳理

iOS繪圖事務的運行驗證

https://www.cnblogs.com/feng9exe/p/10343121.html

 

1、runloop接收到觸摸事件,調用:

__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__

__dispatchPreprocessedEventFromEventQueue

進行事件處理,UI調整標記;

2、在runloop當前循環結束時,調用

__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__

CA::Transaction::commit()

CA::Context::commit_transaction(CA::Transaction*)

對視圖調整進行CA事務打包;

3、CA在app進程內執行事務:

不管怎樣,從我們的call stack中也能夠看到這四個完整的過程。

結合WWDC,以我們的call stack為例,來說明這四個過程分別大概都做了什么。

layout過程


layout的過程

從上面layout的過程可以看出,其所做的主要任務就是將圖層調用代理(也就是視圖)實現整個視圖層級的布局;比較有意思的是,autolayout的約束也是在這個時候更新和施加apply的(-[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine])。

display過程


display的過程

按照WWDC視頻的說法,display這個階段是負責draw圖層的內容。如果你的某些視圖實現了drawRect:方法或者其他UIKit提供的視圖采用了圖層的drawInContext:方法來提供內容的,比如這里的UILabel的實現,那么也會在這個階段去調用。從這個階段的工作來講,其是CPU bound(受限於CPU性能)的。

prepare過程


prepare的過程

prepare過程也是Core Animation准備圖層內容的環節,但是這個過程的准備是給那些沒有實現drawInContext:(視頻說的是drawRect,但實際上drawRect的底層是圖層的drawInContext)而通過其他方式設置圖層內容的視圖准備的;比如,UIImageView,它的實現就是直接解碼壓縮格式的png圖,設置給其layer的contents屬性的。那么圖片解碼的過程就在這個prepare階段。

commit過程

commit階段是講所有的圖層數據打包,通過進程間通信給到render server(另外一個進程)來繪制顯示到屏幕上。通常情況下,CPU的工作在這個階段是比較少的-從上面的10次重復動畫來看,這個過程僅僅分擔了1ms左右的cpu時間。但是WWDC指出,如果你的視圖層級過於復雜,有非常多的圖層,那這個commit的過程也會耗費比較長的時間。


commit的過程

 

https://www.awsomejiang.com/2018/03/06/about-core-animtion-animation-stages/

  

4、ca處理完事務提交給render進程進一步處理

在 APP 外部的2個階段:

當這些數據到達 render server 后,會被反序列化成 render tree。然后 render server 會做下面的兩件事:

  • 根據 layer 的各種屬性(如果是動畫的,會計算動畫 layer 的屬性的中間值),用 OpenGL 准備渲染。
  • 渲染這些可視的 layer 到屏幕。

 

5、render進程調用GPU接口進行視圖繪制;

繪制完成的位圖存進GPU緩存;

 

6、GPU的定時機制會將緩存中的位圖定時渲染到屏幕。

如果視圖沒有修改,繼續使用gpu緩存中的位圖;

如果有事件發生,進行新一輪的循環。

 

五、coreanimation處於居中協調的位置

Core Animation is a graphics rendering and animation infrastructure available on both iOS and OS X that you use to animate the views and other visual elements of your app. With Core Animation, most of the work required to draw each frame of an animation is done for you. All you have to do is configure a few animation parameters (such as the start and end points) and tell Core Animation to start. Core Animation does the rest, handing most of the actual drawing work off to the onboard graphics hardware to accelerate the rende

 

Core Animation Manages Your App’s Content

Core Animation is not a drawing system itself. It is an infrastructure for compositing and manipulating your app’s content in hardware. At the heart of this infrastructure are layer objects, which you use to manage and manipulate your content. A layer captures your content into a bitmap that can be manipulated easily by the graphics hardware. In most apps, layers are used as a way to manage the content of views but you can also create standalone layers depending on your needs.

 

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

 

Core Animation 流水線的詳細過程如下:

  • 首先,由 app 處理事件(Handle Events),如:用戶的點擊操作,在此過程中 app 可能需要更新 視圖樹,相應地,圖層樹 也會被更新。
  • 其次,app 通過 CPU 完成對顯示內容的計算,如:視圖的創建、布局計算、圖片解碼、文本繪制等。在完成對顯示內容的計算之后,app 對圖層進行打包,並在下一次 RunLoop 時將其發送至 Render Server,即完成了一次 Commit Transaction 操作。
  • Render Server 主要執行 Open GL、Core Graphics 相關程序,並調用 GPU
  • GPU 則在物理層上完成了對圖像的渲染。
  • 最終,GPU 通過 Frame Buffer、視頻控制器等相關部件,將圖像顯示在屏幕上。

https://www.cnblogs.com/feng9exe/p/10896042.html

 

 

 https://www.cnblogs.com/feng9exe/p/10339907.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM