When you’re playing a video or rendering other animations, there are two View components to choose from, SurfaceView and TextureView, GLSurfaceView is SurfaceView subclass, so I’m going to put it under SurfaceView.

  • SurfaceView implementation mechanism
  • Double buffering mechanism
  • TextureView implementation mechanism
  • Pros and cons of TextureView and SurfaceView

1.SurfaceView implementation mechanism

The SurfaceView inherits from the View, so it’s also a View. But this View is a little bit different than a regular View.SurfaceView has its own SurfaceIn Android, a View has its own Surface, which has the corresponding WindowState in WMS and the corresponding Layer in SurfaceFlinger.

A typical Activity contains multiple views that form a tree of hierachy views, and only the top-level DecorView, the root View, is visible to THE WMS. This DecorView has a corresponding Windows State in THE WMS. Accordingly, the corresponding Layer in SurfaceFlinger. The SurfaceView comes with a Surface, which has its own WindowState in WMS and its own Layer in SurfaceFlinger. Although the SurfaceView is still in the View hierachy on the Application side, it is separated from the host window on the Server side (WMS and SurfaceFlinger).

Why is the SurfaceView designed this way? Advantages: The advantage is that rendering the Surface can be done in a separate thread, rendering complex animations without affecting the response of the main thread.

Disadvantages: Because the Surface is not in the hierachy of View, its display is not controlled by the attributes of View, so it cannot be translated, zooming and other transformations, nor can it be placed in other viewgroups, and some features in View cannot be used.

2. Double buffering

A brief explanation:

  • What is unbuffered
  • What is single buffering
  • What is double buffering

Drawing directly on a window without a canvas is called unbuffered drawing.

If you take a canvas, and you paint everything on the canvas first, and you paint the whole thing on the window, that’s called a single buffer drawing, and that canvas is a buffer.

Two canvases are used, one for the temporary drawing and one for the final drawing, which is called double-buffered drawing.

The SurfaceView itself has two buffers, one background buffer and one foreground buffer. Each time the background buffer receives data, it exchanges the data to the foreground buffer when it is complete. This ensures that the foreground buffer is complete. Double buffering: The SurfaceView updates the view with two Canvases:

  • FrontCanvas: The canvas that is actually displayed
  • BackCanvas: Stores the canvas before the last change

Of course, the more efficient method is that frontCanvas and backCanvas will be swapped after each drawing, frontCanvas becomes backCanvas, backCanvas becomes frontCanvas.

The advantages of double buffering are obvious:

  • Improve rendering efficiency
  • The flashing phenomenon caused by high refresh frequency can be avoided

3.TextureView implementation mechanism

Introduced in Android4.0(API level 14), inherits View like SurfaceView, it can project content streams directly into a View, it can be used to implement features such as Live preview.

  • Different from SurfaceView, WMS does not create a separate window, but as a normal View in the View hierachy, so can move, rotate, zoom, animation and other changes like other normal View.
  • Unlike SurfaceView, TextureView must be in a hardware-accelerated window.
  • It displays content stream data that can come from either the Application process or a remote process.
  • TextureView is inherited from the View, which is managed and drawn in hierachy View like all other views. TextureView overloads the draw() method, where the image data received from the main SurfaceTexture is updated as a texture to the corresponding HardwareLayer.

Cons: must be used in hardware-accelerated Windows, takes up more memory than SurfaceView, render in main thread before 5.0, separate render thread after 5.0.

4. Pros and cons of TextureView and SurfaceView

SurfaceView TextureView
memory low high
Power consumption low high
Draw the efficiency In a timely manner 1 to 3 frame delay
screenshots Does not support support
animation Does not support support

GLSurfaceView is a subclass of SurfaceView. In addition to SurfaceView’s advantages, GLSurfaceView also supports screenshot and animation operations.