//frameworks/native/libs/ui/GraphicBufferAllocator.cpp
classGraphicBufferAllocator:publicSingleton<GraphicBufferAllocator>{std::unique_ptr<constGrallocAllocator>mAllocator;status_tGraphicBufferAllocator::allocateHelper(uint32_twidth,uint32_theight,PixelFormatformat,uint32_tlayerCount,uint64_tusage,buffer_handle_t*handle,uint32_t*stride,std::stringrequestorName,boolimportBuffer){ATRACE_CALL();// make sure to not allocate a N x 0 or 0 x N buffer, since this is
// allowed from an API stand-point allocate a 1x1 buffer instead.
// 如果宽或者高为0, 则将宽高设置为1
if(!width||!height)width=height=1;constuint32_tbpp=bytesPerPixel(format);if(std::numeric_limits<size_t>::max()/width/height<static_cast<size_t>(bpp)){ALOGE("Failed to allocate (%u x %u) layerCount %u format %d ""usage %"PRIx64": Requesting too large a buffer size",width,height,layerCount,format,usage);returnBAD_VALUE;}// Ensure that layerCount is valid.
// 如果图层的数量少于1, 则将图层的数量设置为1
if(layerCount<1){layerCount=1;}// TODO(b/72323293, b/72703005): Remove these invalid bits from callers
usage&=~static_cast<uint64_t>((1<<10)|(1<<13));// 分配内存,使用的是 GrallocAllocator 指针,根据不同的版本有哦不同的实现,这里我们假设它的实现是 Gralloc3Allocator
status_terror=mAllocator->allocate(requestorName,width,height,format,layerCount,usage,1,stride,handle,importBuffer);if(error!=NO_ERROR){ALOGE("Failed to allocate (%u x %u) layerCount %u format %d ""usage %"PRIx64": %d",width,height,layerCount,format,usage,error);returnerror;}if(!importBuffer){returnNO_ERROR;}size_tbufSize;// if stride has no meaning or is too large,
// approximate size with the input width instead
if((*stride)!=0&&std::numeric_limits<size_t>::max()/height/(*stride)<static_cast<size_t>(bpp)){bufSize=static_cast<size_t>(width)*height*bpp;}else{bufSize=static_cast<size_t>((*stride))*height*bpp;}// 初始化参数
Mutex::Autolock_l(sLock);KeyedVector<buffer_handle_t,alloc_rec_t>&list(sAllocList);alloc_rec_trec;rec.width=width;rec.height=height;rec.stride=*stride;rec.format=format;rec.layerCount=layerCount;rec.usage=usage;rec.size=bufSize;rec.requestorName=std::move(requestorName);list.add(*handle,rec);returnNO_ERROR;}}
//framewrks/native/libs/ui/Gralloc3.cpp
sp<hardware::graphics::allocator::V3_0::IAllocator>mAllocator;classGralloc3Allocator:publicGrallocAllocator{status_tGralloc3Allocator::allocate(std::string/*requestorName*/,uint32_twidth,uint32_theight,android::PixelFormatformat,uint32_tlayerCount,uint64_tusage,uint32_tbufferCount,uint32_t*outStride,buffer_handle_t*outBufferHandles,boolimportBuffers)const{IMapper::BufferDescriptorInfodescriptorInfo;sBufferDescriptorInfo(width,height,format,layerCount,usage,&descriptorInfo);BufferDescriptordescriptor;status_terror=mMapper.createDescriptor(static_cast<void*>(&descriptorInfo),static_cast<void*>(&descriptor));if(error!=NO_ERROR){returnerror;}autoret=mAllocator->allocate(descriptor,bufferCount,[&](constauto&tmpError,constauto&tmpStride,constauto&tmpBuffers){error=static_cast<status_t>(tmpError);if(tmpError!=Error::NONE){return;}if(importBuffers){for(uint32_ti=0;i<bufferCount;i++){error=mMapper.importBuffer(tmpBuffers[i],&outBufferHandles[i]);if(error!=NO_ERROR){for(uint32_tj=0;j<i;j++){mMapper.freeBuffer(outBufferHandles[j]);outBufferHandles[j]=nullptr;}return;}}}else{for(uint32_ti=0;i<bufferCount;i++){outBufferHandles[i]=native_handle_clone(tmpBuffers[i].getNativeHandle());if(!outBufferHandles[i]){for(uint32_tj=0;j<i;j++){autobuffer=const_cast<native_handle_t*>(outBufferHandles[j]);native_handle_close(buffer);native_handle_delete(buffer);outBufferHandles[j]=nullptr;}}}}*outStride=tmpStride;});// make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now
hardware::IPCThreadState::self()->flushCommands();return(ret.isOk())?error:static_cast<status_t>(kTransactionError);}}