Android 7.0 では TextureView よりも SurfaceView が推奨されるらしい。
まじかよ!ってなったので。
たぶん、大半の人にとってはどうでもよいです。
公式リファレンスの Android N for Developers の英語版には以下の記述があります。(いまのところ日本語には訳されていないです)
Android 7.0 brings synchronous movement to the SurfaceView class, which provides better battery performance than TextureView in certain cases: When rendering video or 3D content, apps with scrolling and animated video position use less power with SurfaceView than with TextureView.
The SurfaceView class enables more battery-efficient compositing on screen, because it is composited in dedicated hardware, separately from app window content. As a result, it makes fewer intermediate copies than TextureView.
A SurfaceView object's content position is now updated synchronously with the containing app content. One result of this change is that simple translations or scales of a video playing in a SurfaceView no longer produce black bars alongside the view as it moves.
Starting with Android 7.0, we strongly recommend that you save power by using SurfaceView instead of TextureView
Android 7.0 のマルチウインドウ環境において、SurfaceView
はハードウェア上でアプリケーションの Window
と合成されるため、バッテリ効率が非常によくなっているのだそうです。
このため TextureView
の代替として SurfaceView
の使用が強く推奨されるとか。
SurfaceView
って何?
Android 2.X の古代において、普通の View
を描画するよりも高速だったものです。
原理としては、別スレッドでバックバッファの描画を行い、通常の View
階層ではなく独自の Window
に描画を行います。
Android 3.0 でハードウェアレンダリングが一般の View でも使われるようになり、4.0 でデフォルト化すると、ソフトウェアレンダリングを行う SurfaceView
は通常の View
より低速になったと言われてます[要出典]。
OpenGL で描画を行える、GLSurfaceView
もありましたが、ほぼ OpenGL ES 1.0 なので、奇特な人しか使ってないと思ってます。RSSurfaceView
のことは、忘れてあげてください。
TextureView
って何?
Android 4.0 で鳴り物入りで登場したものの、いまいち流行らなかったやつです。
SurfaceView
と似たようなインターフェースを備え、Canvas
の簡単な API で OpenGL ES の高速描画を享受でき、それだけでなく通常の View
階層に組み込んで使え、アフィン変換やアルファブレンドも行える!などなど、利点が超アピールされていた気がします。さりげなく Camera2 API のプレビューでも使われています。
実際のところ、OpenGL で Bitmap
を延々と生成して表示しているだけなのでメモリ消費が半端なく、生の OpenGL ES 2.0 が分かるならそちらを使った方が効率がよい上に、分からない人にも Unity などのゲームエンジンが普及したので、どの辺の層が使っているのか謎な感じです。
ともあれ、SurfaceView
の強化版のような触れ込みで登場しましたが、両者は全く別物ということだけは言えます。
なぜ TextureView
が必要とされたのか
Android 4.0 Graphics and Animations によれば、
Because a SurfaceView’s content does not live in the application’s window, it cannot be transformed (moved, scaled, rotated) efficiently.
SurfaceView
の弱点はアプリケーションの Window
に属していないので、効率的な変換が不可能だったということにあります。
しかし Android 7.0 でマルチウインドウ化するとともに、
A SurfaceView object's content position is now updated synchronously with the containing app content.
SurfaceView
がアプリケーションのコンテンツと同期を取れる(※ただし Android 7.0 以上のみ)能力を持つに至り、もはやバッテリーを食うだけの TextureView
は不要、みたいな感じなんでしょうか。
これに伴う混乱
SurfaceView
と TextureView
で検索するとこういう感じの情報が得られます。
- Android 2.0 時代に書かれた「
SurfaceView
は高速にレンダリングできるView
である」という極めて古い資料 - Android 4.0 時代に書かれた「より新しい
TextureView
に乗り換えるべき」という期待に満ち溢れた古い資料 - Android 5.0 時代に書かれた「
TextureView
はトラップなので使うべきではない」という怨嗟を感じるやや古い資料 - Android 6.0 時代に書かれた「
SurfaceView
は低速なので使うべきではない」という妥当だけどタイトルが裏目に出ている資料 - そして、Android 7.0 時代に書かれる「
SurfaceView
はバッテリ効率が良いのでTextureView
より推奨される」という資料
何を信じればいいんだ…。
個人的な結論
SurfaceView
も TextureView
も今となっては使うべきではなく、Canvas API で独自の描画をしたいならカスタム View
を作ればいいと思います。
カスタム View
では実現できない高速な描画が必要?
流行の最先端である Vulkan Graphics API を覚えるのがお勧めですよ!!!