What is SurfaceView? Is there a difference with Surface? What is SurfaceHolder and their relationship?

Reference: https://www.zhihu.com/question/30922650

SurfaceView is an inherited class of View, this view A Surface dedicated to drawing is embedded in it. You can control the format and size of this Surface. Surfaceview controls the drawing position of this Surface. The surface is Z-ordered, which means that it is always behind the window where it is. Surfaceview provides a visible area, only the part of the surface in this visible area is visible, and the part outside the visible area is not visible. The layout of the surface is affected by the view hierarchy, and its sibling view nodes will be displayed at the top. This means that the content of the surface will be obscured by its sibling views. This feature can be used to place overlays (for example, controls such as text and buttons). Note that if there are transparent controls on the surface, each change of it will cause the framework to recalculate the transparency effects of it and the top-level controls, which will affect performance. You can access this surface through the SurfaceHolder interface, which can be obtained by the getHolder() method. When the surfaceview becomes visible, the surface is created; before the surfaceview is hidden, the surface is destroyed. This saves resources. If you want to see when the surface is created and destroyed, you can overload surfaceCreated(SurfaceHolder) and surfaceDestroyed(SurfaceHolder). The core of surfaceview is to provide two threads: UI thread and rendering thread. It should be noted here: 1> All SurfaceView and SurfaceHolder.Callback methods should be called in the UI thread, which is generally the main thread of the application. Various variables to be accessed by the rendering thread should be synchronized. 2> Since the surface may be destroyed, it is only valid between SurfaceHolder.Callback.surfaceCreated() and SurfaceHolder.Callback.surfaceDestroyed(), so make sure that the rendering thread accesses a legal and valid surface. Next, let’s talk about my understanding of it. 1. Definition. Image data can be obtained directly from hardware interfaces such as memory or DMA. It is a very important drawing container. Its characteristic is: you can draw on the screen in a thread other than the main thread. This can avoid blocking the main thread when the drawing task is heavy, thereby improving the response speed of the program. SurfaceView is often used in game development, and the background, characters, animation, etc. in the game are drawn on the canvas as much as possible. 2. The realization first inherits SurfaceView and implements the SurfaceHolder.Callback interface. The reason for using the interface: Because there is a principle in using SurfaceView, all drawing work must be started after the Surface is created (Surface—surface, this concept is often used in graphics programming) Mentioned. Basically we can regard it as a mapping of video memory, the content written to the Surface can be directly copied to the video memory to be displayed, which makes the display speed very fast), and must end before the Surface is destroyed. So surfaceCreated and surfaceDestroyed in Callback become the boundary of drawing processing code. Canvas is a data structure built by the Java layer and a canvas for View. ViewGroup will split its Canvas into child Views. View will draw the graphics data on the Canvas it gets in the onDraw method. The Surface is a data structure built by the Native layer and a canvas for SurfaceFlinger. It is a data structure directly used to draw to the screen. The View that developers generally use is drawn on Canvas, and then the Canvas data information of the topmost View (usually DecorView) is converted to a Surface. SurfaceFlinger will synthesize the Surface of each application window, and then draw it on the screen (actually it is a Buffer, but generally developers do not need to consider these, so some concepts are omitted). So why is there a SurfaceView? This is because the amount of calculation for View measurement (Measure), layout (Layout) and drawing (Draw) is relatively large. After the calculation is completed, it is time-consuming to convert the data from Canvas to Surface, and then draw the screen. There is no problem with conventional UI drawing, but it is unacceptable for application scenarios like Camera preview and video playback. SurfaceView is to solve this problem. SurfaceView content is no longer drawn on Canvas, but directly drawn on a Surface it holds. Because many steps are omitted, its drawing performance is greatly improved. The SurfaceView itself is only used to control the size and position of the Surface. One is the device layer and the other is the drawing layer. Generally, the graphics interface abstracts the two levels of surface and graphics. The former is more oriented to hardware and window/view devices, and the latter is more oriented to graphics interfaces. “To be continued”

Leave a Comment

Your email address will not be published.