同样还是surfaceflinger,最近有点进展,特地分享!
android 源代码中的类名真的是乱的一塌糊涂,经过一周多的研读The Fucking Source Code,终于有点眉头。首先surfaceflinger(以后简称SF),负责管理渲染UI的,他是个service,一直跟application交互,实时刷新surface。
知道这个,我从service这个点入手,之前尝试过直接看SF类等等,但是都搞不清楚头绪,所以改道service。
在源码frameworks/native/service/surfaceflinger/main_surfaceflinger.cpp 中,让我们扒一扒是啥玩意。。
int main(int argc, char** argv) { // When SF is launched in its own process, limit the number of // binder threads to 4. ProcessState::self()->setThreadPoolMaxThreadCount(4); //忽略。。 // start the thread pool sp<ProcessState> ps(ProcessState::self());//忽略 ps->startThreadPool(); // instantiate surfaceflinger sp<SurfaceFlinger> flinger = new SurfaceFlinger();//好戏从这里开始,得到一个SF对象 #if defined(HAVE_PTHREADS) setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY); #endif set_sched_policy(0, SP_FOREGROUND); // initialize before clients can connect flinger->init();//SF的初始化 //publish surface flinger sm(defaultServiceManager()); //得到一个ServiceManager对象 sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false); // // run in this thread flinger->run();//服务开始运行了!!! }
是不是无语了。。。对就是这么简单粗暴。但是丫的没这么简单吧。好了我们开始从
sp<IServiceManager> sm(defaultServiceManager()); 开始,这里有个sp,简单介绍</span>
sp<IServiceManager> defaultServiceManager() { if (gDefaultServiceManager != NULL) return gDefaultServiceManager;//忽略</span> { AutoMutex _l(gDefaultServiceManagerLock); //这个就是上锁的意思</span> while (gDefaultServiceManager == NULL) { gDefaultServiceManager = interface_cast<IServiceManager>( //重头戏在这 ProcessState::self()->getContextObject(NULL)); if (gDefaultServiceManager == NULL) sleep(1); } } return gDefaultServiceManager; }
gDefaultServiceManager是怎么来的。
gDefaultServiceManager = interface_cast<IServiceManager>( //重头戏在这 ProcessState::self()->getContextObject(NULL));我FTSC!!又调用了函数,我们乖乖跟踪下 interface_cast<IServiceManager>( ProcessState::self()->getContextObject(NULL)) 这个函数。。首先看一下它那吓死人的参数:
sp<ProcessState> ProcessState::self() { Mutex::Autolock _l(gProcessMutex); if (gProcess != NULL) { return gProcess; } gProcess = new ProcessState; return gProcess; }
sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& caller) { return getStrongProxyForHandle(0); }
sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) { sp<IBinder> result; AutoMutex _l(mLock);//上锁 handle_entry* e = lookupHandleLocked(handle);// if (e != NULL) { IBinder* b = e->binder; if (b == NULL || !e->refs->attemptIncWeak(this)) //刚开始b肯定为NULL if (handle == 0) {//刚刚传进来的handle是0! Parcel data; //下面做了什么我也不知道估计不影响 status_t status = IPCThreadState::self()->transact( 0, IBinder::PING_TRANSACTION, data, NULL, 0); if (status == DEAD_OBJECT) return NULL; } //这是重头戏啊 又来了个BpBinder?是啥玩意待会再说 b = new BpBinder(handle); e->binder = b; if (b) e->refs = b->getWeakRefs(); result = b; } else { result.force_set(b); e->refs->decWeak(this); } } return result;//总之返回的是一个BpBinder类。}
template<typename INTERFACE> inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) { return INTERFACE::asInterface(obj); }
#define DECLARE_META_INTERFACE(INTERFACE) \ static const android::String16 descriptor; \ static android::sp<I##INTERFACE> asInterface( \ const android::sp<android::IBinder>& obj); \ virtual const android::String16& getInterfaceDescriptor() const; \ I##INTERFACE(); \ virtual ~I##INTERFACE(); \这是声明。。。。接下来是实现的宏定义:
#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ const android::String16 I##INTERFACE::descriptor(NAME); \ const android::String16& \ I##INTERFACE::getInterfaceDescriptor() const { \ return I##INTERFACE::descriptor; \ } \ android::sp<I##INTERFACE> I##INTERFACE::asInterface( \ const android::sp<android::IBinder>& obj) \ { \ android::sp<I##INTERFACE> intr; \ if (obj != NULL) { \ intr = static_cast<I##INTERFACE*>( \ obj->queryLocalInterface( \ I##INTERFACE::descriptor).get()); \ if (intr == NULL) { \ intr = new Bp##INTERFACE(obj); \ } \ } \ return intr; \ } \ I##INTERFACE::I##INTERFACE() { } \ I##INTERFACE::~I##INTERFACE() { } \脑袋瓜表示不够用了,好一个乱七八糟,乱八九糟,乱九十糟。。。。。红色部分分别是asInterface的声明和定义。这个时候我在想这是两个宏而已,到底他在哪里被调用了。
class IServiceManager : public IInterface { public: DECLARE_META_INTERFACE(ServiceManager); virtual status_t addService( const String16& name, const sp<IBinder>& service) = 0; // 。。。。。。 };
static const android::String16 descriptor; static android::sp< IServiceManager > asInterface(const android::sp<android::IBinder>& obj) virtual const android::String16& getInterfaceDescriptor() const; IServiceManager (); \ virtual ~IServiceManager();
const android::String16 IServiceManager::descriptor(“android.os.IServiceManager”); const android::String16& IServiceManager::getInterfaceDescriptor() const { return IServiceManager::descriptor;//返回上面那个android.os.IServiceManager } android::sp<IServiceManager> IServiceManager::asInterface( const android::sp<android::IBinder>& obj) { android::sp<IServiceManager> intr; if (obj != NULL) { intr = static_cast<IServiceManager *>( obj->queryLocalInterface(IServiceManager::descriptor).get()); if (intr == NULL) { intr = new BpServiceManager(obj); } } return intr; } IServiceManager::IServiceManager () { } IServiceManager::~ IServiceManager() { }好我们重点看下 asInterface 这个函数体,妈的绕了这么久终于可以接下去走了。简单分析下,程序走到第一个if进去了,然后第一次intr还是null所以又走到第二个if,对intr被初始化成一个 BpServiceManager对象。。。。
sp<IServiceManager> sm(defaultServiceManager());
class BpServiceManager : public BpInterface<IServiceManager> { public: BpServiceManager(const sp<IBinder>& impl) : BpInterface<IServiceManager>(impl) {
//构造函数的形参是IBinder参数,但是实际上传进来的是new BpBinder(0) (ps:为什么呢?之前最开始的时候传进来的参数就是BpBinder类的,只是形参一直写成IBinder又是BpBinder,而且参数名是impl,敏感吗,不就是implement的缩写,实现?难道有猫腻?接着看。 } virtual sp<IBinder> getService(const String16& name) const { //忽略 } virtual sp<IBinder> checkService( const String16& name) const { 。。。。。忽略 } //这是个重要的函数 不能忽略先把他记在心里,待会会解释 virtual status_t addService(const String16& name, const sp<IBinder>& service, bool allowIsolated) { Parcel data, reply; data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); data.writeString16(name); data.writeStrongBinder(service); data.writeInt32(allowIsolated ? 1 : 0); status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply); return err == NO_ERROR ? reply.readExceptionCode() : err; } virtual Vector<String16> listServices() { //忽略 } };好了,我们知道上一环节有提到 BpServiceManager的是这行代码 intr = new BpServiceManager(obj);传进去的obj参数是IBinder类的,构造函数是 BpServiceManager(const sp<IBinder>& impl) : BpInterface<IServiceManager>(impl)
template<typename INTERFACE> inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote) : BpRefBase(remote) { }我了个cacaca,传进来的impl改名为 remote,又来个 BpRefBase(remote),android元老们,你爸妈造你这么吊吗。。。。继续跟踪这个函数:
BpRefBase::BpRefBase(const sp<IBinder>& o) : mRemote(o.get()), mRefs(NULL), mState(0) { extendObjectLifetime(OBJECT_LIFETIME_WEAK); if (mRemote) { mRemote->incStrong(this); // Removed on first IncStrong(). mRefs = mRemote->createWeak(this); // Held for our entire lifetime. } }
int main(int argc, char** argv) { // When SF is launched in its own process, limit the number of // binder threads to 4. ProcessState::self()->setThreadPoolMaxThreadCount(4); //忽略。。 // start the thread pool sp<ProcessState> ps(ProcessState::self()); //忽略。。 ps->startThreadPool(); // instantiate surfaceflinger sp<SurfaceFlinger> flinger = new SurfaceFlinger();//好戏从这里开始,得到一个SF对象 #if defined(HAVE_PTHREADS) setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY); #endif set_sched_policy(0, SP_FOREGROUND); // initialize before clients can connect flinger->init();//SF的初始化 // publish surface flinger sp<IServiceManager> sm(defaultServiceManager()); //得到一个ServiceManager对象 sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false); // // run in this thread flinger->run();//服务开始运行了!!! }这个时候走到 sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false);
virtual status_t addService(const String16& name, const sp<IBinder>& service, bool allowIsolated) { Parcel data, reply; data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor()); data.writeString16(name); data.writeStrongBinder(service); data.writeInt32(allowIsolated ? 1 : 0); status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply); return err == NO_ERROR ? reply.readExceptionCode() : err; }