在上一篇 Android远程代理对象BpSurface的获取过程源码分析文章中,介绍了应用程序在SurfaceFlinger服务端创建对应的Layer对象过程,同时返回BpSurface远程代理对象给应用程序端,应用程序得到BpSurface代理对象后,将构造SurfaceControl对象:
sp<ISurface> surface = mClient->createSurface(&data, name,display, w, h, format, flags);if (surface != 0) { result = new SurfaceControl(this, surface, data);}参数this指向SurfaceComposerClient对象,参数surface就是BSurface的远程代理对象BpSurface,data是指在SurfaceFlinger服务端创建的Layer对象的参数信息。
SurfaceControl::SurfaceControl( const sp<SurfaceComposerClient>& client, const sp<ISurface>& surface, const ISurfaceComposerClient::surface_data_t& data) : mClient(client), mSurface(surface), mToken(data.token), mIdentity(data.identity){}SurfaceControl对象的构造过程很简单,只是初始化一些成员变量的值。
应用程序端有了SurfaceControl对象后,就可以在应用程序端创建Surface对象了:
sp<Surface> s = control->getSurface();通过SurfaceControl的getSurface()函数在应用程序端创建Surface对象
sp<Surface> SurfaceControl::getSurface() const{ Mutex::Autolock _l(mLock); if (mSurfaceData == 0) { sp<SurfaceControl> surface_control(const_cast<SurfaceControl*>(this)); mSurfaceData = new Surface(surface_control); } return mSurfaceData;}该函数只是利用SurfaceControl对象来构造一个新的Surface对象
Surface::Surface(const sp<SurfaceControl>& surface) : SurfaceTextureClient(), mSurface(surface->mSurface), mIdentity(surface->mIdentity){ sp<ISurfaceTexture> st; if (mSurface != NULL) { st = mSurface->getSurfaceTexture(); } init(st);}
在了解Surface对象的构造过程前,先介绍一下Surface类继承关系图:
SurfaceTextureClient::SurfaceTextureClient() { SurfaceTextureClient::init();}SurfaceTextureClient对象构造时,通过init函数来初始化对象
void SurfaceTextureClient::init() { // Initialize the ANativeWindow function pointers. ANativeWindow::setSwapInterval = hook_setSwapInterval; ANativeWindow::dequeueBuffer = hook_dequeueBuffer; ANativeWindow::cancelBuffer = hook_cancelBuffer; ANativeWindow::lockBuffer = hook_lockBuffer; ANativeWindow::queueBuffer = hook_queueBuffer; ANativeWindow::query = hook_query; ANativeWindow::perform = hook_perform; const_cast<int&>(ANativeWindow::minSwapInterval) = 0; const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; mReqWidth = 0; mReqHeight = 0; mReqFormat = 0; mReqUsage = 0; mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; mCrop.clear(); mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; mTransform = 0; mDefaultWidth = 0; mDefaultHeight = 0; mUserWidth = 0; mUserHeight = 0; mTransformHint = 0; mConsumerRunningBehind = false; mConnectedToCpu = false;}
Surface对象自身构造过程只是简单利用SurfaceControl对象来初始化Surface的一些成员变量,然后通过BSurface的远程代理对象BpSurface来获取BufferQueue的远程代理对象BpSurfaceTexture,关于BpSurface代理对象的获取过程请参看Android远程代理对象BpSurface的获取过程源码分析。
BpSurfaceTexture代理对象获取过程
BpSurfaceTexture代理对象是通过BpSurface代理对象向服务端的BSurface请求获得的
sp<ISurfaceTexture> st = mSurface->getSurfaceTexture();mSurface的类型为BpSurface,这里调用BpSurface的getSurfaceTexture()函数通过Binder进程间通信机制向BSurface发起RPC请求
virtual sp<ISurfaceTexture> getSurfaceTexture() const { Parcel data, reply; data.writeInterfaceToken(ISurface::getInterfaceDescriptor()); remote()->transact(GET_SURFACE_TEXTURE, data, &reply); return interface_cast<ISurfaceTexture>(reply.readStrongBinder());}这里通过Android的Binder通信机制将函数调用码及函数参数发送到服务端BnSurface,关于Binder进程间通信机制内容请阅读Android服务函数远程调用源码分析。
status_t BnSurface::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){ switch(code) { case GET_SURFACE_TEXTURE: { CHECK_INTERFACE(ISurface, data, reply); reply->writeStrongBinder( getSurfaceTexture()->asBinder() ); return NO_ERROR; } default: return BBinder::onTransact(code, data, reply, flags); }}函数getSurfaceTexture()实现在BnSurface的子类BSurface中
frameworks\native\services\surfaceflinger\Layer.cpp
virtual sp<ISurfaceTexture> getSurfaceTexture() const { sp<ISurfaceTexture> res; sp<const Layer> that( mOwner.promote() ); if (that != NULL) { res = that->mSurfaceTexture->getBufferQueue(); } return res;}这里得到BufferQueue本地对象,并将BufferQueue的远程代理对象BpSurfaceTexture返回给应用程序进程端。Surface得到BpSurfaceTexture代理对象后,接着调用init函数来初始化当前创建 的Surface对象。
void Surface::init(const sp<ISurfaceTexture>& surfaceTexture){ if (mSurface != NULL || surfaceTexture != NULL) { ALOGE_IF(surfaceTexture==0, "got a NULL ISurfaceTexture from ISurface"); if (surfaceTexture != NULL) { setISurfaceTexture(surfaceTexture); setUsage(GraphicBuffer::USAGE_HW_RENDER); } DisplayInfo dinfo; SurfaceComposerClient::getDisplayInfo(0, &dinfo); const_cast<float&>(ANativeWindow::xdpi) = dinfo.xdpi; const_cast<float&>(ANativeWindow::ydpi) = dinfo.ydpi; const_cast<uint32_t&>(ANativeWindow::flags) = 0; }}通过setISurfaceTexture(surfaceTexture)将得到的BpSurfaceTexture代理对象保存到Surface对象的成员变量mSurfaceTexture中,方便以后利用BpSurfaceTexture代理对象向服务端BufferQueue发起RPC请求。
ISurface,BpSurface,BSurface的定义仅仅是用于获取BpSurfaceTexture远程代理对象,前面连续三篇文章都是在介绍Android图形系统中获取服务的远程代理对象过程,归纳总结如下:
1. 通过ComposerService辅助类来从ServiceManager进程中查询SurfaceFlinger的远程代理对象BpSurfaceComposer ;
2. 通过BpSurfaceComposer向SurfaceFlinger发起RPC调用createConnection(),在服务进程中创建Client对象,并返回该对象的远程代理对象BpSurfaceComposerClient;
3. 通过BpSurfaceComposerClient向Client发起RPC调用createSurface(),在服务进程中创建Layer,BSurface及SurfaceTextureLayer对象,并返回BSurface对象的远程代理对象BpSurface;
4. 通过BpSurface向BSurface发起RPC调用getSurface(),返回SurfaceTextureLayer对象的远程代理对象BpSurfaceTexture;