發(fā)現(xiàn)一個(gè)比較好的介紹graphic帖子, 后面會(huì)跟著帖子來(lái)梳理下graphic相關(guān)知識(shí)點(diǎn)
http://blog.csdn.net/u014409795/article/details/51276468
BufferQueue
?????? class BufferQueue {
???????????? ?class ProxyConsumerListener : public BnConsumerListener;
????????????? static void createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
??????????????????????? sp<IGraphicBufferConsumer>* outConsumer,
????????????????????????const sp<IGraphicBufferAlloc>& allocator = NULL);
????????private:
???????????? BufferQueue(); // Create through createBufferQueue
???? }
???? //看起來(lái)只有consumer一個(gè)角色败京?
??? void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
??????? sp<IGraphicBufferConsumer>* outConsumer,
??????? const sp<IGraphicBufferAlloc>& allocator) {
??????? sp<BufferQueueCore> core(new BufferQueueCore(allocator));
??????? sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core));
??????? sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
? BufferQueue核心是BufferQueueCore,
??看BufferQueueCore里面是管理什么的?
? class BufferQueueCore : public virtual RefBase {
??????????friend class BufferQueueProducer;
??????????friend class BufferQueueConsumer;
????????? ?typedef Vector<BufferItem> Fifo;? //一個(gè)BufferItem Vector
???????? private:
???????? // mAllocator is the connection to SurfaceFlinger that is used to allocate
???????? // new GraphicBuffer objects.
??????? sp<IGraphicBufferAlloc> mAllocator;
???????? // mSlots is an array of buffer slots that must be mirrored on the producer
??????? // side. This allows buffer ownership to be transferred between the producer
??????? // and consumer without sending a GraphicBuffer over Binder. The entire
?????? // array is initialized to NULL at construction time, and buffers are
?????? // allocated for a slot when requestBuffer is called with that slot's index.
??????? BufferQueueDefs::SlotsType mSlots;?? //
??????? BufferSlot()
??? : mEglDisplay(EGL_NO_DISPLAY),
????? mBufferState(BufferSlot::FREE),???//state
????? mRequestBufferCalled(false),
????? mFrameNumber(0),
????? mEglFence(EGL_NO_SYNC_KHR),
????? mAcquireCalled(false),
????? mNeedsCleanupOnRelease(false),
????? mAttachedByConsumer(false) {
??? }
??? // mGraphicBuffer points to the buffer allocated for this slot or is NULL
??? // if no buffer has been allocated.
??? sp<GraphicBuffer> mGraphicBuffer;?? //bufferslot中指向GraphicBuffer
?? ?enum BufferState { // state?表示buffer?state
??? ?FREE = 0,??//FREE indicates that the buffer is available to be dequeued by the producer.
?????//?允許produce獲取該buffer,填充數(shù)據(jù)惋戏,狀態(tài)變?yōu)閐equeue??? free 時(shí)表示該buffer還被bufferqueue 所有
?? DEQUEUED = 1,//?DEQUEUED indicates that the buffer has been dequeued by the ?producer, but has not yet been queued or canceled. The slot is "owned" by the producer.? 處于待填充數(shù)據(jù)狀態(tài),被producer所有
? QUEUED = 2,//QUEUED indicates that the buffer has been filled by the producer and queued for use by the consumer; producer填充完數(shù)據(jù)春贸,準(zhǔn)備提供給consumer消費(fèi),被bufferqueue所有
? ?ACQUIRED = 3//ACQUIRED indicates that the buffer has been acquired by the ?consumer.
?? buffer被consumer獲取到狱杰,消費(fèi)完后轉(zhuǎn)為free
?? 在bufferqueuecore中bufferItem又是什么
?? class BufferItem : public Flattenable<BufferItem> {
?? // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
??? // if the buffer in this slot has been acquired in the past (see
??? // BufferSlot.mAcquireCalled).
??? sp<GraphicBuffer> mGraphicBuffer; //也有GraphicBuffer指針?
??? ?union {
??????? // mSlot is the slot index of this buffer (default INVALID_BUFFER_SLOT).
??????? int mSlot; // slot數(shù)組中索引
??????? // mBuf is the former name for mSlot
??????? int mBuf;? //?舊名字
??? };
?? BufferSlot 與 BufferItem 都指向GraphicBuffer,然后又 通過(guò)mSlot下標(biāo)關(guān)聯(lián)起來(lái)差凹,至于兩者使用場(chǎng)景再看期奔,至于GraphicBuffer是如何再通過(guò)GraphicBufferAlloc分配的,待進(jìn)一步trace,目前繼續(xù)聚焦于BufferQueue使用
在SurfaceMediaSource::read()就是一個(gè)consumer acquire場(chǎng)景
SurfaceMediaSource::signalBufferReturned()是consumer release場(chǎng)景危尿,但什么時(shí)候調(diào)用的呢呐萌?是通過(guò)mediaBuffer自身的observer來(lái)通知consumer release該buffer
void MediaBuffer::release() {
mObserver->signalBufferReturned(this);
struct VideoNativeMetadata {
??? MetadataBufferType eType;?????????????? // must be kMetadataBufferTypeANWBuffer
#ifdef OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS
??? OMX_PTR pBuffer;
#else
??? struct ANativeWindowBuffer* pBuffer;
#endif
??? int nFenceFd;?????????????????????????? // -1 if unused
};
下面聚焦BufferQueue使用過(guò)程
?BufferQueue::createBufferQueue(&mProducer, &mConsumer);
可以跳轉(zhuǎn)到文章開(kāi)頭看CreateBufferQueue, Producer與Consumer 實(shí)際是共用bufferslot,然后通過(guò)不同的BufferState 來(lái)判斷實(shí)際空間上數(shù)據(jù)是歸屬于誰(shuí),正被誰(shuí)使用
mConsumer->setDefaultBufferSize(bufferWidth, bufferHeight); //這個(gè)寬和高是codec要編解碼的寬和高
至于producer是如何跟surfaceview關(guān)聯(lián)起來(lái)的谊娇?
base/core/jni/android_view_Surface.cpp:jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env
?sp<Surface> surface(new Surface(bufferProducer, true))搁胆;這樣關(guān)聯(lián)起來(lái)了
生產(chǎn)者
status_t BufferQueueProducer::dequeueBuffer(){
while (found == BufferItem::INVALID_BUFFER_SLOT) {
??????????? status_t status = waitForFreeSlotThenRelock("dequeueBuffer", async,
??????????????????? &found, &returnFlags);} //得到一個(gè)空閑的slot 號(hào) found
?const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
??????? if ((buffer == NULL) ||?buffer->needsReallocation(width, height, format, usage)){}else{}
判斷graphic buffer是否需要重新分配
if (returnFlags & BUFFER_NEEDS_REALLOCATION) {
sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
??????????????? width, height, format, usage, &error)); }
return returnFlags;}
填充數(shù)據(jù)完成
status_t BufferQueueProducer::queueBuffer(){
input.deflate(×tamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode,
??????????? &transform, &async, &fence, &stickyTransform);
mSlots[slot].mBufferState = BufferSlot::QUEUED;
BufferItem item;
?mCore->mQueue.push_back(item);// 將buffer 放入bufferqueuecore隊(duì)列,供consumer到時(shí)acquire
?mCore->mDequeueCondition.broadcast();? }
BufferQueueProducer::cancelBuffer(){
??? mCore->mFreeBuffers.push_front(slot);
??? mSlots[slot].mBufferState = BufferSlot::FREE; }
BufferQueueProducer::connect(){}
BufferQueueProducer::disconnect(int api){}? //BufferQueue跟app的生死綁定到了一起邮绿,當(dāng)app莫名其妙的死掉以后,flinger服務(wù)中的BufferQueue就知道攀例,會(huì)做一些清理工作
void BufferQueueProducer::allocateBuffers(){
newBufferCount =
??????????????????? static_cast<size_t>(maxBufferCount - currentBufferCount);
for (size_t i = 0; i <? newBufferCount; ++i) {
sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer(
??????????????????? allocWidth, allocHeight, allocFormat, allocUsage, &result));?}? }//這個(gè)buffer分配被void Surface::allocateBuffers()調(diào)用
消費(fèi)者
status_t BufferQueueConsumer::acquireBuffer(){
?BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
??int slot = front->mSlot;
??*outBuffer = *front;
???mSlots[slot].mBufferState = BufferSlot::ACQUIRED;
?? mCore->mQueue.erase(front);??? }
status_t BufferQueueConsumer::releaseBuffer(int slot..){
??????? if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
??????????? mSlots[slot].mBufferState = BufferSlot::FREE;
??????????? mCore->mFreeBuffers.push_back(slot); }??//acquire, release?只是簡(jiǎn)單的狀態(tài)改變和插入對(duì)應(yīng)隊(duì)列?}