Flutter在iOS上採用Metal驅動GPU

沒有年薪的碼農發表於2020-05-07

具體內容稍後整理後更新...更新中ing

前言

Flutter1.17正式釋出

關於1.17正式版

移動效能和尺寸改進

此版本的主要重點是在效能和記憶體改進方面繼續進行總體工作。只需將您的應用程式升級到此版本,您的使用者就會看到更快的動畫,更小的應用程式和更低的記憶體利用率。在此版本中,預設導航情況(不透明的不透明路線)的速度將提高20%-37%。對於簡單的iOS動畫,您還將看到最多減少40%的CPU / GPU使用率,具體取決於硬體(在PR 14104和PR 13976中進行了詳細介紹)。

此版本還提供了一個相當大的應用程式大小的改善是由於幾個 補丁 是 新增 了一個大的結果。例如,Android的FlutterGallery示例在2019年底為9.6MB,現在為8.1MB,減少了18.5%。

對於記憶體使用,此版本將快速滾動瀏覽大影像減少了70%的記憶體,這也可能導致效能提高,具體取決於裝置的記憶體量。

Flutter在iOS上採用Metal驅動GPU

Metal支援可將iOS的效能提高50%

蘋果公司在iOS上對Metal的支援幾乎可以直接訪問底層GPU,並且是蘋果公司推薦的圖形API。在完全支援Metal的iOS裝置上,Flutter現在預設情況下使用它,這使您的Flutter應用程式大多數時候執行得更快,平均將渲染速度提高了約50%(取決於您的工作量)。

Flutter在iOS上採用Metal驅動GPU

對於不完全支援Metal的裝置(A7處理器之前的版本或執行10之前的iOS版本的裝置),Flutter會像過去一樣使用OpenGL,從而為較舊的裝置提供本地渲染速度。

新增Widgets:NavigationRail,DatePicker等

在此版本中,我們新增NavigationRail了一個新的小部件,該小部件提供了響應式應用程式導航模型。它是由Google Material Design團隊設計和實施的。該NavigationRail是偉大的,可因為它是很容易在一個交換的移動和桌上型電腦外形之間切換應用程式BottomNavigator為您的應用程式的螢幕尺寸的增大。

實質性文字比例:使Flutter文字主題現代化

在此版本中,Flutter團隊完成了2018 Material Design規範的Type Scale部分的實現,同時沒有破壞現有的Flutter應用程式。2018年10月的PR 22330中增加了對新配置的選擇加入支援,但不對新名稱提供支援。現有的文字樣式名稱未更改,因為這樣做是一個重大的API更改,可能會影響大多數應用程式。此版本更新了TextThemeAPI以匹配當前的Material規範,但保留了舊名稱,以使您的程式碼不會中斷。但是,舊名稱已被棄用,因此您將收到警告,以鼓勵您採用新名稱.

Google字型用於Flutter

文字和字型齊頭並進,因此,如果您對新的Material Text Scale實現感到興奮,那麼您可能也會對新的Flutter v1.0版Google字型感到興奮。

Google字型允許開發人員在其應用中輕鬆地嘗試和使用fonts.google.com中的任何字型。當應用程式準備釋出時,開發人員將決定使用者是否通過從API下載字型來接收字型,或者它與應用程式包預先捆綁在一起。

可訪問性和國際化

在此發行版中,我們已完成了全部工作,包括滾動,文字 欄位和其他輸入小部件的可訪問性修復程式。您將在GitHub的該版本中看到有關可訪問性問題的完整列表。我們鼓勵您測試自己的應用程式的可訪問性,並且還通過一些推薦的最佳實踐更新了此版本中的文件。

工具:Dart DevTools移植到Flutter,Android快速入門等

此版本使我們非常接近將Dart DevTools的當前版本換成新的Flutter版本。如果您想嘗試一下,請啟動DevTools並單擊DevTools右上角的“燒杯”圖示。

Flutter在iOS上採用Metal驅動GPU

Devtools還有其它很多有趣的更新等待你的發掘。

1.17 Flutter Metal

The Flutter Engine will migrate to using the Metal Rendering API instead of OpenGL on iOS.

Why use Metal?

