整理笔记

本文详细介绍了sp<T>类的管理引用计数机制,以及如何正确使用sp<T>和Thread类。同时,解释了BootAnimation类是如何通过SurfaceComposerClient获取屏幕显示参数并进行动画展示的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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类才行,在看RefBaseincStrong()

     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()产生的对象;


待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值