在上一篇 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类继承关系图:
在Surface的构造过程中,首先调用其父类SurfaceTextureClient的构造函数
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代理对象获取过程
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中。在上一篇
Android远程代理对象BpSurface的获取过程源码分析中,Layer对象构造完成后,该对象的onFirstRef()函数被调用,在该函数中会构造一个SurfaceTextureLayer对象,该对象是BufferQueue的子类对象,同时以该对象为参数构造SurfaceTexture对象,并保存到创建的Layer对象的成员变量mSurfaceTexture中,通过调用Layer的父类LayerBaseClient的getSurface()函数创建一个ISurface的本地对象BSurface,并将该对象的远程代理对象通过Binder进程通信方式返回给应用程序进程。BSurface的构造过程:
BSurface(const sp<SurfaceFlinger>& flinger,const sp<Layer>& layer): LayerCleaner(flinger, layer), mOwner(layer) { }
使用创建的Layer对象来初始化BSurface的mOwner成员变量。BSurface实现了ISurface接口定义的方法getSurfaceTexture(),该方法用于向应用程序进程返回ISurfaceTexture的远程代理对象。
virtual sp<ISurfaceTexture> getSurfaceTexture() const {
sp<ISurfaceTexture> res;
sp<const Layer> that( mOwner.promote() );
if (that != NULL) {
res = that->mSurfaceTexture->getBufferQueue();
}
return res;
}
BSurface的成员变量mOwner保存的是应用程序请求创建的Layer对象,该Layer对象的成员变量mSurfaceTexture指向的是SurfaceTexture对象,这里调用SurfaceTexture的getBufferQueue()函数来获取构造SurfaceTexture对象时传入的BufferQueue子类对象,即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请求。