A Flutter user interface is displayed on screen by accessing the GPU on the platform using an accelerated graphics API. This API has been some version of OpenGL ES on iOS. OpenGL ES and associated APIs were formally deprecated by Apple in 2018 with the introduction of iOS 12. Metal is now the recommended API for low-level, low-overhead rendering on iOS and Mac. While the cross platform aspects of OpenGL were certainly useful, the Flutter team agrees with the case for Metal. The deprecation of OpenGL ES also appears to have resulted in a less well maintained OpenGL driver on iOS. There is an uptick in iOS specific OpenGL issues that are increasingly harder to debug and unlikely to be fixed. For these reasons, a migration to Metal on iOS is imperative.

Where will Flutter use Metal?

Metal will be used on Apple A7 devices or later running iOS 10 or above. When Metal cannot be used, OpenGL will be used instead.

Why can’t Flutter always use Metal on iOS?

Flutter currently supports running on iOS 8 and above. This includes devices on which Metal itself is not available. Specifically, Metal is only available on iOS devices with Apple A7 or later. This would exclude devices like the iPhone 5C that can be updated to iOS 10 but only have an Apple A6 SOC. A policy that mandates the use of Metal everywhere would mean we would have to leave some devices behind. That is not something we are prepared to consider now.

That still leaves a small number of devices that support iOS versions between 8 and 10 that have an Apple A7 SOC (or later) but have not updated to the latest iOS versions. Flutter could technically use Metal on these devices as well but currently doesn’t. This is because versions of the Metal API on earlier versions of iOS weren’t expressive enough for the features needed by Flutter (specifically the Skia rendering library used by Flutter). This requires authoring workarounds or fallbacks for cases like dual-source blending. To reduce the implementation and testing burden it was decided to pick iOS 10 as the baseline instead. This decision can be revised if necessary.

Does this change the minimum version of iOS supported by Flutter? No. Flutter will use Metal on newer devices and iOS versions but the OpenGL backend remains as-is and present. It will be used when Metal is unavailable.

As a Flutter Developer, is there anything I need to do to migrate to using Metal on iOS?

Except for the highly unlikely case that your application makes assumptions about Flutter using OpenGL under the hood, no. The updated Flutter Engine requires no changes to the Flutter application or how it is embedded.

Is there an impact to the binary size because of the addition of Metal support?

Yes. The size of the uncompressed Flutter Engine binary will increase by ~250KB. Incremental reductions to this size will continue to be made but a significant reduction requires the removal of the OpenGL backend. This is not something we have considered yet.

Will plugins for the camera preview, video playback, webview, maps, etc. keep working?

Yes. There are no updates necessary to these plugins and they will continue to work as expected. All plugins in the Flutter Plugins repo have also been tested to work with the Metal backend.

How can I tell if my application makes assumptions about Flutter using OpenGL under the hood?

The rendering API used by Flutter is an implementation detail of the Flutter Engine. However, Flutter does provide the ability for the underlying platform to provide textures for inline composition within a Flutter hierarchy. Developers (or plugins) do this by using a Texture widget in their applications and writing some platform specific code to provide and update these textures. If the platform specific code that operates on the textures provided to the Flutter Engine does not specify that the texture may interoperate with the Metal API, inline composition of these textures may fail. For guidance on ensuring your textures are interoperable with Metal, please follow the documentation on how to “Create an Interoperable Texture”. Specifically, the kCVPixelBufferMetalCompatibilityKey must be specified in addition to the kCVPixelBufferOpenGLCompatibilityKey key when creating the pixel buffer describing the texture is given to Flutter.

If you application does not use the Texture widget, there is nothing to do. If it does, the texture provided to Flutter must be checked to see that they are interoperable with Metal.

This is a very unique use case and no plugins in the Flutter Plugins repo use this mechanism.

If my application uses OpenGL to provide textures for inline composition in a Flutter application, will this work with the Metal backend?

Yes. As mentioned earlier, as long as the textures provided to Flutter are interoperable with Metal, you may continue to use OpenGL to update the texture and Flutter will composite the same using Metal.

How can I tell at runtime if my Flutter application is using Metal to render instead of OpenGL?

The FlutterViewControllers view will have a class that is a CAMetalLayer. Flutter will always use either Metal or OpenGL. That is, in a process, there is no chance of some Flutter application instances using Metal and others OpenGL.

Will Flutter use Metal on iOS Simulators?

No. Not currently. Flutter will continue to use the software backend on iOS simulators as before. Flutter on simulators is something on the radar though.

相關文章