Understanding Android Graphics Internals – The Graphic Surface Service Interfaces

As pointed out in previous posts, Android does not allow UI applications to write to the frame buffer directly; SurfaceFlinger administrates the allocation of surfaces/graphic buffers to UI applications, oversees the composition of surface buffers to the back frame buffer and display overlays. In accordance to this arrangement, its design features a suite of Interfaces which form the de facto  service contracts between the implementors and corresponding clients.

In this post, we cover ISurfaceComposer and ISurfaceComposerClient.

  • ISurfaceComposer

ISurfaceComposer is an advertised service Interface (i.e. registered with ServiceManager). The SurfaceFlinger implements BnSurfaceComposer while a client UI app must establish a BpSurfaceComposer proxy object by querying ServiceManager.

The methods in ISurfaceComposer have been in change in number and method signature, and will continue to evolve. Here we quote the interface declaration below.

/*  * This class defines the Binder IPC interface for accessing various  * SurfaceFlinger features.  */ class ISurfaceComposer: public IInterface { public:     DECLARE_META_INTERFACE(SurfaceComposer);

// flags for setTransactionState()

enum {         eSynchronous = 0x01,         eAnimation   = 0x02,     };

enum {         eDisplayIdMain = 0,         eDisplayIdHdmi = 1     };

/* create connection with surface flinger, requires      * ACCESS_SURFACE_FLINGER permission      */

virtual sp<ISurfaceComposerClient> createConnection() = 0;

/* create a graphic buffer allocator      */

virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc() = 0;

/* return an IDisplayEventConnection */

virtual sp<IDisplayEventConnection> createDisplayEventConnection() = 0;

/* create a display      * requires ACCESS_SURFACE_FLINGER permission.      */

virtual sp<IBinder> createDisplay(const String8& displayName,             bool secure) = 0;

/* get the token for the existing default displays. possible values      * for id are eDisplayIdMain and eDisplayIdHdmi.      */     virtual sp<IBinder> getBuiltInDisplay(int32_t id) = 0;

/* open/close transactions. requires ACCESS_SURFACE_FLINGER permission */

virtual void setTransactionState(const Vector<ComposerState>& state,             const Vector<DisplayState>& displays, uint32_t flags) = 0;

/* signal that we’re done booting.      * Requires ACCESS_SURFACE_FLINGER permission      */

virtual void bootFinished() = 0;

/* verify that an IGraphicBufferProducer was created by SurfaceFlinger.      */

virtual bool authenticateSurfaceTexture(             const sp<IGraphicBufferProducer>& surface) const = 0;

/* triggers screen off and waits for it to complete      * requires ACCESS_SURFACE_FLINGER permission.      */

virtual void blank(const sp<IBinder>& display) = 0;

/* triggers screen on and waits for it to complete      * requires ACCESS_SURFACE_FLINGER permission.      */

virtual void unblank(const sp<IBinder>& display) = 0;

/* returns information about a display      * intended to be used to get information about built-in displays */

virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) = 0;

/* Capture the specified screen. requires READ_FRAME_BUFFER permission
* This function will fail if there is a secure window on screen.
*/
virtual status_t captureScreen(const sp<IBinder>& display,
const sp<IGraphicBufferProducer>& producer,
uint32_t reqWidth, uint32_t reqHeight,
uint32_t minLayerZ, uint32_t maxLayerZ,
bool isCpuConsumer) = 0;
};

Notes on the usage.

1.  In surface operations, createConnection() is the first method to be invoked to obtain a BpSurfaceComposer object. Inside SurfaceFlinger, an instance of ISurfaceComposerClient implementator Client (declared in  native/services/surfaceflinger/Client.h) is created; SurfaceFlinger does not explicitly track outstanding connections, the Client instances is associated with Layers ( declared in native/services/surfaceflinger/Layer.h)  it created later which are tracked within SufaceFlinger.

2. createGraphicBufferAlloc () is invoked to obtain IGraphiBufferAlloc interface for the client.

3. setTransactionState() is invoked to inform SurfaceFlinger state of changes of the surface; changes are categorized in two types, layer_state_t and Display_state (both declared in native/include/private/gui/LayerState.h)  respectively. layer_state_t tracks changes in position, depth, sizes,  color scheme, alpha property and crop property, etc; Display_state tracks changes in orientation, etc.

4. createDisplayEventConnection() is used for vsync purpose.

5. captureScreen() is used for capturing a snapshot of screen display.

  •  ISurfaceComposerClient

The interface is declared in frameworks/native/include/gui/ISurfaceComposerClient.h.

class ISurfaceComposerClient : public IInterface { public:     DECLARE_META_INTERFACE(SurfaceComposerClient);

// flags for createSurface()

enum {

// (keep in sync with Surface.java)

eHidden             = 0x00000004,

eDestroyBackbuffer  = 0x00000020,

eSecure             = 0x00000080,

eNonPremultiplied   = 0x00000100,

eOpaque             = 0x00000400,

eProtectedByApp     = 0x00000800,

eProtectedByDRM     = 0x00001000,

eFXSurfaceNormal    = 0x00000000,

eFXSurfaceDim       = 0x00020000,

eFXSurfaceMask      = 0x000F0000,     };

/*      * Requires ACCESS_SURFACE_FLINGER permission      */

virtual status_t createSurface(             const String8& name, int32_t w, uint32_t h,             PixelFormat format, uint32_t flags,             sp<IBinder>* handle,             sp<IGraphicBufferProducer>* gbp) = 0;

/*      * Requires ACCESS_SURFACE_FLINGER permission      */

virtual status_t destroySurface(const sp<IBinder>& handle) = 0; };

In the createSurface() method, the initial width, height and the color scheme is specified. Other attributes such as depth on the screen, position, etc. are specified in the SurfaceFlinger::setTransactionState(). All Surface attributes are subject to change anyway.

One thought on “Understanding Android Graphics Internals – The Graphic Surface Service Interfaces”

Leave a comment