Surface在C++層的創建源碼解析
源碼為:android4.4.4
1、創建SurfaceComposerClient繪圖客戶端
| // create a client to surfaceflinger sp<SurfaceComposerClient> client = new SurfaceComposerClient(); //創建SurfaceFlinger的本地代理 |
展開SurfaceComposerClient源碼:frameworks\native\libs\gui\SurfaceComposerClient.cpp
| SurfaceComposerClient::SurfaceComposerClient() : mStatus(NO_INIT), mComposer(Composer::getInstance()) // Composer::getInstance() 應該是一個單例模式 { } |
mStatus: 為status_t類型變量 , mComposer:為Composer&的引用
Composer類為SurfaceComposerClient類的友元類。
Composer::getInstance()獲取一個Composer對象賦值給mComposer。
現在client對象的兩個變量mStatus和mComposer都有值了。
2、通過client對象獲取屏幕信息
| DisplayInfo display; //獲取屏幕的寬高等信息 client->getDisplayInfo(client->getBuiltInDisplay(HWC_DISPLAY_PRIMARY), &display); |
將獲取到的屏幕信息存放發哦display這個結構體里。
看看client->getBuiltInDisplay(HWC_DISPLAY_PRIMARY)這個是什么意思。
| sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) { return Composer::getInstance().getBuiltInDisplay(id); } //調用Composer的getBuiltInDisplay方法 |
| sp<IBinder> Composer::getBuiltInDisplay(int32_t id) { return ComposerService::getComposerService()->getBuiltInDisplay(id); }//再次調用ComposerService對象的getBuiltInDisplay方法 |
client-> getBuiltInDisplay最終獲取到一個Ibinder對象
再看看client->getDisplayInfo這個方法
| status_t SurfaceComposerClient::getDisplayInfo( const sp<IBinder>& display, DisplayInfo* info) { return ComposerService::getComposerService()->getDisplayInfo(display, info); } |
根據HWC_DISPLAY_PRIMARY獲取Ibinder對象,根據Binder對象獲取ComposerSeveice對象調用取得屏幕信息。
3、根據SurfaceComposerClient對象創建SurfafeControl對象
| //創建SurfaceControl的本地代理 sp<SurfaceControl> surfaceControl = client->createSurface(String8("testsurface"), display.w, display.h, PIXEL_FORMAT_RGBA_8888, 0); |
看看createSurface源碼
| sp<SurfaceControl> SurfaceComposerClient::createSurface( const String8& name, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) { sp<SurfaceControl> sur; if (mStatus == NO_ERROR) { sp<IBinder> handle; sp<IGraphicBufferProducer> gbp; status_t err = mClient->createSurface(name, w, h, format, flags, &handle, &gbp); //通知服務端申請surface ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err)); if (err == NO_ERROR) { sur = new SurfaceControl(this, handle, gbp); //如果申請成功就創建 } } return sur; } |
mClient是什么東西:sp<ISurfaceComposerClient> mClient; IsurfaceComposerClient是Binder的一個接口類。
先申請surface申請成功就創建一個surfaceControl的本地代理。
4、設置surface的圖層Z軸
| SurfaceComposerClient::openGlobalTransaction(); surfaceControl->setLayer(120000); //設置z軸 surfaceControl->setSize(display.w, display.h); surfaceControl->setPosition(0, 0); //起始位置 SurfaceComposerClient::closeGlobalTransaction(); //默認false |
SurfaceComposerClient::openGlobalTransaction() 源碼展開后:
| void SurfaceComposerClient::openGlobalTransaction() { Composer::openGlobalTransaction(); } |
| static void openGlobalTransaction() { Composer::getInstance().openGlobalTransactionImpl(); } |
| void Composer::openGlobalTransactionImpl() { { // scope for the lock Mutex::Autolock _l(mLock); mTransactionNestCount += 1; //最終是給這個變量加1 } } |
看看surfaceControl->setLayer(120000)設置z軸展開:
| status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) { return getComposer().setLayer(this, id, z); } |
| status_t Composer::setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id, int32_t z) { Mutex::Autolock _l(mLock); layer_state_t* s = getLayerStateLocked(client, id);//狀態加鎖 if (!s) return BAD_INDEX; s->what |= layer_state_t::eLayerChanged; s->z = z; return NO_ERROR; } |
| layer_state_t* Composer::getLayerStateLocked( const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
ComposerState s; s.client = client->mClient; s.state.surface = id; //將設置的Z軸坐標存在ComposerState對象里
ssize_t index = mComposerStates.indexOf(s); //查看鏈表中是否有該坐標 if (index < 0) { //沒有就添加 // we don't have it, add an initialized layer_state to our list index = mComposerStates.add(s); //將這個ComposerState對象存放到鏈表中 }
ComposerState* const out = mComposerStates.editArray(); return &(out[index].state); } |
以上就是設置Z軸方法,調用Composer對象getLayerStateLocked進行加鎖,創建一個ComposerState對象,將傳遞進來的值存進行檢查,檢查是否存在,如果不存在就直接儲到mComposerStates鏈表中。
SurfaceComposerClient::closeGlobalTransaction()展開:
| void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) { Composer::closeGlobalTransaction(synchronous); } |
| static void closeGlobalTransaction(bool synchronous) { Composer::getInstance().closeGlobalTransactionImpl(synchronous); } |
| void Composer::closeGlobalTransactionImpl(bool synchronous) { sp<ISurfaceComposer> sm(ComposerService::getComposerService());
Vector<ComposerState> transaction; Vector<DisplayState> displayTransaction; uint32_t flags = 0;
{ // scope for the lock Mutex::Autolock _l(mLock); mForceSynchronous |= synchronous; if (!mTransactionNestCount) { ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior " "call to openGlobalTransaction()."); } else if (--mTransactionNestCount) { //和鎖定時正好相反 return; 直接退出 } //后面是沒有進行上面的鎖定,才執行下面該部分 transaction = mComposerStates; mComposerStates.clear();
displayTransaction = mDisplayStates; mDisplayStates.clear();
if (mForceSynchronous) { flags |= ISurfaceComposer::eSynchronous; } if (mAnimation) { flags |= ISurfaceComposer::eAnimation; }
mForceSynchronous = false; mAnimation = false; }
sm->setTransactionState(transaction, displayTransaction, flags); } |
5、獲取surface
| // 獲取Surface本地代理 sp<Surface> surface = surfaceControl->getSurface();//獲取surface |
| sp<Surface> SurfaceControl::getSurface() const { Mutex::Autolock _l(mLock); if (mSurfaceData == 0) { // This surface is always consumed by SurfaceFlinger, so the // producerControlledByApp value doesn't matter; using false. mSurfaceData = new Surface(mGraphicBufferProducer, false); } return mSurfaceData; } |
直接new一個suface對象返回
6、surface->lock() 和surface->unlockAndPost();
Lock從屏幕緩沖隊列中申請屏幕,再使用unlockAndpost將申請的屏幕加入到緩沖隊列中交給surfaceflinger進行組合並顯示在屏幕上。其實這才是繪圖顯示最重要的階段,繪圖顯示快慢和這里優化有直接關系。
