Surfaceflinger of the Android GUI system (6) Bufferqueue buffer allocation

Source: Internet
Author: User

Buffer Allocation in 1.1.1 Bufferqueue

We know that there is a mslots array in the bufferqueue that manages each buffer within it, with a maximum capacity of 32. From the way it is declared, this mslots is statically allocated 32 bufferslot size spaces at the beginning of the program. However, this does not mean that the buffer is also a one-time static allocation, on the contrary, from the Bufferslot internal variable pointer mgraphicbuffer can be seen that the space allocation of the buffer should be dynamic (from the following comments can also see some clues):

Mgraphicbuffer points to the buffer allocated for this slot or IsNULL if no buffer has been.

Sp<graphicbuffer> Mgraphicbuffer;

Now the question is shifted to, under what circumstances will give a slot actual allocation space?

The first thing you can think of is dequeuebuffer. The reasons are as follows:

The spatial allocation of buffer should not only meet the needs of users, but also prevent waste. The latter point Mslots has been met since it did not take the first static pre-allocation way

Since producer is "active" in the operation of the buffer, it means that it is the initiator of the entire demand. In other words, as long as it has no dequeuebuffer, or Dequeuebuffer can get the available buffer, then of course there is no need to redistribute space

To analyze this function in detail:

/*frameworks/native/libs/gui/bufferqueue.cpp*/

status_t bufferqueue::d equeuebuffer (int *outbuf, uint32_t w,uint32_t H, uint32_t format, uint32_t usage) {...

status_t returnflags (OK);

...

{//Scope for the lock

Mutex::autolocklock (Mmutex); /* The automatic lock is used here, so the above need to add a "{", so that when the lock variable is born

Locks are automatically released after the end of the life cycle. This kind of writing is very common *

...

int found =-1;

int foundsync =-1;

int dequeuedcount = 0;

bool Tryagain = true;

while (Tryagain) {/*step1. Loops to find the slot*/that meet the requirements

...

Found = invalid_buffer_slot;//Initial value

Foundsync =invalid_buffer_slot;

Dequeuedcount = 0;

for (int i = 0; i< mbuffercount; i++) {

Const INTSTATE = mslots[i].mbufferstate;

/*step2. Statistics dequeued buffer number, will be used in the following * *

if (State ==bufferslot::D equeued) {

dequeuedcount++;

}

if (false) {

Definitely not going this way.

} else {

if (state== bufferslot::free) {/*step3. Find the slot*/that meets the requirements

Boolisolder = Mslots[i].mframenumber <mSlots[found].mFrameNumber;

if (Found < 0 | | isolder) {

Foundsync = i;

found = i; Find the slot that meet the requirements

}

}//if (state ==bufferslot::free) end

}//if (false) Else end

}//for Loop End

/*step4. If the client does not set the buffer count, it is not allowed to dequeue more than one buffer*/

if (!mclientbuffercount && dequeuedcount) {

St_loge ("Dequeuebuffer:can ' t dequeue multiple buffers without the buffer count");

Return-einval;

}

...

/*step5. To determine if you want to retry the *

Tryagain = found== Invalid_buffer_slot;

if (Tryagain) {

Mdequeuecondition.wait (Mmutex);

}

}//while Loop End

if (found ==invalid_buffer_slot) {

/* Because the front while loop does not return if it is not found, this is not theoretically possible.

St_loge ("Dequeuebuffer:no Available buffer slots");

Return-ebusy;

}

const int BUF = found;

*outbuf = found; return value

/* Successfully found the available slot sequence number, and then start to the specified slot for the initial operation, and state changes, etc. * *

...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.