Thursday, January 16, 2014

SurfaceFlinger Basics

Thank you Guys, For such a great response for my last post on SurfaceFlinger. Here I am trying to add few more things which will help you clear your understanding on SurfaceFlinger. 

Terms User in SurfaceFlinger:

  • Canvas : 2D drawing object, object which used to draw on screen bitmap.
  • Surface : Drawing Buffer
  • Window : A visual area containing some kind of user interface.
  • SurfaceFlinger : Surface Manager
  • View : This is user interface, it’s a rectangular area on the screen for drawing and event handling.
  • ViewGroup : Subclass of view that contain other views(called children). It is the base class for layouts which are invisible containers that hold other views or viewgroups.
How Views are Created: 
  • When view is ready to be drawn it will inform using requestLayout() to the parent to schedule a traversal.
  • Which is turn will traversal up till ViewRoot.
  • scheduleTraversal() will post message to UI thread to start performTraversal().
  • performTraversal() does 3 things.
  1. Measure Pass : It is called to find out how big a view should be and how it should be measured and positioned.
  2. Layout Pass : Each parent will position all of its children using the sizes computed in the measure pass. 
  3. Deawing : ViewRoot will get the canvas by calling surface.lockCanvas(), it updates the canvas by passing it to view.drawCanvas(). 
  • After drawing the ViewRoot will pass the canvas to surface by surface.unlockCanvasAndPost().
As mentioned earlier, whenever there is a change in view, relayoutWindow()[ViewRootImpl.java] will be called. Further flows will be like 

relayoutWindow()[ViewrootImpl.java] ---> relayoutWindow()[WindowManagerService.java]------

-->createSurfaceLocked()[WindowStateAnimator.java] -----> SurfaceControl()[SurfaceControl.java]---

---> nativeCreate()[android_view_SurfaceControl.cpp] -------> createSurface()[Client.cpp] --------

--> createLayer()[SurfaceFlinger.cpp] --------> createNormalLayer()[SurfaceFlinger.cpp] -----------

--> setBuffer()[Layer.cpp] -------> 

setDefaultBufferSize()/ setDefaultBufferFormat()/ setDefaultBufferUsageBits()[BufferQueue.cpp]


How Surface is drawn:

So now as surface has been created, so created surface reference will be returned to ViewRootImpl. Further it will call

draw()[ViewRootImpl.java]----------------------------------------------------------------------------------> drawSoftware()[ViewRootImpl.java] ---------> lockCanvas()[Surface.java]
                                                              ---------> drawColors()[Canvas.java]
                                                              ---------> draw()[View.java]
                                                              ---------> unlockCanvas()[Surface.java]

Here draw() [View.java]draws different things like 1) draw background 2) draw views content 3) draw children(like animation) 4) draw decorations( scrollbars for ex).

lockCanvas() is used to attach the canvas to layer and internally it calls dequeueBuffer() which gets the GraphicBuffer to put the content in it.

And unlockCanvas() is used to detach the canvas from layer and internally it will call queuebuffer() which is used to post the content.

Added one more post on SurfaceFlinger, ,check it out  here 


No comments: