前面我们已经分析了activity从创建到调用本地方法去创建SurfaceComposerClient的过程。在本文中,我们将详细分析SurfaceComposerClient创建时是如何绑定SurfaceFlinger。
[-> android_view_SurfaceSession.cpp]
static jlong nativeCreate(JNIEnv* env, jclass clazz) {
SurfaceComposerClient* client = new SurfaceComposerClient();//见第二小节
client->incStrong((void*)nativeCreate);
return reinterpret_cast(client);
}
[-> SurfaceComposerClient.cpp]
SurfaceComposerClient构造函数实现如下:
SurfaceComposerClient::SurfaceComposerClient()
: mStatus(NO_INIT), mComposer(Composer::getInstance())
{
}
若是第一次执行会执行onFirstRef方法
void SurfaceComposerClient::onFirstRef() {
sp<ISurfaceComposer> sm(ComposerService::getComposerService());//见第2.1节
if (sm != 0) {
sp<ISurfaceComposerClient> conn = sm->createConnection();//见第2.2节
if (conn != 0) {
mClient = conn;
mStatus = NO_ERROR;
}
}
}
sp<ISurfaceComposer> ComposerService::getComposerService() {
ComposerService& instance = ComposerService::getInstance();
Mutex::Autolock _l(instance.mLock);
if (instance.mComposerService == NULL) {
ComposerService::getInstance().connectLocked();//见2.1.1节
assert(instance.mComposerService != NULL);
ALOGD("ComposerService reconnected");
}
return instance.mComposerService;
}
ComposerService是单例模式,上述方法的作用是,若ComposerService已经创建则直接返回,若没有创建,则创建并执行connectLocked方法。
在链接过程中,主要做了两件事情:
1、获取对应的服务
2、创建死亡监督者并绑定
void ComposerService::connectLocked() {
const String16 name("SurfaceFlinger");//按名称查找服务
while (getService(name, &mComposerService) != NO_ERROR) {//见2.1.2节
usleep(250000);
}
assert(mComposerService != NULL);
// Create the death listener.
class DeathObserver : public IBinder::DeathRecipient {//死亡监督类
ComposerService& mComposerService;
virtual void binderDied(const wp& who) {
ALOGW("ComposerService remote (surfaceflinger) died [%p]",
who.unsafe_get());
mComposerService.composerServiceDied();
}
public:
DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
};
mDeathObserver = new DeathObserver(*const_cast(this));//创建死亡监督对象
IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);//链接死亡监督对象
}
getService是一个模板函数,通过参数name去查找获取对应的服务,并将服务的IBinder转化为接口对象输出
template<typename INTERFACE>
status_t getService(const String16& name, sp* outService)
{
const sp sm = defaultServiceManager();//服务管理者
if (sm != NULL) {
//获取SurfaceFlinger的IBinder,并强转为ISurfaceComposer
*outService = interface_cast(sm->getService(name));
if ((*outService) != NULL) return NO_ERROR;
}
return NAME_NOT_FOUND;
}
通过ISurfaceComposer的接口createConnection在surfaceFlinger本地创建对应的client对象,用于与应用对应。
代理端实现如下:
virtual sp<ISurfaceComposerClient> createConnection()
{
Parcel data, reply;
data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);//Binder远程调用
return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
}
远程服务端实现如下:
status_t BnSurfaceComposer::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case CREATE_CONNECTION: {
CHECK_INTERFACE(ISurfaceComposer, data, reply);
sp<IBinder> b = IInterface::asBinder(createConnection());//在服务端surfaceFlinger实现
reply->writeStrongBinder(b);
return NO_ERROR;
}
。。。。。。
}
到此,应用端就获取到surfaceFlinger本地client对象,而ISurfaceComposerClient则是应用与client的通信桥梁
sp SurfaceFlinger::createConnection()
{
sp bclient;
sp client(new Client(this));//在服务本地创建client与应用对应
status_t err = client->initCheck();
if (err == NO_ERROR) {
bclient = client;
}
return bclient;//返回client接口的IBinder
}
未完待续。。。。。。