1、sp指针
sp指针原型模板和构造函数如下:
template<typename T>
sp<T>::sp(const sp<T>& other):m_ptr(other.m_ptr)
{
if(m_ptr) m_ptr->incStrong(this);
}
template<typename T>
sp<T>::~sp()
{
if(m_ptr) m_ptr->decStrong(this);
}
在构造函数中调用了incStrong(),在析构函数中调用的decStrong(),显然是管理引用计数的函数,但是sp类的中并没有定义这两个函数,
这两个函数是在RefBase类中定义的,由此可以得出结论:要想使用sp<T>或者wp<T>, T必需要继承RefBase类才行,在看RefBase的incStrong(),
voidRefBase::incStrong(const void* id) const
{
weakref_impl* const refs = mRefs;
refs->incWeak(id);
refs->addStrongRef(id);
constint32_t c = android_atomic_inc(&refs->mStrong);
ALOG_ASSERT(c > 0,"incStrong() called on %p after last strong ref", refs);
#ifPRINT_REFS
ALOGD("incStrongof %p from %p: cnt=%d\n", this, id, c);
#endif
if (c !=INITIAL_STRONG_VALUE) {
return;
}
android_atomic_add(-INITIAL_STRONG_VALUE,&refs->mStrong);
refs->mBase->onFirstRef(); //使用sp指针要继承与RefBase和实现OnFirstRef()
}
voidRefBase::decStrong(const void* id) const
{
weakref_impl* const refs = mRefs;
refs->removeStrongRef(id);
const int32_t c =android_atomic_dec(&refs->mStrong); //T指针引用次数
#ifPRINT_REFS
ALOGD("decStrongof %p from %p: cnt=%d\n", this, id, c);
#endif
ALOG_ASSERT(c >= 1,"decStrong() called on %p too many times", refs);
if (c == 1) {
refs->mBase->onLastStrongRef(id);
if ((refs->mFlags&OBJECT_LIFETIME_MASK)== OBJECT_LIFETIME_STRONG) {
delete this; //如果是最后一个delete
}
}
refs->decWeak(id);
}
2、Thread类调用流程
使用方法:new Thread(bool flag).run("name", priority);
status_t Thread::run(const char* name, int32_tpriority, size_t stack)
{
Mutex::Autolock _l(mLock);
if(mRunning) {
//thread already started
return INVALID_OPERATION;
}
.........................
mThread =thread_id_t(-1);
//mCanCallJava在new时的flag
if(mCanCallJava) {
res =createThreadEtc(_threadLoop,
this, name, priority, stack, &mThread);
} else {
/* 此函数中实现pthread_create(&thread,&attr,(android_pthread_entry)entryFunction, userData),此时
entryFunction就是_threadLoop */
res =androidCreateRawThreadEtc(_threadLoop,
this, name, priority, stack, &mThread);
}
........................
returnNO_ERROR;
}
int Thread::_threadLoop(void* user)
{
Thread*const self = static_cast<Thread*>(user);
........
do {
boolresult;
if(first) {
first = false;
self->mStatus= self->readyToRun();
result = (self->mStatus == NO_ERROR);
if (result && !self->exitPending()) {
result = self->threadLoop();
}
}else {
result = self->threadLoop();
}
...........
strong.clear();
//And immediately, re-acquire a strong reference for the next loop
strong = weak.promote();
}while(strong != 0);
return 0;
}
所以Thread的使用要实现readyToRun()和threadLoop();
3、BootAnimation
BootAnimation::BootAnimation() : Thread(false)
{
mSession= new SurfaceComposerClient();
}
SurfaceComposerClient::SurfaceComposerClient()
: mStatus(NO_INIT),mComposer(Composer::getInstance())
{
}
Composer::getInstance()就是获取surfaceflinger服务Binder,mComposer就是surfaceflinger的代理;
在函数BootAnimation::readyToRun()中的SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)就是获取surfaceflinger类中成员:
mDefaultDisplays,而mDefaultDisplays[i] = new BBinder(),其中i为DisplayDevice::DisplayType,即mDefaultDisplays其实实质为Binder;
在SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo)中到surfaceflinger的getDisplayInfo()的屏幕显示参数,从HWComposer中的mDisplayData获取,而HWComposer 在surfaceflinger的readyToRun()产生的对象;
待续。。。